Skip to content

Commit

Permalink
feat: support generic interface generation (vektra#175)
Browse files Browse the repository at this point in the history
Allow the generation of mocks for generics as introduced in golang 1.18
  • Loading branch information
cgorenflo authored and LandonTClipp committed Nov 20, 2023
1 parent 3f801fc commit 835b694
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
40 changes: 36 additions & 4 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,22 @@ 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}} = &{{.MockName}}{}
var _ {{$.SrcPkgQualifier}}{{.InterfaceName -}}
{{- if .TypeParams }}[
{{- range $index, $param := .TypeParams}}
{{- if $index}}, {{end -}}
{{if $param.Constraint}}{{$param.Constraint.String}}{{else}}{{$param.TypeString}}{{end}}
{{- end -}}
]
{{- end }} = &{{.MockName}}
{{- if .TypeParams }}[
{{- range $index, $param := .TypeParams}}
{{- if $index}}, {{end -}}
{{if $param.Constraint}}{{$param.Constraint.String}}{{else}}{{$param.TypeString}}{{end}}
{{- end -}}
]
{{- end -}}
{}
{{- end}}
// {{.MockName}} is a mock implementation of {{$.SrcPkgQualifier}}{{.InterfaceName}}.
Expand All @@ -68,7 +83,12 @@ var _ {{$.SrcPkgQualifier}}{{.InterfaceName}} = &{{.MockName}}{}
// // and then make assertions.
//
// }
type {{.MockName}} struct {
type {{.MockName}}
{{- if .TypeParams -}}
[{{- range $index, $param := .TypeParams}}
{{- if $index}}, {{end}}{{$param.Name | Exported}} {{$param.TypeString}}
{{- end -}}]
{{- end }} struct {
{{- range .Methods}}
// {{.Name}}Func mocks the {{.Name}} method.
{{.Name}}Func func({{.ArgList}}) {{.ReturnArgTypeList}}
Expand All @@ -91,7 +111,13 @@ type {{.MockName}} struct {
}
{{range .Methods}}
// {{.Name}} calls {{.Name}}Func.
func (mock *{{$mock.MockName}}) {{.Name}}({{.ArgList}}) {{.ReturnArgTypeList}} {
func (mock *{{$mock.MockName}}
{{- if $mock.TypeParams -}}
[{{- range $index, $param := $mock.TypeParams}}
{{- if $index}}, {{end}}{{$param.Name | Exported}}
{{- end -}}]
{{- end -}}
) {{.Name}}({{.ArgList}}) {{.ReturnArgTypeList}} {
{{- if not $.StubImpl}}
if mock.{{.Name}}Func == nil {
panic("{{$mock.MockName}}.{{.Name}}Func: method is nil but {{$mock.InterfaceName}}.{{.Name}} was just called")
Expand Down Expand Up @@ -134,7 +160,13 @@ func (mock *{{$mock.MockName}}) {{.Name}}({{.ArgList}}) {{.ReturnArgTypeList}} {
// {{.Name}}Calls gets all the calls that were made to {{.Name}}.
// Check the length with:
// len(mocked{{$mock.InterfaceName}}.{{.Name}}Calls())
func (mock *{{$mock.MockName}}) {{.Name}}Calls() []struct {
func (mock *{{$mock.MockName}}
{{- if $mock.TypeParams -}}
[{{- range $index, $param := $mock.TypeParams}}
{{- if $index}}, {{end}}{{$param.Name | Exported}}
{{- end -}}]
{{- end -}}
) {{.Name}}Calls() []struct {
{{- range .Params}}
{{.Name | Exported}} {{.TypeString}}
{{- end}}
Expand Down
7 changes: 7 additions & 0 deletions internal/template/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"fmt"
"go/types"
"strings"

"github.com/matryer/moq/internal/registry"
Expand Down Expand Up @@ -33,6 +34,7 @@ func (d Data) MocksSomeMethod() bool {
type MockData struct {
InterfaceName string
MockName string
TypeParams []TypeParamData
Methods []MethodData
}

Expand Down Expand Up @@ -87,6 +89,11 @@ func (m MethodData) ReturnArgNameList() string {
return strings.Join(params, ", ")
}

type TypeParamData struct {
ParamData
Constraint types.Type
}

// ParamData is the data which represents a parameter to some method of
// an interface.
type ParamData struct {
Expand Down

0 comments on commit 835b694

Please sign in to comment.