Skip to content

Commit

Permalink
fix expecter and void rolled varaidic
Browse files Browse the repository at this point in the history
  • Loading branch information
jokly committed Aug 30, 2023
1 parent 8965d12 commit 80c5909
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.

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

1 change: 1 addition & 0 deletions pkg/fixtures/expecter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Expecter interface {
ManyArgsReturns(str string, i int) (strs []string, err error)
Variadic(ints ...int) error
VariadicMany(i int, a string, intfs ...interface{}) error
VariadicNoReturn(j int, is ...interface{})
}
19 changes: 15 additions & 4 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
"unicode/utf8"

"github.com/rs/zerolog"
"github.com/vektra/mockery/v2/pkg/logging"
"golang.org/x/tools/imports"

"github.com/vektra/mockery/v2/pkg/logging"
)

const mockConstructorParamTypeNamePrefix = "mockConstructorTestingT"
Expand Down Expand Up @@ -930,10 +931,20 @@ func (g *Generator) generateCalled(list *paramList) (preamble string, called str
if namesLen == 0 || !list.Variadic || !g.config.UnrollVariadic {
if list.Variadic && !g.config.UnrollVariadic && g.config.WithExpecter {
variadicName := list.Names[namesLen-1]
tmpRet := resolveCollision(list.Names, "tmpRet")

preamble = fmt.Sprintf("\n\tvar " + tmpRet + " mock.Arguments\n\tif len(" + variadicName + ") > 0 {\n\t\t" + tmpRet + " = _m.Called(" + strings.Join(list.Names, ", ") + ")\n\t} else {\n\t\t" + tmpRet + " = _m.Called(" + strings.Join(list.Names[:len(list.Names)-1], ", ") + ")\n\t}\n\n\t")
called = tmpRet
called = fmt.Sprintf(
`func() mock.Arguments {
if len(%s) > 0 {
return _m.Called(%s)
} else {
return _m.Called(%s)
}
}()`,
variadicName,
strings.Join(list.Names, ", "),
strings.Join(list.Names[:len(list.Names)-1], ", "),
)

return
}

Expand Down

0 comments on commit 80c5909

Please sign in to comment.