Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe pointer #460

Merged
merged 2 commits into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions mocks/pkg/fixtures/UnsafeInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions mocks/pkg/fixtures/example_project/context/CollideWithStdLib.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/fixtures/unsafe.go
@@ -0,0 +1,7 @@
package test

import "unsafe"

type UnsafeInterface interface {
Do(ptr *unsafe.Pointer)
}
3 changes: 3 additions & 0 deletions pkg/generator.go
Expand Up @@ -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())
Expand Down
25 changes: 25 additions & 0 deletions pkg/generator_test.go
Expand Up @@ -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 + `"
Expand Down