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 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 | 364 |
365 /** | 365 /** |
366 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. | 366 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. |
367 */ | 367 */ |
368 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 368 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
369 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { | 369 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { |
370 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; | 370 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
371 } | 371 } |
372 | 372 |
373 /** | 373 /** |
374 * Wraps the [callback] in a new function and returns that function. The new | 374 * *Deprecated* |
375 * function will be able to handle exceptions by directing them to the correct | 375 * |
376 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that | 376 * All tests are now run an isolated [Zone]. |
377 * might optionally be called but may never be called during the test. | 377 * |
378 * [callback] should take 0 positional arguments (named arguments are not | 378 * You can safely remove calls to this method. |
379 * supported). [id] can be used to identify the callback in error | |
380 * messages (for example if it is called after the test case is complete). | |
381 */ | 379 */ |
382 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 380 @deprecated |
383 Function protectAsync0(Function callback, {String id}) { | 381 Function protectAsync0(Function callback, {String id}) { |
384 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke0; | 382 return callback; |
385 } | 383 } |
386 | 384 |
387 /** | 385 /** |
388 * Like [protectAsync0] but [callback] should take 1 positional argument. | 386 * *Deprecated* |
387 * | |
388 * All tests are now run an isolated [Zone]. | |
389 * | |
390 * You can safely remove calls to this method. | |
389 */ | 391 */ |
390 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 392 @deprecated |
391 Function protectAsync1(Function callback, {String id}) { | 393 Function protectAsync1(Function callback, {String id}) { |
392 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke1; | 394 return callback; |
393 } | 395 } |
394 | 396 |
395 /** | 397 /** |
396 * Like [protectAsync0] but [callback] should take 2 positional arguments. | 398 * *Deprecated* |
399 * | |
400 * All tests are now run an isolated [Zone]. | |
401 * | |
402 * You can safely remove calls to this method. | |
397 */ | 403 */ |
398 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 404 @deprecated |
399 Function protectAsync2(Function callback, {String id}) { | 405 Function protectAsync2(Function callback, {String id}) { |
400 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke2; | 406 return callback; |
Siggi Cherem (dart-lang)
2014/02/04 02:20:05
not sure if this works. runZoned only intercepts 0
kevmoo
2014/02/04 02:26:18
This is basically a no-op. The assumption is that
nweiz
2014/02/04 02:34:26
I don't think this is accurate. All asynchronous c
Siggi Cherem (dart-lang)
2014/02/04 02:42:01
To clarify what my worry was. Zones used to only h
kevmoo
2014/02/04 02:45:28
I'm not sure how Zones handle callbacks with N arg
| |
401 } | 407 } |
402 | 408 |
403 /** | 409 /** |
404 * Creates a new named group of tests. Calls to group() or test() within the | 410 * Creates a new named group of tests. Calls to group() or test() within the |
405 * body of the function passed to this will inherit this group's description. | 411 * body of the function passed to this will inherit this group's description. |
406 */ | 412 */ |
407 void group(String description, void body()) { | 413 void group(String description, void body()) { |
408 ensureInitialized(); | 414 ensureInitialized(); |
409 _requireNotRunning(); | 415 _requireNotRunning(); |
410 _currentContext = new _GroupContext(_currentContext, description); | 416 _currentContext = new _GroupContext(_currentContext, description); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
510 /** Runs all queued tests, one at a time. */ | 516 /** Runs all queued tests, one at a time. */ |
511 void runTests() { | 517 void runTests() { |
512 _requireNotRunning(); | 518 _requireNotRunning(); |
513 _ensureInitialized(false); | 519 _ensureInitialized(false); |
514 _currentTestCaseIndex = 0; | 520 _currentTestCaseIndex = 0; |
515 _config.onStart(); | 521 _config.onStart(); |
516 _runTest(); | 522 _runTest(); |
517 } | 523 } |
518 | 524 |
519 /** | 525 /** |
520 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is | 526 * *Deprecated* |
521 * passed to the corresponding test. | |
522 * | 527 * |
523 * The value returned by [tryBody] (if any) is returned by [guardAsync]. | 528 * All tests are now run an isolated [Zone]. |
529 * | |
530 * You can safely remove calls to this method. | |
524 */ | 531 */ |
532 @deprecated | |
525 guardAsync(Function tryBody) { | 533 guardAsync(Function tryBody) { |
526 return _guardAsync(tryBody, null, currentTestCase); | 534 return tryBody(); |
527 } | |
528 | |
529 _guardAsync(Function tryBody, Function finallyBody, TestCase testCase) { | |
530 assert(testCase != null); | |
531 try { | |
532 return tryBody(); | |
533 } catch (e, trace) { | |
534 _registerException(testCase, e, trace); | |
535 } finally { | |
536 if (finallyBody != null) finallyBody(); | |
537 } | |
538 } | 535 } |
539 | 536 |
540 /** | 537 /** |
541 * Registers that an exception was caught for the current test. | 538 * Registers that an exception was caught for the current test. |
542 */ | 539 */ |
543 void registerException(e, [trace]) { | 540 void registerException(e, [trace]) { |
544 _registerException(currentTestCase, e, trace); | 541 _registerException(currentTestCase, e, trace); |
545 } | 542 } |
546 | 543 |
547 /** | 544 /** |
(...skipping 10 matching lines...) Expand all Loading... | |
558 | 555 |
559 /** | 556 /** |
560 * Runs the next test. | 557 * Runs the next test. |
561 */ | 558 */ |
562 void _runTest() { | 559 void _runTest() { |
563 if (_currentTestCaseIndex >= testCases.length) { | 560 if (_currentTestCaseIndex >= testCases.length) { |
564 assert(_currentTestCaseIndex == testCases.length); | 561 assert(_currentTestCaseIndex == testCases.length); |
565 _completeTests(); | 562 _completeTests(); |
566 } else { | 563 } else { |
567 var testCase = testCases[_currentTestCaseIndex]; | 564 var testCase = testCases[_currentTestCaseIndex]; |
568 Future f = _guardAsync(testCase._run, null, testCase); | 565 Future f = runZoned(testCase._run, onError: (error, stack) { |
566 _registerException(testCase, error, stack); | |
567 }); | |
568 | |
569 var timeout = unittestConfiguration.timeout; | 569 var timeout = unittestConfiguration.timeout; |
570 | 570 |
571 Timer timer; | 571 Timer timer; |
572 if (timeout != null) { | 572 if (timeout != null) { |
573 try { | 573 try { |
574 timer = new Timer(timeout, () { | 574 timer = new Timer(timeout, () { |
575 testCase._error("Test timed out after ${timeout.inSeconds} seconds."); | 575 testCase._error("Test timed out after ${timeout.inSeconds} seconds."); |
576 _nextTestCase(); | 576 _nextTestCase(); |
577 }); | 577 }); |
578 } on UnsupportedError catch (e) { | 578 } on UnsupportedError catch (e) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
711 | 711 |
712 if (!filterStacks) return trace; | 712 if (!filterStacks) return trace; |
713 | 713 |
714 // Format the stack trace by removing everything above TestCase._runTest, | 714 // Format the stack trace by removing everything above TestCase._runTest, |
715 // which is usually going to be irrelevant. Also fold together unittest and | 715 // which is usually going to be irrelevant. Also fold together unittest and |
716 // core library calls so only the function the user called is visible. | 716 // core library calls so only the function the user called is visible. |
717 return new Trace(trace.frames.takeWhile((frame) { | 717 return new Trace(trace.frames.takeWhile((frame) { |
718 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 718 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
719 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 719 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
720 } | 720 } |
OLD | NEW |