diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index a006121..c4d4b8e 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -221,84 +221,3 @@ var templateFuncs = template.FuncMap{ 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 -}}` diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index d54d70b..ab17925 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -2,6 +2,7 @@ package moq import ( "bytes" + "log" "strings" "testing" ) @@ -27,9 +28,9 @@ func TestMoq(t *testing.T) { "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\")", - "mock.CallsTo.lockGet.Lock()", + "lockPersonStoreMockGet.Lock()", "mock.CallsTo.Get = append(mock.CallsTo.Get, struct{", - "mock.CallsTo.lockGet.Unlock()", + "lockPersonStoreMockGet.Unlock()", } for _, str := range strs { if !strings.Contains(s, str) { @@ -64,6 +65,7 @@ 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 new file mode 100644 index 0000000..e9029fa --- /dev/null +++ b/pkg/moq/template.go @@ -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 -}}`