changed the way locks work to keep them out of the mock structure itself

This commit is contained in:
Mat Ryer 2017-07-11 21:00:24 +01:00
parent c156bf3a75
commit b442eec838
3 changed files with 94 additions and 83 deletions

View File

@ -221,84 +221,3 @@ var templateFuncs = template.FuncMap{
return strings.ToUpper(s[0:1]) + s[1:] return strings.ToUpper(s[0:1]) + s[1:]
}, },
} }
// moqImports are the imports all moq files get.
var moqImports = []string{"sync"}
// moqTemplate is the template for mocked code.
var moqTemplate = `package {{.PackageName}}
// AUTOGENERATED BY MOQ
// github.com/matryer/moq
import (
{{- range .Imports }}
"{{.}}"
{{- end }}
)
{{ range $i, $obj := .Objects }}
// {{.InterfaceName}}Mock is a mock implementation of {{.InterfaceName}}.
//
// func TestSomethingThatUses{{.InterfaceName}}(t *testing.T) {
//
// // make and configure a mocked {{.InterfaceName}}
// mocked{{.InterfaceName}} := &{{.InterfaceName}}Mock{ {{ range .Methods }}
// {{.Name}}Func: func({{ .Arglist }}) {{.ReturnArglist}} {
// panic("TODO: mock out the {{.Name}} method")
// },{{- end }}
// }
//
// // 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 {
{{- range .Methods }}
lock{{.Name}} sync.Mutex // protects {{ .Name }}
// {{ .Name }} holds details about calls to the {{.Name}} method.
{{ .Name }} []struct {
{{- range .Params }}
// {{ .Name | Exported }} is the {{ .Name }} argument value.
{{ .Name | Exported }} {{ .Type }}
{{- end }}
}
{{- end }}
}
}
{{ range .Methods }}
// {{.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")
}
mock.CallsTo.lock{{.Name}}.Lock()
mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{
{{- range .Params }}
{{ .Name | Exported }} {{ .Type }}
{{- end }}
}{
{{- range .Params }}
{{ .Name | Exported }}: {{ .Name }},
{{- end }}
})
mock.CallsTo.lock{{.Name}}.Unlock()
{{- if .ReturnArglist }}
return mock.{{.Name}}Func({{.ArgCallList}})
{{- else }}
mock.{{.Name}}Func({{.ArgCallList}})
{{- end }}
}
{{ end -}}
{{ end -}}`

View File

@ -2,6 +2,7 @@ package moq
import ( import (
"bytes" "bytes"
"log"
"strings" "strings"
"testing" "testing"
) )
@ -27,9 +28,9 @@ func TestMoq(t *testing.T) {
"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 was just called\")",
"panic(\"moq: PersonStoreMock.GetFunc is nil but was just called\")", "panic(\"moq: PersonStoreMock.GetFunc is nil but was just called\")",
"mock.CallsTo.lockGet.Lock()", "lockPersonStoreMockGet.Lock()",
"mock.CallsTo.Get = append(mock.CallsTo.Get, struct{", "mock.CallsTo.Get = append(mock.CallsTo.Get, struct{",
"mock.CallsTo.lockGet.Unlock()", "lockPersonStoreMockGet.Unlock()",
} }
for _, str := range strs { for _, str := range strs {
if !strings.Contains(s, str) { if !strings.Contains(s, str) {
@ -64,6 +65,7 @@ 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

90
pkg/moq/template.go Normal file
View File

@ -0,0 +1,90 @@
package moq
// moqImports are the imports all moq files get.
var moqImports = []string{"sync"}
// moqTemplate is the template for mocked code.
var moqTemplate = `package {{.PackageName}}
// AUTOGENERATED BY MOQ - DO NOT EDIT
// github.com/matryer/moq
import (
{{- range .Imports }}
"{{.}}"
{{- end }}
)
{{ 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
{{- end }}
)
// {{.InterfaceName}}Mock is a mock implementation of {{.InterfaceName}}.
//
// func TestSomethingThatUses{{.InterfaceName}}(t *testing.T) {
//
// // make and configure a mocked {{.InterfaceName}}
// mocked{{.InterfaceName}} := &{{.InterfaceName}}Mock{ {{ range .Methods }}
// {{.Name}}Func: func({{ .Arglist }}) {{.ReturnArglist}} {
// panic("TODO: mock out the {{.Name}} method")
// },{{- end }}
// }
//
// // 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 {
{{- range .Methods }}
// {{ .Name }} holds details about calls to the {{.Name}} method.
{{ .Name }} []struct {
{{- range .Params }}
// {{ .Name | Exported }} is the {{ .Name }} argument value.
{{ .Name | Exported }} {{ .Type }}
{{- end }}
}
{{- end }}
}
}
{{ range .Methods }}
// {{.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")
}
lock{{$obj.InterfaceName}}Mock{{.Name}}.Lock()
mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{
{{- range .Params }}
{{ .Name | Exported }} {{ .Type }}
{{- end }}
}{
{{- range .Params }}
{{ .Name | Exported }}: {{ .Name }},
{{- end }}
})
lock{{$obj.InterfaceName}}Mock{{.Name}}.Unlock()
{{- if .ReturnArglist }}
return mock.{{.Name}}Func({{.ArgCallList}})
{{- else }}
mock.{{.Name}}Func({{.ArgCallList}})
{{- end }}
}
{{ end -}}
{{ end -}}`