From 6c7fb6b12e94e6ad373670761a108d1608a4f313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Fragoso=20P=C3=A9rez?= Date: Mon, 28 Oct 2024 00:39:49 +0100 Subject: [PATCH] fix(generator): support nil returns when using interface typed params As referenced on https://github.com/vektra/mockery/issues/513 Types using generic typed interface params, should allow nil values on return --- .mockery.yaml | 8 ++ .../mockery/v2/pkg/fixtures/EmbeddedGet.go | 4 +- .../vektra/mockery/v2/pkg/fixtures/Example.go | 51 +++++++++++- .../v2/pkg/fixtures/GenericInterface.go | 8 +- .../mockery/v2/pkg/fixtures/GetGeneric.go | 4 +- .../mockery/v2/pkg/fixtures/ReplaceGeneric.go | 24 ++++-- .../v2/pkg/fixtures/ReplaceGenericSelf.go | 4 +- .../v2/pkg/fixtures/RequesterGenerics.go | 20 +++-- .../mockery/v2/pkg/fixtures/Variadic.go | 17 ++-- .../v2/pkg/fixtures/requesterUnexported.go | 64 ++++++++++++++ .../v2/pkg/fixtures/requester_unexported.go | 64 -------------- .../getter_iface_typed_param.go | 7 ++ .../getter_iface_typed_param_mock_test.go | 83 +++++++++++++++++++ pkg/fixtures/iface_typed_param/main_test.go | 29 +++++++ pkg/generator.go | 2 +- 15 files changed, 291 insertions(+), 98 deletions(-) create mode 100644 mocks/github.com/vektra/mockery/v2/pkg/fixtures/requesterUnexported.go delete mode 100644 mocks/github.com/vektra/mockery/v2/pkg/fixtures/requester_unexported.go create mode 100644 pkg/fixtures/iface_typed_param/getter_iface_typed_param.go create mode 100644 pkg/fixtures/iface_typed_param/getter_iface_typed_param_mock_test.go create mode 100644 pkg/fixtures/iface_typed_param/main_test.go diff --git a/.mockery.yaml b/.mockery.yaml index ffeed52b..ed90f364 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -70,3 +70,11 @@ packages: filename: "mock_{{.InterfaceName}}_test.go" inpackage: True keeptree: False + github.com/vektra/mockery/v2/pkg/fixtures/iface_typed_param: + config: + all: True + dir: "{{.InterfaceDir}}" + mockname: "{{.InterfaceName}}" + outpkg: "{{.PackageName}}_test" + filename: "{{.InterfaceNameSnake}}_mock_test.go" + keeptree: True diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go index a87f878a..a913d106 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go @@ -32,7 +32,9 @@ func (_m *EmbeddedGet[T]) Get() T { if rf, ok := ret.Get(0).(func() T); ok { r0 = rf() } else { - r0 = ret.Get(0).(T) + if ret.Get(0) != nil { + r0 = ret.Get(0).(T) + } } return r0 diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go index 382004a6..7cfeaae4 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go @@ -3,10 +3,11 @@ package mocks import ( - http "net/http" - + _2345678http "github.com/vektra/mockery/v2/pkg/fixtures/12345678/http" fixtureshttp "github.com/vektra/mockery/v2/pkg/fixtures/http" + http "net/http" + mock "github.com/stretchr/testify/mock" ) @@ -116,6 +117,52 @@ func (_c *Example_B_Call) RunAndReturn(run func(string) fixtureshttp.MyStruct) * return _c } +// C provides a mock function with given fields: _a0 +func (_m *Example) C(_a0 string) _2345678http.MyStruct { + ret := _m.Called(_a0) + + if len(ret) == 0 { + panic("no return value specified for C") + } + + var r0 _2345678http.MyStruct + if rf, ok := ret.Get(0).(func(string) _2345678http.MyStruct); ok { + r0 = rf(_a0) + } else { + r0 = ret.Get(0).(_2345678http.MyStruct) + } + + return r0 +} + +// Example_C_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'C' +type Example_C_Call struct { + *mock.Call +} + +// C is a helper method to define mock.On call +// - _a0 string +func (_e *Example_Expecter) C(_a0 interface{}) *Example_C_Call { + return &Example_C_Call{Call: _e.mock.On("C", _a0)} +} + +func (_c *Example_C_Call) Run(run func(_a0 string)) *Example_C_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *Example_C_Call) Return(_a0 _2345678http.MyStruct) *Example_C_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Example_C_Call) RunAndReturn(run func(string) _2345678http.MyStruct) *Example_C_Call { + _c.Call.Return(run) + return _c +} + // NewExample creates a new instance of Example. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewExample(t interface { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GenericInterface.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GenericInterface.go index edd2c24c..8dbdaba1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GenericInterface.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GenericInterface.go @@ -5,11 +5,11 @@ package mocks import mock "github.com/stretchr/testify/mock" // GenericInterface is an autogenerated mock type for the GenericInterface type -type GenericInterface[M interface{}] struct { +type GenericInterface[M any] struct { mock.Mock } -type GenericInterface_Expecter[M interface{}] struct { +type GenericInterface_Expecter[M any] struct { mock *mock.Mock } @@ -36,7 +36,7 @@ func (_m *GenericInterface[M]) Func(arg *M) int { } // GenericInterface_Func_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Func' -type GenericInterface_Func_Call[M interface{}] struct { +type GenericInterface_Func_Call[M any] struct { *mock.Call } @@ -65,7 +65,7 @@ func (_c *GenericInterface_Func_Call[M]) RunAndReturn(run func(*M) int) *Generic // NewGenericInterface creates a new instance of GenericInterface. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. -func NewGenericInterface[M interface{}](t interface { +func NewGenericInterface[M any](t interface { mock.TestingT Cleanup(func()) }) *GenericInterface[M] { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go index fadb46f7..1e31cad1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go @@ -32,7 +32,9 @@ func (_m *GetGeneric[T]) Get() T { if rf, ok := ret.Get(0).(func() T); ok { r0 = rf() } else { - r0 = ret.Get(0).(T) + if ret.Get(0) != nil { + r0 = ret.Get(0).(T) + } } return r0 diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGeneric.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGeneric.go index 0727b975..7c37f578 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGeneric.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGeneric.go @@ -10,11 +10,11 @@ import ( ) // ReplaceGeneric is an autogenerated mock type for the ReplaceGeneric type -type ReplaceGeneric[TConstraint constraints.String, TKeep interface{}] struct { +type ReplaceGeneric[TConstraint constraints.String, TKeep any] struct { mock.Mock } -type ReplaceGeneric_Expecter[TConstraint constraints.String, TKeep interface{}] struct { +type ReplaceGeneric_Expecter[TConstraint constraints.String, TKeep any] struct { mock *mock.Mock } @@ -34,14 +34,16 @@ func (_m *ReplaceGeneric[TConstraint, TKeep]) A(t1 test.B) TKeep { if rf, ok := ret.Get(0).(func(test.B) TKeep); ok { r0 = rf(t1) } else { - r0 = ret.Get(0).(TKeep) + if ret.Get(0) != nil { + r0 = ret.Get(0).(TKeep) + } } return r0 } // ReplaceGeneric_A_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'A' -type ReplaceGeneric_A_Call[TConstraint constraints.String, TKeep interface{}] struct { +type ReplaceGeneric_A_Call[TConstraint constraints.String, TKeep any] struct { *mock.Call } @@ -80,14 +82,16 @@ func (_m *ReplaceGeneric[TConstraint, TKeep]) B() test.B { if rf, ok := ret.Get(0).(func() test.B); ok { r0 = rf() } else { - r0 = ret.Get(0).(test.B) + if ret.Get(0) != nil { + r0 = ret.Get(0).(test.B) + } } return r0 } // ReplaceGeneric_B_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'B' -type ReplaceGeneric_B_Call[TConstraint constraints.String, TKeep interface{}] struct { +type ReplaceGeneric_B_Call[TConstraint constraints.String, TKeep any] struct { *mock.Call } @@ -125,14 +129,16 @@ func (_m *ReplaceGeneric[TConstraint, TKeep]) C() TConstraint { if rf, ok := ret.Get(0).(func() TConstraint); ok { r0 = rf() } else { - r0 = ret.Get(0).(TConstraint) + if ret.Get(0) != nil { + r0 = ret.Get(0).(TConstraint) + } } return r0 } // ReplaceGeneric_C_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'C' -type ReplaceGeneric_C_Call[TConstraint constraints.String, TKeep interface{}] struct { +type ReplaceGeneric_C_Call[TConstraint constraints.String, TKeep any] struct { *mock.Call } @@ -160,7 +166,7 @@ func (_c *ReplaceGeneric_C_Call[TConstraint, TKeep]) RunAndReturn(run func() TCo // NewReplaceGeneric creates a new instance of ReplaceGeneric. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. -func NewReplaceGeneric[TConstraint constraints.String, TKeep interface{}](t interface { +func NewReplaceGeneric[TConstraint constraints.String, TKeep any](t interface { mock.TestingT Cleanup(func()) }) *ReplaceGeneric[TConstraint, TKeep] { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGenericSelf.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGenericSelf.go index 8d2ef76e..dbd758a4 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGenericSelf.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGenericSelf.go @@ -29,7 +29,9 @@ func (_m *ReplaceGenericSelf) A() *ReplaceGenericSelf { if rf, ok := ret.Get(0).(func() *ReplaceGenericSelf); ok { r0 = rf() } else { - r0 = ret.Get(0).(*ReplaceGenericSelf) + if ret.Get(0) != nil { + r0 = ret.Get(0).(*ReplaceGenericSelf) + } } return r0 diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go index c813b557..c051a4c2 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go @@ -13,14 +13,14 @@ import ( ) // RequesterGenerics is an autogenerated mock type for the RequesterGenerics type -type RequesterGenerics[TAny interface{}, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { +type RequesterGenerics[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { ~int | test.GenericType[int, test.GetInt] comparable }] struct { mock.Mock } -type RequesterGenerics_Expecter[TAny interface{}, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { +type RequesterGenerics_Expecter[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { ~int | test.GenericType[int, test.GetInt] comparable }] struct { @@ -58,7 +58,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG } // RequesterGenerics_GenericAnonymousStructs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GenericAnonymousStructs' -type RequesterGenerics_GenericAnonymousStructs_Call[TAny interface{}, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { +type RequesterGenerics_GenericAnonymousStructs_Call[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { ~int | test.GenericType[int, test.GetInt] comparable }] struct { @@ -108,20 +108,24 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG if rf, ok := ret.Get(0).(func(TAny, TComparable) TSigned); ok { r0 = rf(_a0, _a1) } else { - r0 = ret.Get(0).(TSigned) + if ret.Get(0) != nil { + r0 = ret.Get(0).(TSigned) + } } if rf, ok := ret.Get(1).(func(TAny, TComparable) TIntf); ok { r1 = rf(_a0, _a1) } else { - r1 = ret.Get(1).(TIntf) + if ret.Get(1) != nil { + r1 = ret.Get(1).(TIntf) + } } return r0, r1 } // RequesterGenerics_GenericArguments_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GenericArguments' -type RequesterGenerics_GenericArguments_Call[TAny interface{}, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { +type RequesterGenerics_GenericArguments_Call[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { ~int | test.GenericType[int, test.GetInt] comparable }] struct { @@ -171,7 +175,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG } // RequesterGenerics_GenericStructs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GenericStructs' -type RequesterGenerics_GenericStructs_Call[TAny interface{}, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { +type RequesterGenerics_GenericStructs_Call[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { ~int | test.GenericType[int, test.GetInt] comparable }] struct { @@ -203,7 +207,7 @@ func (_c *RequesterGenerics_GenericStructs_Call[TAny, TComparable, TSigned, TInt // NewRequesterGenerics creates a new instance of RequesterGenerics. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. -func NewRequesterGenerics[TAny interface{}, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { +func NewRequesterGenerics[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { ~int | test.GenericType[int, test.GetInt] comparable }](t interface { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go index e78f02c8..bca366b1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go @@ -2,7 +2,10 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + test "github.com/vektra/mockery/v2/pkg/fixtures" +) // Variadic is an autogenerated mock type for the Variadic type type Variadic struct { @@ -18,7 +21,7 @@ func (_m *Variadic) EXPECT() *Variadic_Expecter { } // VariadicFunction provides a mock function with given fields: str, vFunc -func (_m *Variadic) VariadicFunction(str string, vFunc func(string, ...interface{}) interface{}) error { +func (_m *Variadic) VariadicFunction(str string, vFunc test.VariadicFunction) error { ret := _m.Called(str, vFunc) if len(ret) == 0 { @@ -26,7 +29,7 @@ func (_m *Variadic) VariadicFunction(str string, vFunc func(string, ...interface } var r0 error - if rf, ok := ret.Get(0).(func(string, func(string, ...interface{}) interface{}) error); ok { + if rf, ok := ret.Get(0).(func(string, test.VariadicFunction) error); ok { r0 = rf(str, vFunc) } else { r0 = ret.Error(0) @@ -42,14 +45,14 @@ type Variadic_VariadicFunction_Call struct { // VariadicFunction is a helper method to define mock.On call // - str string -// - vFunc func(string , ...interface{}) interface{} +// - vFunc test.VariadicFunction func (_e *Variadic_Expecter) VariadicFunction(str interface{}, vFunc interface{}) *Variadic_VariadicFunction_Call { return &Variadic_VariadicFunction_Call{Call: _e.mock.On("VariadicFunction", str, vFunc)} } -func (_c *Variadic_VariadicFunction_Call) Run(run func(str string, vFunc func(string, ...interface{}) interface{})) *Variadic_VariadicFunction_Call { +func (_c *Variadic_VariadicFunction_Call) Run(run func(str string, vFunc test.VariadicFunction)) *Variadic_VariadicFunction_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(func(string, ...interface{}) interface{})) + run(args[0].(string), args[1].(test.VariadicFunction)) }) return _c } @@ -59,7 +62,7 @@ func (_c *Variadic_VariadicFunction_Call) Return(_a0 error) *Variadic_VariadicFu return _c } -func (_c *Variadic_VariadicFunction_Call) RunAndReturn(run func(string, func(string, ...interface{}) interface{}) error) *Variadic_VariadicFunction_Call { +func (_c *Variadic_VariadicFunction_Call) RunAndReturn(run func(string, test.VariadicFunction) error) *Variadic_VariadicFunction_Call { _c.Call.Return(run) return _c } diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/requesterUnexported.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/requesterUnexported.go new file mode 100644 index 00000000..381e991d --- /dev/null +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/requesterUnexported.go @@ -0,0 +1,64 @@ +// Code generated by mockery. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// requesterUnexported is an autogenerated mock type for the requesterUnexported type +type requesterUnexported struct { + mock.Mock +} + +type requesterUnexported_Expecter struct { + mock *mock.Mock +} + +func (_m *requesterUnexported) EXPECT() *requesterUnexported_Expecter { + return &requesterUnexported_Expecter{mock: &_m.Mock} +} + +// Get provides a mock function with given fields: +func (_m *requesterUnexported) Get() { + _m.Called() +} + +// requesterUnexported_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get' +type requesterUnexported_Get_Call struct { + *mock.Call +} + +// Get is a helper method to define mock.On call +func (_e *requesterUnexported_Expecter) Get() *requesterUnexported_Get_Call { + return &requesterUnexported_Get_Call{Call: _e.mock.On("Get")} +} + +func (_c *requesterUnexported_Get_Call) Run(run func()) *requesterUnexported_Get_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *requesterUnexported_Get_Call) Return() *requesterUnexported_Get_Call { + _c.Call.Return() + return _c +} + +func (_c *requesterUnexported_Get_Call) RunAndReturn(run func()) *requesterUnexported_Get_Call { + _c.Call.Return(run) + return _c +} + +// newRequesterUnexported creates a new instance of requesterUnexported. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func newRequesterUnexported(t interface { + mock.TestingT + Cleanup(func()) +}) *requesterUnexported { + mock := &requesterUnexported{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/requester_unexported.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/requester_unexported.go deleted file mode 100644 index 0eddd983..00000000 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/requester_unexported.go +++ /dev/null @@ -1,64 +0,0 @@ -// Code generated by mockery. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// requester_unexported is an autogenerated mock type for the requester_unexported type -type requester_unexported struct { - mock.Mock -} - -type requester_unexported_Expecter struct { - mock *mock.Mock -} - -func (_m *requester_unexported) EXPECT() *requester_unexported_Expecter { - return &requester_unexported_Expecter{mock: &_m.Mock} -} - -// Get provides a mock function with given fields: -func (_m *requester_unexported) Get() { - _m.Called() -} - -// requester_unexported_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get' -type requester_unexported_Get_Call struct { - *mock.Call -} - -// Get is a helper method to define mock.On call -func (_e *requester_unexported_Expecter) Get() *requester_unexported_Get_Call { - return &requester_unexported_Get_Call{Call: _e.mock.On("Get")} -} - -func (_c *requester_unexported_Get_Call) Run(run func()) *requester_unexported_Get_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *requester_unexported_Get_Call) Return() *requester_unexported_Get_Call { - _c.Call.Return() - return _c -} - -func (_c *requester_unexported_Get_Call) RunAndReturn(run func()) *requester_unexported_Get_Call { - _c.Call.Return(run) - return _c -} - -// newRequester_unexported creates a new instance of requester_unexported. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func newRequester_unexported(t interface { - mock.TestingT - Cleanup(func()) -}) *requester_unexported { - mock := &requester_unexported{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/pkg/fixtures/iface_typed_param/getter_iface_typed_param.go b/pkg/fixtures/iface_typed_param/getter_iface_typed_param.go new file mode 100644 index 00000000..7b57f95b --- /dev/null +++ b/pkg/fixtures/iface_typed_param/getter_iface_typed_param.go @@ -0,0 +1,7 @@ +package iface_typed_param + +import "io" + +type GetterIfaceTypedParam[T io.Reader] interface { + Get() T +} diff --git a/pkg/fixtures/iface_typed_param/getter_iface_typed_param_mock_test.go b/pkg/fixtures/iface_typed_param/getter_iface_typed_param_mock_test.go new file mode 100644 index 00000000..0d2223c2 --- /dev/null +++ b/pkg/fixtures/iface_typed_param/getter_iface_typed_param_mock_test.go @@ -0,0 +1,83 @@ +// Code generated by mockery. DO NOT EDIT. + +package iface_typed_param_test + +import ( + io "io" + + mock "github.com/stretchr/testify/mock" +) + +// GetterIfaceTypedParam is an autogenerated mock type for the GetterIfaceTypedParam type +type GetterIfaceTypedParam[T io.Reader] struct { + mock.Mock +} + +type GetterIfaceTypedParam_Expecter[T io.Reader] struct { + mock *mock.Mock +} + +func (_m *GetterIfaceTypedParam[T]) EXPECT() *GetterIfaceTypedParam_Expecter[T] { + return &GetterIfaceTypedParam_Expecter[T]{mock: &_m.Mock} +} + +// Get provides a mock function with given fields: +func (_m *GetterIfaceTypedParam[T]) Get() T { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Get") + } + + var r0 T + if rf, ok := ret.Get(0).(func() T); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(T) + } + } + + return r0 +} + +// GetterIfaceTypedParam_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get' +type GetterIfaceTypedParam_Get_Call[T io.Reader] struct { + *mock.Call +} + +// Get is a helper method to define mock.On call +func (_e *GetterIfaceTypedParam_Expecter[T]) Get() *GetterIfaceTypedParam_Get_Call[T] { + return &GetterIfaceTypedParam_Get_Call[T]{Call: _e.mock.On("Get")} +} + +func (_c *GetterIfaceTypedParam_Get_Call[T]) Run(run func()) *GetterIfaceTypedParam_Get_Call[T] { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *GetterIfaceTypedParam_Get_Call[T]) Return(_a0 T) *GetterIfaceTypedParam_Get_Call[T] { + _c.Call.Return(_a0) + return _c +} + +func (_c *GetterIfaceTypedParam_Get_Call[T]) RunAndReturn(run func() T) *GetterIfaceTypedParam_Get_Call[T] { + _c.Call.Return(run) + return _c +} + +// NewGetterIfaceTypedParam creates a new instance of GetterIfaceTypedParam. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewGetterIfaceTypedParam[T io.Reader](t interface { + mock.TestingT + Cleanup(func()) +}) *GetterIfaceTypedParam[T] { + mock := &GetterIfaceTypedParam[T]{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/iface_typed_param/main_test.go b/pkg/fixtures/iface_typed_param/main_test.go new file mode 100644 index 00000000..01c5f44b --- /dev/null +++ b/pkg/fixtures/iface_typed_param/main_test.go @@ -0,0 +1,29 @@ +package iface_typed_param_test + +import ( + "bufio" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIfaceWithIfaceTypedParamReturnValues(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + returnVal *bufio.Reader + }{ + {"nil return val", nil}, + {"returning val", bufio.NewReader(http.NoBody)}, + } + for _, test := range tests { + t.Run(test.name, func(st *testing.T) { + m := NewGetterIfaceTypedParam[*bufio.Reader](st) + m.EXPECT().Get().Return(test.returnVal) + + assert.Equal(st, test.returnVal, m.Get()) + }) + } +} diff --git a/pkg/generator.go b/pkg/generator.go index f015a651..4f600c78 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -665,7 +665,7 @@ func isNillable(typ types.Type) bool { switch t := typ.(type) { case *types.Pointer, *types.Array, *types.Map, *types.Interface, *types.Signature, *types.Chan, *types.Slice: return true - case *types.Named, *types.Alias: + case *types.Named, *types.Alias, *types.TypeParam: return isNillable(t.Underlying()) } return false