diff --git a/.mockery.yaml b/.mockery.yaml index ffeed52b2..ed90f364c 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/RequesterIfaceWithInterfaceTypedParam.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIfaceWithInterfaceTypedParam.go deleted file mode 100644 index 3ac33b9e0..000000000 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIfaceWithInterfaceTypedParam.go +++ /dev/null @@ -1,83 +0,0 @@ -// Code generated by mockery. DO NOT EDIT. - -package mocks - -import ( - io "io" - - mock "github.com/stretchr/testify/mock" -) - -// RequesterIfaceWithInterfaceTypedParam is an autogenerated mock type for the RequesterIfaceWithInterfaceTypedParam type -type RequesterIfaceWithInterfaceTypedParam[T io.Reader] struct { - mock.Mock -} - -type RequesterIfaceWithInterfaceTypedParam_Expecter[T io.Reader] struct { - mock *mock.Mock -} - -func (_m *RequesterIfaceWithInterfaceTypedParam[T]) EXPECT() *RequesterIfaceWithInterfaceTypedParam_Expecter[T] { - return &RequesterIfaceWithInterfaceTypedParam_Expecter[T]{mock: &_m.Mock} -} - -// Get provides a mock function with given fields: -func (_m *RequesterIfaceWithInterfaceTypedParam[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 -} - -// RequesterIfaceWithInterfaceTypedParam_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get' -type RequesterIfaceWithInterfaceTypedParam_Get_Call[T io.Reader] struct { - *mock.Call -} - -// Get is a helper method to define mock.On call -func (_e *RequesterIfaceWithInterfaceTypedParam_Expecter[T]) Get() *RequesterIfaceWithInterfaceTypedParam_Get_Call[T] { - return &RequesterIfaceWithInterfaceTypedParam_Get_Call[T]{Call: _e.mock.On("Get")} -} - -func (_c *RequesterIfaceWithInterfaceTypedParam_Get_Call[T]) Run(run func()) *RequesterIfaceWithInterfaceTypedParam_Get_Call[T] { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *RequesterIfaceWithInterfaceTypedParam_Get_Call[T]) Return(_a0 T) *RequesterIfaceWithInterfaceTypedParam_Get_Call[T] { - _c.Call.Return(_a0) - return _c -} - -func (_c *RequesterIfaceWithInterfaceTypedParam_Get_Call[T]) RunAndReturn(run func() T) *RequesterIfaceWithInterfaceTypedParam_Get_Call[T] { - _c.Call.Return(run) - return _c -} - -// NewRequesterIfaceWithInterfaceTypedParam creates a new instance of RequesterIfaceWithInterfaceTypedParam. 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 NewRequesterIfaceWithInterfaceTypedParam[T io.Reader](t interface { - mock.TestingT - Cleanup(func()) -}) *RequesterIfaceWithInterfaceTypedParam[T] { - mock := &RequesterIfaceWithInterfaceTypedParam[T]{} - 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 000000000..7b57f95b0 --- /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 000000000..0d2223c25 --- /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 000000000..01c5f44bd --- /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/fixtures/requester_iface.go b/pkg/fixtures/requester_iface.go index e110e6ca4..c9df99e03 100644 --- a/pkg/fixtures/requester_iface.go +++ b/pkg/fixtures/requester_iface.go @@ -5,7 +5,3 @@ import "io" type RequesterIface interface { Get() io.Reader } - -type RequesterIfaceWithInterfaceTypedParam[T io.Reader] interface { - Get() T -} diff --git a/pkg/generator_test.go b/pkg/generator_test.go index cdf50bf64..293ea2eb4 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -337,22 +337,6 @@ func (s *GeneratorSuite) TestGeneratorChecksInterfacesForNilable() { s.checkGeneration("requester_iface.go", "RequesterIface", false, "", "") } -func (s *GeneratorSuite) TestGeneratorChecksInterfacesWithGenericInterfaceTypedParamForNilable() { - expectedBytes, err := os.ReadFile(getMocksPath("RequesterIfaceWithInterfaceTypedParam.go")) - s.Require().NoError(err) - - expected := string(expectedBytes) - expected = expected[strings.Index(expected, "// RequesterIfaceWithInterfaceTypedParam is"):] - - cfg := GeneratorConfig{ - StructName: "RequesterIfaceWithInterfaceTypedParam", - WithExpecter: true, - UnrollVariadic: false, - } - - s.checkGenerationWithConfig("requester_iface.go", "RequesterIfaceWithInterfaceTypedParam", cfg, expected) -} - func (s *GeneratorSuite) TestGeneratorTreatsAnyAsNilable() { cfg := GeneratorConfig{ WithExpecter: true,