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

Commit dbb6401

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

File tree

4 files changed

+1044
-3
lines changed

4 files changed

+1044
-3
lines changed

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/validate"
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 := validate.InputAndOutputSig(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 := validate.InputSig(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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy