Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinJSilk committed Feb 14, 2024
1 parent e724b8a commit 78755a1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ packages:
config:
replace-type:
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[-TImport]=github.com/vektra/mockery/v2/pkg/fixtures/redefined_type_b.B
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[TConstraint]=github.com/vektra/mockery/v2/pkg/fixtures/constraints.Integer
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[TConstraint]=github.com/vektra/mockery/v2/pkg/fixtures/constraints.String
# Replace a generic param with the parent type
ReplaceGenericSelf:
config:
Expand Down
45 changes: 42 additions & 3 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,58 @@ func (_m *Handler) HandleMessage(m pubsub.Message) error {
Generic type constraints can also be replaced by targeting the changed parameter with the square bracket notation on the left hand side.

```shell
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[T]=baz:github.com/vektra/mockery/v2/baz.Baz
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[T]=github.com/vektra/mockery/v2/baz.Baz
```

For example:

```go
type InternalBaz[T any] struct{}

func (*InternalBaz[T]) Foo() T {}

// Becomes
type InternalBaz[T baz.Baz] struct{}

func (*InternalBaz[T]) Foo() T {}
```

If a type constraint needs to be removed and replaced with a type, target the constraint with square brackets and include a '-' in front to have it removed.

```shell
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[-T]=baz:github.com/vektra/mockery/v2/baz.Baz
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[-T]=github.com/vektra/mockery/v2/baz.Baz
```

For example:

```go
type InternalBaz[T any] struct{}

func (*InternalBaz[T]) Foo() T {}

// Becomes
type InternalBaz struct{}

func (*InternalBaz) Foo() baz.Baz {}
```

When replacing a generic constraint, you can replace the type with a pointer by adding a '*' before the output type name.

```shell
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[-T]=baz:github.com/vektra/mockery/v2/baz.*Baz
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[-T]=github.com/vektra/mockery/v2/baz.*Baz
```

For example:

```go
type InternalBaz[T any] struct{}

func (*InternalBaz[T]) Foo() T {}

// Becomes
type InternalBaz struct{}

func (*InternalBaz) Foo() *baz.Baz {}
```

`packages` configuration
Expand Down

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

4 changes: 4 additions & 0 deletions pkg/fixtures/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ type Signed interface {
type Integer interface {
~int
}

type String interface {
~string
}
30 changes: 30 additions & 0 deletions pkg/fixtures/test/generic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test

import (
"testing"

"github.com/stretchr/testify/assert"
mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures"
rtb "github.com/vektra/mockery/v2/pkg/fixtures/redefined_type_b"
)

func TestReplaceGeneric(t *testing.T) {
type str string

m := mocks.NewReplaceGeneric[str, str](t)

m.EXPECT().A(rtb.B(1)).Return("")
assert.Equal(t, m.A(rtb.B(1)), str(""))

m.EXPECT().B().Return(2)
assert.Equal(t, m.B(), rtb.B(2))

m.EXPECT().C().Return("")
assert.Equal(t, m.C(), str(""))
}

func TestReplaceGenericSelf(t *testing.T) {
m := mocks.NewReplaceGenericSelf(t)
m.EXPECT().A().Return(m)
assert.Equal(t, m.A(), m)
}
7 changes: 1 addition & 6 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,12 +1164,7 @@ func parseReplaceType(t string) *replaceType {
// Match type parameter substitution
match := regexp.MustCompile(`\[(.*?)\]$`).FindStringSubmatch(t)
if len(match) >= 2 {
if match[1][:1] == "-" {
ret.param = match[1][1:]
ret.rmvParam = true
} else {
ret.param = match[1]
}
ret.param, ret.rmvParam = strings.CutPrefix(match[1], "-")
t = strings.ReplaceAll(t, match[0], "")
}

Expand Down
26 changes: 0 additions & 26 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,32 +760,6 @@ func (s *GeneratorSuite) TestReplaceTypePackageMultiple() {
})
}

func (s *GeneratorSuite) TestReplaceTypeGeneric() {
cfg := GeneratorConfig{InPackage: false, ReplaceType: []string{
"github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[-TImport]=github.com/vektra/mockery/v2/pkg/fixtures/redefined_type_b.B",
"github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[TConstraint]=github.com/vektra/mockery/v2/pkg/fixtures/constraints.Integer",
"github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGenericSelf[-T]=github.com/vektra/mockery/v2/pkg/fixtures.*ReplaceGenericSelf",
}}

s.checkGenerationRegexWithConfig("generic.go", "ReplaceGeneric", cfg, []regexpExpected{
// type ReplaceGeneric[TConstraint constraints.Integer, TKeep interface{}] struct
{true, regexp.MustCompile(`type ReplaceGeneric\[TConstraint constraints.Integer\, TKeep interface\{\}] struct`)},
// func (_m *ReplaceGeneric[TConstraint, TKeep]) A(t1 test.B) TKeep
{true, regexp.MustCompile(`func \(_m \*ReplaceGeneric\[TConstraint, TKeep\]\) A\(t1 test\.B\) TKeep`)},
// func (_m *ReplaceGeneric[TConstraint, TKeep]) B() test.B
{true, regexp.MustCompile(`func \(_m \*ReplaceGeneric\[TConstraint, TKeep\]\) B\(\) test\.B`)},
// func (_m *ReplaceGeneric[TConstraint, TKeep]) C() TConstraint
{true, regexp.MustCompile(`func \(_m \*ReplaceGeneric\[TConstraint, TKeep\]\) C\(\) TConstraint`)},
})

s.checkGenerationRegexWithConfig("generic.go", "ReplaceGenericSelf", cfg, []regexpExpected{
// type ReplaceGenericSelf struct
{true, regexp.MustCompile(`type ReplaceGenericSelf struct`)},
// func (_m *ReplaceGenericSelf) A() *ReplaceGenericSelf
{true, regexp.MustCompile(`func \(_m \*ReplaceGenericSelf\) A\(\) \*ReplaceGenericSelf`)},
})
}

func (s *GeneratorSuite) TestGenericGenerator() {
s.checkGeneration("generic.go", "RequesterGenerics", false, "", "")
}
Expand Down

0 comments on commit 78755a1

Please sign in to comment.