Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Update error message
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblum committed Jan 5, 2022
1 parent aacf129 commit 72ff7a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ func (c *Call) MaxTimes(n int) *Call {
// DoAndReturn declares the action to run when the call is matched.
// The return values from this function are returned by the mocked function.
// It takes an interface{} argument to support n-arity functions.
// The anonymous function must have the same number of input and output arguments as the mocked method.
// The anonymous function must match the function signature mocked method.
func (c *Call) DoAndReturn(f interface{}) *Call {
// TODO: Check arity and types here, rather than dying badly elsewhere.
v := reflect.ValueOf(f)

c.addAction(func(args []interface{}) []interface{} {
c.t.Helper()
ft := v.Type()
if c.methodType.NumIn() != ft.NumIn() && !ft.IsVariadic() {
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]",
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
if c.methodType.NumIn() != ft.NumIn() {
if ft.IsVariadic() {
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v The function signature must match the mocked method, a variadic function cannot be used.",
c.receiver, c.method)
} else {
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]",
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
}
return nil
}
vArgs := make([]reflect.Value, len(args))
Expand All @@ -144,17 +149,22 @@ func (c *Call) DoAndReturn(f interface{}) *Call {
// return values are ignored to retain backward compatibility. To use the
// return values call DoAndReturn.
// It takes an interface{} argument to support n-arity functions.
// The anonymous function must have the same number of input arguments as the mocked method.
// The anonymous function must match the function signature mocked method.
func (c *Call) Do(f interface{}) *Call {
// TODO: Check arity and types here, rather than dying badly elsewhere.
v := reflect.ValueOf(f)

c.addAction(func(args []interface{}) []interface{} {
c.t.Helper()
ft := v.Type()
if c.methodType.NumIn() != ft.NumIn() && !ft.IsVariadic() {
c.t.Fatalf("wrong number of arguments in Do func for %T.%v: got %d, want %d [%s]",
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
if c.methodType.NumIn() != ft.NumIn() {
if ft.IsVariadic() {
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v The function signature must match the mocked method, a variadic function cannot be used.",
c.receiver, c.method)
} else {
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]",
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
}
return nil
}
vArgs := make([]reflect.Value, len(args))
Expand Down
4 changes: 2 additions & 2 deletions gomock/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func TestCall_Do_NumArgValidation(t *testing.T) {
methodType: reflect.TypeOf(func(one, two string) {}),
doFn: func(args ...interface{}) {},
args: []interface{}{"just", "right"},
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestCall_DoAndReturn_NumArgValidation(t *testing.T) {
methodType: reflect.TypeOf(func(one, two string) {}),
doFn: func(args ...interface{}) string { return "" },
args: []interface{}{"just", "right"},
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 72ff7a4

Please sign in to comment.