From edc3a126a04ed283080c8f7df33aefb221e0978d Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Thu, 6 Oct 2016 11:44:32 +0100 Subject: [PATCH] support for variadic functions - fixes #5 --- package/moq/moq.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/package/moq/moq.go b/package/moq/moq.go index 95b0d7d..ea8d71a 100644 --- a/package/moq/moq.go +++ b/package/moq/moq.go @@ -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,