Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 953a5bb

Browse files
authored
update user mock to be in test package (#566)
Fixes: #560
1 parent c59ba11 commit 953a5bb

File tree

7 files changed

+122
-99
lines changed

7 files changed

+122
-99
lines changed

gomock/doc_test.go

Lines changed: 0 additions & 82 deletions
This file was deleted.

gomock/example_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package gomock_test
2+
3+
//go:generate mockgen -destination mock_test.go -package gomock_test -source example_test.go
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
"time"
9+
10+
"github.com/golang/mock/gomock"
11+
)
12+
13+
type Foo interface {
14+
Bar(string) string
15+
}
16+
17+
func ExampleCall_DoAndReturn_latency() {
18+
t := &testing.T{} // provided by test
19+
ctrl := gomock.NewController(t)
20+
mockIndex := NewMockFoo(ctrl)
21+
22+
mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn(
23+
// signature of anonymous function must have the same number of input and output arguments as the mocked method.
24+
func(arg string) string {
25+
time.Sleep(1 * time.Millisecond)
26+
return "I'm sleepy"
27+
},
28+
)
29+
30+
r := mockIndex.Bar("foo")
31+
fmt.Println(r)
32+
// Output: I'm sleepy
33+
}
34+
35+
func ExampleCall_DoAndReturn_captureArguments() {
36+
t := &testing.T{} // provided by test
37+
ctrl := gomock.NewController(t)
38+
mockIndex := NewMockFoo(ctrl)
39+
var s string
40+
41+
mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn(
42+
// signature of anonymous function must have the same number of input and output arguments as the mocked method.
43+
func(arg string) interface{} {
44+
s = arg
45+
return "I'm sleepy"
46+
},
47+
)
48+
49+
r := mockIndex.Bar("foo")
50+
fmt.Printf("%s %s", r, s)
51+
// Output: I'm sleepy foo
52+
}

gomock/mock_test.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface that can be mocked with GoMock. The interesting files are:
1010
interfaces from `user.go` are used. This demonstrates how to create mock
1111
objects, set up expectations, and so on.
1212

13-
* `mock_user/mock_user.go`: The generated mock code. See ../gomock/matchers.go
13+
* `mock_user_test.go`: The generated mock code. See ../gomock/matchers.go
1414
for the `go:generate` command used to generate it.
1515

1616
To run the test,

sample/mock_user/mock_user.go renamed to sample/mock_user_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/user.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Package user is an example package with an interface.
22
package user
33

4-
//go:generate mockgen -destination mock_user/mock_user.go github.com/golang/mock/sample Index,Embed,Embedded
4+
//go:generate mockgen -destination mock_user_test.go -package user_test github.com/golang/mock/sample Index,Embed,Embedded
55

66
// Random bunch of imports to test mockgen.
7-
import "io"
87
import (
8+
"io"
9+
910
btz "bytes"
1011
"hash"
1112
"log"
@@ -14,17 +15,22 @@ import (
1415

1516
// Two imports with the same base name.
1617
t1 "html/template"
18+
1719
t2 "text/template"
18-
)
1920

20-
// Dependencies outside the standard library.
21-
import (
2221
"github.com/golang/mock/sample/imp1"
22+
23+
// Dependencies outside the standard library.
24+
2325
renamed2 "github.com/golang/mock/sample/imp2"
26+
2427
. "github.com/golang/mock/sample/imp3"
25-
"github.com/golang/mock/sample/imp4" // calls itself "imp_four"
28+
29+
imp_four "github.com/golang/mock/sample/imp4"
2630
)
2731

32+
// calls itself "imp_four"
33+
2834
// A bizarre interface to test corner cases in mockgen.
2935
// This would normally be in its own file or package,
3036
// separate from the user of it (e.g. io.Reader).

sample/user_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77
"github.com/golang/mock/gomock"
88
user "github.com/golang/mock/sample"
99
"github.com/golang/mock/sample/imp1"
10-
mock_user "github.com/golang/mock/sample/mock_user"
1110
)
1211

1312
func TestRemember(t *testing.T) {
1413
ctrl := gomock.NewController(t)
1514
defer ctrl.Finish()
1615

17-
mockIndex := mock_user.NewMockIndex(ctrl)
16+
mockIndex := NewMockIndex(ctrl)
1817
mockIndex.EXPECT().Put("a", 1) // literals work
1918
mockIndex.EXPECT().Put("b", gomock.Eq(2)) // matchers work too
2019

@@ -67,7 +66,7 @@ func TestVariadicFunction(t *testing.T) {
6766
ctrl := gomock.NewController(t)
6867
defer ctrl.Finish()
6968

70-
mockIndex := mock_user.NewMockIndex(ctrl)
69+
mockIndex := NewMockIndex(ctrl)
7170
mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
7271
sum := 0
7372
for _, value := range nums {
@@ -125,7 +124,7 @@ func TestGrabPointer(t *testing.T) {
125124
ctrl := gomock.NewController(t)
126125
defer ctrl.Finish()
127126

128-
mockIndex := mock_user.NewMockIndex(ctrl)
127+
mockIndex := NewMockIndex(ctrl)
129128
mockIndex.EXPECT().Ptr(gomock.Any()).SetArg(0, 7) // set first argument to 7
130129

131130
i := user.GrabPointer(mockIndex)
@@ -138,7 +137,7 @@ func TestEmbeddedInterface(t *testing.T) {
138137
ctrl := gomock.NewController(t)
139138
defer ctrl.Finish()
140139

141-
mockEmbed := mock_user.NewMockEmbed(ctrl)
140+
mockEmbed := NewMockEmbed(ctrl)
142141
mockEmbed.EXPECT().RegularMethod()
143142
mockEmbed.EXPECT().EmbeddedMethod()
144143
mockEmbed.EXPECT().ForeignEmbeddedMethod()
@@ -155,7 +154,7 @@ func TestExpectTrueNil(t *testing.T) {
155154
ctrl := gomock.NewController(t)
156155
defer ctrl.Finish()
157156

158-
mockIndex := mock_user.NewMockIndex(ctrl)
157+
mockIndex := NewMockIndex(ctrl)
159158
mockIndex.EXPECT().Ptr(nil) // this nil is a nil interface{}
160159
mockIndex.Ptr(nil) // this nil is a nil *int
161160
}
@@ -165,7 +164,7 @@ func TestDoAndReturnSignature(t *testing.T) {
165164
ctrl := gomock.NewController(t)
166165
defer ctrl.Finish()
167166

168-
mockIndex := mock_user.NewMockIndex(ctrl)
167+
mockIndex := NewMockIndex(ctrl)
169168

170169
mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn(
171170
func(_ []int, _ []byte) {},
@@ -184,7 +183,7 @@ func TestDoAndReturnSignature(t *testing.T) {
184183
ctrl := gomock.NewController(t)
185184
defer ctrl.Finish()
186185

187-
mockIndex := mock_user.NewMockIndex(ctrl)
186+
mockIndex := NewMockIndex(ctrl)
188187

189188
mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn(
190189
func(_ []int, _ []byte) bool {

0 commit comments

Comments
 (0)