Skip to content

Commit

Permalink
Fix using no-name structs. Fixes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Apr 7, 2016
1 parent 4257be7 commit 84d7661
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mockery/fixtures/consul.go
@@ -0,0 +1,6 @@
package test

type ConsulLock interface {
Lock(<-chan struct{}) (<-chan struct{}, error)
Unlock() error
}
14 changes: 14 additions & 0 deletions mockery/generator.go
Expand Up @@ -191,6 +191,20 @@ func renderType(t types.Type) string {
default:
return "chan<- " + renderType(t.Elem())
}
case *types.Struct:
var fields []string

for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)

if f.Anonymous() {
fields = append(fields, renderType(f.Type()))
} else {
fields = append(fields, fmt.Sprintf("%s %s", f.Name(), renderType(f.Type())))
}
}

return fmt.Sprintf("struct{%s}", strings.Join(fields, ";"))
case namer:
return t.Name()
default:
Expand Down
60 changes: 60 additions & 0 deletions mockery/generator_test.go
@@ -1,6 +1,7 @@
package mockery

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -786,3 +787,62 @@ func (_m *MyReader) Read(p []byte) (int, error) {

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

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

iface, err := parser.Find("ConsulLock")
require.NoError(t, err)

gen := NewGenerator(iface)

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

expected := `// This is an autogenerated mock type for the ConsulLock type
type ConsulLock struct {
mock.Mock
}
// Lock provides a mock function with given fields: _a0
func (_m *ConsulLock) Lock(_a0 <-chan struct{}) (<-chan struct{}, error) {
ret := _m.Called(_a0)
var r0 <-chan struct{}
if rf, ok := ret.Get(0).(func(<-chan struct{}) <-chan struct{}); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(<-chan struct{})
}
}
var r1 error
if rf, ok := ret.Get(1).(func(<-chan struct{}) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Unlock provides a mock function with given fields:
func (_m *ConsulLock) Unlock() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
`

fmt.Println(gen.buf.String())

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

0 comments on commit 84d7661

Please sign in to comment.