| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 * Support for writing Dart unit tests. | 6 * Support for writing Dart unit tests. |
| 7 * | 7 * |
| 8 * For information on installing and importing this library, see the | 8 * For information on installing and importing this library, see the |
| 9 * [unittest package on pub.dartlang.org] | 9 * [unittest package on pub.dartlang.org] |
| 10 * (http://pub.dartlang.org/packages/unittest). | 10 * (http://pub.dartlang.org/packages/unittest). |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 359 |
| 360 /** | 360 /** |
| 361 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. | 361 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. |
| 362 */ | 362 */ |
| 363 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 363 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
| 364 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { | 364 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { |
| 365 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; | 365 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
| 366 } | 366 } |
| 367 | 367 |
| 368 /** | 368 /** |
| 369 * Wraps the [callback] in a new function and returns that function. The new | 369 * *Deprecated* |
| 370 * function will be able to handle exceptions by directing them to the correct | 370 * |
| 371 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that | 371 * All tests are now run an isolated [Zone]. |
| 372 * might optionally be called but may never be called during the test. | 372 * |
| 373 * [callback] should take 0 positional arguments (named arguments are not | 373 * You can safely remove calls to this method. |
| 374 * supported). [id] can be used to identify the callback in error | |
| 375 * messages (for example if it is called after the test case is complete). | |
| 376 */ | 374 */ |
| 377 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 375 @deprecated |
| 378 Function protectAsync0(Function callback, {String id}) { | 376 Function protectAsync0(Function callback, {String id}) { |
| 379 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke0; | 377 return callback; |
| 380 } | 378 } |
| 381 | 379 |
| 382 /** | 380 /** |
| 383 * Like [protectAsync0] but [callback] should take 1 positional argument. | 381 * *Deprecated* |
| 382 * |
| 383 * All tests are now run an isolated [Zone]. |
| 384 * |
| 385 * You can safely remove calls to this method. |
| 384 */ | 386 */ |
| 385 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 387 @deprecated |
| 386 Function protectAsync1(Function callback, {String id}) { | 388 Function protectAsync1(Function callback, {String id}) { |
| 387 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke1; | 389 return callback; |
| 388 } | 390 } |
| 389 | 391 |
| 390 /** | 392 /** |
| 391 * Like [protectAsync0] but [callback] should take 2 positional arguments. | 393 * *Deprecated* |
| 394 * |
| 395 * All tests are now run an isolated [Zone]. |
| 396 * |
| 397 * You can safely remove calls to this method. |
| 392 */ | 398 */ |
| 393 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 399 @deprecated |
| 394 Function protectAsync2(Function callback, {String id}) { | 400 Function protectAsync2(Function callback, {String id}) { |
| 395 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke2; | 401 return callback; |
| 396 } | 402 } |
| 397 | 403 |
| 398 /** | 404 /** |
| 399 * Creates a new named group of tests. Calls to group() or test() within the | 405 * Creates a new named group of tests. Calls to group() or test() within the |
| 400 * body of the function passed to this will inherit this group's description. | 406 * body of the function passed to this will inherit this group's description. |
| 401 */ | 407 */ |
| 402 void group(String description, void body()) { | 408 void group(String description, void body()) { |
| 403 ensureInitialized(); | 409 ensureInitialized(); |
| 404 _requireNotRunning(); | 410 _requireNotRunning(); |
| 405 _currentContext = new _GroupContext(_currentContext, description); | 411 _currentContext = new _GroupContext(_currentContext, description); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 /** Runs all queued tests, one at a time. */ | 505 /** Runs all queued tests, one at a time. */ |
| 500 void runTests() { | 506 void runTests() { |
| 501 _requireNotRunning(); | 507 _requireNotRunning(); |
| 502 _ensureInitialized(false); | 508 _ensureInitialized(false); |
| 503 _currentTestCaseIndex = 0; | 509 _currentTestCaseIndex = 0; |
| 504 _config.onStart(); | 510 _config.onStart(); |
| 505 _runTest(); | 511 _runTest(); |
| 506 } | 512 } |
| 507 | 513 |
| 508 /** | 514 /** |
| 509 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is | 515 * *Deprecated* |
| 510 * passed to the corresponding test. | |
| 511 * | 516 * |
| 512 * The value returned by [tryBody] (if any) is returned by [guardAsync]. | 517 * All tests are now run an isolated [Zone]. |
| 518 * |
| 519 * You can safely remove calls to this method. |
| 513 */ | 520 */ |
| 521 @deprecated |
| 514 guardAsync(Function tryBody) { | 522 guardAsync(Function tryBody) { |
| 515 return _guardAsync(tryBody, null, currentTestCase); | 523 return tryBody(); |
| 516 } | |
| 517 | |
| 518 _guardAsync(Function tryBody, Function finallyBody, TestCase testCase) { | |
| 519 assert(testCase != null); | |
| 520 try { | |
| 521 return tryBody(); | |
| 522 } catch (e, trace) { | |
| 523 _registerException(testCase, e, trace); | |
| 524 } finally { | |
| 525 if (finallyBody != null) finallyBody(); | |
| 526 } | |
| 527 } | 524 } |
| 528 | 525 |
| 529 /** | 526 /** |
| 530 * Registers that an exception was caught for the current test. | 527 * Registers that an exception was caught for the current test. |
| 531 */ | 528 */ |
| 532 void registerException(e, [trace]) { | 529 void registerException(e, [trace]) { |
| 533 _registerException(currentTestCase, e, trace); | 530 _registerException(currentTestCase, e, trace); |
| 534 } | 531 } |
| 535 | 532 |
| 536 /** | 533 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 547 | 544 |
| 548 /** | 545 /** |
| 549 * Runs the next test. | 546 * Runs the next test. |
| 550 */ | 547 */ |
| 551 void _runTest() { | 548 void _runTest() { |
| 552 if (_currentTestCaseIndex >= testCases.length) { | 549 if (_currentTestCaseIndex >= testCases.length) { |
| 553 assert(_currentTestCaseIndex == testCases.length); | 550 assert(_currentTestCaseIndex == testCases.length); |
| 554 _completeTests(); | 551 _completeTests(); |
| 555 } else { | 552 } else { |
| 556 var testCase = testCases[_currentTestCaseIndex]; | 553 var testCase = testCases[_currentTestCaseIndex]; |
| 557 Future f = _guardAsync(testCase._run, null, testCase); | 554 Future f = runZoned(testCase._run, onError: (error, stack) { |
| 555 // TODO(kevmoo) Do a better job of flagging these are async errors. |
| 556 // https://code.google.com/p/dart/issues/detail?id=16530 |
| 557 _registerException(testCase, error, stack); |
| 558 }); |
| 559 |
| 558 var timeout = unittestConfiguration.timeout; | 560 var timeout = unittestConfiguration.timeout; |
| 559 | 561 |
| 560 Timer timer; | 562 Timer timer; |
| 561 if (timeout != null) { | 563 if (timeout != null) { |
| 562 try { | 564 try { |
| 563 timer = new Timer(timeout, () { | 565 timer = new Timer(timeout, () { |
| 564 testCase._error("Test timed out after ${timeout.inSeconds} seconds."); | 566 testCase._error("Test timed out after ${timeout.inSeconds} seconds."); |
| 565 _nextTestCase(); | 567 _nextTestCase(); |
| 566 }); | 568 }); |
| 567 } on UnsupportedError catch (e) { | 569 } on UnsupportedError catch (e) { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 | 702 |
| 701 if (!filterStacks) return trace; | 703 if (!filterStacks) return trace; |
| 702 | 704 |
| 703 // Format the stack trace by removing everything above TestCase._runTest, | 705 // Format the stack trace by removing everything above TestCase._runTest, |
| 704 // which is usually going to be irrelevant. Also fold together unittest and | 706 // which is usually going to be irrelevant. Also fold together unittest and |
| 705 // core library calls so only the function the user called is visible. | 707 // core library calls so only the function the user called is visible. |
| 706 return new Trace(trace.frames.takeWhile((frame) { | 708 return new Trace(trace.frames.takeWhile((frame) { |
| 707 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 709 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
| 708 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 710 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
| 709 } | 711 } |
| OLD | NEW |