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 + DoAndReturn signature change error msg
Browse files Browse the repository at this point in the history
Update the error handling for Call.Do and Call.DoAndReturn in the case where
the argument passed 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

Call.DoAndReturn has additional validations on the return signature

* 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 6, 2020
1 parent 5c85495 commit 5aae07b
Show file tree
Hide file tree
Showing 6 changed files with 1,049 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/golang/mock

require (
github.com/pkg/errors v0.9.1
golang.org/x/tools v0.0.0-20190425150028-36563e24a262
rsc.io/quote/v3 v3.1.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
28 changes: 26 additions & 2 deletions 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/validate"
)

// Call represents an expected call to a mock.
Expand Down Expand Up @@ -106,9 +108,20 @@ func (c *Call) MaxTimes(n int) *Call {
// The return values from this function are returned by the mocked function.
// It takes an interface{} argument to support n-arity functions.
func (c *Call) DoAndReturn(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 := validate.InputAndOutputSig(ft, mt); err != nil {
panic(fmt.Sprintf("DoAndReturn: %s", err))
}
default:
panic("DoAndReturn: argument must be a function")
}

c.addAction(func(args []interface{}) []interface{} {
vargs := make([]reflect.Value, len(args))
ft := v.Type()
Expand All @@ -135,9 +148,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 := validate.InputSig(ft, mt); err != nil {
panic(fmt.Sprintf("Do: %s", 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 5aae07b

Please sign in to comment.