Skip to content

Commit

Permalink
Undo go:build statements, make code a little DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Sep 28, 2024
1 parent cf26d69 commit 720caa9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 161 deletions.
29 changes: 18 additions & 11 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,21 +499,28 @@ type namer interface {
Name() string
}

func (g *Generator) renderNamedType(ctx context.Context, t interface {
Obj() *types.TypeName
TypeArgs() *types.TypeList
}) string {
name := g.getPackageScopedType(ctx, t.Obj())
if t.TypeArgs() == nil || t.TypeArgs().Len() == 0 {
return name
}
args := make([]string, 0, t.TypeArgs().Len())
for i := 0; i < t.TypeArgs().Len(); i++ {
arg := t.TypeArgs().At(i)
args = append(args, g.renderType(ctx, arg))
}
return fmt.Sprintf("%s[%s]", name, strings.Join(args, ","))
}

func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
switch t := typ.(type) {
case *types.Named:
name := g.getPackageScopedType(ctx, t.Obj())
if t.TypeArgs() == nil || t.TypeArgs().Len() == 0 {
return name
}
args := make([]string, 0, t.TypeArgs().Len())
for i := 0; i < t.TypeArgs().Len(); i++ {
arg := t.TypeArgs().At(i)
args = append(args, g.renderType(ctx, arg))
}
return fmt.Sprintf("%s[%s]", name, strings.Join(args, ","))
return g.renderNamedType(ctx, t)
case *types.Alias:
return g.renderTypeAlias(ctx, t)
return g.renderNamedType(ctx, t)
case *types.TypeParam:
if t.Constraint() != nil {
name := t.Obj().Name()
Expand Down
12 changes: 0 additions & 12 deletions pkg/generator_alias.go

This file was deleted.

23 changes: 0 additions & 23 deletions pkg/generator_alias_go123.go

This file was deleted.

115 changes: 0 additions & 115 deletions pkg/generator_go123_test.go

This file was deleted.

59 changes: 59 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,62 @@ func TestParseReplaceType(t *testing.T) {
assert.Equal(t, test.expected, *actual)
}
}


func (s *GeneratorSuite) TestReplaceTypePackagePrologueGo123() {
expected := `package mocks
import baz "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz"
import mock "github.com/stretchr/testify/mock"
`
generator := NewGenerator(
s.ctx,
GeneratorConfig{InPackage: false},
s.getInterfaceFromFile("example_project/baz/foo.go", "Foo"),
pkg,
)

s.checkPrologueGeneration(generator, expected)
}

func (s *GeneratorSuite) TestReplaceTypePackageGo123() {
cfg := GeneratorConfig{InPackage: false}

s.checkGenerationRegexWithConfig("example_project/baz/foo.go", "Foo", cfg, []regexpExpected{
// func (_m *Foo) GetBaz() (*baz.Baz, error)
{true, regexp.MustCompile(`func \([^\)]+\) GetBaz\(\) \(\*baz\.Baz`)},
// func (_m *Foo) GetBaz() (*foo.InternalBaz, error)
{false, regexp.MustCompile(`func \([^\)]+\) GetBaz\(\) \(\*foo\.InternalBaz`)},
})
}

func (s *GeneratorSuite) TestReplaceTypePackageMultiplePrologueGo123() {
expected := `package mocks
import mock "github.com/stretchr/testify/mock"
import replace_type "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type"
import rt1 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt1"
import rt2 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt2"
`
generator := NewGenerator(
s.ctx,
GeneratorConfig{InPackage: false},
s.getInterfaceFromFile("example_project/replace_type/rt.go", "RType"),
pkg,
)

s.checkPrologueGeneration(generator, expected)
}

func (s *GeneratorSuite) TestReplaceTypePackageMultipleGo123() {
cfg := GeneratorConfig{InPackage: false}

s.checkGenerationRegexWithConfig("example_project/replace_type/rt.go", "RType", cfg, []regexpExpected{
// func (_m *RType) Replace1(f rt1.RType1)
{true, regexp.MustCompile(`func \([^\)]+\) Replace1\(f rt1\.RType1`)},
// func (_m *RType) Replace2(f rt2.RType2)
{true, regexp.MustCompile(`func \([^\)]+\) Replace2\(f rt2\.RType2`)},
})
}

0 comments on commit 720caa9

Please sign in to comment.