Improved the way calls and locks work

This commit is contained in:
Mat Ryer 2017-07-11 21:27:08 +01:00
parent b442eec838
commit 9dfde8fc79
3 changed files with 165 additions and 22 deletions

134
example/mockpersonstore_test.go Executable file
View File

@ -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
}

View File

@ -2,7 +2,6 @@ package moq
import ( import (
"bytes" "bytes"
"log"
"strings" "strings"
"testing" "testing"
) )
@ -26,10 +25,10 @@ func TestMoq(t *testing.T) {
"GetFunc func(ctx context.Context, id string) (*Person, error)", "GetFunc func(ctx context.Context, id string) (*Person, error)",
"func (mock *PersonStoreMock) Create(ctx context.Context, person *Person, confirm bool) error", "func (mock *PersonStoreMock) Create(ctx context.Context, person *Person, confirm bool) error",
"func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, 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.CreateFunc is nil but PersonStore.Create was just called\")",
"panic(\"moq: PersonStoreMock.GetFunc is nil but was just called\")", "panic(\"moq: PersonStoreMock.GetFunc is nil but PersonStore.Get was just called\")",
"lockPersonStoreMockGet.Lock()", "lockPersonStoreMockGet.Lock()",
"mock.CallsTo.Get = append(mock.CallsTo.Get, struct{", "mock.calls.Get = append(mock.calls.Get, callInfo)",
"lockPersonStoreMockGet.Unlock()", "lockPersonStoreMockGet.Unlock()",
} }
for _, str := range strs { for _, str := range strs {
@ -37,7 +36,6 @@ func TestMoq(t *testing.T) {
t.Errorf("expected but missing: \"%s\"", str) t.Errorf("expected but missing: \"%s\"", str)
} }
} }
} }
func TestMoqExplicitPackage(t *testing.T) { func TestMoqExplicitPackage(t *testing.T) {
@ -65,7 +63,6 @@ func TestMoqExplicitPackage(t *testing.T) {
t.Errorf("expected but missing: \"%s\"", str) t.Errorf("expected but missing: \"%s\"", str)
} }
} }
log.Println(s)
} }
// TestVeradicArguments tests to ensure variadic work as // TestVeradicArguments tests to ensure variadic work as

View File

@ -16,11 +16,9 @@ import (
) )
{{ range $i, $obj := .Objects -}} {{ range $i, $obj := .Objects -}}
// These locks allow you to use the mocks in a safe way
// in concurrent code.
var ( var (
{{- range .Methods }} {{- range .Methods }}
lock{{$obj.InterfaceName}}Mock{{.Name}} sync.Mutex lock{{$obj.InterfaceName}}Mock{{.Name}} sync.RWMutex
{{- end }} {{- end }}
) )
@ -37,12 +35,6 @@ var (
// //
// // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}} // // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}}
// // and then make assertions. // // 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 { type {{.InterfaceName}}Mock struct {
@ -50,8 +42,8 @@ type {{.InterfaceName}}Mock struct {
// {{.Name}}Func mocks the {{.Name}} method. // {{.Name}}Func mocks the {{.Name}} method.
{{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}} {{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}}
{{ end }} {{ end }}
// CallsTo tracks calls to the methods. // calls tracks calls to the methods.
CallsTo struct { calls struct {
{{- range .Methods }} {{- range .Methods }}
// {{ .Name }} holds details about calls to the {{.Name}} method. // {{ .Name }} holds details about calls to the {{.Name}} method.
{{ .Name }} []struct { {{ .Name }} []struct {
@ -67,10 +59,9 @@ type {{.InterfaceName}}Mock struct {
// {{.Name}} calls {{.Name}}Func. // {{.Name}} calls {{.Name}}Func.
func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist}} { func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist}} {
if mock.{{.Name}}Func == nil { 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() callInfo := struct {
mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{
{{- range .Params }} {{- range .Params }}
{{ .Name | Exported }} {{ .Type }} {{ .Name | Exported }} {{ .Type }}
{{- end }} {{- end }}
@ -78,7 +69,9 @@ func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist
{{- range .Params }} {{- range .Params }}
{{ .Name | Exported }}: {{ .Name }}, {{ .Name | Exported }}: {{ .Name }},
{{- end }} {{- end }}
}) }
lock{{$obj.InterfaceName}}Mock{{.Name}}.Lock()
mock.calls.{{.Name}} = append(mock.calls.{{.Name}}, callInfo)
lock{{$obj.InterfaceName}}Mock{{.Name}}.Unlock() lock{{$obj.InterfaceName}}Mock{{.Name}}.Unlock()
{{- if .ReturnArglist }} {{- if .ReturnArglist }}
return mock.{{.Name}}Func({{.ArgCallList}}) return mock.{{.Name}}Func({{.ArgCallList}})
@ -86,5 +79,24 @@ func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist
mock.{{.Name}}Func({{.ArgCallList}}) mock.{{.Name}}Func({{.ArgCallList}})
{{- end }} {{- 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 -}}
{{ end -}}` {{ end -}}`