Skip to content

Commit

Permalink
fix: issue 707
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovak committed Dec 12, 2023
1 parent f54eea9 commit a032aa0
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 10 deletions.
7 changes: 3 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ tasks:
- "**/*.go"
cmds:
- go fmt ./...

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 +75,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.

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

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

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

type VariadicExtra 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 @@ -481,19 +481,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 @@ -562,13 +562,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 {

Check warning on line 565 in pkg/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/generator.go#L565

Added line #L565 was not covered by tests
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()))
}

Check warning on line 578 in pkg/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/generator.go#L577-L578

Added lines #L577 - L578 were not covered by tests
}

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

0 comments on commit a032aa0

Please sign in to comment.