-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66dba08
commit 42d905a
Showing
4 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !go1.23 | ||
|
||
package pkg | ||
|
||
import ( | ||
"context" | ||
"go/types" | ||
) | ||
|
||
func (g *Generator) renderTypeAlias(ctx context.Context, t *types.Alias) string { | ||
return g.getPackageScopedType(ctx, t.Obj()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build go1.23 | ||
|
||
package pkg | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"go/types" | ||
"strings" | ||
) | ||
|
||
func (g *Generator) renderTypeAlias(ctx context.Context, t *types.Alias) 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, ",")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//go:build go1.23 | ||
|
||
package pkg | ||
|
||
import "regexp" | ||
|
||
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`)}, | ||
}) | ||
} |