From bea853e93dfb166e3749c00f2816f63f6d563028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Tue, 26 Apr 2022 20:56:43 +0200 Subject: [PATCH 1/2] Add missing mock --- .../context/CollideWithStdLib.go | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 mocks/pkg/fixtures/example_project/context/CollideWithStdLib.go diff --git a/mocks/pkg/fixtures/example_project/context/CollideWithStdLib.go b/mocks/pkg/fixtures/example_project/context/CollideWithStdLib.go new file mode 100644 index 00000000..7e4cf1c0 --- /dev/null +++ b/mocks/pkg/fixtures/example_project/context/CollideWithStdLib.go @@ -0,0 +1,31 @@ +// Code generated by mockery. DO NOT EDIT. + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + + testing "testing" +) + +// CollideWithStdLib is an autogenerated mock type for the CollideWithStdLib type +type CollideWithStdLib struct { + mock.Mock +} + +// NewClient provides a mock function with given fields: ctx +func (_m *CollideWithStdLib) NewClient(ctx context.Context) { + _m.Called(ctx) +} + +// NewCollideWithStdLib creates a new instance of CollideWithStdLib. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations. +func NewCollideWithStdLib(t testing.TB) *CollideWithStdLib { + mock := &CollideWithStdLib{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} From 989253d1a4672c73dd000c3bee8b9bed91fcb067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Tue, 26 Apr 2022 20:57:12 +0200 Subject: [PATCH 2/2] Fix *unsafe.Pointer --- mocks/pkg/fixtures/UnsafeInterface.go | 31 +++++++++++++++++++++++++++ pkg/fixtures/unsafe.go | 7 ++++++ pkg/generator.go | 3 +++ pkg/generator_test.go | 25 +++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 mocks/pkg/fixtures/UnsafeInterface.go create mode 100644 pkg/fixtures/unsafe.go diff --git a/mocks/pkg/fixtures/UnsafeInterface.go b/mocks/pkg/fixtures/UnsafeInterface.go new file mode 100644 index 00000000..096eac9f --- /dev/null +++ b/mocks/pkg/fixtures/UnsafeInterface.go @@ -0,0 +1,31 @@ +// Code generated by mockery. DO NOT EDIT. + +package mocks + +import ( + "unsafe" + + mock "github.com/stretchr/testify/mock" + + testing "testing" +) + +// UnsafeInterface is an autogenerated mock type for the UnsafeInterface type +type UnsafeInterface struct { + mock.Mock +} + +// Do provides a mock function with given fields: ptr +func (_m *UnsafeInterface) Do(ptr *unsafe.Pointer) { + _m.Called(ptr) +} + +// NewUnsafeInterface creates a new instance of UnsafeInterface. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations. +func NewUnsafeInterface(t testing.TB) *UnsafeInterface { + mock := &UnsafeInterface{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/unsafe.go b/pkg/fixtures/unsafe.go new file mode 100644 index 00000000..cf49b147 --- /dev/null +++ b/pkg/fixtures/unsafe.go @@ -0,0 +1,7 @@ +package test + +import "unsafe" + +type UnsafeInterface interface { + Do(ptr *unsafe.Pointer) +} diff --git a/pkg/generator.go b/pkg/generator.go index 8ddbe6a7..63e1aad3 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -342,6 +342,9 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string { } return g.addPackageImport(ctx, o.Pkg()) + "." + o.Name() case *types.Basic: + if t.Kind() == types.UnsafePointer { + return "unsafe.Pointer" + } return t.Name() case *types.Pointer: return "*" + g.renderType(ctx, t.Elem()) diff --git a/pkg/generator_test.go b/pkg/generator_test.go index ed694622..b191a137 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -1659,10 +1659,35 @@ func NewImportsSameAsPackage(t testing.TB) *ImportsSameAsPackage { s.checkGeneration("imports_same_as_package.go", "ImportsSameAsPackage", false, "", expected) } +func (s *GeneratorSuite) TestGeneratorWithUnsafePointer() { + expected := `// UnsafeInterface is an autogenerated mock type for the UnsafeInterface type +type UnsafeInterface struct { + mock.Mock +} + +// Do provides a mock function with given fields: ptr +func (_m *UnsafeInterface) Do(ptr *unsafe.Pointer) { + _m.Called(ptr) +} + +// NewUnsafeInterface creates a new instance of UnsafeInterface. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations. +func NewUnsafeInterface(t testing.TB) *UnsafeInterface { + mock := &UnsafeInterface{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} +` + s.checkGeneration("unsafe.go", "UnsafeInterface", false, "", expected) +} + func (s *GeneratorSuite) TestPrologueWithImportSameAsLocalPackage() { generator := s.getGenerator( "imports_same_as_package.go", "ImportsSameAsPackage", false, "", ) + expected := `package mocks import fixtures "` + generator.iface.QualifiedName + `"