| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * The error formatter for mocking is a bit different from the default one | 6 * The error formatter for mocking is a bit different from the default one |
| 7 * for unit testing; instead of the third argument being a 'reason' | 7 * for unit testing; instead of the third argument being a 'reason' |
| 8 * it is instead a [signature] describing the method signature filter | 8 * it is instead a [signature] describing the method signature filter |
| 9 * that was used to select the logs that were verified. | 9 * that was used to select the logs that were verified. |
| 10 */ | 10 */ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 _MockFailureHandler _mockFailureHandler = null; | 36 _MockFailureHandler _mockFailureHandler = null; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * [_noArg] is a sentinel value representing no argument. | 39 * [_noArg] is a sentinel value representing no argument. |
| 40 */ | 40 */ |
| 41 final _noArg = const _Sentinel(); | 41 final _noArg = const _Sentinel(); |
| 42 | 42 |
| 43 /** The ways in which a call to a mock method can be handled. */ | 43 /** The ways in which a call to a mock method can be handled. */ |
| 44 final RETURN = 0; | 44 final _IGNORE = 0; /** Do nothing (void method) */ |
| 45 final THROW = 1; | 45 final _RETURN = 1; /** Return a supplied value. */ |
| 46 final PROXY = 2; | 46 final _THROW = 2; /** Throw a supplied value. */ |
| 47 final _PROXY = 3; /** Call a supplied function. */ |
| 47 | 48 |
| 48 /** | 49 /** |
| 49 * The behavior of a method call in the mock library is specified | 50 * The behavior of a method call in the mock library is specified |
| 50 * with [Responder]s. A [Responder] has a [value] to throw | 51 * with [Responder]s. A [Responder] has a [value] to throw |
| 51 * or return (depending on whether [isThrow] is true or not, respectively), | 52 * or return (depending on whether [isThrow] is true or not, respectively), |
| 52 * and can either be one-shot, multi-shot, or infinitely repeating, | 53 * and can either be one-shot, multi-shot, or infinitely repeating, |
| 53 * depending on the value of [count (1, greater than 1, or 0 respectively). | 54 * depending on the value of [count (1, greater than 1, or 0 respectively). |
| 54 */ | 55 */ |
| 55 class Responder { | 56 class Responder { |
| 56 var value; | 57 var value; |
| 57 int action; | 58 int action; |
| 58 int count; | 59 int count; |
| 59 Responder(this.value, [this.count = 1, this.action = RETURN]); | 60 Responder(this.value, [this.count = 1, this.action = _RETURN]); |
| 60 } | 61 } |
| 61 | 62 |
| 62 /** | 63 /** |
| 63 * A [CallMatcher] is a special matcher used to match method calls (i.e. | 64 * A [CallMatcher] is a special matcher used to match method calls (i.e. |
| 64 * a method name and set of arguments). It is not a [Matcher] like the | 65 * a method name and set of arguments). It is not a [Matcher] like the |
| 65 * unit test [Matcher], but instead represents a collection of [Matcher]s, | 66 * unit test [Matcher], but instead represents a method name and a |
| 66 * one per argument, that will be applied to the parameters to decide if | 67 * collection of [Matcher]s, one per argument, that will be applied |
| 67 * the method call is a match. | 68 * to the parameters to decide if the method call is a match. |
| 68 */ | 69 */ |
| 69 class CallMatcher { | 70 class CallMatcher { |
| 70 String name; | 71 String name; |
| 71 List<Matcher> argMatchers; | 72 List<Matcher> argMatchers; |
| 72 | 73 |
| 73 CallMatcher(String method, [ | 74 CallMatcher(String method, [ |
| 74 arg0 = _noArg, | 75 arg0 = _noArg, |
| 75 arg1 = _noArg, | 76 arg1 = _noArg, |
| 76 arg2 = _noArg, | 77 arg2 = _noArg, |
| 77 arg3 = _noArg, | 78 arg3 = _noArg, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 168 |
| 168 Behavior (this.matcher) { | 169 Behavior (this.matcher) { |
| 169 actions = new List<Responder>(); | 170 actions = new List<Responder>(); |
| 170 } | 171 } |
| 171 | 172 |
| 172 /** | 173 /** |
| 173 * Adds a [Responder] that returns a [value] for [count] calls | 174 * Adds a [Responder] that returns a [value] for [count] calls |
| 174 * (1 by default). | 175 * (1 by default). |
| 175 */ | 176 */ |
| 176 Behavior thenReturn(value, [count = 1]) { | 177 Behavior thenReturn(value, [count = 1]) { |
| 177 actions.add(new Responder(value, count, RETURN)); | 178 actions.add(new Responder(value, count, _RETURN)); |
| 178 return this; // For chaining calls. | 179 return this; // For chaining calls. |
| 179 } | 180 } |
| 180 | 181 |
| 181 /** Adds a [Responder] that repeatedly returns a [value]. */ | 182 /** Adds a [Responder] that repeatedly returns a [value]. */ |
| 182 Behavior alwaysReturn(value) { | 183 Behavior alwaysReturn(value) { |
| 183 return thenReturn(value, 0); | 184 return thenReturn(value, 0); |
| 184 } | 185 } |
| 185 | 186 |
| 186 /** | 187 /** |
| 187 * Adds a [Responder] that throws [value] [count] | 188 * Adds a [Responder] that throws [value] [count] |
| 188 * times (1 by default). | 189 * times (1 by default). |
| 189 */ | 190 */ |
| 190 Behavior thenThrow(value, [count = 1]) { | 191 Behavior thenThrow(value, [count = 1]) { |
| 191 actions.add(new Responder(value, count, THROW)); | 192 actions.add(new Responder(value, count, _THROW)); |
| 192 return this; // For chaining calls. | 193 return this; // For chaining calls. |
| 193 } | 194 } |
| 194 | 195 |
| 195 /** Adds a [Responder] that throws [value] endlessly. */ | 196 /** Adds a [Responder] that throws [value] endlessly. */ |
| 196 Behavior alwaysThrow(value) { | 197 Behavior alwaysThrow(value) { |
| 197 return thenThrow(value, 0); | 198 return thenThrow(value, 0); |
| 198 } | 199 } |
| 199 | 200 |
| 200 /** | 201 /** |
| 201 * [thenCall] creates a proxy Responder, that is called [count] | 202 * [thenCall] creates a proxy Responder, that is called [count] |
| 202 * times (1 by default; 0 is used for unlimited calls, and is | 203 * times (1 by default; 0 is used for unlimited calls, and is |
| 203 * exposed as [alwaysCall]). [value] is the function that will | 204 * exposed as [alwaysCall]). [value] is the function that will |
| 204 * be called with the same arguments that were passed to the | 205 * be called with the same arguments that were passed to the |
| 205 * mock. Proxies can be used to wrap real objects or to define | 206 * mock. Proxies can be used to wrap real objects or to define |
| 206 * more complex return/throw behavior. You could even (if you | 207 * more complex return/throw behavior. You could even (if you |
| 207 * wanted) use proxies to emulate the behavior of thenReturn; | 208 * wanted) use proxies to emulate the behavior of thenReturn; |
| 208 * e.g.: | 209 * e.g.: |
| 209 * | 210 * |
| 210 * m.when(callsTo('foo')).thenReturn(0) | 211 * m.when(callsTo('foo')).thenReturn(0) |
| 211 * | 212 * |
| 212 * is equivalent to: | 213 * is equivalent to: |
| 213 * | 214 * |
| 214 * m.when(callsTo('foo')).thenCall(() => 0) | 215 * m.when(callsTo('foo')).thenCall(() => 0) |
| 215 */ | 216 */ |
| 216 Behavior thenCall(value, [count = 1]) { | 217 Behavior thenCall(value, [count = 1]) { |
| 217 actions.add(new Responder(value, count, PROXY)); | 218 actions.add(new Responder(value, count, _PROXY)); |
| 218 return this; // For chaining calls. | 219 return this; // For chaining calls. |
| 219 } | 220 } |
| 220 | 221 |
| 221 /** Creates a repeating proxy call. */ | 222 /** Creates a repeating proxy call. */ |
| 222 Behavior alwaysCall(value) { | 223 Behavior alwaysCall(value) { |
| 223 return thenCall(value, 0); | 224 return thenCall(value, 0); |
| 224 } | 225 } |
| 225 | 226 |
| 226 /** Returns true if a method call matches the [Behavior]. */ | 227 /** Returns true if a method call matches the [Behavior]. */ |
| 227 bool matches(name, args) => matcher.matches(name, args); | 228 bool matches(name, args) => matcher.matches(name, args); |
| 228 | 229 |
| 229 /** Returns the [matcher]'s representation. */ | 230 /** Returns the [matcher]'s representation. */ |
| 230 String toString() => matcher.toString(); | 231 String toString() => matcher.toString(); |
| 231 } | 232 } |
| 232 | 233 |
| 233 /** | 234 /** |
| 234 * Every call to a [Mock] object method is logged. The logs are | 235 * Every call to a [Mock] object method is logged. The logs are |
| 235 * kept in instances of [LogEntry]. | 236 * kept in instances of [LogEntry]. |
| 236 */ | 237 */ |
| 237 class LogEntry { | 238 class LogEntry { |
| 238 final String name; // The method name. | 239 /** The time of the event. */ |
| 239 final List args; // The parameters. | 240 Date when; |
| 240 final int action; // The behavior that resulted. | |
| 241 final value; // The value that was returned (if no throw). | |
| 242 | 241 |
| 243 const LogEntry(this.name, this.args, this.action, [this.value = null]); | 242 /** The mock object name, if any. */ |
| 243 final String mockName; |
| 244 |
| 245 /** The method name. */ |
| 246 final String methodName; |
| 247 |
| 248 /** The parameters. */ |
| 249 final List args; |
| 250 |
| 251 /** The behavior that resulted. */ |
| 252 final int action; |
| 253 |
| 254 /** The value that was returned (if no throw). */ |
| 255 final value; |
| 256 |
| 257 LogEntry(this.mockName, this.methodName, |
| 258 this.args, this.action, [this.value = null]) { |
| 259 when = new Date.now(); |
| 260 } |
| 261 |
| 262 String _pad2(int value) => (value >= 10 ? '$value' : '0$value'); |
| 263 |
| 264 String toString([Date baseTime = null]) { |
| 265 Description d = new StringDescription(); |
| 266 if (baseTime == null) { |
| 267 // Show absolute time. |
| 268 d.add('${when.hour}:${_pad2(when.minute)}:' |
| 269 '${_pad2(when.second)}.${when.millisecond}> '); |
| 270 } else { |
| 271 // Show relative time. |
| 272 int delta = when.millisecondsSinceEpoch - baseTime.millisecondsSinceEpoch; |
| 273 int secs = delta ~/ 1000; |
| 274 int msecs = delta % 1000; |
| 275 d.add('$secs.$msecs> '); |
| 276 } |
| 277 d.add('${_qualifiedName(mockName, methodName)}('); |
| 278 for (var i = 0; i < args.length; i++) { |
| 279 if (i != 0) d.add(', '); |
| 280 d.addDescriptionOf(args[i]); |
| 281 } |
| 282 d.add(') ${action == _THROW ? "threw" : "returned"} '); |
| 283 d.addDescriptionOf(value); |
| 284 return d.toString(); |
| 285 } |
| 244 } | 286 } |
| 245 | 287 |
| 288 /** Utility function for optionally qualified method names */ |
| 289 String _qualifiedName(String owner, String method) { |
| 290 if (owner == null) { |
| 291 return method; |
| 292 } else { |
| 293 return '$owner.$method'; |
| 294 } |
| 295 } |
| 246 /** | 296 /** |
| 247 * We do verification on a list of [LogEntry]s. To allow chaining | 297 * We do verification on a list of [LogEntry]s. To allow chaining |
| 248 * of calls to verify, we encapsulate such a list in the [LogEntryList] | 298 * of calls to verify, we encapsulate such a list in the [LogEntryList] |
| 249 * class. | 299 * class. |
| 250 */ | 300 */ |
| 251 class LogEntryList { | 301 class LogEntryList { |
| 252 final String filter; | 302 final String filter; |
| 253 final List<LogEntry> logs; | 303 List<LogEntry> logs; |
| 254 const LogEntryList(this.logs, [this.filter = null]); | 304 |
| 305 LogEntryList([this.filter = null]) { |
| 306 logs = new List<LogEntry>(); |
| 307 } |
| 255 | 308 |
| 256 /** Add a [LogEntry] to the log. */ | 309 /** Add a [LogEntry] to the log. */ |
| 257 add(LogEntry entry) => logs.add(entry); | 310 add(LogEntry entry) => logs.add(entry); |
| 258 | 311 |
| 259 /** | 312 /** |
| 260 * Create a new [LogEntryList] consisting of [LogEntry]s from | 313 * Create a new [LogEntryList] consisting of [LogEntry]s from |
| 261 * this list that match the specified [logfilter]. If [destructive] | 314 * this list that match the specified [mockName] and [logfilter]. |
| 315 * If [mockName] is null, all entries will be checked. If [destructive] |
| 262 * is true, the log entries are removed from the original list. | 316 * is true, the log entries are removed from the original list. |
| 263 */ | 317 */ |
| 264 LogEntryList getMatches(CallMatcher logfilter, bool destructive) { | 318 LogEntryList getMatches(String mockName, |
| 265 LogEntryList rtn = | 319 CallMatcher logFilter, |
| 266 new LogEntryList(new List<LogEntry>(), logfilter.toString()); | 320 [Matcher actionMatcher = null, |
| 321 bool destructive = false]) { |
| 322 String filterName = _qualifiedName(mockName, logFilter.toString()); |
| 323 LogEntryList rtn = new LogEntryList(filterName); |
| 267 for (var i = 0; i < logs.length; i++) { | 324 for (var i = 0; i < logs.length; i++) { |
| 268 LogEntry entry = logs[i]; | 325 LogEntry entry = logs[i]; |
| 269 if (logfilter.matches(entry.name, entry.args)) { | 326 if (mockName != null && mockName != entry.mockName) { |
| 270 rtn.add(entry); | 327 continue; |
| 271 if (destructive) { | 328 } |
| 272 logs.removeRange(i--, 1); | 329 if (logFilter.matches(entry.methodName, entry.args)) { |
| 330 if (actionMatcher == null || actionMatcher.matches(entry)) { |
| 331 rtn.add(entry); |
| 332 if (destructive) { |
| 333 logs.removeRange(i--, 1); |
| 334 } |
| 273 } | 335 } |
| 274 } | 336 } |
| 275 } | 337 } |
| 276 return rtn; | 338 return rtn; |
| 277 } | 339 } |
| 278 | 340 |
| 279 /** Apply a unit test [Matcher] to the [LogEntryList]. */ | 341 /** Apply a unit test [Matcher] to the [LogEntryList]. */ |
| 280 LogEntryList verify(Matcher matcher) { | 342 LogEntryList verify(Matcher matcher) { |
| 281 if (_mockFailureHandler == null) { | 343 if (_mockFailureHandler == null) { |
| 282 _mockFailureHandler = | 344 _mockFailureHandler = |
| 283 new _MockFailureHandler(getOrCreateExpectFailureHandler()); | 345 new _MockFailureHandler(getOrCreateExpectFailureHandler()); |
| 284 } | 346 } |
| 285 expect(logs, matcher, filter, _mockFailureHandler); | 347 expect(logs, matcher, filter, _mockFailureHandler); |
| 286 return this; | 348 return this; |
| 287 } | 349 } |
| 350 |
| 351 String toString([Date baseTime = null]) { |
| 352 String s = ''; |
| 353 for (var e in logs) { |
| 354 s = '$s${e.toString(baseTime)}\n'; |
| 355 } |
| 356 return s; |
| 357 } |
| 288 } | 358 } |
| 289 | 359 |
| 290 /** | 360 /** |
| 291 * [_TimesMatcher]s are used to make assertions about the number of | 361 * [_TimesMatcher]s are used to make assertions about the number of |
| 292 * times a method was called. | 362 * times a method was called. |
| 293 */ | 363 */ |
| 294 class _TimesMatcher extends BaseMatcher { | 364 class _TimesMatcher extends BaseMatcher { |
| 295 final int min, max; | 365 final int min, max; |
| 296 | 366 |
| 297 const _TimesMatcher(this.min, [this.max = -1]); | 367 const _TimesMatcher(this.min, [this.max = -1]); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 309 } else { | 379 } else { |
| 310 description.add('between $min and $max'); | 380 description.add('between $min and $max'); |
| 311 } | 381 } |
| 312 return description.add(' times'); | 382 return description.add(' times'); |
| 313 } | 383 } |
| 314 | 384 |
| 315 Description describeMismatch(log, Description mismatchDescription) => | 385 Description describeMismatch(log, Description mismatchDescription) => |
| 316 mismatchDescription.add('was called ${log.length} times'); | 386 mismatchDescription.add('was called ${log.length} times'); |
| 317 } | 387 } |
| 318 | 388 |
| 319 /** [calledExactly] matches an exact number of calls. */ | 389 /** [happenedExactly] matches an exact number of calls. */ |
| 320 Matcher calledExactly(count) { | 390 Matcher happenedExactly(count) { |
| 321 return new _TimesMatcher(count, count); | 391 return new _TimesMatcher(count, count); |
| 322 } | 392 } |
| 323 | 393 |
| 324 /** [calledAtLeast] matches a minimum number of calls. */ | 394 /** [happenedAtLeast] matches a minimum number of calls. */ |
| 325 Matcher calledAtLeast(count) { | 395 Matcher happenedAtLeast(count) { |
| 326 return new _TimesMatcher(count); | 396 return new _TimesMatcher(count); |
| 327 } | 397 } |
| 328 | 398 |
| 329 /** [calledAtMost] matches a maximum number of calls. */ | 399 /** [happenedAtMost] matches a maximum number of calls. */ |
| 330 Matcher calledAtMost(count) { | 400 Matcher happenedAtMost(count) { |
| 331 return new _TimesMatcher(0, count); | 401 return new _TimesMatcher(0, count); |
| 332 } | 402 } |
| 333 | 403 |
| 334 /** [neverCalled] matches zero calls. */ | 404 /** [neverHappened] matches zero calls. */ |
| 335 final Matcher neverCalled = const _TimesMatcher(0, 0); | 405 final Matcher neverHappened = const _TimesMatcher(0, 0); |
| 336 | 406 |
| 337 /** [calledOnce] matches exactly one call. */ | 407 /** [happenedOnce] matches exactly one call. */ |
| 338 final Matcher calledOnce = const _TimesMatcher(1, 1); | 408 final Matcher happenedOnce = const _TimesMatcher(1, 1); |
| 339 | 409 |
| 340 /** [calledAtLeastOnce] matches one or more calls. */ | 410 /** [happenedAtLeastOnce] matches one or more calls. */ |
| 341 final Matcher calledAtLeastOnce = const _TimesMatcher(1); | 411 final Matcher happenedAtLeastOnce = const _TimesMatcher(1); |
| 342 | 412 |
| 343 /** [calledAtMostOnce] matches zero or one call. */ | 413 /** [happenedAtMostOnce] matches zero or one call. */ |
| 344 final Matcher calledAtMostOnce = const _TimesMatcher(0, 1); | 414 final Matcher happenedAtMostOnce = const _TimesMatcher(0, 1); |
| 345 | 415 |
| 346 /** Special values for use with [_ResultMatcher] [frequency]. */ | |
| 347 final int ALL = 0; | |
| 348 final int SOME = 1; | |
| 349 final int NONE = 2; | |
| 350 /** | 416 /** |
| 351 * [_ResultMatcher]s are used to make assertions about the results | 417 * [_ResultMatcher]s are used to make assertions about the results |
| 352 * of method calls. When filtering an execution log by calling | 418 * of method calls. These can be used as optional parameters to getLogs(). |
| 353 * [forThe], a [LogEntrySet] of matching call logs is returned; | |
| 354 * [_ResultMatcher]s can then assert various things about this | |
| 355 * (sub)set of logs. | |
| 356 */ | 419 */ |
| 357 class _ResultMatcher extends BaseMatcher { | 420 class _ResultMatcher extends BaseMatcher { |
| 358 final int action; | 421 final int action; |
| 359 final value; | 422 final value; |
| 423 |
| 424 const _ResultMatcher(this.action, this.value); |
| 425 |
| 426 bool matches(item) { |
| 427 if (item is! LogEntry) { |
| 428 return false; |
| 429 } |
| 430 // normalize the action; _PROXY is like _RETURN. |
| 431 int eaction = (item.action == _THROW) ? _THROW : _RETURN; |
| 432 return (eaction == action && value.matches(item.value)); |
| 433 } |
| 434 |
| 435 Description describe(Description description) { |
| 436 description.add(' to '); |
| 437 if (action == _RETURN || action == _PROXY) |
| 438 description.add('return '); |
| 439 else |
| 440 description.add('throw '); |
| 441 return description.addDescriptionOf(value); |
| 442 } |
| 443 |
| 444 Description describeMismatch(log, Description mismatchDescription) { |
| 445 if (entry.action == _RETURN || entry.action == _PROXY) { |
| 446 mismatchDescription.add('returned '); |
| 447 } else { |
| 448 mismatchDescription.add('threw '); |
| 449 } |
| 450 mismatchDescription.add(entry.value); |
| 451 return mismatchDescription; |
| 452 } |
| 453 } |
| 454 |
| 455 /** |
| 456 *[returning] matches log entries where the call to a method returned |
| 457 * a value that matched [value]. |
| 458 */ |
| 459 Matcher returning(value) => |
| 460 new _ResultMatcher(_RETURN, wrapMatcher(value)); |
| 461 |
| 462 /** |
| 463 *[throwing] matches log entrues where the call to a method threw |
| 464 * a value that matched [value]. |
| 465 */ |
| 466 Matcher throwing(value) => |
| 467 new _ResultMatcher(_THROW, wrapMatcher(value)); |
| 468 |
| 469 /** Special values for use with [_ResultSetMatcher] [frequency]. */ |
| 470 final int _ALL = 0; /** Every call/throw must match */ |
| 471 final int _SOME = 1; /** At least one call/throw must match. */ |
| 472 final int _NONE = 2; /** No calls/throws should match. */ |
| 473 |
| 474 /** |
| 475 * [_ResultSetMatcher]s are used to make assertions about the results |
| 476 * of method calls. When filtering an execution log by calling |
| 477 * [getLogs], a [LogEntrySet] of matching call logs is returned; |
| 478 * [_ResultSetMatcher]s can then assert various things about this |
| 479 * (sub)set of logs. |
| 480 * |
| 481 * We could make this class use _ResultMatcher but it doesn't buy that |
| 482 * match and adds some perf hit, so there is some duplication here. |
| 483 */ |
| 484 class _ResultSetMatcher extends BaseMatcher { |
| 485 final int action; |
| 486 final value; |
| 360 final int frequency; // -1 for all, 0 for none, 1 for some. | 487 final int frequency; // -1 for all, 0 for none, 1 for some. |
| 361 | 488 |
| 362 const _ResultMatcher(this.action, this.value, this.frequency); | 489 const _ResultSetMatcher(this.action, this.value, this.frequency); |
| 363 | 490 |
| 364 bool matches(log) { | 491 bool matches(log) { |
| 365 for (LogEntry entry in log) { | 492 for (LogEntry entry in log) { |
| 366 // normalize the action; PROXY is like RETURN. | 493 // normalize the action; _PROXY is like _RETURN. |
| 367 int eaction = (entry.action == THROW) ? THROW : RETURN; | 494 int eaction = (entry.action == _THROW) ? _THROW : _RETURN; |
| 368 if (eaction == action && value.matches(entry.value)) { | 495 if (eaction == action && value.matches(entry.value)) { |
| 369 if (frequency == NONE) { | 496 if (frequency == _NONE) { |
| 370 return false; | 497 return false; |
| 371 } else if (frequency == SOME) { | 498 } else if (frequency == _SOME) { |
| 372 return true; | 499 return true; |
| 373 } | 500 } |
| 374 } else { | 501 } else { |
| 375 // Mismatch. | 502 // Mismatch. |
| 376 if (frequency == ALL) { // We need just one mismatch to fail. | 503 if (frequency == _ALL) { // We need just one mismatch to fail. |
| 377 return false; | 504 return false; |
| 378 } | 505 } |
| 379 } | 506 } |
| 380 } | 507 } |
| 381 // If we get here, then if count is ALL we got all matches and | 508 // If we get here, then if count is _ALL we got all matches and |
| 382 // this is success; otherwise we got all mismatched which is | 509 // this is success; otherwise we got all mismatched which is |
| 383 // success for count == NONE and failure for count == SOME. | 510 // success for count == _NONE and failure for count == _SOME. |
| 384 return (frequency != SOME); | 511 return (frequency != _SOME); |
| 385 } | 512 } |
| 386 | 513 |
| 387 Description describe(Description description) { | 514 Description describe(Description description) { |
| 388 description.add(' to '); | 515 description.add(' to '); |
| 389 description.add(frequency == ALL ? 'alway ' : | 516 description.add(frequency == _ALL ? 'alway ' : |
| 390 (frequency == NONE ? 'never ' : 'sometimes ')); | 517 (frequency == _NONE ? 'never ' : 'sometimes ')); |
| 391 if (action == RETURN || action == PROXY) | 518 if (action == _RETURN || action == _PROXY) |
| 392 description.add('return '); | 519 description.add('return '); |
| 393 else | 520 else |
| 394 description.add('throw '); | 521 description.add('throw '); |
| 395 return description.addDescriptionOf(value); | 522 return description.addDescriptionOf(value); |
| 396 } | 523 } |
| 397 | 524 |
| 398 Description describeMismatch(log, Description mismatchDescription) { | 525 Description describeMismatch(log, Description mismatchDescription) { |
| 399 if (frequency != SOME) { | 526 if (frequency != _SOME) { |
| 400 for (LogEntry entry in log) { | 527 for (LogEntry entry in log) { |
| 401 if (entry.action != action || !value.matches(entry.value)) { | 528 if (entry.action != action || !value.matches(entry.value)) { |
| 402 if (entry.action == RETURN || entry.action == PROXY) | 529 if (entry.action == _RETURN || entry.action == _PROXY) |
| 403 mismatchDescription.add('returned '); | 530 mismatchDescription.add('returned '); |
| 404 else | 531 else |
| 405 mismatchDescription.add('threw '); | 532 mismatchDescription.add('threw '); |
| 406 mismatchDescription.add(entry.value); | 533 mismatchDescription.add(entry.value); |
| 407 mismatchDescription.add(' at least once'); | 534 mismatchDescription.add(' at least once'); |
| 408 break; | 535 break; |
| 409 } | 536 } |
| 410 } | 537 } |
| 411 } else { | 538 } else { |
| 412 mismatchDescription.add('never did'); | 539 mismatchDescription.add('never did'); |
| 413 } | 540 } |
| 414 return mismatchDescription; | 541 return mismatchDescription; |
| 415 } | 542 } |
| 416 } | 543 } |
| 417 | 544 |
| 418 /** | 545 /** |
| 419 *[alwaysReturned] asserts that all matching calls to a method returned | 546 *[alwaysReturned] asserts that all matching calls to a method returned |
| 420 * a value that matched [value]. | 547 * a value that matched [value]. |
| 421 */ | 548 */ |
| 422 Matcher alwaysReturned(value) => | 549 Matcher alwaysReturned(value) => |
| 423 new _ResultMatcher(RETURN, wrapMatcher(value), ALL); | 550 new _ResultSetMatcher(_RETURN, wrapMatcher(value), _ALL); |
| 424 | 551 |
| 425 /** | 552 /** |
| 426 *[sometimeReturned] asserts that at least one matching call to a method | 553 *[sometimeReturned] asserts that at least one matching call to a method |
| 427 * returned a value that matched [value]. | 554 * returned a value that matched [value]. |
| 428 */ | 555 */ |
| 429 Matcher sometimeReturned(value) => | 556 Matcher sometimeReturned(value) => |
| 430 new _ResultMatcher(RETURN, wrapMatcher(value), SOME); | 557 new _ResultSetMatcher(_RETURN, wrapMatcher(value), _SOME); |
| 431 | 558 |
| 432 /** | 559 /** |
| 433 *[neverReturned] asserts that no matching calls to a method returned | 560 *[neverReturned] asserts that no matching calls to a method returned |
| 434 * a value that matched [value]. | 561 * a value that matched [value]. |
| 435 */ | 562 */ |
| 436 Matcher neverReturned(value) => | 563 Matcher neverReturned(value) => |
| 437 new _ResultMatcher(RETURN, wrapMatcher(value), NONE); | 564 new _ResultSetMatcher(_RETURN, wrapMatcher(value), _NONE); |
| 438 | 565 |
| 439 /** | 566 /** |
| 440 *[alwaysThrew] asserts that all matching calls to a method threw | 567 *[alwaysThrew] asserts that all matching calls to a method threw |
| 441 * a value that matched [value]. | 568 * a value that matched [value]. |
| 442 */ | 569 */ |
| 443 Matcher alwaysThrew(value) => | 570 Matcher alwaysThrew(value) => |
| 444 new _ResultMatcher(THROW, wrapMatcher(value), ALL); | 571 new _ResultSetMatcher(_THROW, wrapMatcher(value), _ALL); |
| 445 | 572 |
| 446 /** | 573 /** |
| 447 *[sometimeThrew] asserts that at least one matching call to a method threw | 574 *[sometimeThrew] asserts that at least one matching call to a method threw |
| 448 * a value that matched [value]. | 575 * a value that matched [value]. |
| 449 */ | 576 */ |
| 450 Matcher sometimeThrew(value) => | 577 Matcher sometimeThrew(value) => |
| 451 new _ResultMatcher(THROW, wrapMatcher(value), SOME); | 578 new _ResultSetMatcher(_THROW, wrapMatcher(value), _SOME); |
| 452 | 579 |
| 453 /** | 580 /** |
| 454 *[neverThrew] asserts that no matching call to a method threw | 581 *[neverThrew] asserts that no matching call to a method threw |
| 455 * a value that matched [value]. | 582 * a value that matched [value]. |
| 456 */ | 583 */ |
| 457 Matcher neverThrew(value) => | 584 Matcher neverThrew(value) => |
| 458 new _ResultMatcher(THROW, wrapMatcher(value), NONE); | 585 new _ResultSetMatcher(_THROW, wrapMatcher(value), _NONE); |
| 586 |
| 587 /** The shared log used for named mocks. */ |
| 588 LogEntryList sharedLog = null; |
| 459 | 589 |
| 460 /** | 590 /** |
| 461 * [Mock] is the base class for all mocked objects, with | 591 * [Mock] is the base class for all mocked objects, with |
| 462 * support for basic mocking. | 592 * support for basic mocking. |
| 463 * | 593 * |
| 464 * To create a mock objects for some class T, create a new class using: | 594 * To create a mock objects for some class T, create a new class using: |
| 465 * | 595 * |
| 466 * class MockT extends Mock implements T {}; | 596 * class MockT extends Mock implements T {}; |
| 467 * | 597 * |
| 468 * Then specify the behavior of the Mock for different methods using | 598 * Then specify the behavior of the Mock for different methods using |
| 469 * [when] (to select the method and parameters) and [thenReturn], | 599 * [when] (to select the method and parameters) and [thenReturn], |
| 470 * [alwaysReturn], [thenThrow], [alwaysThrow], [thenCall] or [alwaysCall]. | 600 * [alwaysReturn], [thenThrow], [alwaysThrow], [thenCall] or [alwaysCall]. |
| 471 * [thenReturn], [thenThrow] and [thenCall] are one-shot so you would | 601 * [thenReturn], [thenThrow] and [thenCall] are one-shot so you would |
| 472 * typically call these more than once to specify a sequence of actions; | 602 * typically call these more than once to specify a sequence of actions; |
| 473 * this can be done with chained calls, e.g.: | 603 * this can be done with chained calls, e.g.: |
| 474 * | 604 * |
| 475 * m.when(callsTo('foo')). | 605 * m.when(callsTo('foo')). |
| 476 * thenReturn(0).thenReturn(1).thenReturn(2); | 606 * thenReturn(0).thenReturn(1).thenReturn(2); |
| 477 * | 607 * |
| 478 * [thenCall] and [alwaysCall] allow you to proxy mocked methods, chaining | 608 * [thenCall] and [alwaysCall] allow you to proxy mocked methods, chaining |
| 479 * to some other implementation. This provides a way to implement 'spies'. | 609 * to some other implementation. This provides a way to implement 'spies'. |
| 480 * | 610 * |
| 481 * You can then use the mock object. Once you are done, to verify the | 611 * You can then use the mock object. Once you are done, to verify the |
| 482 * behavior, use [forThe] to extract a relevant subset of method call | 612 * behavior, use [getLogs] to extract a relevant subset of method call |
| 483 * logs and apply [Matchers] to these through calling [verify]. | 613 * logs and apply [Matchers] to these through calling [verify]. |
| 484 * | 614 * |
| 615 * A Mock can be given a name when constructed. In this case instead of |
| 616 * keeping its own log, it uses a shared log. This can be useful to get an |
| 617 * audit trail of interleaved behavior. It is the responsibility of the user |
| 618 * to ensure that mock names, if used, are unique. |
| 619 * |
| 485 * Limitations: | 620 * Limitations: |
| 486 * - only positional parameters are supported (up to 10); | 621 * - only positional parameters are supported (up to 10); |
| 487 * - to mock getters you will need to include parentheses in the call | 622 * - to mock getters you will need to include parentheses in the call |
| 488 * (e.g. m.length() will work but not m.length). | 623 * (e.g. m.length() will work but not m.length). |
| 489 * | 624 * |
| 490 * Here is a simple example: | 625 * Here is a simple example: |
| 491 * | 626 * |
| 492 * class MockList extends Mock implements List {}; | 627 * class MockList extends Mock implements List {}; |
| 493 * | 628 * |
| 494 * List m = new MockList(); | 629 * List m = new MockList(); |
| 495 * m.when(callsTo('add', anything)).alwaysReturn(0); | 630 * m.when(callsTo('add', anything)).alwaysReturn(0); |
| 496 * | 631 * |
| 497 * m.add('foo'); | 632 * m.add('foo'); |
| 498 * m.add('bar'); | 633 * m.add('bar'); |
| 499 * | 634 * |
| 500 * getLogs(m, callsTo('add', anything)).verify(calledExactly(2)); | 635 * getLogs(m, callsTo('add', anything)).verify(happenedExactly(2)); |
| 501 * getLogs(m, callsTo('add', 'foo')).verify(calledOnce); | 636 * getLogs(m, callsTo('add', 'foo')).verify(happenedOnce); |
| 502 * getLogs(m, callsTo('add', 'isNull)).verify(neverCalled); | 637 * getLogs(m, callsTo('add', 'isNull)).verify(neverHappened); |
| 503 * | 638 * |
| 504 * Note that we don't need to provide argument matchers for all arguments, | 639 * Note that we don't need to provide argument matchers for all arguments, |
| 505 * but we do need to provide arguments for all matchers. So this is allowed: | 640 * but we do need to provide arguments for all matchers. So this is allowed: |
| 506 * | 641 * |
| 507 * m.when(callsTo('add')).alwaysReturn(0); | 642 * m.when(callsTo('add')).alwaysReturn(0); |
| 508 * m.add(1, 2); | 643 * m.add(1, 2); |
| 509 * | 644 * |
| 510 * But this is not allowed and will throw an exception: | 645 * But this is not allowed and will throw an exception: |
| 511 * | 646 * |
| 512 * m.when(callsTo('add', anything, anything)).alwaysReturn(0); | 647 * m.when(callsTo('add', anything, anything)).alwaysReturn(0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 523 * class MockFoo extends Mock implements Foo { | 658 * class MockFoo extends Mock implements Foo { |
| 524 * Foo real; | 659 * Foo real; |
| 525 * MockFoo() { | 660 * MockFoo() { |
| 526 * real = new Foo(); | 661 * real = new Foo(); |
| 527 * this.when(callsTo('bar')).alwaysCall(real.bar); | 662 * this.when(callsTo('bar')).alwaysCall(real.bar); |
| 528 * } | 663 * } |
| 529 * } | 664 * } |
| 530 * | 665 * |
| 531 */ | 666 */ |
| 532 class Mock { | 667 class Mock { |
| 668 String name; |
| 533 Map<String,Behavior> behaviors; /** The set of [behavior]s supported. */ | 669 Map<String,Behavior> behaviors; /** The set of [behavior]s supported. */ |
| 534 LogEntryList log; /** The [log] of calls made. */ | 670 LogEntryList log; /** The [log] of calls made. Only used if [name] is null. */ |
| 671 bool throwIfNoBehavior; /** If false, swallow unknown method calls. */ |
| 535 | 672 |
| 536 Mock() { | 673 Mock([this.name = null, this.throwIfNoBehavior = false, this.log = null]) { |
| 674 if (log == null) { |
| 675 log = new LogEntryList(); |
| 676 } |
| 537 behaviors = new Map<String,Behavior>(); | 677 behaviors = new Map<String,Behavior>(); |
| 538 log = new LogEntryList(new List<LogEntry>()); | |
| 539 } | 678 } |
| 540 | 679 |
| 541 /** | 680 /** |
| 542 * [when] is used to create a new or extend an existing [Behavior]. | 681 * [when] is used to create a new or extend an existing [Behavior]. |
| 543 * A [CallMatcher] [filter] must be supplied, and the [Behavior]s for | 682 * A [CallMatcher] [filter] must be supplied, and the [Behavior]s for |
| 544 * that signature are returned (being created first if needed). | 683 * that signature are returned (being created first if needed). |
| 545 * | 684 * |
| 546 * Typical use case: | 685 * Typical use case: |
| 547 * | 686 * |
| 548 * mock.when(callsTo(...)).alwaysReturn(...); | 687 * mock.when(callsTo(...)).alwaysReturn(...); |
| 549 */ | 688 */ |
| 550 Behavior when(CallMatcher logFilter) { | 689 Behavior when(CallMatcher logFilter) { |
| 551 String key = logFilter.toString(); | 690 String key = logFilter.toString(); |
| 552 if (!behaviors.containsKey(key)) { | 691 if (!behaviors.containsKey(key)) { |
| 553 Behavior b = new Behavior(logFilter); | 692 Behavior b = new Behavior(logFilter); |
| 554 behaviors[key] = b; | 693 behaviors[key] = b; |
| 555 return b; | 694 return b; |
| 556 } else { | 695 } else { |
| 557 return behaviors[key]; | 696 return behaviors[key]; |
| 558 } | 697 } |
| 559 } | 698 } |
| 560 | 699 |
| 561 /** | 700 /** |
| 562 * This is the handler for method calls. We loo through the list | 701 * This is the handler for method calls. We loo through the list |
| 563 * of [Behavior]s, and find the first match that still has return | 702 * of [Behavior]s, and find the first match that still has return |
| 564 * values available, and then do the action specified by that | 703 * values available, and then do the action specified by that |
| 565 * return value. If we find no [Behavior] to apply an exception is | 704 * return value. If we find no [Behavior] to apply an exception is |
| 566 * thrown. | 705 * thrown. |
| 567 */ | 706 */ |
| 568 noSuchMethod(String name, List args) { | 707 noSuchMethod(String method, List args) { |
| 708 if (method.startsWith('get:')) { |
| 709 method = 'get ${method.substring(4)}'; |
| 710 } |
| 711 bool matchedMethodName = false; |
| 569 for (String k in behaviors.getKeys()) { | 712 for (String k in behaviors.getKeys()) { |
| 570 Behavior b = behaviors[k]; | 713 Behavior b = behaviors[k]; |
| 571 if (b.matches(name, args)) { | 714 if (b.matcher.name == method) { |
| 715 matchedMethodName = true; |
| 716 } |
| 717 if (b.matches(method, args)) { |
| 572 List actions = b.actions; | 718 List actions = b.actions; |
| 573 if (actions == null || actions.length == 0) { | 719 if (actions == null || actions.length == 0) { |
| 574 continue; // No return values left in this Behavior. | 720 continue; // No return values left in this Behavior. |
| 575 } | 721 } |
| 576 // Get the first response. | 722 // Get the first response. |
| 577 Responder response = actions[0]; | 723 Responder response = actions[0]; |
| 578 // If it is exhausted, remove it from the list. | 724 // If it is exhausted, remove it from the list. |
| 579 // Note that for endlessly repeating values, we started the count at | 725 // Note that for endlessly repeating values, we started the count at |
| 580 // 0, so we get a potentially useful value here, which is the | 726 // 0, so we get a potentially useful value here, which is the |
| 581 // (negation of) the number of times we returned the value. | 727 // (negation of) the number of times we returned the value. |
| 582 if (--response.count == 0) { | 728 if (--response.count == 0) { |
| 583 actions.removeRange(0, 1); | 729 actions.removeRange(0, 1); |
| 584 if (actions.length == 0) { | |
| 585 // Remove the behavior. Note that in the future there | |
| 586 // may be some value in preserving the behaviors for | |
| 587 // auditing purposes (e.g. how many times was this behavior used?). | |
| 588 // If we do decide to keep them and perf is an issue instead of | |
| 589 // deleting we could move this to a separate list. | |
| 590 behaviors.remove(k); | |
| 591 } | |
| 592 } | 730 } |
| 593 // Do the response. | 731 // Do the response. |
| 594 var action = response.action; | 732 var action = response.action; |
| 595 var value = response.value; | 733 var value = response.value; |
| 596 switch (action) { | 734 switch (action) { |
| 597 case RETURN: | 735 case _RETURN: |
| 598 log.add(new LogEntry(name, args, action, value)); | 736 log.add(new LogEntry(name, method, args, action, value)); |
| 599 return value; | 737 return value; |
| 600 case THROW: | 738 case _THROW: |
| 601 log.add(new LogEntry(name, args, action, value)); | 739 log.add(new LogEntry(name, method, args, action, value)); |
| 602 throw value; | 740 throw value; |
| 603 case PROXY: | 741 case _PROXY: |
| 604 var rtn; | 742 var rtn; |
| 605 switch (args.length) { | 743 switch (args.length) { |
| 606 case 0: | 744 case 0: |
| 607 rtn = value(); | 745 rtn = value(); |
| 608 break; | 746 break; |
| 609 case 1: | 747 case 1: |
| 610 rtn = value(args[0]); | 748 rtn = value(args[0]); |
| 611 break; | 749 break; |
| 612 case 2: | 750 case 2: |
| 613 rtn = value(args[0], args[1]); | 751 rtn = value(args[0], args[1]); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 638 args[4], args[5], args[6], args[7], args[8]); | 776 args[4], args[5], args[6], args[7], args[8]); |
| 639 break; | 777 break; |
| 640 case 9: | 778 case 9: |
| 641 rtn = value(args[0], args[1], args[2], args[3], | 779 rtn = value(args[0], args[1], args[2], args[3], |
| 642 args[4], args[5], args[6], args[7], args[8], args[9]); | 780 args[4], args[5], args[6], args[7], args[8], args[9]); |
| 643 break; | 781 break; |
| 644 default: | 782 default: |
| 645 throw new Exception( | 783 throw new Exception( |
| 646 "Cannot proxy calls with more than 10 parameters"); | 784 "Cannot proxy calls with more than 10 parameters"); |
| 647 } | 785 } |
| 648 log.add(new LogEntry(name, args, action, rtn)); | 786 log.add(new LogEntry(name, method, args, action, rtn)); |
| 649 return rtn; | 787 return rtn; |
| 650 } | 788 } |
| 651 } | 789 } |
| 652 } | 790 } |
| 653 throw new Exception('No behavior specified for method $name'); | 791 if (matchedMethodName) { |
| 792 // User did specify behavior for this method, but all the |
| 793 // actions are exhausted. This is considered an error. |
| 794 throw new Exception('No more actions for method ' |
| 795 '${_qualifiedName(name, method)}'); |
| 796 } else if (throwIfNoBehavior) { |
| 797 throw new Exception('No behavior specified for method ' |
| 798 '${_qualifiedName(name, method)}'); |
| 799 } |
| 800 // User hasn't specified behavior for this method; we don't throw |
| 801 // so we can underspecify. |
| 802 log.add(new LogEntry(name, method, args, _IGNORE)); |
| 654 } | 803 } |
| 655 | 804 |
| 656 /** [verifyZeroInteractions] returns true if no calls were made */ | 805 /** [verifyZeroInteractions] returns true if no calls were made */ |
| 657 bool verifyZeroInteractions() => log.logs.length == 0; | 806 bool verifyZeroInteractions() => log.logs.length == 0; |
| 658 } | |
| 659 | 807 |
| 660 /** | 808 /** |
| 661 * [getLogs] extracts all calls from the call log of [mock] that match the | 809 * [getLogs] extracts all calls from the call log that match the |
| 662 * [logFilter] [CallMatcher], and returns the matching list of | 810 * [logFilter] [CallMatcher], and returns the matching list of |
| 663 * [LogEntry]s. If [destructive] is false (the default) the matching | 811 * [LogEntry]s. If [destructive] is false (the default) the matching |
| 664 * calls are left in the mock object's log, else they are removed. | 812 * calls are left in the log, else they are removed. Removal allows |
| 665 * Removal allows us to verify a set of interactions and then verify | 813 * us to verify a set of interactions and then verify that there are |
| 666 * that there are no other interactions left. | 814 * no other interactions left. [actionMatcher] can be used to further |
| 667 * | 815 * restrict the returned logs based on the action the mock performed. |
| 668 * Typical usage: | 816 * |
| 669 * | 817 * Typical usage: |
| 670 * getLogs(mock, callsTo(...)).verify(...); | 818 * |
| 671 */ | 819 * getLogs(callsTo(...)).verify(...); |
| 672 LogEntryList getLogs(Mock mock, CallMatcher logFilter, | 820 */ |
| 673 [bool destructive = false]) { | 821 LogEntryList getLogs(CallMatcher logFilter, [Matcher actionMatcher = null, |
| 674 return mock.log.getMatches(logFilter, destructive); | 822 bool destructive = false]) { |
| 823 return log.getMatches(name, logFilter, actionMatcher, destructive); |
| 824 } |
| 675 } | 825 } |
| 676 | 826 |
| 677 | 827 |
| 828 |
| 829 |
| OLD | NEW |