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

Commit 7d8f57b

Browse files
committed
Fix #71 Do + DoAndReturn signature change error msg
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
1 parent 5c85495 commit 7d8f57b

File tree

6 files changed

+632
-3
lines changed

6 files changed

+632
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/golang/mock
22

33
require (
4+
github.com/pkg/errors v0.9.1
45
golang.org/x/tools v0.0.0-20190425150028-36563e24a262
56
rsc.io/quote/v3 v3.1.0
67
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
13
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
24
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
35
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

gomock/call.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"reflect"
2020
"strconv"
2121
"strings"
22+
23+
"github.com/golang/mock/gomock/internal/calldo"
2224
)
2325

2426
// Call represents an expected call to a mock.
@@ -106,9 +108,20 @@ func (c *Call) MaxTimes(n int) *Call {
106108
// The return values from this function are returned by the mocked function.
107109
// It takes an interface{} argument to support n-arity functions.
108110
func (c *Call) DoAndReturn(f interface{}) *Call {
109-
// TODO: Check arity and types here, rather than dying badly elsewhere.
110111
v := reflect.ValueOf(f)
111112

113+
switch v.Kind() {
114+
case reflect.Func:
115+
mt := c.methodType
116+
117+
ft := v.Type()
118+
if err := calldo.ValidateInputAndOutputSig(ft, mt); err != nil {
119+
panic(fmt.Sprintf("DoAndReturn: %s", err))
120+
}
121+
default:
122+
panic("DoAndReturn: argument must be a function")
123+
}
124+
112125
c.addAction(func(args []interface{}) []interface{} {
113126
vargs := make([]reflect.Value, len(args))
114127
ft := v.Type()
@@ -135,9 +148,20 @@ func (c *Call) DoAndReturn(f interface{}) *Call {
135148
// return values call DoAndReturn.
136149
// It takes an interface{} argument to support n-arity functions.
137150
func (c *Call) Do(f interface{}) *Call {
138-
// TODO: Check arity and types here, rather than dying badly elsewhere.
139151
v := reflect.ValueOf(f)
140152

153+
switch v.Kind() {
154+
case reflect.Func:
155+
mt := c.methodType
156+
157+
ft := v.Type()
158+
if err := calldo.ValidateInputAndOutputSig(ft, mt); err != nil {
159+
panic(fmt.Sprintf("Do: %s", err))
160+
}
161+
default:
162+
panic("Do: argument must be a function")
163+
}
164+
141165
c.addAction(func(args []interface{}) []interface{} {
142166
vargs := make([]reflect.Value, len(args))
143167
ft := v.Type()

0 commit comments

Comments
 (0)