ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1 | // Copyright 2010 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package gomock |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | // Call represents an expected call to a mock. |
| 25 | type Call struct { |
| 26 | t TestHelper // for triggering test failures on invalid call setup |
| 27 | |
| 28 | receiver interface{} // the receiver of the method call |
| 29 | method string // the name of the method |
| 30 | methodType reflect.Type // the type of the method |
| 31 | args []Matcher // the args |
| 32 | origin string // file and line number of call setup |
| 33 | |
| 34 | preReqs []*Call // prerequisite calls |
| 35 | |
| 36 | // Expectations |
| 37 | minCalls, maxCalls int |
| 38 | |
| 39 | numCalls int // actual number made |
| 40 | |
| 41 | // actions are called when this Call is called. Each action gets the args and |
| 42 | // can set the return values by returning a non-nil slice. Actions run in the |
| 43 | // order they are created. |
| 44 | actions []func([]interface{}) []interface{} |
| 45 | } |
| 46 | |
| 47 | // newCall creates a *Call. It requires the method type in order to support |
| 48 | // unexported methods. |
| 49 | func newCall(t TestHelper, receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call { |
| 50 | t.Helper() |
| 51 | |
| 52 | // TODO: check arity, types. |
| 53 | margs := make([]Matcher, len(args)) |
| 54 | for i, arg := range args { |
| 55 | if m, ok := arg.(Matcher); ok { |
| 56 | margs[i] = m |
| 57 | } else if arg == nil { |
| 58 | // Handle nil specially so that passing a nil interface value |
| 59 | // will match the typed nils of concrete args. |
| 60 | margs[i] = Nil() |
| 61 | } else { |
| 62 | margs[i] = Eq(arg) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // callerInfo's skip should be updated if the number of calls between the user's test |
| 67 | // and this line changes, i.e. this code is wrapped in another anonymous function. |
| 68 | // 0 is us, 1 is RecordCallWithMethodType(), 2 is the generated recorder, and 3 is the user's test. |
| 69 | origin := callerInfo(3) |
| 70 | actions := []func([]interface{}) []interface{}{func([]interface{}) []interface{} { |
| 71 | // Synthesize the zero value for each of the return args' types. |
| 72 | rets := make([]interface{}, methodType.NumOut()) |
| 73 | for i := 0; i < methodType.NumOut(); i++ { |
| 74 | rets[i] = reflect.Zero(methodType.Out(i)).Interface() |
| 75 | } |
| 76 | return rets |
| 77 | }} |
| 78 | return &Call{t: t, receiver: receiver, method: method, methodType: methodType, |
| 79 | args: margs, origin: origin, minCalls: 1, maxCalls: 1, actions: actions} |
| 80 | } |
| 81 | |
| 82 | // AnyTimes allows the expectation to be called 0 or more times |
| 83 | func (c *Call) AnyTimes() *Call { |
| 84 | c.minCalls, c.maxCalls = 0, 1e8 // close enough to infinity |
| 85 | return c |
| 86 | } |
| 87 | |
| 88 | // MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called or if MaxTimes |
| 89 | // was previously called with 1, MinTimes also sets the maximum number of calls to infinity. |
| 90 | func (c *Call) MinTimes(n int) *Call { |
| 91 | c.minCalls = n |
| 92 | if c.maxCalls == 1 { |
| 93 | c.maxCalls = 1e8 |
| 94 | } |
| 95 | return c |
| 96 | } |
| 97 | |
| 98 | // MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called or if MinTimes was |
| 99 | // previously called with 1, MaxTimes also sets the minimum number of calls to 0. |
| 100 | func (c *Call) MaxTimes(n int) *Call { |
| 101 | c.maxCalls = n |
| 102 | if c.minCalls == 1 { |
| 103 | c.minCalls = 0 |
| 104 | } |
| 105 | return c |
| 106 | } |
| 107 | |
| 108 | // DoAndReturn declares the action to run when the call is matched. |
| 109 | // The return values from this function are returned by the mocked function. |
| 110 | // It takes an interface{} argument to support n-arity functions. |
| 111 | func (c *Call) DoAndReturn(f interface{}) *Call { |
| 112 | // TODO: Check arity and types here, rather than dying badly elsewhere. |
| 113 | v := reflect.ValueOf(f) |
| 114 | |
| 115 | c.addAction(func(args []interface{}) []interface{} { |
| 116 | vargs := make([]reflect.Value, len(args)) |
| 117 | ft := v.Type() |
| 118 | for i := 0; i < len(args); i++ { |
| 119 | if args[i] != nil { |
| 120 | vargs[i] = reflect.ValueOf(args[i]) |
| 121 | } else { |
| 122 | // Use the zero value for the arg. |
| 123 | vargs[i] = reflect.Zero(ft.In(i)) |
| 124 | } |
| 125 | } |
| 126 | vrets := v.Call(vargs) |
| 127 | rets := make([]interface{}, len(vrets)) |
| 128 | for i, ret := range vrets { |
| 129 | rets[i] = ret.Interface() |
| 130 | } |
| 131 | return rets |
| 132 | }) |
| 133 | return c |
| 134 | } |
| 135 | |
| 136 | // Do declares the action to run when the call is matched. The function's |
| 137 | // return values are ignored to retain backward compatibility. To use the |
| 138 | // return values call DoAndReturn. |
| 139 | // It takes an interface{} argument to support n-arity functions. |
| 140 | func (c *Call) Do(f interface{}) *Call { |
| 141 | // TODO: Check arity and types here, rather than dying badly elsewhere. |
| 142 | v := reflect.ValueOf(f) |
| 143 | |
| 144 | c.addAction(func(args []interface{}) []interface{} { |
| 145 | vargs := make([]reflect.Value, len(args)) |
| 146 | ft := v.Type() |
| 147 | for i := 0; i < len(args); i++ { |
| 148 | if args[i] != nil { |
| 149 | vargs[i] = reflect.ValueOf(args[i]) |
| 150 | } else { |
| 151 | // Use the zero value for the arg. |
| 152 | vargs[i] = reflect.Zero(ft.In(i)) |
| 153 | } |
| 154 | } |
| 155 | v.Call(vargs) |
| 156 | return nil |
| 157 | }) |
| 158 | return c |
| 159 | } |
| 160 | |
| 161 | // Return declares the values to be returned by the mocked function call. |
| 162 | func (c *Call) Return(rets ...interface{}) *Call { |
| 163 | c.t.Helper() |
| 164 | |
| 165 | mt := c.methodType |
| 166 | if len(rets) != mt.NumOut() { |
| 167 | c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d [%s]", |
| 168 | c.receiver, c.method, len(rets), mt.NumOut(), c.origin) |
| 169 | } |
| 170 | for i, ret := range rets { |
| 171 | if got, want := reflect.TypeOf(ret), mt.Out(i); got == want { |
| 172 | // Identical types; nothing to do. |
| 173 | } else if got == nil { |
| 174 | // Nil needs special handling. |
| 175 | switch want.Kind() { |
| 176 | case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| 177 | // ok |
| 178 | default: |
| 179 | c.t.Fatalf("argument %d to Return for %T.%v is nil, but %v is not nillable [%s]", |
| 180 | i, c.receiver, c.method, want, c.origin) |
| 181 | } |
| 182 | } else if got.AssignableTo(want) { |
| 183 | // Assignable type relation. Make the assignment now so that the generated code |
| 184 | // can return the values with a type assertion. |
| 185 | v := reflect.New(want).Elem() |
| 186 | v.Set(reflect.ValueOf(ret)) |
| 187 | rets[i] = v.Interface() |
| 188 | } else { |
| 189 | c.t.Fatalf("wrong type of argument %d to Return for %T.%v: %v is not assignable to %v [%s]", |
| 190 | i, c.receiver, c.method, got, want, c.origin) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | c.addAction(func([]interface{}) []interface{} { |
| 195 | return rets |
| 196 | }) |
| 197 | |
| 198 | return c |
| 199 | } |
| 200 | |
| 201 | // Times declares the exact number of times a function call is expected to be executed. |
| 202 | func (c *Call) Times(n int) *Call { |
| 203 | c.minCalls, c.maxCalls = n, n |
| 204 | return c |
| 205 | } |
| 206 | |
| 207 | // SetArg declares an action that will set the nth argument's value, |
| 208 | // indirected through a pointer. Or, in the case of a slice, SetArg |
| 209 | // will copy value's elements into the nth argument. |
| 210 | func (c *Call) SetArg(n int, value interface{}) *Call { |
| 211 | c.t.Helper() |
| 212 | |
| 213 | mt := c.methodType |
| 214 | // TODO: This will break on variadic methods. |
| 215 | // We will need to check those at invocation time. |
| 216 | if n < 0 || n >= mt.NumIn() { |
| 217 | c.t.Fatalf("SetArg(%d, ...) called for a method with %d args [%s]", |
| 218 | n, mt.NumIn(), c.origin) |
| 219 | } |
| 220 | // Permit setting argument through an interface. |
| 221 | // In the interface case, we don't (nay, can't) check the type here. |
| 222 | at := mt.In(n) |
| 223 | switch at.Kind() { |
| 224 | case reflect.Ptr: |
| 225 | dt := at.Elem() |
| 226 | if vt := reflect.TypeOf(value); !vt.AssignableTo(dt) { |
| 227 | c.t.Fatalf("SetArg(%d, ...) argument is a %v, not assignable to %v [%s]", |
| 228 | n, vt, dt, c.origin) |
| 229 | } |
| 230 | case reflect.Interface: |
| 231 | // nothing to do |
| 232 | case reflect.Slice: |
| 233 | // nothing to do |
| 234 | default: |
| 235 | c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v [%s]", |
| 236 | n, at, c.origin) |
| 237 | } |
| 238 | |
| 239 | c.addAction(func(args []interface{}) []interface{} { |
| 240 | v := reflect.ValueOf(value) |
| 241 | switch reflect.TypeOf(args[n]).Kind() { |
| 242 | case reflect.Slice: |
| 243 | setSlice(args[n], v) |
| 244 | default: |
| 245 | reflect.ValueOf(args[n]).Elem().Set(v) |
| 246 | } |
| 247 | return nil |
| 248 | }) |
| 249 | return c |
| 250 | } |
| 251 | |
| 252 | // isPreReq returns true if other is a direct or indirect prerequisite to c. |
| 253 | func (c *Call) isPreReq(other *Call) bool { |
| 254 | for _, preReq := range c.preReqs { |
| 255 | if other == preReq || preReq.isPreReq(other) { |
| 256 | return true |
| 257 | } |
| 258 | } |
| 259 | return false |
| 260 | } |
| 261 | |
| 262 | // After declares that the call may only match after preReq has been exhausted. |
| 263 | func (c *Call) After(preReq *Call) *Call { |
| 264 | c.t.Helper() |
| 265 | |
| 266 | if c == preReq { |
| 267 | c.t.Fatalf("A call isn't allowed to be its own prerequisite") |
| 268 | } |
| 269 | if preReq.isPreReq(c) { |
| 270 | c.t.Fatalf("Loop in call order: %v is a prerequisite to %v (possibly indirectly).", c, preReq) |
| 271 | } |
| 272 | |
| 273 | c.preReqs = append(c.preReqs, preReq) |
| 274 | return c |
| 275 | } |
| 276 | |
| 277 | // Returns true if the minimum number of calls have been made. |
| 278 | func (c *Call) satisfied() bool { |
| 279 | return c.numCalls >= c.minCalls |
| 280 | } |
| 281 | |
| 282 | // Returns true if the maximum number of calls have been made. |
| 283 | func (c *Call) exhausted() bool { |
| 284 | return c.numCalls >= c.maxCalls |
| 285 | } |
| 286 | |
| 287 | func (c *Call) String() string { |
| 288 | args := make([]string, len(c.args)) |
| 289 | for i, arg := range c.args { |
| 290 | args[i] = arg.String() |
| 291 | } |
| 292 | arguments := strings.Join(args, ", ") |
| 293 | return fmt.Sprintf("%T.%v(%s) %s", c.receiver, c.method, arguments, c.origin) |
| 294 | } |
| 295 | |
| 296 | // Tests if the given call matches the expected call. |
| 297 | // If yes, returns nil. If no, returns error with message explaining why it does not match. |
| 298 | func (c *Call) matches(args []interface{}) error { |
| 299 | if !c.methodType.IsVariadic() { |
| 300 | if len(args) != len(c.args) { |
| 301 | return fmt.Errorf("expected call at %s has the wrong number of arguments. Got: %d, want: %d", |
| 302 | c.origin, len(args), len(c.args)) |
| 303 | } |
| 304 | |
| 305 | for i, m := range c.args { |
| 306 | if !m.Matches(args[i]) { |
| 307 | return fmt.Errorf( |
| 308 | "expected call at %s doesn't match the argument at index %d.\nGot: %v\nWant: %v", |
| 309 | c.origin, i, formatGottenArg(m, args[i]), m, |
| 310 | ) |
| 311 | } |
| 312 | } |
| 313 | } else { |
| 314 | if len(c.args) < c.methodType.NumIn()-1 { |
| 315 | return fmt.Errorf("expected call at %s has the wrong number of matchers. Got: %d, want: %d", |
| 316 | c.origin, len(c.args), c.methodType.NumIn()-1) |
| 317 | } |
| 318 | if len(c.args) != c.methodType.NumIn() && len(args) != len(c.args) { |
| 319 | return fmt.Errorf("expected call at %s has the wrong number of arguments. Got: %d, want: %d", |
| 320 | c.origin, len(args), len(c.args)) |
| 321 | } |
| 322 | if len(args) < len(c.args)-1 { |
| 323 | return fmt.Errorf("expected call at %s has the wrong number of arguments. Got: %d, want: greater than or equal to %d", |
| 324 | c.origin, len(args), len(c.args)-1) |
| 325 | } |
| 326 | |
| 327 | for i, m := range c.args { |
| 328 | if i < c.methodType.NumIn()-1 { |
| 329 | // Non-variadic args |
| 330 | if !m.Matches(args[i]) { |
| 331 | return fmt.Errorf("expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v", |
| 332 | c.origin, strconv.Itoa(i), formatGottenArg(m, args[i]), m) |
| 333 | } |
| 334 | continue |
| 335 | } |
| 336 | // The last arg has a possibility of a variadic argument, so let it branch |
| 337 | |
| 338 | // sample: Foo(a int, b int, c ...int) |
| 339 | if i < len(c.args) && i < len(args) { |
| 340 | if m.Matches(args[i]) { |
| 341 | // Got Foo(a, b, c) want Foo(matcherA, matcherB, gomock.Any()) |
| 342 | // Got Foo(a, b, c) want Foo(matcherA, matcherB, someSliceMatcher) |
| 343 | // Got Foo(a, b, c) want Foo(matcherA, matcherB, matcherC) |
| 344 | // Got Foo(a, b) want Foo(matcherA, matcherB) |
| 345 | // Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD) |
| 346 | continue |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // The number of actual args don't match the number of matchers, |
| 351 | // or the last matcher is a slice and the last arg is not. |
| 352 | // If this function still matches it is because the last matcher |
| 353 | // matches all the remaining arguments or the lack of any. |
| 354 | // Convert the remaining arguments, if any, into a slice of the |
| 355 | // expected type. |
| 356 | vargsType := c.methodType.In(c.methodType.NumIn() - 1) |
| 357 | vargs := reflect.MakeSlice(vargsType, 0, len(args)-i) |
| 358 | for _, arg := range args[i:] { |
| 359 | vargs = reflect.Append(vargs, reflect.ValueOf(arg)) |
| 360 | } |
| 361 | if m.Matches(vargs.Interface()) { |
| 362 | // Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, gomock.Any()) |
| 363 | // Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, someSliceMatcher) |
| 364 | // Got Foo(a, b) want Foo(matcherA, matcherB, gomock.Any()) |
| 365 | // Got Foo(a, b) want Foo(matcherA, matcherB, someEmptySliceMatcher) |
| 366 | break |
| 367 | } |
| 368 | // Wrong number of matchers or not match. Fail. |
| 369 | // Got Foo(a, b) want Foo(matcherA, matcherB, matcherC, matcherD) |
| 370 | // Got Foo(a, b, c) want Foo(matcherA, matcherB, matcherC, matcherD) |
| 371 | // Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD, matcherE) |
| 372 | // Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, matcherC, matcherD) |
| 373 | // Got Foo(a, b, c) want Foo(matcherA, matcherB) |
| 374 | |
| 375 | return fmt.Errorf("expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v", |
| 376 | c.origin, strconv.Itoa(i), formatGottenArg(m, args[i:]), c.args[i]) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Check that all prerequisite calls have been satisfied. |
| 381 | for _, preReqCall := range c.preReqs { |
| 382 | if !preReqCall.satisfied() { |
| 383 | return fmt.Errorf("Expected call at %s doesn't have a prerequisite call satisfied:\n%v\nshould be called before:\n%v", |
| 384 | c.origin, preReqCall, c) |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // Check that the call is not exhausted. |
| 389 | if c.exhausted() { |
| 390 | return fmt.Errorf("expected call at %s has already been called the max number of times", c.origin) |
| 391 | } |
| 392 | |
| 393 | return nil |
| 394 | } |
| 395 | |
| 396 | // dropPrereqs tells the expected Call to not re-check prerequisite calls any |
| 397 | // longer, and to return its current set. |
| 398 | func (c *Call) dropPrereqs() (preReqs []*Call) { |
| 399 | preReqs = c.preReqs |
| 400 | c.preReqs = nil |
| 401 | return |
| 402 | } |
| 403 | |
| 404 | func (c *Call) call() []func([]interface{}) []interface{} { |
| 405 | c.numCalls++ |
| 406 | return c.actions |
| 407 | } |
| 408 | |
| 409 | // InOrder declares that the given calls should occur in order. |
| 410 | func InOrder(calls ...*Call) { |
| 411 | for i := 1; i < len(calls); i++ { |
| 412 | calls[i].After(calls[i-1]) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func setSlice(arg interface{}, v reflect.Value) { |
| 417 | va := reflect.ValueOf(arg) |
| 418 | for i := 0; i < v.Len(); i++ { |
| 419 | va.Index(i).Set(v.Index(i)) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | func (c *Call) addAction(action func([]interface{}) []interface{}) { |
| 424 | c.actions = append(c.actions, action) |
| 425 | } |
| 426 | |
| 427 | func formatGottenArg(m Matcher, arg interface{}) string { |
| 428 | got := fmt.Sprintf("%v", arg) |
| 429 | if gs, ok := m.(GotFormatter); ok { |
| 430 | got = gs.Got(arg) |
| 431 | } |
| 432 | return got |
| 433 | } |