From d5bc8597b1e46f6b4979a75d353589e44b8fa4f8 Mon Sep 17 00:00:00 2001 From: Rangel Reale Date: Fri, 3 Jan 2025 15:48:15 -0300 Subject: [PATCH] Support *ast.SelectorExpr aliases (#881) * support interface alias --- .../iface_new_type/iface_new_type_test.go | 6 +- pkg/fixtures/iface_new_type/interface.go | 1 + .../iface_new_type/mock_interface_4_test.go | 64 +++++++++++++++++++ pkg/parse.go | 16 ++++- 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 pkg/fixtures/iface_new_type/mock_interface_4_test.go diff --git a/pkg/fixtures/iface_new_type/iface_new_type_test.go b/pkg/fixtures/iface_new_type/iface_new_type_test.go index 3b387665..8de56ce3 100644 --- a/pkg/fixtures/iface_new_type/iface_new_type_test.go +++ b/pkg/fixtures/iface_new_type/iface_new_type_test.go @@ -14,7 +14,7 @@ func TestParsing(t *testing.T) { 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"} { + for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3", "Interface4"} { iface, err := parser.Find(ifaceName) require.NoError(t, err) require.NotNil(t, iface) @@ -33,4 +33,8 @@ func TestUsage(t *testing.T) { interface3 := NewMockInterface3(t) interface3.EXPECT().Method1().Return() interface3.Method1() + + interface4 := NewMockInterface4(t) + interface4.EXPECT().Method1().Return() + interface4.Method1() } diff --git a/pkg/fixtures/iface_new_type/interface.go b/pkg/fixtures/iface_new_type/interface.go index 2d231b70..685fabc3 100644 --- a/pkg/fixtures/iface_new_type/interface.go +++ b/pkg/fixtures/iface_new_type/interface.go @@ -9,4 +9,5 @@ type Interface1 interface { type ( Interface2 Interface1 Interface3 subpkg.SubPkgInterface + Interface4 = subpkg.SubPkgInterface ) diff --git a/pkg/fixtures/iface_new_type/mock_interface_4_test.go b/pkg/fixtures/iface_new_type/mock_interface_4_test.go new file mode 100644 index 00000000..4a2a7990 --- /dev/null +++ b/pkg/fixtures/iface_new_type/mock_interface_4_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" + +// MockInterface4 is an autogenerated mock type for the Interface4 type +type MockInterface4 struct { + mock.Mock +} + +type MockInterface4_Expecter struct { + mock *mock.Mock +} + +func (_m *MockInterface4) EXPECT() *MockInterface4_Expecter { + return &MockInterface4_Expecter{mock: &_m.Mock} +} + +// Method1 provides a mock function with no fields +func (_m *MockInterface4) Method1() { + _m.Called() +} + +// MockInterface4_Method1_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Method1' +type MockInterface4_Method1_Call struct { + *mock.Call +} + +// Method1 is a helper method to define mock.On call +func (_e *MockInterface4_Expecter) Method1() *MockInterface4_Method1_Call { + return &MockInterface4_Method1_Call{Call: _e.mock.On("Method1")} +} + +func (_c *MockInterface4_Method1_Call) Run(run func()) *MockInterface4_Method1_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockInterface4_Method1_Call) Return() *MockInterface4_Method1_Call { + _c.Call.Return() + return _c +} + +func (_c *MockInterface4_Method1_Call) RunAndReturn(run func()) *MockInterface4_Method1_Call { + _c.Run(run) + return _c +} + +// NewMockInterface4 creates a new instance of MockInterface4. 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 NewMockInterface4(t interface { + mock.TestingT + Cleanup(func()) +}) *MockInterface4 { + mock := &MockInterface4{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/parse.go b/pkg/parse.go index 7df968c1..235e286b 100644 --- a/pkg/parse.go +++ b/pkg/parse.go @@ -247,12 +247,24 @@ func (p *Parser) packageInterfaces( continue } - typ, ok := obj.Type().(*types.Named) + var typ *types.Named + var name string + + ttyp := obj.Type() + + if talias, ok := obj.Type().(*types.Alias); ok { + name = talias.Obj().Name() + ttyp = types.Unalias(obj.Type()) + } + + typ, ok := ttyp.(*types.Named) if !ok { continue } - name = typ.Obj().Name() + if name == "" { + name = typ.Obj().Name() + } if typ.Obj().Pkg() == nil { continue