Skip to content

Commit

Permalink
Merge pull request #460 from grongor/fix-unsafe-pointer
Browse files Browse the repository at this point in the history
Fix unsafe pointer
  • Loading branch information
LandonTClipp committed May 5, 2022
2 parents 2fcd83d + 989253d commit 1d92e73
Show file tree
Hide file tree
Showing 5 changed files with 97 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.

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

0 comments on commit 1d92e73

Please sign in to comment.