Merge pull request #7 from matryer/variadic-args-5
Support for variadic functions
This commit is contained in:
commit
adfa414458
@ -2,7 +2,7 @@ package example
|
||||
|
||||
import "context"
|
||||
|
||||
//go:generate moq PersonStore -out mockpersonstore_test.go
|
||||
//go:generate moq -out mockpersonstore_test.go PersonStore
|
||||
|
||||
type Person struct {
|
||||
ID string
|
||||
|
@ -95,8 +95,8 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
||||
Name: meth.Name(),
|
||||
}
|
||||
obj.Methods = append(obj.Methods, method)
|
||||
method.Params = m.extractArgs(sig.Params(), "in%d")
|
||||
method.Returns = m.extractArgs(sig.Results(), "out%d")
|
||||
method.Params = m.extractArgs(sig, sig.Params(), "in%d")
|
||||
method.Returns = m.extractArgs(sig, sig.Results(), "out%d")
|
||||
}
|
||||
objs = append(objs, obj)
|
||||
}
|
||||
@ -121,15 +121,20 @@ func (m *Mocker) packageQualifier(pkg *types.Package) string {
|
||||
return pkg.Name()
|
||||
}
|
||||
|
||||
func (m *Mocker) extractArgs(list *types.Tuple, nameFormat string) []*param {
|
||||
func (m *Mocker) extractArgs(sig *types.Signature, list *types.Tuple, nameFormat string) []*param {
|
||||
var params []*param
|
||||
for ii := 0; ii < list.Len(); ii++ {
|
||||
listLen := list.Len()
|
||||
for ii := 0; ii < listLen; ii++ {
|
||||
p := list.At(ii)
|
||||
name := p.Name()
|
||||
if name == "" {
|
||||
name = fmt.Sprintf(nameFormat, ii+1)
|
||||
}
|
||||
typename := types.TypeString(p.Type(), m.packageQualifier)
|
||||
// check for final variadic argument
|
||||
if sig.Variadic() && ii == listLen-1 && typename[0:2] == "[]" {
|
||||
typename = "..." + typename[2:]
|
||||
}
|
||||
param := ¶m{
|
||||
Name: name,
|
||||
Type: typename,
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestMoq(t *testing.T) {
|
||||
m, err := New("../../example", "")
|
||||
m, err := New("testdata/example", "")
|
||||
if err != nil {
|
||||
t.Errorf("moq.New: %s", err)
|
||||
}
|
||||
@ -34,7 +34,7 @@ func TestMoq(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMoqExplicitPackage(t *testing.T) {
|
||||
m, err := New("../../example", "different")
|
||||
m, err := New("testdata/example", "different")
|
||||
if err != nil {
|
||||
t.Errorf("moq.New: %s", err)
|
||||
}
|
||||
@ -59,3 +59,30 @@ func TestMoqExplicitPackage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestVeradicArguments tests to ensure variadic work as
|
||||
// expected.
|
||||
// see https://github.com/matryer/moq/issues/5
|
||||
func TestVariadicArguments(t *testing.T) {
|
||||
m, err := New("testdata/variadic", "")
|
||||
if err != nil {
|
||||
t.Errorf("moq.New: %s", err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
err = m.Mock(&buf, "Greeter")
|
||||
if err != nil {
|
||||
t.Errorf("m.Mock: %s", err)
|
||||
}
|
||||
s := buf.String()
|
||||
// assertions of things that should be mentioned
|
||||
var strs = []string{
|
||||
"package variadic",
|
||||
"type GreeterMock struct",
|
||||
"GreetFunc func(ctx context.Context, names ...string) string",
|
||||
}
|
||||
for _, str := range strs {
|
||||
if !strings.Contains(s, str) {
|
||||
t.Errorf("expected but missing: \"%s\"", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
package/moq/testdata/example/example.go
vendored
Normal file
15
package/moq/testdata/example/example.go
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
package example
|
||||
|
||||
import "context"
|
||||
|
||||
type Person struct {
|
||||
ID string
|
||||
Name string
|
||||
Company string
|
||||
Website string
|
||||
}
|
||||
|
||||
type PersonStore interface {
|
||||
Get(ctx context.Context, id string) (*Person, error)
|
||||
Create(ctx context.Context, person *Person, confirm bool) error
|
||||
}
|
8
package/moq/testdata/variadic/greeter.go
vendored
Normal file
8
package/moq/testdata/variadic/greeter.go
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
package variadic
|
||||
|
||||
import "context"
|
||||
|
||||
// Greeter greets people.
|
||||
type Greeter interface {
|
||||
Greet(ctx context.Context, names ...string) string
|
||||
}
|
Loading…
Reference in New Issue
Block a user