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

Commit

Permalink
Fix #71 Do signature change error msg
Browse files Browse the repository at this point in the history
Update the error handling for Call.Do in the case where
the argument passed to Call.Do does not match expectations.

* panic if the argument is not a function
* panic if the number of input arguments do not match those expected by Call
* panic if the types of the input arguments do not match those expected
by Call
* panic if the number of return arguments do not match those expected by
Call
* panic if the types of return arguments do not match those expected by
Call
  • Loading branch information
cvgw committed Feb 2, 2020
1 parent 5c85495 commit 8247798
Show file tree
Hide file tree
Showing 4 changed files with 601 additions and 2 deletions.
15 changes: 14 additions & 1 deletion gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"reflect"
"strconv"
"strings"

"github.com/golang/mock/gomock/internal/calldo"
)

// Call represents an expected call to a mock.
Expand Down Expand Up @@ -135,9 +137,20 @@ func (c *Call) DoAndReturn(f interface{}) *Call {
// return values call DoAndReturn.
// It takes an interface{} argument to support n-arity functions.
func (c *Call) Do(f interface{}) *Call {
// TODO: Check arity and types here, rather than dying badly elsewhere.
v := reflect.ValueOf(f)

switch v.Kind() {
case reflect.Func:
mt := c.methodType

ft := v.Type()
if err := calldo.ValidateInputAndOutputSig(ft, mt); err != nil {
panic(err)
}
default:
panic("Do: argument must be a function")
}

c.addAction(func(args []interface{}) []interface{} {
vargs := make([]reflect.Value, len(args))
ft := v.Type()
Expand Down
Loading

0 comments on commit 8247798

Please sign in to comment.