diff --git a/.mockery.yaml b/.mockery.yaml index ed90f364c..d979257ed 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -61,6 +61,15 @@ packages: mockname: "Mock{{.InterfaceName}}" outpkg: "{{.PackageName}}" inpackage: True + github.com/vektra/mockery/v2/pkg/fixtures/empty_return: + config: + all: True + dir: "{{.InterfaceDir}}" + mockname: "{{.InterfaceName}}Mock" + outpkg: "{{.PackageName}}" + filename: "mock_{{.InterfaceName}}_test.go" + inpackage: True + keeptree: False github.com/vektra/mockery/v2/pkg/fixtures/method_args/same_name_arg_and_type: config: all: True @@ -77,4 +86,4 @@ packages: mockname: "{{.InterfaceName}}" outpkg: "{{.PackageName}}_test" filename: "{{.InterfaceNameSnake}}_mock_test.go" - keeptree: True + keeptree: True diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmptyReturn.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmptyReturn.go deleted file mode 100644 index 37fd97e9b..000000000 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmptyReturn.go +++ /dev/null @@ -1,98 +0,0 @@ -// Code generated by mockery. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// EmptyReturn is an autogenerated mock type for the EmptyReturn type -type EmptyReturn struct { - mock.Mock -} - -type EmptyReturn_Expecter struct { - mock *mock.Mock -} - -func (_m *EmptyReturn) EXPECT() *EmptyReturn_Expecter { - return &EmptyReturn_Expecter{mock: &_m.Mock} -} - -// NoArgs provides a mock function with no fields -func (_m *EmptyReturn) NoArgs() { - _m.Called() -} - -// EmptyReturn_NoArgs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NoArgs' -type EmptyReturn_NoArgs_Call struct { - *mock.Call -} - -// NoArgs is a helper method to define mock.On call -func (_e *EmptyReturn_Expecter) NoArgs() *EmptyReturn_NoArgs_Call { - return &EmptyReturn_NoArgs_Call{Call: _e.mock.On("NoArgs")} -} - -func (_c *EmptyReturn_NoArgs_Call) Run(run func()) *EmptyReturn_NoArgs_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *EmptyReturn_NoArgs_Call) Return() *EmptyReturn_NoArgs_Call { - _c.Call.Return() - return _c -} - -func (_c *EmptyReturn_NoArgs_Call) RunAndReturn(run func()) *EmptyReturn_NoArgs_Call { - _c.Run(run) - return _c -} - -// WithArgs provides a mock function with given fields: a, b -func (_m *EmptyReturn) WithArgs(a int, b string) { - _m.Called(a, b) -} - -// EmptyReturn_WithArgs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'WithArgs' -type EmptyReturn_WithArgs_Call struct { - *mock.Call -} - -// WithArgs is a helper method to define mock.On call -// - a int -// - b string -func (_e *EmptyReturn_Expecter) WithArgs(a interface{}, b interface{}) *EmptyReturn_WithArgs_Call { - return &EmptyReturn_WithArgs_Call{Call: _e.mock.On("WithArgs", a, b)} -} - -func (_c *EmptyReturn_WithArgs_Call) Run(run func(a int, b string)) *EmptyReturn_WithArgs_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(int), args[1].(string)) - }) - return _c -} - -func (_c *EmptyReturn_WithArgs_Call) Return() *EmptyReturn_WithArgs_Call { - _c.Call.Return() - return _c -} - -func (_c *EmptyReturn_WithArgs_Call) RunAndReturn(run func(int, string)) *EmptyReturn_WithArgs_Call { - _c.Run(run) - return _c -} - -// NewEmptyReturn creates a new instance of EmptyReturn. 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 NewEmptyReturn(t interface { - mock.TestingT - Cleanup(func()) -}) *EmptyReturn { - mock := &EmptyReturn{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/pkg/compat_test.go b/pkg/compat_test.go index c752402bf..385e7d592 100644 --- a/pkg/compat_test.go +++ b/pkg/compat_test.go @@ -6,7 +6,6 @@ import ( "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures" - test "github.com/vektra/mockery/v2/pkg/fixtures" ) // CompatSuite covers compatibility with github.com/stretchr/testify/mock. @@ -36,37 +35,6 @@ func (s *CompatSuite) TestOnAnythingOfTypeVariadicArgs() { m.AssertCalled(t, "Sprintf", "int: %d string: %s", 22, "twenty two") } -func (s *CompatSuite) TestOnEmptyReturn() { - m := mocks.NewEmptyReturn(s.T()) - var target test.EmptyReturn = m - - s.Run("NoArgs", func() { - run := false - - m.EXPECT().NoArgs().RunAndReturn(func() { - run = true - }) - - target.NoArgs() - - s.True(run) - }) - - s.Run("WithArgs", func() { - run := false - - m.EXPECT().WithArgs(42, "foo").RunAndReturn(func(arg0 int, arg1 string) { - run = true - s.Equal(42, arg0) - s.Equal("foo", arg1) - }) - - target.WithArgs(42, "foo") - - s.True(run) - }) -} - func TestCompatSuite(t *testing.T) { mockcompatSuite := new(CompatSuite) suite.Run(t, mockcompatSuite) diff --git a/pkg/fixtures/empty_return.go b/pkg/fixtures/empty_return/interface.go similarity index 100% rename from pkg/fixtures/empty_return.go rename to pkg/fixtures/empty_return/interface.go diff --git a/pkg/fixtures/empty_return/interface_test.go b/pkg/fixtures/empty_return/interface_test.go new file mode 100644 index 000000000..6039d146b --- /dev/null +++ b/pkg/fixtures/empty_return/interface_test.go @@ -0,0 +1,38 @@ +package test + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test(t *testing.T) { + m := NewEmptyReturnMock(t) + var target EmptyReturn = m + + t.Run("NoArgs", func(t *testing.T) { + run := false + + m.EXPECT().NoArgs().RunAndReturn(func() { + run = true + }) + + target.NoArgs() + + require.True(t, run) + }) + + t.Run("WithArgs", func(t *testing.T) { + run := false + + m.EXPECT().WithArgs(42, "foo").RunAndReturn(func(arg0 int, arg1 string) { + run = true + require.Equal(t, 42, arg0) + require.Equal(t, "foo", arg1) + }) + + target.WithArgs(42, "foo") + + require.True(t, run) + }) +} diff --git a/pkg/fixtures/empty_return/mock_EmptyReturn_test.go b/pkg/fixtures/empty_return/mock_EmptyReturn_test.go new file mode 100644 index 000000000..4f086b621 --- /dev/null +++ b/pkg/fixtures/empty_return/mock_EmptyReturn_test.go @@ -0,0 +1,98 @@ +// Code generated by mockery. DO NOT EDIT. + +package test + +import mock "github.com/stretchr/testify/mock" + +// EmptyReturnMock is an autogenerated mock type for the EmptyReturn type +type EmptyReturnMock struct { + mock.Mock +} + +type EmptyReturnMock_Expecter struct { + mock *mock.Mock +} + +func (_m *EmptyReturnMock) EXPECT() *EmptyReturnMock_Expecter { + return &EmptyReturnMock_Expecter{mock: &_m.Mock} +} + +// NoArgs provides a mock function with no fields +func (_m *EmptyReturnMock) NoArgs() { + _m.Called() +} + +// EmptyReturnMock_NoArgs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NoArgs' +type EmptyReturnMock_NoArgs_Call struct { + *mock.Call +} + +// NoArgs is a helper method to define mock.On call +func (_e *EmptyReturnMock_Expecter) NoArgs() *EmptyReturnMock_NoArgs_Call { + return &EmptyReturnMock_NoArgs_Call{Call: _e.mock.On("NoArgs")} +} + +func (_c *EmptyReturnMock_NoArgs_Call) Run(run func()) *EmptyReturnMock_NoArgs_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *EmptyReturnMock_NoArgs_Call) Return() *EmptyReturnMock_NoArgs_Call { + _c.Call.Return() + return _c +} + +func (_c *EmptyReturnMock_NoArgs_Call) RunAndReturn(run func()) *EmptyReturnMock_NoArgs_Call { + _c.Run(run) + return _c +} + +// WithArgs provides a mock function with given fields: a, b +func (_m *EmptyReturnMock) WithArgs(a int, b string) { + _m.Called(a, b) +} + +// EmptyReturnMock_WithArgs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'WithArgs' +type EmptyReturnMock_WithArgs_Call struct { + *mock.Call +} + +// WithArgs is a helper method to define mock.On call +// - a int +// - b string +func (_e *EmptyReturnMock_Expecter) WithArgs(a interface{}, b interface{}) *EmptyReturnMock_WithArgs_Call { + return &EmptyReturnMock_WithArgs_Call{Call: _e.mock.On("WithArgs", a, b)} +} + +func (_c *EmptyReturnMock_WithArgs_Call) Run(run func(a int, b string)) *EmptyReturnMock_WithArgs_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(int), args[1].(string)) + }) + return _c +} + +func (_c *EmptyReturnMock_WithArgs_Call) Return() *EmptyReturnMock_WithArgs_Call { + _c.Call.Return() + return _c +} + +func (_c *EmptyReturnMock_WithArgs_Call) RunAndReturn(run func(int, string)) *EmptyReturnMock_WithArgs_Call { + _c.Run(run) + return _c +} + +// NewEmptyReturnMock creates a new instance of EmptyReturnMock. 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 NewEmptyReturnMock(t interface { + mock.TestingT + Cleanup(func()) +}) *EmptyReturnMock { + mock := &EmptyReturnMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} 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 index 0d2223c25..93de8541c 100644 --- 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 @@ -21,7 +21,7 @@ func (_m *GetterIfaceTypedParam[T]) EXPECT() *GetterIfaceTypedParam_Expecter[T] return &GetterIfaceTypedParam_Expecter[T]{mock: &_m.Mock} } -// Get provides a mock function with given fields: +// Get provides a mock function with no fields func (_m *GetterIfaceTypedParam[T]) Get() T { ret := _m.Called()