Skip to content

Commit

Permalink
Fix *unsafe.Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
grongor committed Apr 26, 2022
1 parent bea853e commit 989253d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
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.

7 changes: 7 additions & 0 deletions pkg/fixtures/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

import "unsafe"

type UnsafeInterface interface {
Do(ptr *unsafe.Pointer)
}
3 changes: 3 additions & 0 deletions pkg/generator.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 989253d

Please sign in to comment.