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 * This library contains an Expect class with static methods that can be used | 6 * This library contains an Expect class with static methods that can be used |
7 * for simple unit-tests. | 7 * for simple unit-tests. |
8 */ | 8 */ |
9 library expect; | 9 library expect; |
10 | 10 |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 } | 429 } |
430 | 430 |
431 /** | 431 /** |
432 * Calls the function [f] and verifies that it throws an exception. | 432 * Calls the function [f] and verifies that it throws an exception. |
433 * The optional [check] function can provide additional validation | 433 * The optional [check] function can provide additional validation |
434 * that the correct exception is being thrown. For example, to check | 434 * that the correct exception is being thrown. For example, to check |
435 * the type of the exception you could write this: | 435 * the type of the exception you could write this: |
436 * | 436 * |
437 * Expect.throws(myThrowingFunction, (e) => e is MyException); | 437 * Expect.throws(myThrowingFunction, (e) => e is MyException); |
438 */ | 438 */ |
439 static void throws(void f(), | 439 static void throws(void f(), [_CheckExceptionFn check, String reason]) { |
440 [_CheckExceptionFn check = null, String reason = null]) { | |
441 String msg = reason == null ? "" : "($reason)"; | 440 String msg = reason == null ? "" : "($reason)"; |
442 if (f is! _Nullary) { | 441 if (f is! _Nullary) { |
443 // Only throws from executing the function body should count as throwing. | 442 // Only throws from executing the function body should count as throwing. |
444 // The failure to even call `f` should throw outside the try/catch. | 443 // The failure to even call `f` should throw outside the try/catch. |
445 _fail("Expect.throws$msg: Function f not callable with zero arguments"); | 444 _fail("Expect.throws$msg: Function f not callable with zero arguments"); |
446 } | 445 } |
447 try { | 446 try { |
448 f(); | 447 f(); |
449 } catch (e, s) { | 448 } catch (e, s) { |
450 if (check != null) { | 449 if (check != null) { |
451 if (!check(e)) { | 450 if (!check(e)) { |
452 _fail("Expect.throws$msg: Unexpected '$e'\n$s"); | 451 _fail("Expect.throws$msg: Unexpected '$e'\n$s"); |
453 } | 452 } |
454 } | 453 } |
455 return; | 454 return; |
456 } | 455 } |
457 _fail('Expect.throws$msg fails: Did not throw'); | 456 _fail('Expect.throws$msg fails: Did not throw'); |
458 } | 457 } |
459 | 458 |
| 459 static void throwsArgumentError(void f()) { |
| 460 Expect.throws(f, (error) => error is ArgumentError, "ArgumentError"); |
| 461 } |
| 462 |
| 463 static void throwsCastError(void f()) { |
| 464 Expect.throws(f, (error) => error is CastError, "CastError"); |
| 465 } |
| 466 |
| 467 static void throwsNoSuchMethodError(void f()) { |
| 468 Expect.throws( |
| 469 f, (error) => error is NoSuchMethodError, "NoSuchMethodError"); |
| 470 } |
| 471 |
| 472 static void throwsRangeError(void f()) { |
| 473 Expect.throws(f, (error) => error is RangeError, "RangeError"); |
| 474 } |
| 475 |
| 476 static void throwsStateError(void f()) { |
| 477 Expect.throws(f, (error) => error is StateError, "StateError"); |
| 478 } |
| 479 |
| 480 static void throwsTypeError(void f()) { |
| 481 Expect.throws(f, (error) => error is TypeError, "TypeError"); |
| 482 } |
| 483 |
| 484 static void throwsUnsupportedError(void f()) { |
| 485 Expect.throws(f, (error) => error is UnsupportedError, "UnsupportedError"); |
| 486 } |
| 487 |
460 static String _getMessage(String reason) => | 488 static String _getMessage(String reason) => |
461 (reason == null) ? "" : ", '$reason'"; | 489 (reason == null) ? "" : ", '$reason'"; |
462 | 490 |
463 static void _fail(String message) { | 491 static void _fail(String message) { |
464 throw new ExpectException(message); | 492 throw new ExpectException(message); |
465 } | 493 } |
466 } | 494 } |
467 | 495 |
468 bool _identical(a, b) => identical(a, b); | 496 bool _identical(a, b) => identical(a, b); |
469 | 497 |
(...skipping 28 matching lines...) Expand all Loading... |
498 | 526 |
499 /// Annotation class for testing of dart2js. Use this as metadata on method | 527 /// Annotation class for testing of dart2js. Use this as metadata on method |
500 /// declarations to disable closed world assumptions on parameters, effectively | 528 /// declarations to disable closed world assumptions on parameters, effectively |
501 /// assuming that the runtime arguments could be any value. Note that the | 529 /// assuming that the runtime arguments could be any value. Note that the |
502 /// constraints due to [TrustTypeAnnotations] still apply. | 530 /// constraints due to [TrustTypeAnnotations] still apply. |
503 class AssumeDynamic { | 531 class AssumeDynamic { |
504 const AssumeDynamic(); | 532 const AssumeDynamic(); |
505 } | 533 } |
506 | 534 |
507 /// Is true iff type assertions are enabled. | 535 /// Is true iff type assertions are enabled. |
| 536 // TODO(rnystrom): Remove this once all tests are no longer using it. |
508 final bool typeAssertionsEnabled = (() { | 537 final bool typeAssertionsEnabled = (() { |
509 try { | 538 try { |
510 dynamic i = 42; | 539 dynamic i = 42; |
511 String s = i; | 540 String s = i; |
512 } on TypeError catch (e) { | 541 } on TypeError catch (e) { |
513 return true; | 542 return true; |
514 } | 543 } |
515 return false; | 544 return false; |
516 })(); | 545 })(); |
517 | 546 |
518 /// Is true iff `assert` statements are enabled. | 547 /// Is true iff `assert` statements are enabled. |
519 final bool assertStatementsEnabled = (() { | 548 final bool assertStatementsEnabled = (() { |
520 try { | 549 try { |
521 assert(false); | 550 assert(false); |
522 } on AssertionError catch (e) { | 551 } on AssertionError catch (e) { |
523 return true; | 552 return true; |
524 } | 553 } |
525 return false; | 554 return false; |
526 })(); | 555 })(); |
527 | 556 |
528 /// Is true iff checked mode is enabled. | 557 /// Is true iff checked mode is enabled. |
| 558 // TODO(rnystrom): Remove this once all tests are no longer using it. |
529 final bool checkedModeEnabled = | 559 final bool checkedModeEnabled = |
530 typeAssertionsEnabled && assertStatementsEnabled; | 560 typeAssertionsEnabled && assertStatementsEnabled; |
OLD | NEW |