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

View File

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

View File

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