From 0214a04abb686d25581964960f4f4ab5e7703eac Mon Sep 17 00:00:00 2001 From: "cgwippern@google.com" Date: Fri, 14 Aug 2020 14:11:59 -0700 Subject: [PATCH 1/2] Add example for Call.Do and Call.DoAndReturn fixes issue #469 * include example of DoAndReturn in readme --- README.md | 8 ++++-- gomock/doc_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 gomock/doc_test.go diff --git a/README.md b/README.md index eff0a467..077bbeca 100644 --- a/README.md +++ b/README.md @@ -167,11 +167,15 @@ func TestFoo(t *testing.T) { m := NewMockFoo(ctrl) - // Does not make any assertions. Returns 101 when Bar is invoked with 99. + // Does not make any assertions. Executes the anonymous functions and returns + // its result when Bar is invoked with 99. m. EXPECT(). Bar(gomock.Eq(99)). - Return(101). + DoAndReturn(func(_ int) int { + time.Sleep(1*time.Second) + return 101 + }). AnyTimes() // Does not make any assertions. Returns 103 when Bar is invoked with 101. diff --git a/gomock/doc_test.go b/gomock/doc_test.go new file mode 100644 index 00000000..e99b4a1d --- /dev/null +++ b/gomock/doc_test.go @@ -0,0 +1,66 @@ +package gomock_test + +import ( + "fmt" + "testing" + "time" + + "github.com/golang/mock/gomock" + mock_sample "github.com/golang/mock/sample/mock_user" +) + +func ExampleCall_DoAndReturn() { + t := &testing.T{} // provided by test + ctrl := gomock.NewController(t) + mockIndex := mock_sample.NewMockIndex(ctrl) + + mockIndex.EXPECT().Get(gomock.Any()).DoAndReturn( + func() string { + time.Sleep(1 * time.Second) + return "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( + // When capturing arguments the anonymous function should have the same signature as the mocked method. + func(arg string) interface{} { + time.Sleep(1 * time.Second) + fmt.Println(arg) + return "I'm sleepy" + }, + ) +} + +func ExampleCall_Do() { + t := &testing.T{} // provided by test + ctrl := gomock.NewController(t) + mockIndex := mock_sample.NewMockIndex(ctrl) + + mockIndex.EXPECT().Anon(gomock.Any()).Do( + func() { + time.Sleep(1 * time.Second) + }, + ) +} + +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( + // When capturing arguments the anonymous function should have the same signature as the mocked method. + func(arg string) { + time.Sleep(1 * time.Second) + fmt.Println(arg) + }, + ) +} From 5780a08c0e10329387cc33f8e62e3f58736da273 Mon Sep 17 00:00:00 2001 From: "cgwippern@google.com" Date: Fri, 21 Aug 2020 13:42:11 -0700 Subject: [PATCH 2/2] convert examples to testable examples --- gomock/doc_test.go | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/gomock/doc_test.go b/gomock/doc_test.go index e99b4a1d..59ea5f85 100644 --- a/gomock/doc_test.go +++ b/gomock/doc_test.go @@ -9,17 +9,22 @@ import ( mock_sample "github.com/golang/mock/sample/mock_user" ) -func ExampleCall_DoAndReturn() { +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( - func() string { - time.Sleep(1 * time.Second) + // 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() { @@ -29,25 +34,33 @@ func ExampleCall_DoAndReturn_captureArguments() { var s string mockIndex.EXPECT().Get(gomock.AssignableToTypeOf(s)).DoAndReturn( - // When capturing arguments the anonymous function should have the same signature as the mocked method. + // signature of anonymous function must have the same number of input and output arguments as the mocked method. func(arg string) interface{} { - time.Sleep(1 * time.Second) - fmt.Println(arg) + s = arg return "I'm sleepy" }, ) + + r := mockIndex.Get("foo") + fmt.Printf("%s %s", r, s) + // Output: I'm sleepy foo } -func ExampleCall_Do() { +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( - func() { - time.Sleep(1 * time.Second) + // 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() { @@ -57,10 +70,13 @@ func ExampleCall_Do_captureArguments() { var s string mockIndex.EXPECT().Anon(gomock.AssignableToTypeOf(s)).Do( - // When capturing arguments the anonymous function should have the same signature as the mocked method. + // signature of anonymous function must have the same number of input and output arguments as the mocked method. func(arg string) { - time.Sleep(1 * time.Second) - fmt.Println(arg) + s = arg }, ) + + mockIndex.Anon("foo") + fmt.Println(s) + // Output: foo }