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

fix generates non-compilable code #371

Merged
merged 2 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using reflect.DeepEquals here, but it seems like we have a name collision in that package. I will refactor this later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I planned initially, but since there was a conflict of names, I decided not to touch upon it at the moment.

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
}