Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Generics support to -with-resets #229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
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.

Loading