From b9b5dc75f39f704e38fe187a0d840ff7de787f71 Mon Sep 17 00:00:00 2001 From: n0trace Date: Thu, 6 Jul 2023 12:37:03 +0800 Subject: [PATCH] support type-safe return values (#11) migrate from https://github.com/golang/mock/pull/630 more: https://github.com/golang/mock/issues/622 https://github.com/golang/mock/issues/427 https://github.com/golang/mock/issues/634 https://github.com/golang/mock/issues/657 --------- Co-authored-by: Sung Yoon Whang --- README.md | 2 + mockgen/internal/tests/typed/bugreport.go | 18 + .../internal/tests/typed/bugreport_mock.go | 111 +++ .../internal/tests/typed/bugreport_test.go | 18 + mockgen/internal/tests/typed/external.go | 21 + mockgen/internal/tests/typed/faux/faux.go | 11 + mockgen/internal/tests/typed/generics.go | 40 + mockgen/internal/tests/typed/go.mod | 10 + mockgen/internal/tests/typed/go.sum | 26 + mockgen/internal/tests/typed/other/other.go | 11 + .../tests/typed/source/mock_external_test.go | 413 +++++++++ .../tests/typed/source/mock_generics_test.go | 785 ++++++++++++++++++ mockgen/mockgen.go | 123 ++- mockgen/mockgen_test.go | 2 +- 14 files changed, 1573 insertions(+), 18 deletions(-) create mode 100644 mockgen/internal/tests/typed/bugreport.go create mode 100644 mockgen/internal/tests/typed/bugreport_mock.go create mode 100644 mockgen/internal/tests/typed/bugreport_test.go create mode 100644 mockgen/internal/tests/typed/external.go create mode 100644 mockgen/internal/tests/typed/faux/faux.go create mode 100644 mockgen/internal/tests/typed/generics.go create mode 100644 mockgen/internal/tests/typed/go.mod create mode 100644 mockgen/internal/tests/typed/go.sum create mode 100644 mockgen/internal/tests/typed/other/other.go create mode 100644 mockgen/internal/tests/typed/source/mock_external_test.go create mode 100644 mockgen/internal/tests/typed/source/mock_generics_test.go diff --git a/README.md b/README.md index 6d320ae..870f85a 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,8 @@ It supports the following flags: - `-write_package_comment`: Writes package documentation comment (godoc) if true. (default true) +- `-typed`: Generate Type-safe 'Return', 'Do', 'DoAndReturn' function. (default false) + For an example of the use of `mockgen`, see the `sample/` directory. In simple cases, you will need only the `-source` flag. diff --git a/mockgen/internal/tests/typed/bugreport.go b/mockgen/internal/tests/typed/bugreport.go new file mode 100644 index 0000000..49fc286 --- /dev/null +++ b/mockgen/internal/tests/typed/bugreport.go @@ -0,0 +1,18 @@ +package typed + +//go:generate mockgen -typed -aux_files faux=faux/faux.go -destination bugreport_mock.go -package typed -source=bugreport.go Example + +import ( + "log" + + "go.uber.org/mock/mockgen/internal/tests/typed/faux" +) + +// Source is an interface w/ an embedded foreign interface +type Source interface { + faux.Foreign +} + +func CallForeignMethod(s Source) { + log.Println(s.Method()) +} diff --git a/mockgen/internal/tests/typed/bugreport_mock.go b/mockgen/internal/tests/typed/bugreport_mock.go new file mode 100644 index 0000000..3eba52a --- /dev/null +++ b/mockgen/internal/tests/typed/bugreport_mock.go @@ -0,0 +1,111 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: bugreport.go + +// Package typed is a generated GoMock package. +package typed + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + faux "go.uber.org/mock/mockgen/internal/tests/typed/faux" +) + +// MockSource is a mock of Source interface. +type MockSource struct { + ctrl *gomock.Controller + recorder *MockSourceMockRecorder +} + +// MockSourceMockRecorder is the mock recorder for MockSource. +type MockSourceMockRecorder struct { + mock *MockSource +} + +// NewMockSource creates a new mock instance. +func NewMockSource(ctrl *gomock.Controller) *MockSource { + mock := &MockSource{ctrl: ctrl} + mock.recorder = &MockSourceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockSource) EXPECT() *MockSourceMockRecorder { + return m.recorder +} + +// Error mocks base method. +func (m *MockSource) Error() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Error") + ret0, _ := ret[0].(string) + return ret0 +} + +// Error indicates an expected call of Error. +func (mr *MockSourceMockRecorder) Error() *SourceErrorCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockSource)(nil).Error)) + return &SourceErrorCall{Call: call} +} + +// SourceErrorCall wrap *gomock.Call +type SourceErrorCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *SourceErrorCall) Return(arg0 string) *SourceErrorCall { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *SourceErrorCall) Do(f func() string) *SourceErrorCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *SourceErrorCall) DoAndReturn(f func() string) *SourceErrorCall { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Method mocks base method. +func (m *MockSource) Method() faux.Return { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Method") + ret0, _ := ret[0].(faux.Return) + return ret0 +} + +// Method indicates an expected call of Method. +func (mr *MockSourceMockRecorder) Method() *SourceMethodCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method", reflect.TypeOf((*MockSource)(nil).Method)) + return &SourceMethodCall{Call: call} +} + +// SourceMethodCall wrap *gomock.Call +type SourceMethodCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *SourceMethodCall) Return(arg0 faux.Return) *SourceMethodCall { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *SourceMethodCall) Do(f func() faux.Return) *SourceMethodCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *SourceMethodCall) DoAndReturn(f func() faux.Return) *SourceMethodCall { + c.Call = c.Call.DoAndReturn(f) + return c +} diff --git a/mockgen/internal/tests/typed/bugreport_test.go b/mockgen/internal/tests/typed/bugreport_test.go new file mode 100644 index 0000000..5b60484 --- /dev/null +++ b/mockgen/internal/tests/typed/bugreport_test.go @@ -0,0 +1,18 @@ +package typed + +import ( + "testing" + + "go.uber.org/mock/gomock" +) + +// TestValidInterface assesses whether or not the generated mock is valid +func TestValidInterface(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + s := NewMockSource(ctrl) + s.EXPECT().Method().Return("") + + CallForeignMethod(s) +} diff --git a/mockgen/internal/tests/typed/external.go b/mockgen/internal/tests/typed/external.go new file mode 100644 index 0000000..de02e02 --- /dev/null +++ b/mockgen/internal/tests/typed/external.go @@ -0,0 +1,21 @@ +package typed + +import ( + "go.uber.org/mock/mockgen/internal/tests/typed/other" + "golang.org/x/exp/constraints" +) + +//go:generate mockgen --source=external.go --destination=source/mock_external_test.go --package source -typed + +type ExternalConstraint[I constraints.Integer, F constraints.Float] interface { + One(string) string + Two(I) string + Three(I) F + Four(I) Foo[I, F] + Five(I) Baz[F] + Six(I) *Baz[F] + Seven(I) other.One[I] + Eight(F) other.Two[I, F] + Nine(Iface[I]) + Ten(*I) +} diff --git a/mockgen/internal/tests/typed/faux/faux.go b/mockgen/internal/tests/typed/faux/faux.go new file mode 100644 index 0000000..00f795e --- /dev/null +++ b/mockgen/internal/tests/typed/faux/faux.go @@ -0,0 +1,11 @@ +package faux + +type Foreign interface { + Method() Return + Embedded + error +} + +type Embedded interface{} + +type Return interface{} diff --git a/mockgen/internal/tests/typed/generics.go b/mockgen/internal/tests/typed/generics.go new file mode 100644 index 0000000..9e7746e --- /dev/null +++ b/mockgen/internal/tests/typed/generics.go @@ -0,0 +1,40 @@ +package typed + +import "go.uber.org/mock/mockgen/internal/tests/typed/other" + +//go:generate mockgen --source=generics.go --destination=source/mock_generics_test.go --package source -typed +////go:generate mockgen --destination=reflect/mock_test.go --package reflect . Bar,Bar2 + +type Bar[T any, R any] interface { + One(string) string + Two(T) string + Three(T) R + Four(T) Foo[T, R] + Five(T) Baz[T] + Six(T) *Baz[T] + Seven(T) other.One[T] + Eight(T) other.Two[T, R] + Nine(Iface[T]) + Ten(*T) + Eleven() (*other.One[T], error) + Twelve() (*other.Two[T, R], error) + Thirteen() (Baz[StructType], error) + Fourteen() (*Foo[StructType, StructType2], error) + Fifteen() (Iface[StructType], error) + Sixteen() (Baz[other.Three], error) + Seventeen() (*Foo[other.Three, other.Four], error) + Eighteen() (Iface[*other.Five], error) + Nineteen() AliasType +} + +type Foo[T any, R any] struct{} + +type Baz[T any] struct{} + +type Iface[T any] interface{} + +type StructType struct{} + +type StructType2 struct{} + +type AliasType Baz[other.Three] diff --git a/mockgen/internal/tests/typed/go.mod b/mockgen/internal/tests/typed/go.mod new file mode 100644 index 0000000..831bf3b --- /dev/null +++ b/mockgen/internal/tests/typed/go.mod @@ -0,0 +1,10 @@ +module go.uber.org/mock/mockgen/internal/tests/typed + +go 1.18 + +replace go.uber.org/mock => ../../../.. + +require ( + go.uber.org/mock v0.0.0-00010101000000-000000000000 + golang.org/x/exp v0.0.0-20220609121020-a51bd0440498 +) diff --git a/mockgen/internal/tests/typed/go.sum b/mockgen/internal/tests/typed/go.sum new file mode 100644 index 0000000..e45fa7f --- /dev/null +++ b/mockgen/internal/tests/typed/go.sum @@ -0,0 +1,26 @@ +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20220609121020-a51bd0440498 h1:TF0FvLUGEq/8wOt/9AV1nj6D4ViZGUIGCMQfCv7VRXY= +golang.org/x/exp v0.0.0-20220609121020-a51bd0440498/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys= +golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/mockgen/internal/tests/typed/other/other.go b/mockgen/internal/tests/typed/other/other.go new file mode 100644 index 0000000..9265422 --- /dev/null +++ b/mockgen/internal/tests/typed/other/other.go @@ -0,0 +1,11 @@ +package other + +type One[T any] struct{} + +type Two[T any, R any] struct{} + +type Three struct{} + +type Four struct{} + +type Five interface{} diff --git a/mockgen/internal/tests/typed/source/mock_external_test.go b/mockgen/internal/tests/typed/source/mock_external_test.go new file mode 100644 index 0000000..8e88a99 --- /dev/null +++ b/mockgen/internal/tests/typed/source/mock_external_test.go @@ -0,0 +1,413 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: external.go + +// Package source is a generated GoMock package. +package source + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + typed "go.uber.org/mock/mockgen/internal/tests/typed" + other "go.uber.org/mock/mockgen/internal/tests/typed/other" + constraints "golang.org/x/exp/constraints" +) + +// MockExternalConstraint is a mock of ExternalConstraint interface. +type MockExternalConstraint[I constraints.Integer, F constraints.Float] struct { + ctrl *gomock.Controller + recorder *MockExternalConstraintMockRecorder[I, F] +} + +// MockExternalConstraintMockRecorder is the mock recorder for MockExternalConstraint. +type MockExternalConstraintMockRecorder[I constraints.Integer, F constraints.Float] struct { + mock *MockExternalConstraint[I, F] +} + +// NewMockExternalConstraint creates a new mock instance. +func NewMockExternalConstraint[I constraints.Integer, F constraints.Float](ctrl *gomock.Controller) *MockExternalConstraint[I, F] { + mock := &MockExternalConstraint[I, F]{ctrl: ctrl} + mock.recorder = &MockExternalConstraintMockRecorder[I, F]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockExternalConstraint[I, F]) EXPECT() *MockExternalConstraintMockRecorder[I, F] { + return m.recorder +} + +// Eight mocks base method. +func (m *MockExternalConstraint[I, F]) Eight(arg0 F) other.Two[I, F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eight", arg0) + ret0, _ := ret[0].(other.Two[I, F]) + return ret0 +} + +// Eight indicates an expected call of Eight. +func (mr *MockExternalConstraintMockRecorder[I, F]) Eight(arg0 interface{}) *ExternalConstraintEightCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eight", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Eight), arg0) + return &ExternalConstraintEightCall[I, F]{Call: call} +} + +// ExternalConstraintEightCall wrap *gomock.Call +type ExternalConstraintEightCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintEightCall[I, F]) Return(arg0 other.Two[I, F]) *ExternalConstraintEightCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintEightCall[I, F]) Do(f func(F) other.Two[I, F]) *ExternalConstraintEightCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintEightCall[I, F]) DoAndReturn(f func(F) other.Two[I, F]) *ExternalConstraintEightCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Five mocks base method. +func (m *MockExternalConstraint[I, F]) Five(arg0 I) typed.Baz[F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Five", arg0) + ret0, _ := ret[0].(typed.Baz[F]) + return ret0 +} + +// Five indicates an expected call of Five. +func (mr *MockExternalConstraintMockRecorder[I, F]) Five(arg0 interface{}) *ExternalConstraintFiveCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Five", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Five), arg0) + return &ExternalConstraintFiveCall[I, F]{Call: call} +} + +// ExternalConstraintFiveCall wrap *gomock.Call +type ExternalConstraintFiveCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintFiveCall[I, F]) Return(arg0 typed.Baz[F]) *ExternalConstraintFiveCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintFiveCall[I, F]) Do(f func(I) typed.Baz[F]) *ExternalConstraintFiveCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintFiveCall[I, F]) DoAndReturn(f func(I) typed.Baz[F]) *ExternalConstraintFiveCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Four mocks base method. +func (m *MockExternalConstraint[I, F]) Four(arg0 I) typed.Foo[I, F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Four", arg0) + ret0, _ := ret[0].(typed.Foo[I, F]) + return ret0 +} + +// Four indicates an expected call of Four. +func (mr *MockExternalConstraintMockRecorder[I, F]) Four(arg0 interface{}) *ExternalConstraintFourCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Four", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Four), arg0) + return &ExternalConstraintFourCall[I, F]{Call: call} +} + +// ExternalConstraintFourCall wrap *gomock.Call +type ExternalConstraintFourCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintFourCall[I, F]) Return(arg0 typed.Foo[I, F]) *ExternalConstraintFourCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintFourCall[I, F]) Do(f func(I) typed.Foo[I, F]) *ExternalConstraintFourCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintFourCall[I, F]) DoAndReturn(f func(I) typed.Foo[I, F]) *ExternalConstraintFourCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Nine mocks base method. +func (m *MockExternalConstraint[I, F]) Nine(arg0 typed.Iface[I]) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Nine", arg0) +} + +// Nine indicates an expected call of Nine. +func (mr *MockExternalConstraintMockRecorder[I, F]) Nine(arg0 interface{}) *ExternalConstraintNineCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Nine", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Nine), arg0) + return &ExternalConstraintNineCall[I, F]{Call: call} +} + +// ExternalConstraintNineCall wrap *gomock.Call +type ExternalConstraintNineCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintNineCall[I, F]) Return() *ExternalConstraintNineCall[I, F] { + c.Call = c.Call.Return() + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintNineCall[I, F]) Do(f func(typed.Iface[I])) *ExternalConstraintNineCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintNineCall[I, F]) DoAndReturn(f func(typed.Iface[I])) *ExternalConstraintNineCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// One mocks base method. +func (m *MockExternalConstraint[I, F]) One(arg0 string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "One", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// One indicates an expected call of One. +func (mr *MockExternalConstraintMockRecorder[I, F]) One(arg0 interface{}) *ExternalConstraintOneCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "One", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).One), arg0) + return &ExternalConstraintOneCall[I, F]{Call: call} +} + +// ExternalConstraintOneCall wrap *gomock.Call +type ExternalConstraintOneCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintOneCall[I, F]) Return(arg0 string) *ExternalConstraintOneCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintOneCall[I, F]) Do(f func(string) string) *ExternalConstraintOneCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintOneCall[I, F]) DoAndReturn(f func(string) string) *ExternalConstraintOneCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Seven mocks base method. +func (m *MockExternalConstraint[I, F]) Seven(arg0 I) other.One[I] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Seven", arg0) + ret0, _ := ret[0].(other.One[I]) + return ret0 +} + +// Seven indicates an expected call of Seven. +func (mr *MockExternalConstraintMockRecorder[I, F]) Seven(arg0 interface{}) *ExternalConstraintSevenCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seven", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Seven), arg0) + return &ExternalConstraintSevenCall[I, F]{Call: call} +} + +// ExternalConstraintSevenCall wrap *gomock.Call +type ExternalConstraintSevenCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintSevenCall[I, F]) Return(arg0 other.One[I]) *ExternalConstraintSevenCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintSevenCall[I, F]) Do(f func(I) other.One[I]) *ExternalConstraintSevenCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintSevenCall[I, F]) DoAndReturn(f func(I) other.One[I]) *ExternalConstraintSevenCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Six mocks base method. +func (m *MockExternalConstraint[I, F]) Six(arg0 I) *typed.Baz[F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Six", arg0) + ret0, _ := ret[0].(*typed.Baz[F]) + return ret0 +} + +// Six indicates an expected call of Six. +func (mr *MockExternalConstraintMockRecorder[I, F]) Six(arg0 interface{}) *ExternalConstraintSixCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Six", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Six), arg0) + return &ExternalConstraintSixCall[I, F]{Call: call} +} + +// ExternalConstraintSixCall wrap *gomock.Call +type ExternalConstraintSixCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintSixCall[I, F]) Return(arg0 *typed.Baz[F]) *ExternalConstraintSixCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintSixCall[I, F]) Do(f func(I) *typed.Baz[F]) *ExternalConstraintSixCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintSixCall[I, F]) DoAndReturn(f func(I) *typed.Baz[F]) *ExternalConstraintSixCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Ten mocks base method. +func (m *MockExternalConstraint[I, F]) Ten(arg0 *I) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Ten", arg0) +} + +// Ten indicates an expected call of Ten. +func (mr *MockExternalConstraintMockRecorder[I, F]) Ten(arg0 interface{}) *ExternalConstraintTenCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ten", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Ten), arg0) + return &ExternalConstraintTenCall[I, F]{Call: call} +} + +// ExternalConstraintTenCall wrap *gomock.Call +type ExternalConstraintTenCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintTenCall[I, F]) Return() *ExternalConstraintTenCall[I, F] { + c.Call = c.Call.Return() + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintTenCall[I, F]) Do(f func(*I)) *ExternalConstraintTenCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintTenCall[I, F]) DoAndReturn(f func(*I)) *ExternalConstraintTenCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Three mocks base method. +func (m *MockExternalConstraint[I, F]) Three(arg0 I) F { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Three", arg0) + ret0, _ := ret[0].(F) + return ret0 +} + +// Three indicates an expected call of Three. +func (mr *MockExternalConstraintMockRecorder[I, F]) Three(arg0 interface{}) *ExternalConstraintThreeCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Three", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Three), arg0) + return &ExternalConstraintThreeCall[I, F]{Call: call} +} + +// ExternalConstraintThreeCall wrap *gomock.Call +type ExternalConstraintThreeCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintThreeCall[I, F]) Return(arg0 F) *ExternalConstraintThreeCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintThreeCall[I, F]) Do(f func(I) F) *ExternalConstraintThreeCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintThreeCall[I, F]) DoAndReturn(f func(I) F) *ExternalConstraintThreeCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Two mocks base method. +func (m *MockExternalConstraint[I, F]) Two(arg0 I) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Two", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// Two indicates an expected call of Two. +func (mr *MockExternalConstraintMockRecorder[I, F]) Two(arg0 interface{}) *ExternalConstraintTwoCall[I, F] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Two", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Two), arg0) + return &ExternalConstraintTwoCall[I, F]{Call: call} +} + +// ExternalConstraintTwoCall wrap *gomock.Call +type ExternalConstraintTwoCall[I constraints.Integer, F constraints.Float] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *ExternalConstraintTwoCall[I, F]) Return(arg0 string) *ExternalConstraintTwoCall[I, F] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *ExternalConstraintTwoCall[I, F]) Do(f func(I) string) *ExternalConstraintTwoCall[I, F] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *ExternalConstraintTwoCall[I, F]) DoAndReturn(f func(I) string) *ExternalConstraintTwoCall[I, F] { + c.Call = c.Call.DoAndReturn(f) + return c +} diff --git a/mockgen/internal/tests/typed/source/mock_generics_test.go b/mockgen/internal/tests/typed/source/mock_generics_test.go new file mode 100644 index 0000000..239bb5f --- /dev/null +++ b/mockgen/internal/tests/typed/source/mock_generics_test.go @@ -0,0 +1,785 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: generics.go + +// Package source is a generated GoMock package. +package source + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + typed "go.uber.org/mock/mockgen/internal/tests/typed" + other "go.uber.org/mock/mockgen/internal/tests/typed/other" +) + +// MockBar is a mock of Bar interface. +type MockBar[T any, R any] struct { + ctrl *gomock.Controller + recorder *MockBarMockRecorder[T, R] +} + +// MockBarMockRecorder is the mock recorder for MockBar. +type MockBarMockRecorder[T any, R any] struct { + mock *MockBar[T, R] +} + +// NewMockBar creates a new mock instance. +func NewMockBar[T any, R any](ctrl *gomock.Controller) *MockBar[T, R] { + mock := &MockBar[T, R]{ctrl: ctrl} + mock.recorder = &MockBarMockRecorder[T, R]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBar[T, R]) EXPECT() *MockBarMockRecorder[T, R] { + return m.recorder +} + +// Eight mocks base method. +func (m *MockBar[T, R]) Eight(arg0 T) other.Two[T, R] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eight", arg0) + ret0, _ := ret[0].(other.Two[T, R]) + return ret0 +} + +// Eight indicates an expected call of Eight. +func (mr *MockBarMockRecorder[T, R]) Eight(arg0 interface{}) *BarEightCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eight", reflect.TypeOf((*MockBar[T, R])(nil).Eight), arg0) + return &BarEightCall[T, R]{Call: call} +} + +// BarEightCall wrap *gomock.Call +type BarEightCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarEightCall[T, R]) Return(arg0 other.Two[T, R]) *BarEightCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarEightCall[T, R]) Do(f func(T) other.Two[T, R]) *BarEightCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarEightCall[T, R]) DoAndReturn(f func(T) other.Two[T, R]) *BarEightCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Eighteen mocks base method. +func (m *MockBar[T, R]) Eighteen() (typed.Iface[*other.Five], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eighteen") + ret0, _ := ret[0].(typed.Iface[*other.Five]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Eighteen indicates an expected call of Eighteen. +func (mr *MockBarMockRecorder[T, R]) Eighteen() *BarEighteenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eighteen", reflect.TypeOf((*MockBar[T, R])(nil).Eighteen)) + return &BarEighteenCall[T, R]{Call: call} +} + +// BarEighteenCall wrap *gomock.Call +type BarEighteenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarEighteenCall[T, R]) Return(arg0 typed.Iface[*other.Five], arg1 error) *BarEighteenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarEighteenCall[T, R]) Do(f func() (typed.Iface[*other.Five], error)) *BarEighteenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarEighteenCall[T, R]) DoAndReturn(f func() (typed.Iface[*other.Five], error)) *BarEighteenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Eleven mocks base method. +func (m *MockBar[T, R]) Eleven() (*other.One[T], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eleven") + ret0, _ := ret[0].(*other.One[T]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Eleven indicates an expected call of Eleven. +func (mr *MockBarMockRecorder[T, R]) Eleven() *BarElevenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eleven", reflect.TypeOf((*MockBar[T, R])(nil).Eleven)) + return &BarElevenCall[T, R]{Call: call} +} + +// BarElevenCall wrap *gomock.Call +type BarElevenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarElevenCall[T, R]) Return(arg0 *other.One[T], arg1 error) *BarElevenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarElevenCall[T, R]) Do(f func() (*other.One[T], error)) *BarElevenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarElevenCall[T, R]) DoAndReturn(f func() (*other.One[T], error)) *BarElevenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Fifteen mocks base method. +func (m *MockBar[T, R]) Fifteen() (typed.Iface[typed.StructType], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Fifteen") + ret0, _ := ret[0].(typed.Iface[typed.StructType]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Fifteen indicates an expected call of Fifteen. +func (mr *MockBarMockRecorder[T, R]) Fifteen() *BarFifteenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Fifteen", reflect.TypeOf((*MockBar[T, R])(nil).Fifteen)) + return &BarFifteenCall[T, R]{Call: call} +} + +// BarFifteenCall wrap *gomock.Call +type BarFifteenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarFifteenCall[T, R]) Return(arg0 typed.Iface[typed.StructType], arg1 error) *BarFifteenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarFifteenCall[T, R]) Do(f func() (typed.Iface[typed.StructType], error)) *BarFifteenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarFifteenCall[T, R]) DoAndReturn(f func() (typed.Iface[typed.StructType], error)) *BarFifteenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Five mocks base method. +func (m *MockBar[T, R]) Five(arg0 T) typed.Baz[T] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Five", arg0) + ret0, _ := ret[0].(typed.Baz[T]) + return ret0 +} + +// Five indicates an expected call of Five. +func (mr *MockBarMockRecorder[T, R]) Five(arg0 interface{}) *BarFiveCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Five", reflect.TypeOf((*MockBar[T, R])(nil).Five), arg0) + return &BarFiveCall[T, R]{Call: call} +} + +// BarFiveCall wrap *gomock.Call +type BarFiveCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarFiveCall[T, R]) Return(arg0 typed.Baz[T]) *BarFiveCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarFiveCall[T, R]) Do(f func(T) typed.Baz[T]) *BarFiveCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarFiveCall[T, R]) DoAndReturn(f func(T) typed.Baz[T]) *BarFiveCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Four mocks base method. +func (m *MockBar[T, R]) Four(arg0 T) typed.Foo[T, R] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Four", arg0) + ret0, _ := ret[0].(typed.Foo[T, R]) + return ret0 +} + +// Four indicates an expected call of Four. +func (mr *MockBarMockRecorder[T, R]) Four(arg0 interface{}) *BarFourCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Four", reflect.TypeOf((*MockBar[T, R])(nil).Four), arg0) + return &BarFourCall[T, R]{Call: call} +} + +// BarFourCall wrap *gomock.Call +type BarFourCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarFourCall[T, R]) Return(arg0 typed.Foo[T, R]) *BarFourCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarFourCall[T, R]) Do(f func(T) typed.Foo[T, R]) *BarFourCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarFourCall[T, R]) DoAndReturn(f func(T) typed.Foo[T, R]) *BarFourCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Fourteen mocks base method. +func (m *MockBar[T, R]) Fourteen() (*typed.Foo[typed.StructType, typed.StructType2], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Fourteen") + ret0, _ := ret[0].(*typed.Foo[typed.StructType, typed.StructType2]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Fourteen indicates an expected call of Fourteen. +func (mr *MockBarMockRecorder[T, R]) Fourteen() *BarFourteenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Fourteen", reflect.TypeOf((*MockBar[T, R])(nil).Fourteen)) + return &BarFourteenCall[T, R]{Call: call} +} + +// BarFourteenCall wrap *gomock.Call +type BarFourteenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarFourteenCall[T, R]) Return(arg0 *typed.Foo[typed.StructType, typed.StructType2], arg1 error) *BarFourteenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarFourteenCall[T, R]) Do(f func() (*typed.Foo[typed.StructType, typed.StructType2], error)) *BarFourteenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarFourteenCall[T, R]) DoAndReturn(f func() (*typed.Foo[typed.StructType, typed.StructType2], error)) *BarFourteenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Nine mocks base method. +func (m *MockBar[T, R]) Nine(arg0 typed.Iface[T]) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Nine", arg0) +} + +// Nine indicates an expected call of Nine. +func (mr *MockBarMockRecorder[T, R]) Nine(arg0 interface{}) *BarNineCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Nine", reflect.TypeOf((*MockBar[T, R])(nil).Nine), arg0) + return &BarNineCall[T, R]{Call: call} +} + +// BarNineCall wrap *gomock.Call +type BarNineCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarNineCall[T, R]) Return() *BarNineCall[T, R] { + c.Call = c.Call.Return() + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarNineCall[T, R]) Do(f func(typed.Iface[T])) *BarNineCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarNineCall[T, R]) DoAndReturn(f func(typed.Iface[T])) *BarNineCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Nineteen mocks base method. +func (m *MockBar[T, R]) Nineteen() typed.AliasType { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Nineteen") + ret0, _ := ret[0].(typed.AliasType) + return ret0 +} + +// Nineteen indicates an expected call of Nineteen. +func (mr *MockBarMockRecorder[T, R]) Nineteen() *BarNineteenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Nineteen", reflect.TypeOf((*MockBar[T, R])(nil).Nineteen)) + return &BarNineteenCall[T, R]{Call: call} +} + +// BarNineteenCall wrap *gomock.Call +type BarNineteenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarNineteenCall[T, R]) Return(arg0 typed.AliasType) *BarNineteenCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarNineteenCall[T, R]) Do(f func() typed.AliasType) *BarNineteenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarNineteenCall[T, R]) DoAndReturn(f func() typed.AliasType) *BarNineteenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// One mocks base method. +func (m *MockBar[T, R]) One(arg0 string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "One", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// One indicates an expected call of One. +func (mr *MockBarMockRecorder[T, R]) One(arg0 interface{}) *BarOneCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "One", reflect.TypeOf((*MockBar[T, R])(nil).One), arg0) + return &BarOneCall[T, R]{Call: call} +} + +// BarOneCall wrap *gomock.Call +type BarOneCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarOneCall[T, R]) Return(arg0 string) *BarOneCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarOneCall[T, R]) Do(f func(string) string) *BarOneCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarOneCall[T, R]) DoAndReturn(f func(string) string) *BarOneCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Seven mocks base method. +func (m *MockBar[T, R]) Seven(arg0 T) other.One[T] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Seven", arg0) + ret0, _ := ret[0].(other.One[T]) + return ret0 +} + +// Seven indicates an expected call of Seven. +func (mr *MockBarMockRecorder[T, R]) Seven(arg0 interface{}) *BarSevenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seven", reflect.TypeOf((*MockBar[T, R])(nil).Seven), arg0) + return &BarSevenCall[T, R]{Call: call} +} + +// BarSevenCall wrap *gomock.Call +type BarSevenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarSevenCall[T, R]) Return(arg0 other.One[T]) *BarSevenCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarSevenCall[T, R]) Do(f func(T) other.One[T]) *BarSevenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarSevenCall[T, R]) DoAndReturn(f func(T) other.One[T]) *BarSevenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Seventeen mocks base method. +func (m *MockBar[T, R]) Seventeen() (*typed.Foo[other.Three, other.Four], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Seventeen") + ret0, _ := ret[0].(*typed.Foo[other.Three, other.Four]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Seventeen indicates an expected call of Seventeen. +func (mr *MockBarMockRecorder[T, R]) Seventeen() *BarSeventeenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seventeen", reflect.TypeOf((*MockBar[T, R])(nil).Seventeen)) + return &BarSeventeenCall[T, R]{Call: call} +} + +// BarSeventeenCall wrap *gomock.Call +type BarSeventeenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarSeventeenCall[T, R]) Return(arg0 *typed.Foo[other.Three, other.Four], arg1 error) *BarSeventeenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarSeventeenCall[T, R]) Do(f func() (*typed.Foo[other.Three, other.Four], error)) *BarSeventeenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarSeventeenCall[T, R]) DoAndReturn(f func() (*typed.Foo[other.Three, other.Four], error)) *BarSeventeenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Six mocks base method. +func (m *MockBar[T, R]) Six(arg0 T) *typed.Baz[T] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Six", arg0) + ret0, _ := ret[0].(*typed.Baz[T]) + return ret0 +} + +// Six indicates an expected call of Six. +func (mr *MockBarMockRecorder[T, R]) Six(arg0 interface{}) *BarSixCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Six", reflect.TypeOf((*MockBar[T, R])(nil).Six), arg0) + return &BarSixCall[T, R]{Call: call} +} + +// BarSixCall wrap *gomock.Call +type BarSixCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarSixCall[T, R]) Return(arg0 *typed.Baz[T]) *BarSixCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarSixCall[T, R]) Do(f func(T) *typed.Baz[T]) *BarSixCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarSixCall[T, R]) DoAndReturn(f func(T) *typed.Baz[T]) *BarSixCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Sixteen mocks base method. +func (m *MockBar[T, R]) Sixteen() (typed.Baz[other.Three], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Sixteen") + ret0, _ := ret[0].(typed.Baz[other.Three]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Sixteen indicates an expected call of Sixteen. +func (mr *MockBarMockRecorder[T, R]) Sixteen() *BarSixteenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sixteen", reflect.TypeOf((*MockBar[T, R])(nil).Sixteen)) + return &BarSixteenCall[T, R]{Call: call} +} + +// BarSixteenCall wrap *gomock.Call +type BarSixteenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarSixteenCall[T, R]) Return(arg0 typed.Baz[other.Three], arg1 error) *BarSixteenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarSixteenCall[T, R]) Do(f func() (typed.Baz[other.Three], error)) *BarSixteenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarSixteenCall[T, R]) DoAndReturn(f func() (typed.Baz[other.Three], error)) *BarSixteenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Ten mocks base method. +func (m *MockBar[T, R]) Ten(arg0 *T) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Ten", arg0) +} + +// Ten indicates an expected call of Ten. +func (mr *MockBarMockRecorder[T, R]) Ten(arg0 interface{}) *BarTenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ten", reflect.TypeOf((*MockBar[T, R])(nil).Ten), arg0) + return &BarTenCall[T, R]{Call: call} +} + +// BarTenCall wrap *gomock.Call +type BarTenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarTenCall[T, R]) Return() *BarTenCall[T, R] { + c.Call = c.Call.Return() + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarTenCall[T, R]) Do(f func(*T)) *BarTenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarTenCall[T, R]) DoAndReturn(f func(*T)) *BarTenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Thirteen mocks base method. +func (m *MockBar[T, R]) Thirteen() (typed.Baz[typed.StructType], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Thirteen") + ret0, _ := ret[0].(typed.Baz[typed.StructType]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Thirteen indicates an expected call of Thirteen. +func (mr *MockBarMockRecorder[T, R]) Thirteen() *BarThirteenCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Thirteen", reflect.TypeOf((*MockBar[T, R])(nil).Thirteen)) + return &BarThirteenCall[T, R]{Call: call} +} + +// BarThirteenCall wrap *gomock.Call +type BarThirteenCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarThirteenCall[T, R]) Return(arg0 typed.Baz[typed.StructType], arg1 error) *BarThirteenCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarThirteenCall[T, R]) Do(f func() (typed.Baz[typed.StructType], error)) *BarThirteenCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarThirteenCall[T, R]) DoAndReturn(f func() (typed.Baz[typed.StructType], error)) *BarThirteenCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Three mocks base method. +func (m *MockBar[T, R]) Three(arg0 T) R { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Three", arg0) + ret0, _ := ret[0].(R) + return ret0 +} + +// Three indicates an expected call of Three. +func (mr *MockBarMockRecorder[T, R]) Three(arg0 interface{}) *BarThreeCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Three", reflect.TypeOf((*MockBar[T, R])(nil).Three), arg0) + return &BarThreeCall[T, R]{Call: call} +} + +// BarThreeCall wrap *gomock.Call +type BarThreeCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarThreeCall[T, R]) Return(arg0 R) *BarThreeCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarThreeCall[T, R]) Do(f func(T) R) *BarThreeCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarThreeCall[T, R]) DoAndReturn(f func(T) R) *BarThreeCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Twelve mocks base method. +func (m *MockBar[T, R]) Twelve() (*other.Two[T, R], error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Twelve") + ret0, _ := ret[0].(*other.Two[T, R]) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Twelve indicates an expected call of Twelve. +func (mr *MockBarMockRecorder[T, R]) Twelve() *BarTwelveCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Twelve", reflect.TypeOf((*MockBar[T, R])(nil).Twelve)) + return &BarTwelveCall[T, R]{Call: call} +} + +// BarTwelveCall wrap *gomock.Call +type BarTwelveCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarTwelveCall[T, R]) Return(arg0 *other.Two[T, R], arg1 error) *BarTwelveCall[T, R] { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarTwelveCall[T, R]) Do(f func() (*other.Two[T, R], error)) *BarTwelveCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarTwelveCall[T, R]) DoAndReturn(f func() (*other.Two[T, R], error)) *BarTwelveCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// Two mocks base method. +func (m *MockBar[T, R]) Two(arg0 T) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Two", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// Two indicates an expected call of Two. +func (mr *MockBarMockRecorder[T, R]) Two(arg0 interface{}) *BarTwoCall[T, R] { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Two", reflect.TypeOf((*MockBar[T, R])(nil).Two), arg0) + return &BarTwoCall[T, R]{Call: call} +} + +// BarTwoCall wrap *gomock.Call +type BarTwoCall[T any, R any] struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *BarTwoCall[T, R]) Return(arg0 string) *BarTwoCall[T, R] { + c.Call = c.Call.Return(arg0) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *BarTwoCall[T, R]) Do(f func(T) string) *BarTwoCall[T, R] { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *BarTwoCall[T, R]) DoAndReturn(f func(T) string) *BarTwoCall[T, R] { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// MockIface is a mock of Iface interface. +type MockIface[T any] struct { + ctrl *gomock.Controller + recorder *MockIfaceMockRecorder[T] +} + +// MockIfaceMockRecorder is the mock recorder for MockIface. +type MockIfaceMockRecorder[T any] struct { + mock *MockIface[T] +} + +// NewMockIface creates a new mock instance. +func NewMockIface[T any](ctrl *gomock.Controller) *MockIface[T] { + mock := &MockIface[T]{ctrl: ctrl} + mock.recorder = &MockIfaceMockRecorder[T]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockIface[T]) EXPECT() *MockIfaceMockRecorder[T] { + return m.recorder +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 031b4ea..12b0191 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -61,6 +61,7 @@ var ( selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.") writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.") copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") + typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function") debugParser = flag.Bool("debug_parser", false, "Print out parser results only.") showVersion = flag.Bool("version", false, "Print version.") @@ -444,7 +445,7 @@ func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePa g.out() g.p("}") - g.GenerateMockMethods(mockType, intf, outputPackagePath, shortTp) + g.GenerateMockMethods(mockType, intf, outputPackagePath, longTp, shortTp, *typed) return nil } @@ -455,13 +456,17 @@ func (b byMethodName) Len() int { return len(b) } func (b byMethodName) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b byMethodName) Less(i, j int) bool { return b[i].Name < b[j].Name } -func (g *generator) GenerateMockMethods(mockType string, intf *model.Interface, pkgOverride, shortTp string) { +func (g *generator) GenerateMockMethods(mockType string, intf *model.Interface, pkgOverride, longTp, shortTp string, typed bool) { sort.Sort(byMethodName(intf.Methods)) for _, m := range intf.Methods { g.p("") _ = g.GenerateMockMethod(mockType, m, pkgOverride, shortTp) g.p("") - _ = g.GenerateMockRecorderMethod(mockType, m, shortTp) + _ = g.GenerateMockRecorderMethod(intf, mockType, m, shortTp, typed) + if typed { + g.p("") + _ = g.GenerateMockReturnCallMethod(intf, m, pkgOverride, longTp, shortTp) + } } } @@ -481,8 +486,8 @@ func makeArgString(argNames, argTypes []string) string { // GenerateMockMethod generates a mock method implementation. // If non-empty, pkgOverride is the package in which unqualified types reside. func (g *generator) GenerateMockMethod(mockType string, m *model.Method, pkgOverride, shortTp string) error { - argNames := g.getArgNames(m) - argTypes := g.getArgTypes(m, pkgOverride) + argNames := g.getArgNames(m, true /* in */) + argTypes := g.getArgTypes(m, pkgOverride, true /* in */) argString := makeArgString(argNames, argTypes) rets := make([]string, len(m.Out)) @@ -545,8 +550,8 @@ func (g *generator) GenerateMockMethod(mockType string, m *model.Method, pkgOver return nil } -func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method, shortTp string) error { - argNames := g.getArgNames(m) +func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, mockType string, m *model.Method, shortTp string, typed bool) error { + argNames := g.getArgNames(m, true) var argString string if m.Variadic == nil { @@ -569,7 +574,12 @@ func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method, idRecv := ia.allocateIdentifier("mr") g.p("// %v indicates an expected call of %v.", m.Name, m.Name) - g.p("func (%s *%vMockRecorder%v) %v(%v) *gomock.Call {", idRecv, mockType, shortTp, m.Name, argString) + if typed { + g.p("func (%s *%vMockRecorder%v) %v(%v) *%s%sCall%s {", idRecv, mockType, shortTp, m.Name, argString, intf.Name, m.Name, shortTp) + } else { + g.p("func (%s *%vMockRecorder%v) %v(%v) *gomock.Call {", idRecv, mockType, shortTp, m.Name, argString) + } + g.in() g.p("%s.mock.ctrl.T.Helper()", idRecv) @@ -592,35 +602,114 @@ func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method, callArgs = ", " + idVarArgs + "..." } } - g.p(`return %s.mock.ctrl.RecordCallWithMethodType(%s.mock, "%s", reflect.TypeOf((*%s%s)(nil).%s)%s)`, idRecv, idRecv, m.Name, mockType, shortTp, m.Name, callArgs) + if typed { + g.p(`call := %s.mock.ctrl.RecordCallWithMethodType(%s.mock, "%s", reflect.TypeOf((*%s%s)(nil).%s)%s)`, idRecv, idRecv, m.Name, mockType, shortTp, m.Name, callArgs) + g.p(`return &%s%sCall%s{Call: call}`, intf.Name, m.Name, shortTp) + } else { + g.p(`return %s.mock.ctrl.RecordCallWithMethodType(%s.mock, "%s", reflect.TypeOf((*%s%s)(nil).%s)%s)`, idRecv, idRecv, m.Name, mockType, shortTp, m.Name, callArgs) + } g.out() g.p("}") return nil } -func (g *generator) getArgNames(m *model.Method) []string { - argNames := make([]string, len(m.In)) - for i, p := range m.In { +func (g *generator) GenerateMockReturnCallMethod(intf *model.Interface, m *model.Method, pkgOverride, longTp, shortTp string) error { + argNames := g.getArgNames(m, true /* in */) + retNames := g.getArgNames(m, false /* out */) + argTypes := g.getArgTypes(m, pkgOverride, true /* in */) + retTypes := g.getArgTypes(m, pkgOverride, false /* out */) + argString := strings.Join(argTypes, ", ") + + rets := make([]string, len(m.Out)) + for i, p := range m.Out { + rets[i] = p.Type.String(g.packageMap, pkgOverride) + } + + var retString string + switch { + case len(rets) == 1: + retString = " " + rets[0] + case len(rets) > 1: + retString = " (" + strings.Join(rets, ", ") + ")" + } + + ia := newIdentifierAllocator(argNames) + idRecv := ia.allocateIdentifier("c") + + recvStructName := intf.Name + m.Name + + g.p("// %s%sCall wrap *gomock.Call", intf.Name, m.Name) + g.p("type %s%sCall%s struct{", intf.Name, m.Name, longTp) + g.in() + g.p("*gomock.Call") + g.out() + g.p("}") + + g.p("// Return rewrite *gomock.Call.Return") + g.p("func (%s *%sCall%s) Return(%v) *%sCall%s {", idRecv, recvStructName, shortTp, makeArgString(retNames, retTypes), recvStructName, shortTp) + g.in() + var retArgs string + if len(retNames) > 0 { + retArgs = strings.Join(retNames, ", ") + } + g.p(`%s.Call = %v.Call.Return(%v)`, idRecv, idRecv, retArgs) + g.p("return %s", idRecv) + g.out() + g.p("}") + + g.p("// Do rewrite *gomock.Call.Do") + g.p("func (%s *%sCall%s) Do(f func(%v)%v) *%sCall%s {", idRecv, recvStructName, shortTp, argString, retString, recvStructName, shortTp) + g.in() + g.p(`%s.Call = %v.Call.Do(f)`, idRecv, idRecv) + g.p("return %s", idRecv) + g.out() + g.p("}") + + g.p("// DoAndReturn rewrite *gomock.Call.DoAndReturn") + g.p("func (%s *%sCall%s) DoAndReturn(f func(%v)%v) *%sCall%s {", idRecv, recvStructName, shortTp, argString, retString, recvStructName, shortTp) + g.in() + g.p(`%s.Call = %v.Call.DoAndReturn(f)`, idRecv, idRecv) + g.p("return %s", idRecv) + g.out() + g.p("}") + return nil +} + +func (g *generator) getArgNames(m *model.Method, in bool) []string { + var params []*model.Parameter + if in { + params = m.In + } else { + params = m.Out + } + argNames := make([]string, len(params)) + for i, p := range params { name := p.Name if name == "" || name == "_" { name = fmt.Sprintf("arg%d", i) } argNames[i] = name } - if m.Variadic != nil { + if m.Variadic != nil && in { name := m.Variadic.Name if name == "" { - name = fmt.Sprintf("arg%d", len(m.In)) + name = fmt.Sprintf("arg%d", len(params)) } argNames = append(argNames, name) } return argNames } -func (g *generator) getArgTypes(m *model.Method, pkgOverride string) []string { - argTypes := make([]string, len(m.In)) - for i, p := range m.In { +func (g *generator) getArgTypes(m *model.Method, pkgOverride string, in bool) []string { + var params []*model.Parameter + if in { + params = m.In + } else { + params = m.Out + } + argTypes := make([]string, len(params)) + for i, p := range params { argTypes[i] = p.Type.String(g.packageMap, pkgOverride) } if m.Variadic != nil { diff --git a/mockgen/mockgen_test.go b/mockgen/mockgen_test.go index 5820947..01b34a8 100644 --- a/mockgen/mockgen_test.go +++ b/mockgen/mockgen_test.go @@ -332,7 +332,7 @@ func TestGetArgNames(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { g := generator{} - result := g.getArgNames(testCase.method) + result := g.getArgNames(testCase.method, true) if !reflect.DeepEqual(result, testCase.expected) { t.Fatalf("expected %s, got %s", result, testCase.expected) }