Skip to content

Commit

Permalink
Add Generics support to -with-resets (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewradamis-vividseats authored Nov 22, 2024
1 parent 5dfcd56 commit 00ca6b4
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 7 deletions.
24 changes: 18 additions & 6 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}}
Expand All @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
{
Expand Down
5 changes: 5 additions & 0 deletions pkg/moq/testpackages/withresets/withresets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 20 in pkg/moq/testpackages/withresets/withresets.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type ResetStoreGeneric should have comment or be unexported

Check failure on line 20 in pkg/moq/testpackages/withresets/withresets.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type ResetStoreGeneric should have comment or be unexported

Check failure on line 20 in pkg/moq/testpackages/withresets/withresets.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type ResetStoreGeneric should have comment or be unexported

Check failure on line 20 in pkg/moq/testpackages/withresets/withresets.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type ResetStoreGeneric should have comment or be unexported
Get(ctx context.Context, id T) (string, error)
Create(ctx context.Context, id T, value S) error
}
153 changes: 153 additions & 0 deletions pkg/moq/testpackages/withresets/withresets_moq.golden.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00ca6b4

Please sign in to comment.