Fix code generation for variadic methods.

This commit is contained in:
David Symonds 2012-04-20 14:04:40 +10:00
Родитель 4ed9dda553
Коммит e4ca772fac
4 изменённых файлов: 28 добавлений и 5 удалений

Просмотреть файл

@ -625,6 +625,18 @@ func (g *generator) GenerateMockMethod(mockType, methodName string, f *ast.FuncT
if callArgs != "" {
callArgs = ", " + callArgs
}
if isVariadic(f) {
if len(args.name) == 1 {
// Easy: just use ... to push the arguments through.
callArgs += "..."
} else {
// Hard: create a temporary slice.
g.p("_s := append([]interface{}{%s}, %s...)",
strings.Join(args.name[:len(args.name)-1], ", "),
args.name[len(args.name)-1])
callArgs = ", _s..."
}
}
if f.Results == nil || len(f.Results.List) == 0 {
g.p(`_m.ctrl.Call(_m, "%v"%v)`, methodName, callArgs)
} else {
@ -687,7 +699,14 @@ func (g *generator) GenerateMockRecorderMethod(mockType, methodName string, f *a
callArgs = ", " + callArgs
}
if variadic {
callArgs += fmt.Sprintf(", arg%d", nargs)
if nargs == 0 {
// Easy: just use ... to push the arguments through.
callArgs = ", arg0..."
} else {
// Hard: create a temporary slice.
g.p("_s := append([]interface{}{%s}, arg%d...)", strings.Join(args, ", "), nargs)
callArgs = ", _s..."
}
}
g.p(`return _mr.mock.ctrl.RecordCall(_mr.mock, "%v"%v)`, methodName, callArgs)

Просмотреть файл

@ -135,19 +135,21 @@ func (_mr *_MockIndexRecorder) NillableRet() *gomock.Call {
}
func (_m *MockIndex) Ellip(fmt string, args ...interface{}) {
_m.ctrl.Call(_m, "Ellip", fmt, args)
_s := append([]interface{}{fmt}, args...)
_m.ctrl.Call(_m, "Ellip", _s...)
}
func (_mr *_MockIndexRecorder) Ellip(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Ellip", arg0, arg1)
_s := append([]interface{}{arg0}, arg1...)
return _mr.mock.ctrl.RecordCall(_mr.mock, "Ellip", _s...)
}
func (_m *MockIndex) EllipOnly(_param0 ...interface{}) {
_m.ctrl.Call(_m, "EllipOnly", _param0)
_m.ctrl.Call(_m, "EllipOnly", _param0...)
}
func (_mr *_MockIndexRecorder) EllipOnly(arg0 ...interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "EllipOnly", arg0)
return _mr.mock.ctrl.RecordCall(_mr.mock, "EllipOnly", arg0...)
}
func (_m *MockIndex) Ptr(arg *int) {

Просмотреть файл

@ -90,6 +90,7 @@ func Remember(index Index, keys []string, values []interface{}) {
if len(keys) > 0 && keys[0] == "a" {
index.Ellip("%d", 0, 1, 1, 2, 3)
index.Ellip("%d", 1, 3, 6, 10, 15)
index.EllipOnly("arg")
}
}

Просмотреть файл

@ -25,6 +25,7 @@ func TestRemember(t *testing.T) {
mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3) // direct args
tri := []interface{}{1, 3, 6, 10, 15}
mockIndex.EXPECT().Ellip("%d", tri...) // args from slice
mockIndex.EXPECT().EllipOnly(gomock.Eq("arg"))
user.Remember(mockIndex, []string{"a", "b"}, []interface{}{1, 2})