| 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 /** | 6 /** |
| 7 * Returns a matcher that matches empty strings, maps or collections. | 7 * Returns a matcher that matches empty strings, maps or collections. |
| 8 */ | 8 */ |
| 9 final Matcher isEmpty = const _Empty(); | 9 const Matcher isEmpty = const _Empty(); |
| 10 | 10 |
| 11 class _Empty extends BaseMatcher { | 11 class _Empty extends BaseMatcher { |
| 12 const _Empty(); | 12 const _Empty(); |
| 13 bool matches(item, MatchState matchState) { | 13 bool matches(item, MatchState matchState) { |
| 14 if (item is Map || item is Collection) { | 14 if (item is Map || item is Collection) { |
| 15 return item.isEmpty(); | 15 return item.isEmpty(); |
| 16 } else if (item is String) { | 16 } else if (item is String) { |
| 17 return item.length == 0; | 17 return item.length == 0; |
| 18 } else { | 18 } else { |
| 19 return false; | 19 return false; |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 Description describe(Description description) => | 22 Description describe(Description description) => |
| 23 description.add('empty'); | 23 description.add('empty'); |
| 24 } | 24 } |
| 25 | 25 |
| 26 /** A matcher that matches any null value. */ | 26 /** A matcher that matches any null value. */ |
| 27 final Matcher isNull = const _IsNull(); | 27 const Matcher isNull = const _IsNull(); |
| 28 | 28 |
| 29 /** A matcher that matches any non-null value. */ | 29 /** A matcher that matches any non-null value. */ |
| 30 final Matcher isNotNull = const _IsNotNull(); | 30 const Matcher isNotNull = const _IsNotNull(); |
| 31 | 31 |
| 32 class _IsNull extends BaseMatcher { | 32 class _IsNull extends BaseMatcher { |
| 33 const _IsNull(); | 33 const _IsNull(); |
| 34 bool matches(item, MatchState matchState) => item == null; | 34 bool matches(item, MatchState matchState) => item == null; |
| 35 Description describe(Description description) => | 35 Description describe(Description description) => |
| 36 description.add('null'); | 36 description.add('null'); |
| 37 } | 37 } |
| 38 | 38 |
| 39 class _IsNotNull extends BaseMatcher { | 39 class _IsNotNull extends BaseMatcher { |
| 40 const _IsNotNull(); | 40 const _IsNotNull(); |
| 41 bool matches(item, MatchState matchState) => item != null; | 41 bool matches(item, MatchState matchState) => item != null; |
| 42 Description describe(Description description) => | 42 Description describe(Description description) => |
| 43 description.add('not null'); | 43 description.add('not null'); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** A matcher that matches the Boolean value true. */ | 46 /** A matcher that matches the Boolean value true. */ |
| 47 final Matcher isTrue = const _IsTrue(); | 47 const Matcher isTrue = const _IsTrue(); |
| 48 | 48 |
| 49 /** A matcher that matches anything except the Boolean value true. */ | 49 /** A matcher that matches anything except the Boolean value true. */ |
| 50 final Matcher isFalse = const _IsFalse(); | 50 const Matcher isFalse = const _IsFalse(); |
| 51 | 51 |
| 52 class _IsTrue extends BaseMatcher { | 52 class _IsTrue extends BaseMatcher { |
| 53 const _IsTrue(); | 53 const _IsTrue(); |
| 54 bool matches(item, MatchState matchState) => item == true; | 54 bool matches(item, MatchState matchState) => item == true; |
| 55 Description describe(Description description) => | 55 Description describe(Description description) => |
| 56 description.add('true'); | 56 description.add('true'); |
| 57 } | 57 } |
| 58 | 58 |
| 59 class _IsFalse extends BaseMatcher { | 59 class _IsFalse extends BaseMatcher { |
| 60 const _IsFalse(); | 60 const _IsFalse(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 183 |
| 184 Description describe(Description description) => | 184 Description describe(Description description) => |
| 185 description.addDescriptionOf(_expected); | 185 description.addDescriptionOf(_expected); |
| 186 | 186 |
| 187 Description describeMismatch(item, Description mismatchDescription, | 187 Description describeMismatch(item, Description mismatchDescription, |
| 188 MatchState matchState, bool verbose) => | 188 MatchState matchState, bool verbose) => |
| 189 mismatchDescription.add(_match(_expected, item)); | 189 mismatchDescription.add(_match(_expected, item)); |
| 190 } | 190 } |
| 191 | 191 |
| 192 /** A matcher that matches any value. */ | 192 /** A matcher that matches any value. */ |
| 193 final Matcher anything = const _IsAnything(); | 193 const Matcher anything = const _IsAnything(); |
| 194 | 194 |
| 195 class _IsAnything extends BaseMatcher { | 195 class _IsAnything extends BaseMatcher { |
| 196 const _IsAnything(); | 196 const _IsAnything(); |
| 197 bool matches(item, MatchState matchState) => true; | 197 bool matches(item, MatchState matchState) => true; |
| 198 Description describe(Description description) => | 198 Description describe(Description description) => |
| 199 description.add('anything'); | 199 description.add('anything'); |
| 200 } | 200 } |
| 201 | 201 |
| 202 /** | 202 /** |
| 203 * Returns a matcher that matches if an object is an instance | 203 * Returns a matcher that matches if an object is an instance |
| (...skipping 27 matching lines...) Expand all Loading... |
| 231 * * A [Function] that throws an exception when called. The function cannot | 231 * * A [Function] that throws an exception when called. The function cannot |
| 232 * take any arguments. If you want to test that a function expecting | 232 * take any arguments. If you want to test that a function expecting |
| 233 * arguments throws, wrap it in another zero-argument function that calls | 233 * arguments throws, wrap it in another zero-argument function that calls |
| 234 * the one you want to test. | 234 * the one you want to test. |
| 235 * | 235 * |
| 236 * * A [Future] that completes with an exception. Note that this creates an | 236 * * A [Future] that completes with an exception. Note that this creates an |
| 237 * asynchronous expectation. The call to `expect()` that includes this will | 237 * asynchronous expectation. The call to `expect()` that includes this will |
| 238 * return immediately and execution will continue. Later, when the future | 238 * return immediately and execution will continue. Later, when the future |
| 239 * completes, the actual expectation will run. | 239 * completes, the actual expectation will run. |
| 240 */ | 240 */ |
| 241 final Matcher throws = const _Throws(); | 241 const Matcher throws = const _Throws(); |
| 242 | 242 |
| 243 /** | 243 /** |
| 244 * This can be used to match two kinds of objects: | 244 * This can be used to match two kinds of objects: |
| 245 * | 245 * |
| 246 * * A [Function] that throws an exception when called. The function cannot | 246 * * A [Function] that throws an exception when called. The function cannot |
| 247 * take any arguments. If you want to test that a function expecting | 247 * take any arguments. If you want to test that a function expecting |
| 248 * arguments throws, wrap it in another zero-argument function that calls | 248 * arguments throws, wrap it in another zero-argument function that calls |
| 249 * the one you want to test. | 249 * the one you want to test. |
| 250 * | 250 * |
| 251 * * A [Future] that completes with an exception. Note that this creates an | 251 * * A [Future] that completes with an exception. Note that this creates an |
| 252 * asynchronous expectation. The call to `expect()` that includes this will | 252 * asynchronous expectation. The call to `expect()` that includes this will |
| 253 * return immediately and execution will continue. Later, when the future | 253 * return immediately and execution will continue. Later, when the future |
| 254 * completes, the actual expectation will run. | 254 * completes, the actual expectation will run. |
| 255 * | 255 * |
| 256 * In both cases, when an exception is thrown, this will test that the exception | 256 * In both cases, when an exception is thrown, this will test that the exception |
| 257 * object matches [matcher]. If [matcher] is not an instance of [Matcher], it | 257 * object matches [matcher]. If [matcher] is not an instance of [Matcher], it |
| 258 * will implicitly be treated as `equals(matcher)`. | 258 * will implicitly be treated as `equals(matcher)`. |
| 259 */ | 259 */ |
| 260 Matcher throwsA(matcher) => new _Throws(wrapMatcher(matcher)); | 260 Matcher throwsA(matcher) => new _Throws(wrapMatcher(matcher)); |
| 261 | 261 |
| 262 /** | 262 /** |
| 263 * A matcher that matches a function call against no exception. | 263 * A matcher that matches a function call against no exception. |
| 264 * The function will be called once. Any exceptions will be silently swallowed. | 264 * The function will be called once. Any exceptions will be silently swallowed. |
| 265 * The value passed to expect() should be a reference to the function. | 265 * The value passed to expect() should be a reference to the function. |
| 266 * Note that the function cannot take arguments; to handle this | 266 * Note that the function cannot take arguments; to handle this |
| 267 * a wrapper will have to be created. | 267 * a wrapper will have to be created. |
| 268 */ | 268 */ |
| 269 final Matcher returnsNormally = const _ReturnsNormally(); | 269 const Matcher returnsNormally = const _ReturnsNormally(); |
| 270 | 270 |
| 271 class _Throws extends BaseMatcher { | 271 class _Throws extends BaseMatcher { |
| 272 final Matcher _matcher; | 272 final Matcher _matcher; |
| 273 | 273 |
| 274 const _Throws([Matcher matcher]) : | 274 const _Throws([Matcher matcher]) : |
| 275 this._matcher = matcher; | 275 this._matcher = matcher; |
| 276 | 276 |
| 277 bool matches(item, MatchState matchState) { | 277 bool matches(item, MatchState matchState) { |
| 278 if (item is Future) { | 278 if (item is Future) { |
| 279 // Queue up an asynchronous expectation that validates when the future | 279 // Queue up an asynchronous expectation that validates when the future |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 */ | 394 */ |
| 395 | 395 |
| 396 /* abstract */ class _ExceptionMatcher extends BaseMatcher { | 396 /* abstract */ class _ExceptionMatcher extends BaseMatcher { |
| 397 final String _name; | 397 final String _name; |
| 398 const _ExceptionMatcher(this._name); | 398 const _ExceptionMatcher(this._name); |
| 399 Description describe(Description description) => | 399 Description describe(Description description) => |
| 400 description.add(_name); | 400 description.add(_name); |
| 401 } | 401 } |
| 402 | 402 |
| 403 /** A matcher for FormatExceptions. */ | 403 /** A matcher for FormatExceptions. */ |
| 404 final isFormatException = const _FormatException(); | 404 const isFormatException = const _FormatException(); |
| 405 | 405 |
| 406 /** A matcher for functions that throw FormatException */ | 406 /** A matcher for functions that throw FormatException */ |
| 407 final Matcher throwsFormatException = | 407 const Matcher throwsFormatException = |
| 408 const _Throws(isFormatException); | 408 const _Throws(isFormatException); |
| 409 | 409 |
| 410 class _FormatException extends _ExceptionMatcher { | 410 class _FormatException extends _ExceptionMatcher { |
| 411 const _FormatException() : super("FormatException"); | 411 const _FormatException() : super("FormatException"); |
| 412 bool matches(item, MatchState matchState) => item is FormatException; | 412 bool matches(item, MatchState matchState) => item is FormatException; |
| 413 } | 413 } |
| 414 | 414 |
| 415 /** A matcher for Exceptions. */ | 415 /** A matcher for Exceptions. */ |
| 416 final isException = const _Exception(); | 416 const isException = const _Exception(); |
| 417 | 417 |
| 418 /** A matcher for functions that throw Exception */ | 418 /** A matcher for functions that throw Exception */ |
| 419 final Matcher throwsException = const _Throws(isException); | 419 const Matcher throwsException = const _Throws(isException); |
| 420 | 420 |
| 421 class _Exception extends _ExceptionMatcher { | 421 class _Exception extends _ExceptionMatcher { |
| 422 const _Exception() : super("Exception"); | 422 const _Exception() : super("Exception"); |
| 423 bool matches(item, MatchState matchState) => item is Exception; | 423 bool matches(item, MatchState matchState) => item is Exception; |
| 424 } | 424 } |
| 425 | 425 |
| 426 /** A matcher for IllegalArgumentExceptions. */ | 426 /** A matcher for IllegalArgumentExceptions. */ |
| 427 final isIllegalArgumentException = const _IllegalArgumentException(); | 427 const isIllegalArgumentException = const _IllegalArgumentException(); |
| 428 | 428 |
| 429 /** A matcher for functions that throw IllegalArgumentException */ | 429 /** A matcher for functions that throw IllegalArgumentException */ |
| 430 final Matcher throwsIllegalArgumentException = | 430 const Matcher throwsIllegalArgumentException = |
| 431 const _Throws(isIllegalArgumentException); | 431 const _Throws(isIllegalArgumentException); |
| 432 | 432 |
| 433 class _IllegalArgumentException extends _ExceptionMatcher { | 433 class _IllegalArgumentException extends _ExceptionMatcher { |
| 434 const _IllegalArgumentException() : super("IllegalArgumentException"); | 434 const _IllegalArgumentException() : super("IllegalArgumentException"); |
| 435 bool matches(item, MatchState matchState) => item is IllegalArgumentException; | 435 bool matches(item, MatchState matchState) => item is IllegalArgumentException; |
| 436 } | 436 } |
| 437 | 437 |
| 438 /** A matcher for IllegalJSRegExpExceptions. */ | 438 /** A matcher for IllegalJSRegExpExceptions. */ |
| 439 final isIllegalJSRegExpException = const _IllegalJSRegExpException(); | 439 const isIllegalJSRegExpException = const _IllegalJSRegExpException(); |
| 440 | 440 |
| 441 /** A matcher for functions that throw IllegalJSRegExpException */ | 441 /** A matcher for functions that throw IllegalJSRegExpException */ |
| 442 final Matcher throwsIllegalJSRegExpException = | 442 const Matcher throwsIllegalJSRegExpException = |
| 443 const _Throws(isIllegalJSRegExpException); | 443 const _Throws(isIllegalJSRegExpException); |
| 444 | 444 |
| 445 class _IllegalJSRegExpException extends _ExceptionMatcher { | 445 class _IllegalJSRegExpException extends _ExceptionMatcher { |
| 446 const _IllegalJSRegExpException() : super("IllegalJSRegExpException"); | 446 const _IllegalJSRegExpException() : super("IllegalJSRegExpException"); |
| 447 bool matches(item, MatchState matchState) => item is IllegalJSRegExpException; | 447 bool matches(item, MatchState matchState) => item is IllegalJSRegExpException; |
| 448 } | 448 } |
| 449 | 449 |
| 450 /** A matcher for IndexOutOfRangeExceptions. */ | 450 /** A matcher for IndexOutOfRangeExceptions. */ |
| 451 final isIndexOutOfRangeException = const _IndexOutOfRangeException(); | 451 const isIndexOutOfRangeException = const _IndexOutOfRangeException(); |
| 452 | 452 |
| 453 /** A matcher for functions that throw IndexOutOfRangeException */ | 453 /** A matcher for functions that throw IndexOutOfRangeException */ |
| 454 final Matcher throwsIndexOutOfRangeException = | 454 const Matcher throwsIndexOutOfRangeException = |
| 455 const _Throws(isIndexOutOfRangeException); | 455 const _Throws(isIndexOutOfRangeException); |
| 456 | 456 |
| 457 class _IndexOutOfRangeException extends _ExceptionMatcher { | 457 class _IndexOutOfRangeException extends _ExceptionMatcher { |
| 458 const _IndexOutOfRangeException() : super("IndexOutOfRangeException"); | 458 const _IndexOutOfRangeException() : super("IndexOutOfRangeException"); |
| 459 bool matches(item, MatchState matchState) => item is IndexOutOfRangeException; | 459 bool matches(item, MatchState matchState) => item is IndexOutOfRangeException; |
| 460 } | 460 } |
| 461 | 461 |
| 462 /** A matcher for NoSuchMethodExceptions. */ | 462 /** A matcher for NoSuchMethodExceptions. */ |
| 463 final isNoSuchMethodException = const _NoSuchMethodException(); | 463 const isNoSuchMethodException = const _NoSuchMethodException(); |
| 464 | 464 |
| 465 /** A matcher for functions that throw NoSuchMethodException */ | 465 /** A matcher for functions that throw NoSuchMethodException */ |
| 466 final Matcher throwsNoSuchMethodException = | 466 const Matcher throwsNoSuchMethodException = |
| 467 const _Throws(isNoSuchMethodException); | 467 const _Throws(isNoSuchMethodException); |
| 468 | 468 |
| 469 class _NoSuchMethodException extends _ExceptionMatcher { | 469 class _NoSuchMethodException extends _ExceptionMatcher { |
| 470 const _NoSuchMethodException() : super("NoSuchMethodException"); | 470 const _NoSuchMethodException() : super("NoSuchMethodException"); |
| 471 bool matches(item, MatchState matchState) => item is NoSuchMethodException; | 471 bool matches(item, MatchState matchState) => item is NoSuchMethodException; |
| 472 } | 472 } |
| 473 | 473 |
| 474 /** A matcher for NotImplementedExceptions. */ | 474 /** A matcher for NotImplementedExceptions. */ |
| 475 final isNotImplementedException = const _NotImplementedException(); | 475 const isNotImplementedException = const _NotImplementedException(); |
| 476 | 476 |
| 477 /** A matcher for functions that throw Exception */ | 477 /** A matcher for functions that throw Exception */ |
| 478 final Matcher throwsNotImplementedException = | 478 const Matcher throwsNotImplementedException = |
| 479 const _Throws(isNotImplementedException); | 479 const _Throws(isNotImplementedException); |
| 480 | 480 |
| 481 class _NotImplementedException extends _ExceptionMatcher { | 481 class _NotImplementedException extends _ExceptionMatcher { |
| 482 const _NotImplementedException() : super("NotImplementedException"); | 482 const _NotImplementedException() : super("NotImplementedException"); |
| 483 bool matches(item, MatchState matchState) => item is NotImplementedException; | 483 bool matches(item, MatchState matchState) => item is NotImplementedException; |
| 484 } | 484 } |
| 485 | 485 |
| 486 /** A matcher for NullPointerExceptions. */ | 486 /** A matcher for NullPointerExceptions. */ |
| 487 final isNullPointerException = const _NullPointerException(); | 487 const isNullPointerException = const _NullPointerException(); |
| 488 | 488 |
| 489 /** A matcher for functions that throw NotNullPointerException */ | 489 /** A matcher for functions that throw NotNullPointerException */ |
| 490 final Matcher throwsNullPointerException = | 490 const Matcher throwsNullPointerException = |
| 491 const _Throws(isNullPointerException); | 491 const _Throws(isNullPointerException); |
| 492 | 492 |
| 493 class _NullPointerException extends _ExceptionMatcher { | 493 class _NullPointerException extends _ExceptionMatcher { |
| 494 const _NullPointerException() : super("NullPointerException"); | 494 const _NullPointerException() : super("NullPointerException"); |
| 495 bool matches(item, MatchState matchState) => item is NullPointerException; | 495 bool matches(item, MatchState matchState) => item is NullPointerException; |
| 496 } | 496 } |
| 497 | 497 |
| 498 /** A matcher for UnsupportedOperationExceptions. */ | 498 /** A matcher for UnsupportedOperationExceptions. */ |
| 499 final isUnsupportedOperationException = const _UnsupportedOperationException(); | 499 const isUnsupportedOperationException = const _UnsupportedOperationException(); |
| 500 | 500 |
| 501 /** A matcher for functions that throw UnsupportedOperationException */ | 501 /** A matcher for functions that throw UnsupportedOperationException */ |
| 502 final Matcher throwsUnsupportedOperationException = | 502 const Matcher throwsUnsupportedOperationException = |
| 503 const _Throws(isUnsupportedOperationException); | 503 const _Throws(isUnsupportedOperationException); |
| 504 | 504 |
| 505 class _UnsupportedOperationException extends _ExceptionMatcher { | 505 class _UnsupportedOperationException extends _ExceptionMatcher { |
| 506 const _UnsupportedOperationException() : | 506 const _UnsupportedOperationException() : |
| 507 super("UnsupportedOperationException"); | 507 super("UnsupportedOperationException"); |
| 508 bool matches(item, MatchState matchState) => item is UnsupportedOperationExcep
tion; | 508 bool matches(item, MatchState matchState) => item is UnsupportedOperationExcep
tion; |
| 509 } | 509 } |
| 510 | 510 |
| 511 /** | 511 /** |
| 512 * Returns a matcher that matches if an object has a length property | 512 * Returns a matcher that matches if an object has a length property |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 final _matcher; | 617 final _matcher; |
| 618 final String _description; | 618 final String _description; |
| 619 | 619 |
| 620 const _Predicate(this._matcher, this._description); | 620 const _Predicate(this._matcher, this._description); |
| 621 | 621 |
| 622 bool matches(item, MatchState matchState) => _matcher(item); | 622 bool matches(item, MatchState matchState) => _matcher(item); |
| 623 | 623 |
| 624 Description describe(Description description) => | 624 Description describe(Description description) => |
| 625 description.add(_description); | 625 description.add(_description); |
| 626 } | 626 } |
| OLD | NEW |