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

update user mock to be in test package #566

Merged
merged 1 commit into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 0 additions & 82 deletions gomock/doc_test.go

This file was deleted.

52 changes: 52 additions & 0 deletions 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
}
48 changes: 48 additions & 0 deletions gomock/mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/README.md
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions sample/mock_user/mock_user.go → sample/mock_user_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions 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"
Expand All @@ -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).
Expand Down
15 changes: 7 additions & 8 deletions sample/user_test.go
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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) {},
Expand All @@ -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 {
Expand Down