Skip to content

Commit f1d733d

Browse files
Add Generics support to -with-resets
1 parent 5dfcd56 commit f1d733d

File tree

4 files changed

+177
-7
lines changed

4 files changed

+177
-7
lines changed

internal/template/template.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import (
4848
{{- if not $.SkipEnsure -}}
4949
// Ensure, that {{.MockName}} does implement {{$.SrcPkgQualifier}}{{.InterfaceName}}.
5050
// If this is not the case, regenerate this file with moq.
51-
var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}}
51+
var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}}
5252
{{- if .TypeParams }}[
5353
{{- range $index, $param := .TypeParams}}
5454
{{- if $index}}, {{end -}}
@@ -83,7 +83,7 @@ var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}}
8383
// // and then make assertions.
8484
//
8585
// }
86-
type {{.MockName}}
86+
type {{.MockName}}
8787
{{- if .TypeParams -}}
8888
[{{- range $index, $param := .TypeParams}}
8989
{{- if $index}}, {{end}}{{$param.Name | Exported}} {{$param.TypeString}}
@@ -116,7 +116,7 @@ func (mock *{{$mock.MockName}}
116116
[{{- range $index, $param := $mock.TypeParams}}
117117
{{- if $index}}, {{end}}{{$param.Name | Exported}}
118118
{{- end -}}]
119-
{{- end -}}
119+
{{- end -}}
120120
) {{.Name}}({{.ArgList}}) {{.ReturnArgTypeList}} {
121121
{{- if not $.StubImpl}}
122122
if mock.{{.Name}}Func == nil {
@@ -166,7 +166,7 @@ func (mock *{{$mock.MockName}}
166166
[{{- range $index, $param := $mock.TypeParams}}
167167
{{- if $index}}, {{end}}{{$param.Name | Exported}}
168168
{{- end -}}]
169-
{{- end -}}
169+
{{- end -}}
170170
) {{.Name}}Calls() []struct {
171171
{{- range .Params}}
172172
{{.Name | Exported}} {{.TypeString}}
@@ -184,7 +184,13 @@ func (mock *{{$mock.MockName}}
184184
}
185185
{{- if $.WithResets}}
186186
// Reset{{.Name}}Calls reset all the calls that were made to {{.Name}}.
187-
func (mock *{{$mock.MockName}}) Reset{{.Name}}Calls() {
187+
func (mock *{{$mock.MockName}}
188+
{{- if $mock.TypeParams -}}
189+
[{{- range $index, $param := $mock.TypeParams}}
190+
{{- if $index}}, {{end}}{{$param.Name | Exported}}
191+
{{- end -}}]
192+
{{- end -}}
193+
) Reset{{.Name}}Calls() {
188194
mock.lock{{.Name}}.Lock()
189195
mock.calls.{{.Name}} = nil
190196
mock.lock{{.Name}}.Unlock()
@@ -193,7 +199,13 @@ func (mock *{{$mock.MockName}}) Reset{{.Name}}Calls() {
193199
{{end -}}
194200
{{- if $.WithResets}}
195201
// ResetCalls reset all the calls that were made to all mocked methods.
196-
func (mock *{{$mock.MockName}}) ResetCalls() {
202+
func (mock *{{$mock.MockName}}
203+
{{- if $mock.TypeParams -}}
204+
[{{- range $index, $param := $mock.TypeParams}}
205+
{{- if $index}}, {{end}}{{$param.Name | Exported}}
206+
{{- end -}}]
207+
{{- end -}}
208+
) ResetCalls() {
197209
{{- range .Methods}}
198210
mock.lock{{.Name}}.Lock()
199211
mock.calls.{{.Name}} = nil

pkg/moq/moq_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func TestMockGolden(t *testing.T) {
403403
{
404404
name: "WithResets",
405405
cfg: Config{SrcDir: "testpackages/withresets", WithResets: true},
406-
interfaces: []string{"ResetStore"},
406+
interfaces: []string{"ResetStore", "ResetStoreGeneric"},
407407
goldenFile: filepath.Join("testpackages/withresets", "withresets_moq.golden.go"),
408408
},
409409
{

pkg/moq/testpackages/withresets/withresets.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ type ResetStore interface {
1616
Create(ctx context.Context, person *Reset, confirm bool) error
1717
ClearCache(id string)
1818
}
19+
20+
type ResetStoreGeneric[T, S any] interface {
21+
Get(ctx context.Context, id T) (string, error)
22+
Create(ctx context.Context, id T, value S) error
23+
}

pkg/moq/testpackages/withresets/withresets_moq.golden.go

+153
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)