Skip to content

Commit

Permalink
Merge pull request vektra#88 from atombender/issue_84
Browse files Browse the repository at this point in the history
Mangle argument names if they conflict with import
  • Loading branch information
evanphx committed Jun 6, 2016
2 parents 50fb557 + 19001e7 commit 29089e3
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
7 changes: 7 additions & 0 deletions mockery/fixtures/requester_arg_same_as_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

import "encoding/json"

type RequesterArgSameAsImport interface {
Get(json string) *json.RawMessage
}
7 changes: 7 additions & 0 deletions mockery/fixtures/requester_arg_same_as_named_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

import json "encoding/json"

type RequesterArgSameAsNamedImport interface {
Get(json string) *json.RawMessage
}
5 changes: 5 additions & 0 deletions mockery/fixtures/requester_arg_same_as_pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test

type RequesterArgSameAsPkg interface {
Get(test string)
}
13 changes: 13 additions & 0 deletions mockery/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ func (g *Generator) genList(list *types.Tuple, varadic bool) *paramList {

pname := v.Name()

if pname == g.pkg {
// Argument is same as our package name
pname = ""
} else if g.iface.Pkg != nil {
for _, imp := range g.iface.Pkg.Imports() {
if imp.Name() == pname {
// Argument is same as that of an imported package
pname = ""
break
}
}
}

if pname == "" {
pname = fmt.Sprintf("_a%d", i)
}
Expand Down
103 changes: 103 additions & 0 deletions mockery/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,109 @@ func (_m *RequesterNS) Get(path string) (http.Response, error) {
assert.Equal(t, expected, gen.buf.String())
}

func TestGeneratorWhereArgumentNameConflictsWithImport(t *testing.T) {
parser := NewParser()
parser.Parse(filepath.Join(fixturePath, "requester_arg_same_as_import.go"))

iface, err := parser.Find("RequesterArgSameAsImport")
assert.NoError(t, err)

gen := NewGenerator(iface, pkg)
assert.NoError(t, err)

err = gen.Generate()
assert.NoError(t, err)

expected := `// RequesterArgSameAsImport is an autogenerated mock type for the RequesterArgSameAsImport type
type RequesterArgSameAsImport struct {
mock.Mock
}
// Get provides a mock function with given fields: _a0
func (_m *RequesterArgSameAsImport) Get(_a0 string) *json.RawMessage {
ret := _m.Called(_a0)
var r0 *json.RawMessage
if rf, ok := ret.Get(0).(func(string) *json.RawMessage); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*json.RawMessage)
}
}
return r0
}
`

assert.Equal(t, expected, gen.buf.String())
}

func TestGeneratorWhereArgumentNameConflictsWithNamedImport(t *testing.T) {
parser := NewParser()
parser.Parse(filepath.Join(fixturePath, "requester_arg_same_as_named_import.go"))

iface, err := parser.Find("RequesterArgSameAsNamedImport")
assert.NoError(t, err)

gen := NewGenerator(iface, pkg)
assert.NoError(t, err)

err = gen.Generate()
assert.NoError(t, err)

expected := `// RequesterArgSameAsNamedImport is an autogenerated mock type for the RequesterArgSameAsNamedImport type
type RequesterArgSameAsNamedImport struct {
mock.Mock
}
// Get provides a mock function with given fields: _a0
func (_m *RequesterArgSameAsNamedImport) Get(_a0 string) *json.RawMessage {
ret := _m.Called(_a0)
var r0 *json.RawMessage
if rf, ok := ret.Get(0).(func(string) *json.RawMessage); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*json.RawMessage)
}
}
return r0
}
`

assert.Equal(t, expected, gen.buf.String())
}

func TestGeneratorWhereArgumentNameConflictsWithPackage(t *testing.T) {
parser := NewParser()
parser.Parse(filepath.Join(fixturePath, "requester_arg_same_as_pkg.go"))

iface, err := parser.Find("RequesterArgSameAsPkg")
assert.NoError(t, err)

gen := NewGenerator(iface, pkg)
assert.NoError(t, err)

err = gen.Generate()
assert.NoError(t, err)

expected := `// RequesterArgSameAsPkg is an autogenerated mock type for the RequesterArgSameAsPkg type
type RequesterArgSameAsPkg struct {
mock.Mock
}
// Get provides a mock function with given fields: _a0
func (_m *RequesterArgSameAsPkg) Get(_a0 string) {
_m.Called(_a0)
}
`

assert.Equal(t, expected, gen.buf.String())
}

func TestGeneratorHavingNoNamesOnArguments(t *testing.T) {
parser := NewParser()

Expand Down
5 changes: 3 additions & 2 deletions mockery/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *Parser) Find(name string) (*Interface, error) {

iface := typ.Underlying().(*types.Interface).Complete()

return &Interface{name, p.path, p.file, iface}, nil
return &Interface{name, p.path, p.file, p.pkg, iface}, nil
}

/*
Expand Down Expand Up @@ -117,6 +117,7 @@ type Interface struct {
Name string
Path string
File *ast.File
Pkg *types.Package
Type *types.Interface
}

Expand All @@ -143,7 +144,7 @@ func (p *Parser) Interfaces() []*Interface {
continue
}

ifaces = append(ifaces, &Interface{name, p.path, p.file, iface.Complete()})
ifaces = append(ifaces, &Interface{name, p.path, p.file, p.pkg, iface.Complete()})
}

return ifaces
Expand Down

0 comments on commit 29089e3

Please sign in to comment.