Skip to content

Commit

Permalink
Merge pull request #719 from nicovak/nicovak/fix-707
Browse files Browse the repository at this point in the history
Fixing mocks for interfaces where method returns function with variadic argument.
  • Loading branch information
LandonTClipp authored Jan 12, 2024
2 parents 2502f52 + b6625de commit 4854efd
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ packages:
unroll-variadic: False
- mockname: Expecter
unroll-variadic: True
RequesterReturnElided:
VariadicNoReturnInterface:
config:
with-expecter: True
unroll-variadic: False
RequesterReturnElided:
github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
config:
recursive: True
Expand Down
11 changes: 7 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ tasks:
- "**/*.go"
cmds:
- go fmt ./...


mocks:
desc: generate new mocks from scratch
deps: [mocks.remove, mocks.generate]

mocks.remove:
desc: remove all mock files
cmds:
- find . -name '*_mock.go' | xargs rm
- rm -rf mocks/
- find . -name '*_mock.go' | xargs rm
- rm -rf mocks/

mocks.generate:
desc: generate mockery mocks
Expand Down Expand Up @@ -75,6 +79,5 @@ tasks:
- task: test
- task: test.e2e


default:
deps: [test.ci]
75 changes: 75 additions & 0 deletions mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go

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

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

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

import (
"testing"

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

func TestVariadicReturnFunc(t *testing.T) {
m := mocks.NewVariadicReturnFunc(t)
m.EXPECT().SampleMethod("").Return(func(s string, l []int, a ...any) {
assert.Equal(t, "foo", s)
assert.Equal(t, []int{1, 2, 3}, l)
assert.Equal(t, []any{"one", "two", "three"}, a)
})
m.SampleMethod("")("foo", []int{1, 2, 3}, "one", "two", "three")
}
8 changes: 8 additions & 0 deletions pkg/fixtures/variadic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test

type VariadicFunction = func(args1 string, args2 ...interface{}) interface{}

type Variadic interface {
VariadicFunction(str string, vFunc VariadicFunction) error
}

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

type VariadicReturnFunc interface {
SampleMethod(str string) func(str string, arr []int, a ...interface{})
}
19 changes: 13 additions & 6 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,19 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
case 0:
return fmt.Sprintf(
"func(%s)",
g.renderTypeTuple(ctx, t.Params()),
g.renderTypeTuple(ctx, t.Params(), t.Variadic()),
)
case 1:
return fmt.Sprintf(
"func(%s) %s",
g.renderTypeTuple(ctx, t.Params()),
g.renderTypeTuple(ctx, t.Params(), t.Variadic()),
g.renderType(ctx, t.Results().At(0).Type()),
)
default:
return fmt.Sprintf(
"func(%s)(%s)",
g.renderTypeTuple(ctx, t.Params()),
g.renderTypeTuple(ctx, t.Results()),
g.renderTypeTuple(ctx, t.Params(), t.Variadic()),
g.renderTypeTuple(ctx, t.Results(), t.Variadic()),
)
}
case *types.Map:
Expand Down Expand Up @@ -575,13 +575,20 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
}
}

func (g *Generator) renderTypeTuple(ctx context.Context, tup *types.Tuple) string {
func (g *Generator) renderTypeTuple(ctx context.Context, tup *types.Tuple, variadic bool) string {
var parts []string

for i := 0; i < tup.Len(); i++ {
v := tup.At(i)

parts = append(parts, g.renderType(ctx, v.Type()))
if variadic && i == tup.Len()-1 {
t := v.Type()
elem := t.(*types.Slice).Elem()

parts = append(parts, "..."+g.renderType(ctx, elem))
} else {
parts = append(parts, g.renderType(ctx, v.Type()))
}
}

return strings.Join(parts, " , ")
Expand Down

0 comments on commit 4854efd

Please sign in to comment.