Added concurrency safe counters

This commit is contained in:
Mat Ryer 2017-07-04 18:09:47 -06:00
parent 78083d2e52
commit 3dbdbe86c1
2 changed files with 14 additions and 1 deletions

View File

@ -221,20 +221,30 @@ var moqTemplate = `package {{.PackageName}}
// } // }
// //
// // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}} // // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}}
// // and then make assertions.
// //
// } // }
type {{.InterfaceName}}Mock struct { type {{.InterfaceName}}Mock struct {
{{- range .Methods }} {{- range .Methods }}
// {{.Name}}Func mocks the {{.Name}} method. // {{.Name}}Func mocks the {{.Name}} method.
{{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}} {{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}}
{{ end }}
// CallsTo gets counters for each of the methods indicating
// how many times each one was called.
CallsTo struct {
{{- range .Methods }}
// {{ .Name }} holds the number of calls to the {{.Name}} method.
{{ .Name }} uint64
{{- end }} {{- end }}
} }
}
{{ range .Methods }} {{ range .Methods }}
// {{.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 was just called")
} }
atomic.AddUint64(&mock.CallsTo.{{.Name}}, 1) // count this
{{- if .ReturnArglist }} {{- if .ReturnArglist }}
return mock.{{.Name}}Func({{.ArgCallList}}) return mock.{{.Name}}Func({{.ArgCallList}})
{{- else }} {{- else }}

View File

@ -27,6 +27,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\")",
"CallsTo struct {",
"atomic.AddUint64(&mock.CallsTo.Get, 1)",
"ClearCache uint64",
} }
for _, str := range strs { for _, str := range strs {
if !strings.Contains(s, str) { if !strings.Contains(s, str) {