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

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

Issue 14293015: pkg/unittest: use better async callback function (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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) 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 * A library for writing dart unit tests. 6 * A library for writing dart unit tests.
7 * 7 *
8 * ## Installing ## 8 * ## Installing ##
9 * 9 *
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` 10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml`
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 * [setUp] and [tearDown] should be called within the [group] before any 681 * [setUp] and [tearDown] should be called within the [group] before any
682 * calls to [test]. The [teardownTest] function can be asynchronous; in this 682 * calls to [test]. The [teardownTest] function can be asynchronous; in this
683 * case it must return a [Future]. 683 * case it must return a [Future].
684 */ 684 */
685 void tearDown(Function teardownTest) { 685 void tearDown(Function teardownTest) {
686 _currentContext.testTeardown = teardownTest; 686 _currentContext.testTeardown = teardownTest;
687 } 687 }
688 688
689 /** Advance to the next test case. */ 689 /** Advance to the next test case. */
690 void _nextTestCase() { 690 void _nextTestCase() {
691 _defer(() { 691 runAsync(() {
692 _currentTestCaseIndex++; 692 _currentTestCaseIndex++;
693 _nextBatch(); 693 _nextBatch();
694 }); 694 });
695 } 695 }
696 696
697 /** 697 /**
698 * Utility function that can be used to notify the test framework that an 698 * Utility function that can be used to notify the test framework that an
699 * error was caught outside of this library. 699 * error was caught outside of this library.
700 */ 700 */
701 void _reportTestError(String msg, String trace) { 701 void _reportTestError(String msg, String trace) {
702 if (_currentTestCaseIndex < testCases.length) { 702 if (_currentTestCaseIndex < testCases.length) {
703 final testCase = testCases[_currentTestCaseIndex]; 703 final testCase = testCases[_currentTestCaseIndex];
704 testCase.error(msg, trace); 704 testCase.error(msg, trace);
705 } else { 705 } else {
706 _uncaughtErrorMessage = "$msg: $trace"; 706 _uncaughtErrorMessage = "$msg: $trace";
707 } 707 }
708 } 708 }
709 709
710 /**
711 * Runs [callback] at the end of the event loop. Note that we don't wrap
712 * the callback in guardAsync; this is for test framework functions which
713 * should not be throwing unexpected exceptions that end up failing test
714 * cases! Furthermore, we need the final exception to be thrown but not
715 * caught by the test framework if any test cases failed. However, tests
716 * that make use of a similar defer function *should* wrap the callback
717 * (as we do in unitttest_test.dart).
718 */
719 _defer(void callback()) {
720 (new Future.value()).then((_) => callback());
721 }
722
723 void rerunTests() { 710 void rerunTests() {
724 _uncaughtErrorMessage = null; 711 _uncaughtErrorMessage = null;
725 _initialized = true; // We don't want to reset the test array. 712 _initialized = true; // We don't want to reset the test array.
726 runTests(); 713 runTests();
727 } 714 }
728 715
729 /** 716 /**
730 * Filter the tests. [testFilter] can be a [RegExp], a [String] or a 717 * Filter the tests. [testFilter] can be a [RegExp], a [String] or a
731 * predicate function. This is different to enabling/disabling tests 718 * predicate function. This is different to enabling/disabling tests
732 * in that it removes the tests completely. 719 * in that it removes the tests completely.
(...skipping 16 matching lines...) Expand all
749 _ensureInitialized(false); 736 _ensureInitialized(false);
750 _currentTestCaseIndex = 0; 737 _currentTestCaseIndex = 0;
751 738
752 // If we are soloing a test, remove all the others. 739 // If we are soloing a test, remove all the others.
753 if (_soloTest != null) { 740 if (_soloTest != null) {
754 filterTests((t) => t == _soloTest); 741 filterTests((t) => t == _soloTest);
755 } 742 }
756 743
757 _config.onStart(); 744 _config.onStart();
758 745
759 _defer(() { 746 runAsync(() {
760 _nextBatch(); 747 _nextBatch();
761 }); 748 });
762 } 749 }
763 750
764 /** 751 /**
765 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is 752 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, it is
766 * passed to the corresponding test. 753 * passed to the corresponding test.
767 * 754 *
768 * The value returned by [tryBody] (if any) is returned by [guardAsync]. 755 * The value returned by [tryBody] (if any) is returned by [guardAsync].
769 */ 756 */
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 _uncaughtErrorMessage = null; 856 _uncaughtErrorMessage = null;
870 857
871 if (_config == null) { 858 if (_config == null) {
872 unittestConfiguration = new Configuration(); 859 unittestConfiguration = new Configuration();
873 } 860 }
874 _config.onInit(); 861 _config.onInit();
875 862
876 if (configAutoStart && _config.autoStart) { 863 if (configAutoStart && _config.autoStart) {
877 // Immediately queue the suite up. It will run after a timeout (i.e. after 864 // Immediately queue the suite up. It will run after a timeout (i.e. after
878 // main() has returned). 865 // main() has returned).
879 _defer(runTests); 866 runAsync(runTests);
880 } 867 }
881 } 868 }
882 869
883 /** Select a solo test by ID. */ 870 /** Select a solo test by ID. */
884 void setSoloTest(int id) { 871 void setSoloTest(int id) {
885 for (var i = 0; i < testCases.length; i++) { 872 for (var i = 0; i < testCases.length; i++) {
886 if (testCases[i].id == id) { 873 if (testCases[i].id == id) {
887 _soloTest = testCases[i]; 874 _soloTest = testCases[i];
888 break; 875 break;
889 } 876 }
(...skipping 16 matching lines...) Expand all
906 } 893 }
907 894
908 /** Enable a test by ID. */ 895 /** Enable a test by ID. */
909 void enableTest(int testId) => _setTestEnabledState(testId, true); 896 void enableTest(int testId) => _setTestEnabledState(testId, true);
910 897
911 /** Disable a test by ID. */ 898 /** Disable a test by ID. */
912 void disableTest(int testId) => _setTestEnabledState(testId, false); 899 void disableTest(int testId) => _setTestEnabledState(testId, false);
913 900
914 /** Signature for a test function. */ 901 /** Signature for a test function. */
915 typedef dynamic TestFunction(); 902 typedef dynamic TestFunction();
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