Skip to content

Commit

Permalink
Merge pull request #699 from jokly/master
Browse files Browse the repository at this point in the history
fix expecter and void rolled varaidic
  • Loading branch information
LandonTClipp authored Sep 5, 2023
2 parents 8965d12 + 09e78f7 commit 4e964a8
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ packages:
unroll-variadic: False
- mockname: Expecter
unroll-variadic: True
VariadicNoReturnInterface:
config:
with-expecter: True
unroll-variadic: False
RequesterReturnElided:
github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
config:
Expand Down

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.

4 changes: 4 additions & 0 deletions pkg/fixtures/expecter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ type Expecter interface {
Variadic(ints ...int) error
VariadicMany(i int, a string, intfs ...interface{}) error
}

type VariadicNoReturnInterface interface {
VariadicNoReturn(j int, is ...interface{})
}
59 changes: 48 additions & 11 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 @@ -422,16 +423,24 @@ func (g *Generator) printf(s string, vals ...interface{}) {

var templates = template.New("base template")

func (g *Generator) printTemplate(data interface{}, templateString string) {
func (g *Generator) printTemplateBytes(data interface{}, templateString string) *bytes.Buffer {
tmpl, err := templates.New(templateString).Funcs(templateFuncMap).Parse(templateString)
if err != nil {
// couldn't compile template
panic(err)
}

if err := tmpl.Execute(&g.buf, data); err != nil {
var buf bytes.Buffer

err = tmpl.Execute(&buf, data)
if err != nil {
panic(err)
}

return &buf
}

func (g *Generator) printTemplate(data interface{}, templateString string) {
g.buf.Write(g.printTemplateBytes(data, templateString).Bytes())
}

type namer interface {
Expand Down Expand Up @@ -707,7 +716,7 @@ func (g *Generator) generateMethod(ctx context.Context, method *Method) {

params := g.genList(ctx, ftype.Params(), ftype.Variadic())
returns := g.genList(ctx, ftype.Results(), false)
preamble, called := g.generateCalled(params)
preamble, called := g.generateCalled(params, returns)

data := struct {
FunctionName string
Expand Down Expand Up @@ -925,16 +934,44 @@ func {{ .ConstructorName }}{{ .TypeConstraint }}(t interface {
// steps to prepare its argument list.
//
// It is separate from Generate to avoid cyclomatic complexity through early return statements.
func (g *Generator) generateCalled(list *paramList) (preamble string, called string) {
func (g *Generator) generateCalled(list *paramList, returnList *paramList) (preamble string, called string) {
namesLen := len(list.Names)
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")
isFuncReturns := len(returnList.Names) > 0

var tmpRet, tmpRetWithAssignment string
if isFuncReturns {
tmpRet = resolveCollision(list.Names, "tmpRet")
tmpRetWithAssignment = fmt.Sprintf("%s = ", tmpRet)
}

calledBytes := g.printTemplateBytes(
struct {
ParamList *paramList
ParamNamesWithoutVariadic []string
VariadicName string
IsFuncReturns bool
TmpRet string
TmpRetWithAssignment string
}{
ParamList: list,
ParamNamesWithoutVariadic: list.Names[:len(list.Names)-1],
VariadicName: list.Names[namesLen-1],
IsFuncReturns: isFuncReturns,
TmpRet: tmpRet,
TmpRetWithAssignment: tmpRetWithAssignment,
},
`{{ if .IsFuncReturns }}var {{ .TmpRet }} mock.Arguments {{ end }}
if len({{ .VariadicName }}) > 0 {
{{ .TmpRetWithAssignment }}_m.Called({{ join .ParamList.Names ", " }})
} else {
{{ .TmpRetWithAssignment }}_m.Called({{ join .ParamNamesWithoutVariadic ", " }})
}
`,
)

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
return
return calledBytes.String(), tmpRet
}

called = "_m.Called(" + strings.Join(list.Names, ", ") + ")"
Expand Down
17 changes: 17 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"

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

Expand Down Expand Up @@ -195,6 +196,22 @@ func (s *GeneratorSuite) TestGeneratorExpecterWithRolledVariadic() {
)
}

func (s *GeneratorSuite) TestGeneratorVariadicNoReturn() {
expectedBytes, err := os.ReadFile(getMocksPath("VariadicNoReturnInterface.go"))
s.Require().NoError(err)

expected := string(expectedBytes)
expected = expected[strings.Index(expected, "// VariadicNoReturnInterface is"):]

cfg := GeneratorConfig{
StructName: "VariadicNoReturnInterface",
WithExpecter: true,
UnrollVariadic: false,
}

s.checkGenerationWithConfig("expecter.go", "VariadicNoReturnInterface", cfg, expected)
}

func (s *GeneratorSuite) TestGeneratorFunction() {
s.checkGeneration("function.go", "SendFunc", false, "", "")
}
Expand Down

0 comments on commit 4e964a8

Please sign in to comment.