Skip to content

Commit

Permalink
Merge pull request #295 from LandonTClipp/master
Browse files Browse the repository at this point in the history
Fixing issue with variadic interface arguments
  • Loading branch information
LandonTClipp authored Jun 20, 2020
2 parents 93b34c6 + 318cb42 commit 1dcdbdd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/fixtures/MapToInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test

type MapToInterface interface {
Foo(arg1 ...map[string]interface{})
}
6 changes: 5 additions & 1 deletion pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,11 @@ func (g *Generator) generateCalled(list *paramList, formattedParamNames string)

var variadicArgsName string
variadicName := list.Names[namesLen-1]
variadicIface := strings.Contains(list.Types[namesLen-1], "interface{}")

// list.Types[] will contain a leading '...'. Strip this from the string to
// do easier comparison.
strippedIfaceType := strings.Trim(list.Types[namesLen-1], "...")
variadicIface := strippedIfaceType == "interface{}"

if variadicIface {
// Variadic is already of the interface{} type, so we don't need special handling.
Expand Down
24 changes: 24 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,30 @@ func (s *GeneratorSuite) TestGeneratorWithImportSameAsLocalPackageInpkgNoCycle()
s.NotContains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
}

func (s *GeneratorSuite) TestMapToInterface() {
expected := `// MapToInterface is an autogenerated mock type for the MapToInterface type
type MapToInterface struct {
mock.Mock
}
// Foo provides a mock function with given fields: arg1
func (_m *MapToInterface) Foo(arg1 ...map[string]interface{}) {
_va := make([]interface{}, len(arg1))
for _i := range arg1 {
_va[_i] = arg1[_i]
}
var _ca []interface{}
_ca = append(_ca, _va...)
_m.Called(_ca...)
}
`
s.checkGeneration(
"MapToInterface.go", "MapToInterface", false, "",
expected,
)

}

func (s *GeneratorSuite) TestGeneratorWithImportSameAsLocalPackage() {
expected := `// ImportsSameAsPackage is an autogenerated mock type for the ImportsSameAsPackage type
type ImportsSameAsPackage struct {
Expand Down

0 comments on commit 1dcdbdd

Please sign in to comment.