From 2e2c85bfeccc8b1325e53710159a2e87d45cfb94 Mon Sep 17 00:00:00 2001 From: Link512 Date: Mon, 30 Sep 2024 19:46:02 +0200 Subject: [PATCH] check nillable property for type aliases as well --- .../vektra/mockery/v2/pkg/fixtures/UsesAny.go | 79 +++++++++++++++++++ pkg/fixtures/any_keyword.go | 5 ++ pkg/generator.go | 2 +- pkg/generator_test.go | 13 ++- 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 mocks/github.com/vektra/mockery/v2/pkg/fixtures/UsesAny.go create mode 100644 pkg/fixtures/any_keyword.go diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/UsesAny.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/UsesAny.go new file mode 100644 index 000000000..050afa35c --- /dev/null +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/UsesAny.go @@ -0,0 +1,79 @@ +// Code generated by mockery. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// UsesAny is an autogenerated mock type for the UsesAny type +type UsesAny struct { + mock.Mock +} + +type UsesAny_Expecter struct { + mock *mock.Mock +} + +func (_m *UsesAny) EXPECT() *UsesAny_Expecter { + return &UsesAny_Expecter{mock: &_m.Mock} +} + +// GetReader provides a mock function with given fields: +func (_m *UsesAny) GetReader() any { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetReader") + } + + var r0 any + if rf, ok := ret.Get(0).(func() any); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(any) + } + } + + return r0 +} + +// UsesAny_GetReader_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetReader' +type UsesAny_GetReader_Call struct { + *mock.Call +} + +// GetReader is a helper method to define mock.On call +func (_e *UsesAny_Expecter) GetReader() *UsesAny_GetReader_Call { + return &UsesAny_GetReader_Call{Call: _e.mock.On("GetReader")} +} + +func (_c *UsesAny_GetReader_Call) Run(run func()) *UsesAny_GetReader_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *UsesAny_GetReader_Call) Return(_a0 any) *UsesAny_GetReader_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *UsesAny_GetReader_Call) RunAndReturn(run func() any) *UsesAny_GetReader_Call { + _c.Call.Return(run) + return _c +} + +// NewUsesAny creates a new instance of UsesAny. 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 NewUsesAny(t interface { + mock.TestingT + Cleanup(func()) +}) *UsesAny { + mock := &UsesAny{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/any_keyword.go b/pkg/fixtures/any_keyword.go new file mode 100644 index 000000000..49035c34b --- /dev/null +++ b/pkg/fixtures/any_keyword.go @@ -0,0 +1,5 @@ +package test + +type UsesAny interface { + GetReader() any +} diff --git a/pkg/generator.go b/pkg/generator.go index abf125161..80263a3e7 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -664,7 +664,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: + case *types.Named, *types.Alias: return isNillable(t.Underlying()) } return false diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 71d11760b..827715893 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -340,6 +340,18 @@ func (s *GeneratorSuite) TestGeneratorChecksInterfacesForNilable() { s.checkGeneration("requester_iface.go", "RequesterIface", false, "", "") } +func (s *GeneratorSuite) TestGeneratorTreatsAnyAsNilable() { + cfg := GeneratorConfig{ + WithExpecter: true, + } + expectedBytes, err := os.ReadFile(getMocksPath("UsesAny.go")) + s.Require().NoError(err) + expected := string(expectedBytes) + expected = expected[strings.Index(expected, "// UsesAny is"):] + + s.checkGenerationWithConfig("any_keyword.go", "UsesAny", cfg, expected) +} + func (s *GeneratorSuite) TestGeneratorPointers() { s.checkGeneration("requester_ptr.go", "RequesterPtr", false, "", "") } @@ -816,7 +828,6 @@ func TestParseReplaceType(t *testing.T) { } } - func (s *GeneratorSuite) TestReplaceTypePackagePrologueGo123() { expected := `package mocks