Skip to content

Commit 74ff431

Browse files
committed
Update tests to operate on values rather than pointers
1 parent 84ba20a commit 74ff431

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
)
1010

1111
require (
12+
github.com/google/go-cmp v0.6.0 // indirect
1213
golang.org/x/mod v0.19.0 // indirect
1314
golang.org/x/sync v0.7.0 // indirect
1415
)

testdata/types.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package testdata
22

3-
type M0 struct{ *T0 }
3+
type M0 struct{ T0 }
4+
type T0 struct{}
45

5-
type T0 struct {
6-
simpleCalled bool
7-
}
6+
var simpleCalled bool
87

9-
func (t *T0) Simple() { t.simpleCalled = true }
8+
func (T0) Simple() { simpleCalled = true }
109

1110
func (T0) OneResult() error { return nil }
1211
func (T0) OneNamedResult() (err error) { return }

testdata/types_test.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@ package testdata
33
import (
44
"errors"
55
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/google/go-cmp/cmp/cmpopts"
69
)
710

811
func TestNoMock(t *testing.T) {
9-
m0 := &M0{new(T0)}
12+
t.Cleanup(func() { simpleCalled = false })
13+
var m0 M0
1014
m0.Simple()
11-
if !m0.simpleCalled {
15+
if !simpleCalled {
1216
t.Error("want Simple() call")
1317
}
1418
}
1519

1620
func TestStub(t *testing.T) {
17-
m0 := &M0{new(T0)}
21+
t.Cleanup(func() { simpleCalled = false })
22+
var m0 M0
1823
m0._Simple_Stub()
1924
m0.Simple()
20-
if m0.simpleCalled {
25+
if simpleCalled {
2126
t.Error("want mock, got Simple() call")
2227
}
2328
}
2429

2530
func TestOneResult(t *testing.T) {
26-
m0 := &M0{new(T0)}
31+
var m0 M0
2732
want := errors.New("error result")
2833
m0._OneResult_Return(want)
2934
if got := m0.OneResult(); want != got {
@@ -36,7 +41,7 @@ func TestOneResult(t *testing.T) {
3641
}
3742

3843
func TestOneResultQueue(t *testing.T) {
39-
m0 := &M0{new(T0)}
44+
var m0 M0
4045
err1 := errors.New("error one")
4146
err2 := errors.New("error two")
4247
m0._OneResult_Return(err1)
@@ -51,3 +56,20 @@ func TestOneResultQueue(t *testing.T) {
5156
t.Errorf("M0.OneResult() call #3: want %q, got %q", err1, got)
5257
}
5358
}
59+
60+
func TestCalls(t *testing.T) {
61+
var m0 M0
62+
m0.OneParamNoResult("call one")
63+
m0.OneParamNoResult("call two")
64+
m0.OneParamNoResult("call three")
65+
want := []_M0_OneParamNoResult_Call{
66+
{"call one"},
67+
{"call two"},
68+
{"call three"},
69+
}
70+
got := m0._OneParamNoResult_Calls()
71+
opt := cmpopts.EquateComparable(_M0_OneParamNoResult_Call{})
72+
if !cmp.Equal(want, got, opt) {
73+
t.Errorf("M0._OneParamNoResult_Calls():\n%s", cmp.Diff(want, got, opt))
74+
}
75+
}

0 commit comments

Comments
 (0)