OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 * A library for writing dart unit tests. | 6 * A library for writing dart unit tests. |
7 * | 7 * |
8 * To import this library, specify the relative path to | 8 * To import this library, specify the relative path to |
9 * pkg/unittest/unittest.dart. | 9 * pkg/unittest/unittest.dart. |
10 * | 10 * |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 /** | 210 /** |
211 * (Deprecated) Evaluates the [function] and validates that it throws an | 211 * (Deprecated) Evaluates the [function] and validates that it throws an |
212 * exception. If [callback] is provided, then it will be invoked with the | 212 * exception. If [callback] is provided, then it will be invoked with the |
213 * thrown exception. The callback may do any validation it wants. In addition, | 213 * thrown exception. The callback may do any validation it wants. In addition, |
214 * if it returns `false`, that also indicates an expectation failure. | 214 * if it returns `false`, that also indicates an expectation failure. |
215 */ | 215 */ |
216 void expectThrow(function, [bool callback(exception)]) { | 216 void expectThrow(function, [bool callback(exception)]) { |
217 bool threw = false; | 217 bool threw = false; |
218 try { | 218 try { |
219 function(); | 219 function(); |
220 } catch (var e) { | 220 } catch (e) { |
221 threw = true; | 221 threw = true; |
222 | 222 |
223 // Also let the callback look at it. | 223 // Also let the callback look at it. |
224 if (callback != null) { | 224 if (callback != null) { |
225 var result = callback(e); | 225 var result = callback(e); |
226 | 226 |
227 // If the callback explicitly returned false, treat that like an | 227 // If the callback explicitly returned false, treat that like an |
228 // expectation too. (If it returns null, though, don't.) | 228 // expectation too. (If it returns null, though, don't.) |
229 if (result == false) { | 229 if (result == false) { |
230 _fail('Exception:\n$e\ndid not match expectation.'); | 230 _fail('Exception:\n$e\ndid not match expectation.'); |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 | 575 |
576 // Groups can be nested, so we need to preserve the current | 576 // Groups can be nested, so we need to preserve the current |
577 // settings for test setup/teardown. | 577 // settings for test setup/teardown. |
578 Function parentSetup = _testSetup; | 578 Function parentSetup = _testSetup; |
579 Function parentTeardown = _testTeardown; | 579 Function parentTeardown = _testTeardown; |
580 | 580 |
581 try { | 581 try { |
582 _testSetup = null; | 582 _testSetup = null; |
583 _testTeardown = null; | 583 _testTeardown = null; |
584 body(); | 584 body(); |
585 } catch(var e, var trace) { | 585 } catch (e, trace) { |
586 _reportTestError(e.toString(), trace == null ? '' : trace.toString()); | 586 _reportTestError(e.toString(), trace == null ? '' : trace.toString()); |
587 } finally { | 587 } finally { |
588 // Now that the group is over, restore the previous one. | 588 // Now that the group is over, restore the previous one. |
589 _currentGroup = parentGroup; | 589 _currentGroup = parentGroup; |
590 _testSetup = parentSetup; | 590 _testSetup = parentSetup; |
591 _testTeardown = parentTeardown; | 591 _testTeardown = parentTeardown; |
592 } | 592 } |
593 } | 593 } |
594 | 594 |
595 /** | 595 /** |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 } | 716 } |
717 | 717 |
718 /** | 718 /** |
719 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update | 719 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update |
720 * the [_currentTest] status accordingly. | 720 * the [_currentTest] status accordingly. |
721 */ | 721 */ |
722 guardAsync(tryBody, [finallyBody, testNum = -1]) { | 722 guardAsync(tryBody, [finallyBody, testNum = -1]) { |
723 if (testNum < 0) testNum = _currentTest; | 723 if (testNum < 0) testNum = _currentTest; |
724 try { | 724 try { |
725 return tryBody(); | 725 return tryBody(); |
726 } catch (var e, var trace) { | 726 } catch (e, trace) { |
727 _registerException(testNum, e, trace); | 727 _registerException(testNum, e, trace); |
728 } finally { | 728 } finally { |
729 if (finallyBody != null) finallyBody(); | 729 if (finallyBody != null) finallyBody(); |
730 } | 730 } |
731 } | 731 } |
732 | 732 |
733 /** | 733 /** |
734 * Registers that an exception was caught for the current test. | 734 * Registers that an exception was caught for the current test. |
735 */ | 735 */ |
736 registerException(e, [trace]) { | 736 registerException(e, [trace]) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 } | 852 } |
853 | 853 |
854 /** Enable a test by ID. */ | 854 /** Enable a test by ID. */ |
855 void enableTest(int testId) => _setTestEnabledState(testId, true); | 855 void enableTest(int testId) => _setTestEnabledState(testId, true); |
856 | 856 |
857 /** Disable a test by ID. */ | 857 /** Disable a test by ID. */ |
858 void disableTest(int testId) => _setTestEnabledState(testId, false); | 858 void disableTest(int testId) => _setTestEnabledState(testId, false); |
859 | 859 |
860 /** Signature for a test function. */ | 860 /** Signature for a test function. */ |
861 typedef void TestFunction(); | 861 typedef void TestFunction(); |
OLD | NEW |