diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 072cb7a..e750a68 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -38,9 +38,9 @@ func TestMoq(t *testing.T) { "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "panic(\"PersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")", "panic(\"PersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")", - "lockPersonStoreMockGet.Lock()", + "mock.lockGet.Lock()", "mock.calls.Get = append(mock.calls.Get, callInfo)", - "lockPersonStoreMockGet.Unlock()", + "mock.lockGet.Unlock()", "// ID is the id argument value", } for _, str := range strs { @@ -72,9 +72,9 @@ func TestMoqWithStaticCheck(t *testing.T) { "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "panic(\"PersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")", "panic(\"PersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")", - "lockPersonStoreMockGet.Lock()", + "mock.lockGet.Lock()", "mock.calls.Get = append(mock.calls.Get, callInfo)", - "lockPersonStoreMockGet.Unlock()", + "mock.lockGet.Unlock()", "// ID is the id argument value", } for _, str := range strs { @@ -105,9 +105,9 @@ func TestMoqWithAlias(t *testing.T) { "func (mock *AnotherPersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "panic(\"AnotherPersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")", "panic(\"AnotherPersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")", - "lockAnotherPersonStoreMockGet.Lock()", + "mock.lockGet.Lock()", "mock.calls.Get = append(mock.calls.Get, callInfo)", - "lockAnotherPersonStoreMockGet.Unlock()", + "mock.lockGet.Unlock()", "// ID is the id argument value", } for _, str := range strs { diff --git a/pkg/moq/template.go b/pkg/moq/template.go index bc33de2..7eb9fdd 100644 --- a/pkg/moq/template.go +++ b/pkg/moq/template.go @@ -4,6 +4,7 @@ package moq var moqImports = []string{} // moqTemplate is the template for mocked code. +// language=GoTemplate var moqTemplate = `// Code generated by moq; DO NOT EDIT. // github.com/matryer/moq @@ -17,11 +18,6 @@ import ( ) {{ range $i, $obj := .Objects -}} -var ( -{{- range .Methods }} - lock{{$obj.MockName}}{{.Name}} sync.RWMutex -{{- end }} -) // Ensure, that {{.MockName}} does implement {{$sourcePackagePrefix}}{{.InterfaceName}}. // If this is not the case, regenerate this file with moq. @@ -59,6 +55,9 @@ type {{.MockName}} struct { } {{- end }} } +{{- range .Methods }} + lock{{.Name}} sync.RWMutex +{{- end }} } {{ range .Methods }} // {{.Name}} calls {{.Name}}Func. @@ -75,9 +74,9 @@ func (mock *{{$obj.MockName}}) {{.Name}}({{.Arglist}}) {{.ReturnArglist}} { {{ .Name | Exported }}: {{ .Name }}, {{- end }} } - lock{{$obj.MockName}}{{.Name}}.Lock() + mock.lock{{.Name}}.Lock() mock.calls.{{.Name}} = append(mock.calls.{{.Name}}, callInfo) - lock{{$obj.MockName}}{{.Name}}.Unlock() + mock.lock{{.Name}}.Unlock() {{- if .ReturnArglist }} return mock.{{.Name}}Func({{.ArgCallList}}) {{- else }} @@ -98,9 +97,9 @@ func (mock *{{$obj.MockName}}) {{.Name}}Calls() []struct { {{ .Name | Exported }} {{ .Type }} {{- end }} } - lock{{$obj.MockName}}{{.Name}}.RLock() + mock.lock{{.Name}}.RLock() calls = mock.calls.{{.Name}} - lock{{$obj.MockName}}{{.Name}}.RUnlock() + mock.lock{{.Name}}.RUnlock() return calls } {{ end -}} diff --git a/pkg/moq/testpackages/imports/testdata/gofmt.golden.go b/pkg/moq/testpackages/imports/testdata/gofmt.golden.go index ed565be..9c50834 100644 --- a/pkg/moq/testpackages/imports/testdata/gofmt.golden.go +++ b/pkg/moq/testpackages/imports/testdata/gofmt.golden.go @@ -8,11 +8,6 @@ import ( "sync" ) -var ( - lockDoSomethingMockAnother sync.RWMutex - lockDoSomethingMockDo sync.RWMutex -) - // Ensure, that DoSomethingMock does implement DoSomething. // If this is not the case, regenerate this file with moq. var _ DoSomething = &DoSomethingMock{} @@ -55,6 +50,8 @@ type DoSomethingMock struct { Thing one.Thing } } + lockAnother sync.RWMutex + lockDo sync.RWMutex } // Another calls AnotherFunc. @@ -67,9 +64,9 @@ func (mock *DoSomethingMock) Another(thing one.Thing) error { }{ Thing: thing, } - lockDoSomethingMockAnother.Lock() + mock.lockAnother.Lock() mock.calls.Another = append(mock.calls.Another, callInfo) - lockDoSomethingMockAnother.Unlock() + mock.lockAnother.Unlock() return mock.AnotherFunc(thing) } @@ -82,9 +79,9 @@ func (mock *DoSomethingMock) AnotherCalls() []struct { var calls []struct { Thing one.Thing } - lockDoSomethingMockAnother.RLock() + mock.lockAnother.RLock() calls = mock.calls.Another - lockDoSomethingMockAnother.RUnlock() + mock.lockAnother.RUnlock() return calls } @@ -98,9 +95,9 @@ func (mock *DoSomethingMock) Do(thing one.Thing) error { }{ Thing: thing, } - lockDoSomethingMockDo.Lock() + mock.lockDo.Lock() mock.calls.Do = append(mock.calls.Do, callInfo) - lockDoSomethingMockDo.Unlock() + mock.lockDo.Unlock() return mock.DoFunc(thing) } @@ -113,8 +110,8 @@ func (mock *DoSomethingMock) DoCalls() []struct { var calls []struct { Thing one.Thing } - lockDoSomethingMockDo.RLock() + mock.lockDo.RLock() calls = mock.calls.Do - lockDoSomethingMockDo.RUnlock() + mock.lockDo.RUnlock() return calls } diff --git a/pkg/moq/testpackages/imports/testdata/goimports.golden.go b/pkg/moq/testpackages/imports/testdata/goimports.golden.go index fa5a8c9..526ab6b 100644 --- a/pkg/moq/testpackages/imports/testdata/goimports.golden.go +++ b/pkg/moq/testpackages/imports/testdata/goimports.golden.go @@ -9,11 +9,6 @@ import ( "github.com/matryer/moq/pkg/moq/testpackages/imports/one" ) -var ( - lockDoSomethingMockAnother sync.RWMutex - lockDoSomethingMockDo sync.RWMutex -) - // Ensure, that DoSomethingMock does implement DoSomething. // If this is not the case, regenerate this file with moq. var _ DoSomething = &DoSomethingMock{} @@ -56,6 +51,8 @@ type DoSomethingMock struct { Thing one.Thing } } + lockAnother sync.RWMutex + lockDo sync.RWMutex } // Another calls AnotherFunc. @@ -68,9 +65,9 @@ func (mock *DoSomethingMock) Another(thing one.Thing) error { }{ Thing: thing, } - lockDoSomethingMockAnother.Lock() + mock.lockAnother.Lock() mock.calls.Another = append(mock.calls.Another, callInfo) - lockDoSomethingMockAnother.Unlock() + mock.lockAnother.Unlock() return mock.AnotherFunc(thing) } @@ -83,9 +80,9 @@ func (mock *DoSomethingMock) AnotherCalls() []struct { var calls []struct { Thing one.Thing } - lockDoSomethingMockAnother.RLock() + mock.lockAnother.RLock() calls = mock.calls.Another - lockDoSomethingMockAnother.RUnlock() + mock.lockAnother.RUnlock() return calls } @@ -99,9 +96,9 @@ func (mock *DoSomethingMock) Do(thing one.Thing) error { }{ Thing: thing, } - lockDoSomethingMockDo.Lock() + mock.lockDo.Lock() mock.calls.Do = append(mock.calls.Do, callInfo) - lockDoSomethingMockDo.Unlock() + mock.lockDo.Unlock() return mock.DoFunc(thing) } @@ -114,8 +111,8 @@ func (mock *DoSomethingMock) DoCalls() []struct { var calls []struct { Thing one.Thing } - lockDoSomethingMockDo.RLock() + mock.lockDo.RLock() calls = mock.calls.Do - lockDoSomethingMockDo.RUnlock() + mock.lockDo.RUnlock() return calls } diff --git a/pkg/moq/testpackages/variadic/testdata/echoer.golden.go b/pkg/moq/testpackages/variadic/testdata/echoer.golden.go index a127ad5..df7f8e7 100644 --- a/pkg/moq/testpackages/variadic/testdata/echoer.golden.go +++ b/pkg/moq/testpackages/variadic/testdata/echoer.golden.go @@ -7,10 +7,6 @@ import ( "sync" ) -var ( - lockEchoerMockEcho sync.RWMutex -) - // Ensure, that EchoerMock does implement Echoer. // If this is not the case, regenerate this file with moq. var _ Echoer = &EchoerMock{} @@ -42,6 +38,7 @@ type EchoerMock struct { Ss []string } } + lockEcho sync.RWMutex } // Echo calls EchoFunc. @@ -54,9 +51,9 @@ func (mock *EchoerMock) Echo(ss ...string) []string { }{ Ss: ss, } - lockEchoerMockEcho.Lock() + mock.lockEcho.Lock() mock.calls.Echo = append(mock.calls.Echo, callInfo) - lockEchoerMockEcho.Unlock() + mock.lockEcho.Unlock() return mock.EchoFunc(ss...) } @@ -69,8 +66,8 @@ func (mock *EchoerMock) EchoCalls() []struct { var calls []struct { Ss []string } - lockEchoerMockEcho.RLock() + mock.lockEcho.RLock() calls = mock.calls.Echo - lockEchoerMockEcho.RUnlock() + mock.lockEcho.RUnlock() return calls }