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] 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 + `"