Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: pkg/unittest/core_matchers.dart

Issue 10987041: Make the matcher classes for exceptions public so that users can use them to create matchers for th… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 const Matcher isEmpty = const _Empty(); 9 const Matcher isEmpty = const _Empty();
10 10
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 const 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 const 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
280 // completes. 280 // completes.
281 item.onComplete(expectAsync1((future) { 281 item.onComplete(expectAsync1((future) {
282 if (future.hasValue) { 282 if (future.hasValue) {
283 expect(false, reason: 283 expect(false, reason:
284 "Expected future to fail, but succeeded with '${future.value}'."); 284 "Expected future to fail, but succeeded with '${future.value}'.");
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 * 386 *
387 * bool _isException(x) => x is Exception; 387 * bool _isException(x) => x is Exception;
388 * final Matcher isException = const _Predicate(_isException, "Exception"); 388 * final Matcher isException = const _Predicate(_isException, "Exception");
389 * final Matcher throwsException = const _Throws(isException); 389 * final Matcher throwsException = const _Throws(isException);
390 * 390 *
391 * But currently using static functions in const expressions is not supported. 391 * But currently using static functions in const expressions is not supported.
392 * For now the only solution for all platforms seems to be separate classes 392 * For now the only solution for all platforms seems to be separate classes
393 * for each exception type. 393 * for each exception type.
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 const 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 const 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 const 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 const 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 ArgumentErrors. */ 426 /** A matcher for ArgumentErrors. */
427 const isArgumentError = const _ArgumentError(); 427 const isArgumentError = const _ArgumentError();
428 428
429 /** A matcher for functions that throw ArgumentError */ 429 /** A matcher for functions that throw ArgumentError */
430 const Matcher throwsArgumentError = 430 const Matcher throwsArgumentError =
431 const _Throws(isArgumentError); 431 const Throws(isArgumentError);
432 432
433 class _ArgumentError extends _ExceptionMatcher { 433 class _ArgumentError extends ExceptionMatcher {
434 const _ArgumentError() : super("ArgumentError"); 434 const _ArgumentError() : super("ArgumentError");
435 bool matches(item, MatchState matchState) => item is ArgumentError; 435 bool matches(item, MatchState matchState) => item is ArgumentError;
436 } 436 }
437 437
438 /** A matcher for IllegalJSRegExpExceptions. */ 438 /** A matcher for IllegalJSRegExpExceptions. */
439 const 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 const 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 const 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 const 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 NoSuchMethodErrors. */ 462 /** A matcher for NoSuchMethodErrors. */
463 const isNoSuchMethodError = const _NoSuchMethodError(); 463 const isNoSuchMethodError = const _NoSuchMethodError();
464 464
465 /** A matcher for functions that throw NoSuchMethodError */ 465 /** A matcher for functions that throw NoSuchMethodError */
466 const Matcher throwsNoSuchMethodError = 466 const Matcher throwsNoSuchMethodError =
467 const _Throws(isNoSuchMethodError); 467 const Throws(isNoSuchMethodError);
468 468
469 class _NoSuchMethodError extends _ExceptionMatcher { 469 class _NoSuchMethodError extends ExceptionMatcher {
470 const _NoSuchMethodError() : super("NoSuchMethodError"); 470 const _NoSuchMethodError() : super("NoSuchMethodError");
471 bool matches(item, MatchState matchState) => item is NoSuchMethodError; 471 bool matches(item, MatchState matchState) => item is NoSuchMethodError;
472 } 472 }
473 473
474 /** A matcher for NotImplementedExceptions. */ 474 /** A matcher for NotImplementedExceptions. */
475 const 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 const 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 const 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 const 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 const 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 const 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) =>
509 item is UnsupportedOperationException;
509 } 510 }
510 511
511 /** 512 /**
512 * Returns a matcher that matches if an object has a length property 513 * Returns a matcher that matches if an object has a length property
513 * that matches [matcher]. 514 * that matches [matcher].
514 */ 515 */
515 Matcher hasLength(matcher) => 516 Matcher hasLength(matcher) =>
516 new _HasLength(wrapMatcher(matcher)); 517 new _HasLength(wrapMatcher(matcher));
517 518
518 class _HasLength extends BaseMatcher { 519 class _HasLength extends BaseMatcher {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 final _matcher; 618 final _matcher;
618 final String _description; 619 final String _description;
619 620
620 const _Predicate(this._matcher, this._description); 621 const _Predicate(this._matcher, this._description);
621 622
622 bool matches(item, MatchState matchState) => _matcher(item); 623 bool matches(item, MatchState matchState) => _matcher(item);
623 624
624 Description describe(Description description) => 625 Description describe(Description description) =>
625 description.add(_description); 626 description.add(_description);
626 } 627 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698