diff --git a/example/mockpersonstore_test.go b/example/mockpersonstore_test.go new file mode 100755 index 0000000..46f3338 --- /dev/null +++ b/example/mockpersonstore_test.go @@ -0,0 +1,134 @@ +package example + +// AUTOGENERATED BY MOQ - DO NOT EDIT +// github.com/matryer/moq + +import ( + "sync" + "context" +) + +var ( + lockPersonStoreMockCreate sync.RWMutex + lockPersonStoreMockGet sync.RWMutex +) + +// PersonStoreMock is a mock implementation of PersonStore. +// +// func TestSomethingThatUsesPersonStore(t *testing.T) { +// +// // make and configure a mocked PersonStore +// mockedPersonStore := &PersonStoreMock{ +// CreateFunc: func(ctx context.Context, person *Person, confirm bool) error { +// panic("TODO: mock out the Create method") +// }, +// GetFunc: func(ctx context.Context, id string) (*Person, error) { +// panic("TODO: mock out the Get method") +// }, +// } +// +// // TODO: use mockedPersonStore in code that requires PersonStore +// // and then make assertions. +// +// } +type PersonStoreMock struct { + // CreateFunc mocks the Create method. + CreateFunc func(ctx context.Context, person *Person, confirm bool) error + + // GetFunc mocks the Get method. + GetFunc func(ctx context.Context, id string) (*Person, error) + + // calls tracks calls to the methods. + calls struct { + // Create holds details about calls to the Create method. + Create []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Person is the person argument value. + Person *Person + // Confirm is the confirm argument value. + Confirm bool + } + // Get holds details about calls to the Get method. + Get []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Id is the id argument value. + Id string + } + } +} + +// Create calls CreateFunc. +func (mock *PersonStoreMock) Create(ctx context.Context, person *Person, confirm bool) error { + if mock.CreateFunc == nil { + panic("moq: PersonStoreMock.CreateFunc is nil but PersonStore.Create was just called") + } + callInfo := struct { + Ctx context.Context + Person *Person + Confirm bool + }{ + Ctx: ctx, + Person: person, + Confirm: confirm, + } + lockPersonStoreMockCreate.Lock() + mock.calls.Create = append(mock.calls.Create, callInfo) + lockPersonStoreMockCreate.Unlock() + return mock.CreateFunc(ctx, person, confirm) +} + +// CreateCalls gets all the calls that were made to Create. +// Check the length with: +// len(mockedPersonStore.CreateCalls()) +func (mock *PersonStoreMock) CreateCalls() []struct { + Ctx context.Context + Person *Person + Confirm bool + } { + var calls []struct { + Ctx context.Context + Person *Person + Confirm bool + } + lockPersonStoreMockCreate.RLock() + calls = mock.calls.Create + lockPersonStoreMockCreate.RUnlock() + return calls +} + +// Get calls GetFunc. +func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error) { + if mock.GetFunc == nil { + panic("moq: PersonStoreMock.GetFunc is nil but PersonStore.Get was just called") + } + callInfo := struct { + Ctx context.Context + Id string + }{ + Ctx: ctx, + Id: id, + } + lockPersonStoreMockGet.Lock() + mock.calls.Get = append(mock.calls.Get, callInfo) + lockPersonStoreMockGet.Unlock() + return mock.GetFunc(ctx, id) +} + +// GetCalls gets all the calls that were made to Get. +// Check the length with: +// len(mockedPersonStore.GetCalls()) +func (mock *PersonStoreMock) GetCalls() []struct { + Ctx context.Context + Id string + } { + var calls []struct { + Ctx context.Context + Id string + } + lockPersonStoreMockGet.RLock() + calls = mock.calls.Get + lockPersonStoreMockGet.RUnlock() + return calls +} diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index ab17925..4188440 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -2,7 +2,6 @@ package moq import ( "bytes" - "log" "strings" "testing" ) @@ -26,10 +25,10 @@ func TestMoq(t *testing.T) { "GetFunc func(ctx context.Context, id string) (*Person, error)", "func (mock *PersonStoreMock) Create(ctx context.Context, person *Person, confirm bool) error", "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", - "panic(\"moq: PersonStoreMock.CreateFunc is nil but was just called\")", - "panic(\"moq: PersonStoreMock.GetFunc is nil but was just called\")", + "panic(\"moq: PersonStoreMock.CreateFunc is nil but PersonStore.Create was just called\")", + "panic(\"moq: PersonStoreMock.GetFunc is nil but PersonStore.Get was just called\")", "lockPersonStoreMockGet.Lock()", - "mock.CallsTo.Get = append(mock.CallsTo.Get, struct{", + "mock.calls.Get = append(mock.calls.Get, callInfo)", "lockPersonStoreMockGet.Unlock()", } for _, str := range strs { @@ -37,7 +36,6 @@ func TestMoq(t *testing.T) { t.Errorf("expected but missing: \"%s\"", str) } } - } func TestMoqExplicitPackage(t *testing.T) { @@ -65,7 +63,6 @@ func TestMoqExplicitPackage(t *testing.T) { t.Errorf("expected but missing: \"%s\"", str) } } - log.Println(s) } // TestVeradicArguments tests to ensure variadic work as diff --git a/pkg/moq/template.go b/pkg/moq/template.go index e9029fa..4fd2747 100644 --- a/pkg/moq/template.go +++ b/pkg/moq/template.go @@ -16,11 +16,9 @@ import ( ) {{ range $i, $obj := .Objects -}} -// These locks allow you to use the mocks in a safe way -// in concurrent code. var ( {{- range .Methods }} - lock{{$obj.InterfaceName}}Mock{{.Name}} sync.Mutex + lock{{$obj.InterfaceName}}Mock{{.Name}} sync.RWMutex {{- end }} ) @@ -37,21 +35,15 @@ var ( // // // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}} // // and then make assertions. -// // -// // Use the CallsTo structure to access details about what calls were made: -// // -// // if len(mocked{{.InterfaceName}}.CallsTo.MethodFunc) != 1 { -// // t.Errorf("expected 1 call there were %d", len(mocked{{.InterfaceName}}.CallsTo.MethodFunc)) -// // } -// +// // } type {{.InterfaceName}}Mock struct { {{- range .Methods }} // {{.Name}}Func mocks the {{.Name}} method. {{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}} {{ end }} - // CallsTo tracks calls to the methods. - CallsTo struct { + // calls tracks calls to the methods. + calls struct { {{- range .Methods }} // {{ .Name }} holds details about calls to the {{.Name}} method. {{ .Name }} []struct { @@ -67,10 +59,9 @@ type {{.InterfaceName}}Mock struct { // {{.Name}} calls {{.Name}}Func. func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist}} { if mock.{{.Name}}Func == nil { - panic("moq: {{$obj.InterfaceName}}Mock.{{.Name}}Func is nil but was just called") + panic("moq: {{$obj.InterfaceName}}Mock.{{.Name}}Func is nil but {{$obj.InterfaceName}}.{{.Name}} was just called") } - lock{{$obj.InterfaceName}}Mock{{.Name}}.Lock() - mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{ + callInfo := struct { {{- range .Params }} {{ .Name | Exported }} {{ .Type }} {{- end }} @@ -78,7 +69,9 @@ func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist {{- range .Params }} {{ .Name | Exported }}: {{ .Name }}, {{- end }} - }) + } + lock{{$obj.InterfaceName}}Mock{{.Name}}.Lock() + mock.calls.{{.Name}} = append(mock.calls.{{.Name}}, callInfo) lock{{$obj.InterfaceName}}Mock{{.Name}}.Unlock() {{- if .ReturnArglist }} return mock.{{.Name}}Func({{.ArgCallList}}) @@ -86,5 +79,24 @@ func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist mock.{{.Name}}Func({{.ArgCallList}}) {{- end }} } + +// {{.Name}}Calls gets all the calls that were made to {{.Name}}. +// Check the length with: +// len(mocked{{$obj.InterfaceName}}.{{.Name}}Calls()) +func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}Calls() []struct { + {{- range .Params }} + {{ .Name | Exported }} {{ .Type }} + {{- end }} + } { + var calls []struct { + {{- range .Params }} + {{ .Name | Exported }} {{ .Type }} + {{- end }} + } + lock{{$obj.InterfaceName}}Mock{{.Name}}.RLock() + calls = mock.calls.{{.Name}} + lock{{$obj.InterfaceName}}Mock{{.Name}}.RUnlock() + return calls +} {{ end -}} {{ end -}}`