From 318cb4260cc0d61a0022a1e4d5bcebf37458eebd Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Thu, 18 Jun 2020 23:23:39 -0500 Subject: [PATCH] Fixing issue with variadic interface arguments This fixes #293 and closes #271. --- pkg/fixtures/MapToInterface.go | 5 +++++ pkg/generator.go | 6 +++++- pkg/generator_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 pkg/fixtures/MapToInterface.go diff --git a/pkg/fixtures/MapToInterface.go b/pkg/fixtures/MapToInterface.go new file mode 100644 index 00000000..38481999 --- /dev/null +++ b/pkg/fixtures/MapToInterface.go @@ -0,0 +1,5 @@ +package test + +type MapToInterface interface { + Foo(arg1 ...map[string]interface{}) +} diff --git a/pkg/generator.go b/pkg/generator.go index d1efb1e8..df576d2f 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -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. diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 177b3a1d..71d011b1 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -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 {