Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix generates non-compilable code (#371)
Browse files Browse the repository at this point in the history
mocks will supply arg names when underscore is used in interfaces.
  • Loading branch information
ZergsLaw authored and codyoss committed Dec 31, 2019
1 parent 29da289 commit 817c00c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (g *generator) getArgNames(m *model.Method) []string {
argNames := make([]string, len(m.In))
for i, p := range m.In {
name := p.Name
if name == "" {
if name == "" || name == "_" {
name = fmt.Sprintf("arg%d", i)
}
argNames[i] = name
Expand Down
86 changes: 86 additions & 0 deletions mockgen/mockgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,89 @@ func findMethod(t *testing.T, identifier, methodName string, lines []string) int
t.Fatalf("unable to find 'func (m %s) %s'", identifier, methodName)
panic("unreachable")
}

func TestGetArgNames(t *testing.T) {
for _, testCase := range []struct {
name string
method *model.Method
expected []string
}{
{
name: "NamedArg",
method: &model.Method{
In: []*model.Parameter{
{
Name: "firstArg",
Type: &model.NamedType{Type: "int"},
},
{
Name: "secondArg",
Type: &model.NamedType{Type: "string"},
},
},
},
expected: []string{"firstArg", "secondArg"},
},
{
name: "NotNamedArg",
method: &model.Method{
In: []*model.Parameter{
{
Name: "",
Type: &model.NamedType{Type: "int"},
},
{
Name: "",
Type: &model.NamedType{Type: "string"},
},
},
},
expected: []string{"arg0", "arg1"},
},
{
name: "MixedNameArg",
method: &model.Method{
In: []*model.Parameter{
{
Name: "firstArg",
Type: &model.NamedType{Type: "int"},
},
{
Name: "_",
Type: &model.NamedType{Type: "string"},
},
},
},
expected: []string{"firstArg", "arg1"},
},
} {
t.Run(testCase.name, func(t *testing.T) {
g := generator{}

result := g.getArgNames(testCase.method)
if !testEqSliceStr(t, result, testCase.expected) {
t.Fatalf("expected %s, got %s", result, testCase.expected)
}
})
}
}

func testEqSliceStr(t *testing.T, a, b []string) bool {
t.Helper()

if a == nil || b == nil {
return false
}

if len(a) != len(b) {
return false
}

for i := range a {
if a[i] != b[i] {
return false
}
}

return true
}

0 comments on commit 817c00c

Please sign in to comment.