From 953a5bb40e02a50d43d411521679c19a3843765f Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Fri, 4 Jun 2021 13:31:10 -0600 Subject: [PATCH] update user mock to be in test package (#566) Fixes: #560 --- gomock/doc_test.go | 82 ------------------- gomock/example_test.go | 52 ++++++++++++ gomock/mock_test.go | 48 +++++++++++ sample/README.md | 2 +- .../mock_user.go => mock_user_test.go} | 4 +- sample/user.go | 18 ++-- sample/user_test.go | 15 ++-- 7 files changed, 122 insertions(+), 99 deletions(-) delete mode 100644 gomock/doc_test.go create mode 100644 gomock/example_test.go create mode 100644 gomock/mock_test.go rename sample/{mock_user/mock_user.go => mock_user_test.go} (99%) diff --git a/gomock/doc_test.go b/gomock/doc_test.go deleted file mode 100644 index 59ea5f85..00000000 --- a/gomock/doc_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package gomock_test - -import ( - "fmt" - "testing" - "time" - - "github.com/golang/mock/gomock" - mock_sample "github.com/golang/mock/sample/mock_user" -) - -func ExampleCall_DoAndReturn_latency() { - t := &testing.T{} // provided by test - ctrl := gomock.NewController(t) - mockIndex := mock_sample.NewMockIndex(ctrl) - - mockIndex.EXPECT().Get(gomock.Any()).DoAndReturn( - // signature of anonymous function must have the same number of input and output arguments as the mocked method. - func(arg string) string { - time.Sleep(1 * time.Millisecond) - return "I'm sleepy" - }, - ) - - r := mockIndex.Get("foo") - fmt.Println(r) - // Output: I'm sleepy -} - -func ExampleCall_DoAndReturn_captureArguments() { - t := &testing.T{} // provided by test - ctrl := gomock.NewController(t) - mockIndex := mock_sample.NewMockIndex(ctrl) - var s string - - mockIndex.EXPECT().Get(gomock.AssignableToTypeOf(s)).DoAndReturn( - // signature of anonymous function must have the same number of input and output arguments as the mocked method. - func(arg string) interface{} { - s = arg - return "I'm sleepy" - }, - ) - - r := mockIndex.Get("foo") - fmt.Printf("%s %s", r, s) - // Output: I'm sleepy foo -} - -func ExampleCall_Do_latency() { - t := &testing.T{} // provided by test - ctrl := gomock.NewController(t) - mockIndex := mock_sample.NewMockIndex(ctrl) - - mockIndex.EXPECT().Anon(gomock.Any()).Do( - // signature of anonymous function must have the same number of input and output arguments as the mocked method. - func(_ string) { - fmt.Println("sleeping") - time.Sleep(1 * time.Millisecond) - }, - ) - - mockIndex.Anon("foo") - // Output: sleeping -} - -func ExampleCall_Do_captureArguments() { - t := &testing.T{} // provided by test - ctrl := gomock.NewController(t) - mockIndex := mock_sample.NewMockIndex(ctrl) - - var s string - mockIndex.EXPECT().Anon(gomock.AssignableToTypeOf(s)).Do( - // signature of anonymous function must have the same number of input and output arguments as the mocked method. - func(arg string) { - s = arg - }, - ) - - mockIndex.Anon("foo") - fmt.Println(s) - // Output: foo -} diff --git a/gomock/example_test.go b/gomock/example_test.go new file mode 100644 index 00000000..f5e7b49b --- /dev/null +++ b/gomock/example_test.go @@ -0,0 +1,52 @@ +package gomock_test + +//go:generate mockgen -destination mock_test.go -package gomock_test -source example_test.go + +import ( + "fmt" + "testing" + "time" + + "github.com/golang/mock/gomock" +) + +type Foo interface { + Bar(string) string +} + +func ExampleCall_DoAndReturn_latency() { + t := &testing.T{} // provided by test + ctrl := gomock.NewController(t) + mockIndex := NewMockFoo(ctrl) + + mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn( + // signature of anonymous function must have the same number of input and output arguments as the mocked method. + func(arg string) string { + time.Sleep(1 * time.Millisecond) + return "I'm sleepy" + }, + ) + + r := mockIndex.Bar("foo") + fmt.Println(r) + // Output: I'm sleepy +} + +func ExampleCall_DoAndReturn_captureArguments() { + t := &testing.T{} // provided by test + ctrl := gomock.NewController(t) + mockIndex := NewMockFoo(ctrl) + var s string + + mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn( + // signature of anonymous function must have the same number of input and output arguments as the mocked method. + func(arg string) interface{} { + s = arg + return "I'm sleepy" + }, + ) + + r := mockIndex.Bar("foo") + fmt.Printf("%s %s", r, s) + // Output: I'm sleepy foo +} diff --git a/gomock/mock_test.go b/gomock/mock_test.go new file mode 100644 index 00000000..4f1cfa91 --- /dev/null +++ b/gomock/mock_test.go @@ -0,0 +1,48 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: example_test.go + +// Package gomock_test is a generated GoMock package. +package gomock_test + +import ( + reflect "reflect" + + "github.com/golang/mock/gomock" +) + +// MockFoo is a mock of Foo interface. +type MockFoo struct { + ctrl *gomock.Controller + recorder *MockFooMockRecorder +} + +// MockFooMockRecorder is the mock recorder for MockFoo. +type MockFooMockRecorder struct { + mock *MockFoo +} + +// NewMockFoo creates a new mock instance. +func NewMockFoo(ctrl *gomock.Controller) *MockFoo { + mock := &MockFoo{ctrl: ctrl} + mock.recorder = &MockFooMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockFoo) EXPECT() *MockFooMockRecorder { + return m.recorder +} + +// Bar mocks base method. +func (m *MockFoo) Bar(arg0 string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Bar", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// Bar indicates an expected call of Bar. +func (mr *MockFooMockRecorder) Bar(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), arg0) +} diff --git a/sample/README.md b/sample/README.md index 9ee7c9c9..4d56d484 100644 --- a/sample/README.md +++ b/sample/README.md @@ -10,7 +10,7 @@ interface that can be mocked with GoMock. The interesting files are: interfaces from `user.go` are used. This demonstrates how to create mock objects, set up expectations, and so on. -* `mock_user/mock_user.go`: The generated mock code. See ../gomock/matchers.go +* `mock_user_test.go`: The generated mock code. See ../gomock/matchers.go for the `go:generate` command used to generate it. To run the test, diff --git a/sample/mock_user/mock_user.go b/sample/mock_user_test.go similarity index 99% rename from sample/mock_user/mock_user.go rename to sample/mock_user_test.go index 95dd7470..1e630d2e 100644 --- a/sample/mock_user/mock_user.go +++ b/sample/mock_user_test.go @@ -1,8 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/golang/mock/sample (interfaces: Index,Embed,Embedded) -// Package mock_sample is a generated GoMock package. -package mock_sample +// Package user_test is a generated GoMock package. +package user_test import ( bufio "bufio" diff --git a/sample/user.go b/sample/user.go index 58e08068..adb041e1 100644 --- a/sample/user.go +++ b/sample/user.go @@ -1,11 +1,12 @@ // Package user is an example package with an interface. package user -//go:generate mockgen -destination mock_user/mock_user.go github.com/golang/mock/sample Index,Embed,Embedded +//go:generate mockgen -destination mock_user_test.go -package user_test github.com/golang/mock/sample Index,Embed,Embedded // Random bunch of imports to test mockgen. -import "io" import ( + "io" + btz "bytes" "hash" "log" @@ -14,17 +15,22 @@ import ( // Two imports with the same base name. t1 "html/template" + t2 "text/template" -) -// Dependencies outside the standard library. -import ( "github.com/golang/mock/sample/imp1" + + // Dependencies outside the standard library. + renamed2 "github.com/golang/mock/sample/imp2" + . "github.com/golang/mock/sample/imp3" - "github.com/golang/mock/sample/imp4" // calls itself "imp_four" + + imp_four "github.com/golang/mock/sample/imp4" ) +// calls itself "imp_four" + // A bizarre interface to test corner cases in mockgen. // This would normally be in its own file or package, // separate from the user of it (e.g. io.Reader). diff --git a/sample/user_test.go b/sample/user_test.go index c007cfe6..35cfa0f1 100644 --- a/sample/user_test.go +++ b/sample/user_test.go @@ -7,14 +7,13 @@ import ( "github.com/golang/mock/gomock" user "github.com/golang/mock/sample" "github.com/golang/mock/sample/imp1" - mock_user "github.com/golang/mock/sample/mock_user" ) func TestRemember(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockIndex := mock_user.NewMockIndex(ctrl) + mockIndex := NewMockIndex(ctrl) mockIndex.EXPECT().Put("a", 1) // literals work mockIndex.EXPECT().Put("b", gomock.Eq(2)) // matchers work too @@ -67,7 +66,7 @@ func TestVariadicFunction(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockIndex := mock_user.NewMockIndex(ctrl) + mockIndex := NewMockIndex(ctrl) mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) { sum := 0 for _, value := range nums { @@ -125,7 +124,7 @@ func TestGrabPointer(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockIndex := mock_user.NewMockIndex(ctrl) + mockIndex := NewMockIndex(ctrl) mockIndex.EXPECT().Ptr(gomock.Any()).SetArg(0, 7) // set first argument to 7 i := user.GrabPointer(mockIndex) @@ -138,7 +137,7 @@ func TestEmbeddedInterface(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockEmbed := mock_user.NewMockEmbed(ctrl) + mockEmbed := NewMockEmbed(ctrl) mockEmbed.EXPECT().RegularMethod() mockEmbed.EXPECT().EmbeddedMethod() mockEmbed.EXPECT().ForeignEmbeddedMethod() @@ -155,7 +154,7 @@ func TestExpectTrueNil(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockIndex := mock_user.NewMockIndex(ctrl) + mockIndex := NewMockIndex(ctrl) mockIndex.EXPECT().Ptr(nil) // this nil is a nil interface{} mockIndex.Ptr(nil) // this nil is a nil *int } @@ -165,7 +164,7 @@ func TestDoAndReturnSignature(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockIndex := mock_user.NewMockIndex(ctrl) + mockIndex := NewMockIndex(ctrl) mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn( func(_ []int, _ []byte) {}, @@ -184,7 +183,7 @@ func TestDoAndReturnSignature(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockIndex := mock_user.NewMockIndex(ctrl) + mockIndex := NewMockIndex(ctrl) mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn( func(_ []int, _ []byte) bool {