Skip to content

Commit

Permalink
Merge pull request #821 from Link512/master
Browse files Browse the repository at this point in the history
check nillable property for type aliases as well
  • Loading branch information
LandonTClipp authored Oct 2, 2024
2 parents 87fdecc + 2e2c85b commit 0d1b1a2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
79 changes: 79 additions & 0 deletions mocks/github.com/vektra/mockery/v2/pkg/fixtures/UsesAny.go

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

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

type UsesAny interface {
GetReader() any
}
2 changes: 1 addition & 1 deletion pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func isNillable(typ types.Type) bool {
switch t := typ.(type) {
case *types.Pointer, *types.Array, *types.Map, *types.Interface, *types.Signature, *types.Chan, *types.Slice:
return true
case *types.Named:
case *types.Named, *types.Alias:
return isNillable(t.Underlying())
}
return false
Expand Down
13 changes: 12 additions & 1 deletion pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ func (s *GeneratorSuite) TestGeneratorChecksInterfacesForNilable() {
s.checkGeneration("requester_iface.go", "RequesterIface", false, "", "")
}

func (s *GeneratorSuite) TestGeneratorTreatsAnyAsNilable() {
cfg := GeneratorConfig{
WithExpecter: true,
}
expectedBytes, err := os.ReadFile(getMocksPath("UsesAny.go"))
s.Require().NoError(err)
expected := string(expectedBytes)
expected = expected[strings.Index(expected, "// UsesAny is"):]

s.checkGenerationWithConfig("any_keyword.go", "UsesAny", cfg, expected)
}

func (s *GeneratorSuite) TestGeneratorPointers() {
s.checkGeneration("requester_ptr.go", "RequesterPtr", false, "", "")
}
Expand Down Expand Up @@ -816,7 +828,6 @@ func TestParseReplaceType(t *testing.T) {
}
}


func (s *GeneratorSuite) TestReplaceTypePackagePrologueGo123() {
expected := `package mocks
Expand Down

0 comments on commit 0d1b1a2

Please sign in to comment.