From ede4f2c90f854f51c8618c934930f7bf6cdb5a5d Mon Sep 17 00:00:00 2001 From: Julian Gautier <81595639+jgautier-dd@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:14:18 -0800 Subject: [PATCH] Add new expressions to allowed types (#863) --- .mockery.yaml | 2 + .../iface_new_type/iface_new_type_test.go | 36 +++++++++++ pkg/fixtures/iface_new_type/interface.go | 12 ++++ .../iface_new_type/mock_interface_1_test.go | 64 +++++++++++++++++++ .../iface_new_type/mock_interface_2_test.go | 64 +++++++++++++++++++ .../iface_new_type/mock_interface_3_test.go | 64 +++++++++++++++++++ .../iface_new_type/subpkg/interface.go | 5 ++ pkg/parse.go | 2 +- 8 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 pkg/fixtures/iface_new_type/iface_new_type_test.go create mode 100644 pkg/fixtures/iface_new_type/interface.go create mode 100644 pkg/fixtures/iface_new_type/mock_interface_1_test.go create mode 100644 pkg/fixtures/iface_new_type/mock_interface_2_test.go create mode 100644 pkg/fixtures/iface_new_type/mock_interface_3_test.go create mode 100644 pkg/fixtures/iface_new_type/subpkg/interface.go diff --git a/.mockery.yaml b/.mockery.yaml index 9a6171a5..55839dae 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -94,6 +94,8 @@ packages: config: *inpackage_config github.com/vektra/mockery/v2/pkg/fixtures/index_list_expr: config: *inpackage_config + github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type: + config: *inpackage_config github.com/vektra/mockery/v2/pkg/fixtures/issue845: config: <<: *inpackage_config diff --git a/pkg/fixtures/iface_new_type/iface_new_type_test.go b/pkg/fixtures/iface_new_type/iface_new_type_test.go new file mode 100644 index 00000000..3b387665 --- /dev/null +++ b/pkg/fixtures/iface_new_type/iface_new_type_test.go @@ -0,0 +1,36 @@ +package iface_new_type_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "github.com/vektra/mockery/v2/pkg" +) + +func TestParsing(t *testing.T) { + parser := pkg.NewParser(nil) + ctx := context.Background() + require.NoError(t, parser.ParsePackages(ctx, []string{"github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type"})) + require.NoError(t, parser.Load(ctx)) + + for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3"} { + iface, err := parser.Find(ifaceName) + require.NoError(t, err) + require.NotNil(t, iface) + } +} + +func TestUsage(t *testing.T) { + interface1 := NewMockInterface1(t) + interface1.EXPECT().Method1().Return() + interface1.Method1() + + interface2 := NewMockInterface2(t) + interface2.EXPECT().Method1().Return() + interface2.Method1() + + interface3 := NewMockInterface3(t) + interface3.EXPECT().Method1().Return() + interface3.Method1() +} diff --git a/pkg/fixtures/iface_new_type/interface.go b/pkg/fixtures/iface_new_type/interface.go new file mode 100644 index 00000000..2d231b70 --- /dev/null +++ b/pkg/fixtures/iface_new_type/interface.go @@ -0,0 +1,12 @@ +package iface_new_type + +import "github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type/subpkg" + +type Interface1 interface { + Method1() +} + +type ( + Interface2 Interface1 + Interface3 subpkg.SubPkgInterface +) diff --git a/pkg/fixtures/iface_new_type/mock_interface_1_test.go b/pkg/fixtures/iface_new_type/mock_interface_1_test.go new file mode 100644 index 00000000..654792a0 --- /dev/null +++ b/pkg/fixtures/iface_new_type/mock_interface_1_test.go @@ -0,0 +1,64 @@ +// Code generated by mockery. DO NOT EDIT. + +package iface_new_type_test + +import mock "github.com/stretchr/testify/mock" + +// MockInterface1 is an autogenerated mock type for the Interface1 type +type MockInterface1 struct { + mock.Mock +} + +type MockInterface1_Expecter struct { + mock *mock.Mock +} + +func (_m *MockInterface1) EXPECT() *MockInterface1_Expecter { + return &MockInterface1_Expecter{mock: &_m.Mock} +} + +// Method1 provides a mock function with no fields +func (_m *MockInterface1) Method1() { + _m.Called() +} + +// MockInterface1_Method1_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Method1' +type MockInterface1_Method1_Call struct { + *mock.Call +} + +// Method1 is a helper method to define mock.On call +func (_e *MockInterface1_Expecter) Method1() *MockInterface1_Method1_Call { + return &MockInterface1_Method1_Call{Call: _e.mock.On("Method1")} +} + +func (_c *MockInterface1_Method1_Call) Run(run func()) *MockInterface1_Method1_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockInterface1_Method1_Call) Return() *MockInterface1_Method1_Call { + _c.Call.Return() + return _c +} + +func (_c *MockInterface1_Method1_Call) RunAndReturn(run func()) *MockInterface1_Method1_Call { + _c.Run(run) + return _c +} + +// NewMockInterface1 creates a new instance of MockInterface1. 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 NewMockInterface1(t interface { + mock.TestingT + Cleanup(func()) +}) *MockInterface1 { + mock := &MockInterface1{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/iface_new_type/mock_interface_2_test.go b/pkg/fixtures/iface_new_type/mock_interface_2_test.go new file mode 100644 index 00000000..9e222997 --- /dev/null +++ b/pkg/fixtures/iface_new_type/mock_interface_2_test.go @@ -0,0 +1,64 @@ +// Code generated by mockery. DO NOT EDIT. + +package iface_new_type_test + +import mock "github.com/stretchr/testify/mock" + +// MockInterface2 is an autogenerated mock type for the Interface2 type +type MockInterface2 struct { + mock.Mock +} + +type MockInterface2_Expecter struct { + mock *mock.Mock +} + +func (_m *MockInterface2) EXPECT() *MockInterface2_Expecter { + return &MockInterface2_Expecter{mock: &_m.Mock} +} + +// Method1 provides a mock function with no fields +func (_m *MockInterface2) Method1() { + _m.Called() +} + +// MockInterface2_Method1_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Method1' +type MockInterface2_Method1_Call struct { + *mock.Call +} + +// Method1 is a helper method to define mock.On call +func (_e *MockInterface2_Expecter) Method1() *MockInterface2_Method1_Call { + return &MockInterface2_Method1_Call{Call: _e.mock.On("Method1")} +} + +func (_c *MockInterface2_Method1_Call) Run(run func()) *MockInterface2_Method1_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockInterface2_Method1_Call) Return() *MockInterface2_Method1_Call { + _c.Call.Return() + return _c +} + +func (_c *MockInterface2_Method1_Call) RunAndReturn(run func()) *MockInterface2_Method1_Call { + _c.Run(run) + return _c +} + +// NewMockInterface2 creates a new instance of MockInterface2. 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 NewMockInterface2(t interface { + mock.TestingT + Cleanup(func()) +}) *MockInterface2 { + mock := &MockInterface2{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/iface_new_type/mock_interface_3_test.go b/pkg/fixtures/iface_new_type/mock_interface_3_test.go new file mode 100644 index 00000000..443475ab --- /dev/null +++ b/pkg/fixtures/iface_new_type/mock_interface_3_test.go @@ -0,0 +1,64 @@ +// Code generated by mockery. DO NOT EDIT. + +package iface_new_type_test + +import mock "github.com/stretchr/testify/mock" + +// MockInterface3 is an autogenerated mock type for the Interface3 type +type MockInterface3 struct { + mock.Mock +} + +type MockInterface3_Expecter struct { + mock *mock.Mock +} + +func (_m *MockInterface3) EXPECT() *MockInterface3_Expecter { + return &MockInterface3_Expecter{mock: &_m.Mock} +} + +// Method1 provides a mock function with no fields +func (_m *MockInterface3) Method1() { + _m.Called() +} + +// MockInterface3_Method1_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Method1' +type MockInterface3_Method1_Call struct { + *mock.Call +} + +// Method1 is a helper method to define mock.On call +func (_e *MockInterface3_Expecter) Method1() *MockInterface3_Method1_Call { + return &MockInterface3_Method1_Call{Call: _e.mock.On("Method1")} +} + +func (_c *MockInterface3_Method1_Call) Run(run func()) *MockInterface3_Method1_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockInterface3_Method1_Call) Return() *MockInterface3_Method1_Call { + _c.Call.Return() + return _c +} + +func (_c *MockInterface3_Method1_Call) RunAndReturn(run func()) *MockInterface3_Method1_Call { + _c.Run(run) + return _c +} + +// NewMockInterface3 creates a new instance of MockInterface3. 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 NewMockInterface3(t interface { + mock.TestingT + Cleanup(func()) +}) *MockInterface3 { + mock := &MockInterface3{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/iface_new_type/subpkg/interface.go b/pkg/fixtures/iface_new_type/subpkg/interface.go new file mode 100644 index 00000000..4b32e367 --- /dev/null +++ b/pkg/fixtures/iface_new_type/subpkg/interface.go @@ -0,0 +1,5 @@ +package subpkg + +type SubPkgInterface interface { + Method1() +} diff --git a/pkg/parse.go b/pkg/parse.go index cb739915..7df968c1 100644 --- a/pkg/parse.go +++ b/pkg/parse.go @@ -380,7 +380,7 @@ func (nv *NodeVisitor) Visit(node ast.Node) ast.Visitor { break } nv.add(nv.ctx, n) - case *ast.InterfaceType, *ast.IndexExpr, *ast.IndexListExpr: + case *ast.InterfaceType, *ast.IndexExpr, *ast.IndexListExpr, *ast.SelectorExpr, *ast.Ident: nv.add(nv.ctx, n) default: log.Debug().Msg("found node with unacceptable type for mocking. Rejecting.")