diff --git a/internal/template/template.go b/internal/template/template.go index 9e2672c..b2eaa10 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -48,7 +48,7 @@ import ( {{- if not $.SkipEnsure -}} // Ensure, that {{.MockName}} does implement {{$.SrcPkgQualifier}}{{.InterfaceName}}. // If this is not the case, regenerate this file with moq. -var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}} +var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}} {{- if .TypeParams }}[ {{- range $index, $param := .TypeParams}} {{- if $index}}, {{end -}} @@ -83,7 +83,7 @@ var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}} // // and then make assertions. // // } -type {{.MockName}} +type {{.MockName}} {{- if .TypeParams -}} [{{- range $index, $param := .TypeParams}} {{- if $index}}, {{end}}{{$param.Name | Exported}} {{$param.TypeString}} @@ -116,7 +116,7 @@ func (mock *{{$mock.MockName}} [{{- range $index, $param := $mock.TypeParams}} {{- if $index}}, {{end}}{{$param.Name | Exported}} {{- end -}}] -{{- end -}} +{{- end -}} ) {{.Name}}({{.ArgList}}) {{.ReturnArgTypeList}} { {{- if not $.StubImpl}} if mock.{{.Name}}Func == nil { @@ -166,7 +166,7 @@ func (mock *{{$mock.MockName}} [{{- range $index, $param := $mock.TypeParams}} {{- if $index}}, {{end}}{{$param.Name | Exported}} {{- end -}}] -{{- end -}} +{{- end -}} ) {{.Name}}Calls() []struct { {{- range .Params}} {{.Name | Exported}} {{.TypeString}} @@ -184,7 +184,13 @@ func (mock *{{$mock.MockName}} } {{- if $.WithResets}} // Reset{{.Name}}Calls reset all the calls that were made to {{.Name}}. -func (mock *{{$mock.MockName}}) Reset{{.Name}}Calls() { +func (mock *{{$mock.MockName}} +{{- if $mock.TypeParams -}} + [{{- range $index, $param := $mock.TypeParams}} + {{- if $index}}, {{end}}{{$param.Name | Exported}} + {{- end -}}] +{{- end -}} +) Reset{{.Name}}Calls() { mock.lock{{.Name}}.Lock() mock.calls.{{.Name}} = nil mock.lock{{.Name}}.Unlock() @@ -193,7 +199,13 @@ func (mock *{{$mock.MockName}}) Reset{{.Name}}Calls() { {{end -}} {{- if $.WithResets}} // ResetCalls reset all the calls that were made to all mocked methods. -func (mock *{{$mock.MockName}}) ResetCalls() { +func (mock *{{$mock.MockName}} +{{- if $mock.TypeParams -}} + [{{- range $index, $param := $mock.TypeParams}} + {{- if $index}}, {{end}}{{$param.Name | Exported}} + {{- end -}}] +{{- end -}} +) ResetCalls() { {{- range .Methods}} mock.lock{{.Name}}.Lock() mock.calls.{{.Name}} = nil diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 1e31cfb..4eb5212 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -403,7 +403,7 @@ func TestMockGolden(t *testing.T) { { name: "WithResets", cfg: Config{SrcDir: "testpackages/withresets", WithResets: true}, - interfaces: []string{"ResetStore"}, + interfaces: []string{"ResetStore", "ResetStoreGeneric"}, goldenFile: filepath.Join("testpackages/withresets", "withresets_moq.golden.go"), }, { diff --git a/pkg/moq/testpackages/withresets/withresets.go b/pkg/moq/testpackages/withresets/withresets.go index 8a87bff..d6195d0 100644 --- a/pkg/moq/testpackages/withresets/withresets.go +++ b/pkg/moq/testpackages/withresets/withresets.go @@ -16,3 +16,8 @@ type ResetStore interface { Create(ctx context.Context, person *Reset, confirm bool) error ClearCache(id string) } + +type ResetStoreGeneric[T, S any] interface { + Get(ctx context.Context, id T) (string, error) + Create(ctx context.Context, id T, value S) error +} diff --git a/pkg/moq/testpackages/withresets/withresets_moq.golden.go b/pkg/moq/testpackages/withresets/withresets_moq.golden.go index db08d32..c5395fd 100644 --- a/pkg/moq/testpackages/withresets/withresets_moq.golden.go +++ b/pkg/moq/testpackages/withresets/withresets_moq.golden.go @@ -215,3 +215,156 @@ func (mock *ResetStoreMock) ResetCalls() { mock.calls.Get = nil mock.lockGet.Unlock() } + +// Ensure, that ResetStoreGenericMock does implement ResetStoreGeneric. +// If this is not the case, regenerate this file with moq. +var _ ResetStoreGeneric[any, any] = &ResetStoreGenericMock[any, any]{} + +// ResetStoreGenericMock is a mock implementation of ResetStoreGeneric. +// +// func TestSomethingThatUsesResetStoreGeneric(t *testing.T) { +// +// // make and configure a mocked ResetStoreGeneric +// mockedResetStoreGeneric := &ResetStoreGenericMock{ +// CreateFunc: func(ctx context.Context, id T, value S) error { +// panic("mock out the Create method") +// }, +// GetFunc: func(ctx context.Context, id T) (string, error) { +// panic("mock out the Get method") +// }, +// } +// +// // use mockedResetStoreGeneric in code that requires ResetStoreGeneric +// // and then make assertions. +// +// } +type ResetStoreGenericMock[T any, S any] struct { + // CreateFunc mocks the Create method. + CreateFunc func(ctx context.Context, id T, value S) error + + // GetFunc mocks the Get method. + GetFunc func(ctx context.Context, id T) (string, error) + + // calls tracks calls to the methods. + calls struct { + // Create holds details about calls to the Create method. + Create []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // ID is the id argument value. + ID T + // Value is the value argument value. + Value S + } + // Get holds details about calls to the Get method. + Get []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // ID is the id argument value. + ID T + } + } + lockCreate sync.RWMutex + lockGet sync.RWMutex +} + +// Create calls CreateFunc. +func (mock *ResetStoreGenericMock[T, S]) Create(ctx context.Context, id T, value S) error { + if mock.CreateFunc == nil { + panic("ResetStoreGenericMock.CreateFunc: method is nil but ResetStoreGeneric.Create was just called") + } + callInfo := struct { + Ctx context.Context + ID T + Value S + }{ + Ctx: ctx, + ID: id, + Value: value, + } + mock.lockCreate.Lock() + mock.calls.Create = append(mock.calls.Create, callInfo) + mock.lockCreate.Unlock() + return mock.CreateFunc(ctx, id, value) +} + +// CreateCalls gets all the calls that were made to Create. +// Check the length with: +// +// len(mockedResetStoreGeneric.CreateCalls()) +func (mock *ResetStoreGenericMock[T, S]) CreateCalls() []struct { + Ctx context.Context + ID T + Value S +} { + var calls []struct { + Ctx context.Context + ID T + Value S + } + mock.lockCreate.RLock() + calls = mock.calls.Create + mock.lockCreate.RUnlock() + return calls +} + +// ResetCreateCalls reset all the calls that were made to Create. +func (mock *ResetStoreGenericMock[T, S]) ResetCreateCalls() { + mock.lockCreate.Lock() + mock.calls.Create = nil + mock.lockCreate.Unlock() +} + +// Get calls GetFunc. +func (mock *ResetStoreGenericMock[T, S]) Get(ctx context.Context, id T) (string, error) { + if mock.GetFunc == nil { + panic("ResetStoreGenericMock.GetFunc: method is nil but ResetStoreGeneric.Get was just called") + } + callInfo := struct { + Ctx context.Context + ID T + }{ + Ctx: ctx, + ID: id, + } + mock.lockGet.Lock() + mock.calls.Get = append(mock.calls.Get, callInfo) + mock.lockGet.Unlock() + return mock.GetFunc(ctx, id) +} + +// GetCalls gets all the calls that were made to Get. +// Check the length with: +// +// len(mockedResetStoreGeneric.GetCalls()) +func (mock *ResetStoreGenericMock[T, S]) GetCalls() []struct { + Ctx context.Context + ID T +} { + var calls []struct { + Ctx context.Context + ID T + } + mock.lockGet.RLock() + calls = mock.calls.Get + mock.lockGet.RUnlock() + return calls +} + +// ResetGetCalls reset all the calls that were made to Get. +func (mock *ResetStoreGenericMock[T, S]) ResetGetCalls() { + mock.lockGet.Lock() + mock.calls.Get = nil + mock.lockGet.Unlock() +} + +// ResetCalls reset all the calls that were made to all mocked methods. +func (mock *ResetStoreGenericMock[T, S]) ResetCalls() { + mock.lockCreate.Lock() + mock.calls.Create = nil + mock.lockCreate.Unlock() + + mock.lockGet.Lock() + mock.calls.Get = nil + mock.lockGet.Unlock() +}