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

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

Issue 12213079: setUp/tearDown functions can now be asynchronous. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 10 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 | pkg/unittest/lib/unittest.dart » ('j') | pkg/unittest/lib/unittest.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of unittest; 5 part of unittest;
6 6
7 /** 7 /**
8 * testcase.dart: this file is sourced by unittest.dart. It defines [TestCase] 8 * testcase.dart: this file is sourced by unittest.dart. It defines [TestCase]
9 * and assumes unittest defines the type [TestFunction]. 9 * and assumes unittest defines the type [TestFunction].
10 */ 10 */
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 final String currentGroup; 53 final String currentGroup;
54 54
55 DateTime startTime; 55 DateTime startTime;
56 56
57 Duration runningTime; 57 Duration runningTime;
58 58
59 bool enabled = true; 59 bool enabled = true;
60 60
61 bool _doneTeardown = false; 61 bool _doneTeardown = false;
62 62
63 Completer _testComplete;
64
63 TestCase(this.id, this.description, this.test, 65 TestCase(this.id, this.description, this.test,
64 this.callbackFunctionsOutstanding) 66 this.callbackFunctionsOutstanding)
65 : currentGroup = _currentGroup, 67 : currentGroup = _currentGroup,
66 _setUp = _testSetup, 68 _setUp = _testSetup,
67 _tearDown = _testTeardown; 69 _tearDown = _testTeardown;
68 70
69 bool get isComplete => !enabled || result != null; 71 bool get isComplete => !enabled || result != null;
70 72
71 void run() { 73 void _prepTest() {
72 if (enabled) { 74 _config.onTestStart(this);
73 result = stackTrace = null; 75 startTime = new DateTime.now();
74 message = ''; 76 runningTime = null;
75 _doneTeardown = false; 77 }
76 if (_setUp != null) { 78
77 _setUp(); 79 Future _runTest() {
80 _prepTest();
81 test();
82 if (result == null && callbackFunctionsOutstanding == 0) {
83 pass();
84 }
85 return null;
Siggi Cherem (dart-lang) 2013/02/11 23:14:18 if this always returns null, shouldn't this functi
gram 2013/02/11 23:50:24 Yes, my bad, it had some other code in before that
86 }
87
88 /**
89 * Perform any associated [setUp] function and run the test. Returns
90 * a [Future] that can be used to schedule the next test. If the test runs
91 * to completion synchronously, or is disabled, we return null, to
92 * tell unittest to schedule the next test immediately.
93 */
94 Future run() {
95 if (!enabled) {
96 return null;
97 }
Siggi Cherem (dart-lang) 2013/02/11 23:14:18 fyi - john and I have grown to like the style wher
gram 2013/02/11 23:50:24 Done.
98
99 result = stackTrace = null;
100 message = '';
101 _doneTeardown = false;
102 if (_setUp != null) {
103 var rtn = _setUp();
Siggi Cherem (dart-lang) 2013/02/11 23:14:18 nit: consider merging a few of these branches, for
104 if (rtn is Future) {
105 rtn.then(expectAsync1((_) {
Siggi Cherem (dart-lang) 2013/02/11 23:14:18 nit: consider using => rtn.then(expectAsync1((_)
gram 2013/02/11 23:50:24 Done.
106 _runTest();
107 }, id: '[Async setUp completion handler]'))
108 .catchError((e) {
109 _prepTest();
110 // Calling error() will result in the tearDown being done.
111 // One could debate whether tearDown should be done after
112 // a failed setUp. There is no right answer, but doing it
113 // seems to be the more conservative approach, because
114 // unittest will not stop at a test failure.
115 error("$description: Test setup failed: ${e.error}");
116 });
117 } else {
118 _runTest();
78 } 119 }
79 _config.onTestStart(this); 120 } else {
80 startTime = new DateTime.now(); 121 _runTest();
81 runningTime = null; 122 }
82 test(); 123 if (result == null) { // Not complete.
124 _testComplete = new Completer();
125 return _testComplete.future;
126 }
127 return null;
128 }
129
130 void _nextTest() {
Siggi Cherem (dart-lang) 2013/02/11 23:14:18 nit: rename, '_markComplete'?
gram 2013/02/11 23:50:24 I went for _notifyComplete
131 if (_testComplete != null) {
132 _testComplete.complete(this);
133 _testComplete = null;
83 } 134 }
84 } 135 }
85 136
86 void _complete() { 137 void _complete() {
87 if (runningTime == null) { 138 if (runningTime == null) {
88 // TODO(gram): currently the duration measurement code is blocked 139 // TODO(gram): currently the duration measurement code is blocked
89 // by issue 4437. When that is fixed replace the line below with: 140 // by issue 4437. When that is fixed replace the line below with:
90 // runningTime = new DateTime.now().difference(startTime); 141 // runningTime = new DateTime.now().difference(startTime);
91 runningTime = new Duration(milliseconds: 0); 142 runningTime = new Duration(milliseconds: 0);
92 } 143 }
93 if (!_doneTeardown) { 144 if (!_doneTeardown) {
145 _doneTeardown = true;
94 if (_tearDown != null) { 146 if (_tearDown != null) {
95 _tearDown(); 147 var rtn = _tearDown();
148 if (rtn is Future) {
149 rtn.then(expectAsync1((_) {
150 if (result == null) {
151 // The test passed. In some cases we will already
152 // have set this result (e.g. if the test was async
153 // and all callbacks completed). If not, we do it here.
154 pass();
155 } else {
156 // The test has already been marked as pass/fail.
157 // Just report the updated result.
158 _config.onTestResult(this);
159 }
160 _nextTest();
161 }, id: '[Async tearDown completion handler]'))
162 .catchError((e) {
163 // We don't call fail() as that will potentially result in
164 // spurious messages like 'test failed more than once'.
165 result = ERROR;
166 message = "$description: Test teardown failed: ${e.error}";
167 _config.onTestResult(this);
168 _nextTest();
169 });
170 return;
171 }
96 } 172 }
97 _doneTeardown = true;
98 } 173 }
99 _config.onTestResult(this); 174 _config.onTestResult(this);
175 _nextTest();
100 } 176 }
101 177
102 void pass() { 178 void pass() {
103 result = PASS; 179 result = PASS;
104 _complete(); 180 _complete();
105 } 181 }
106 182
107 void fail(String messageText, [String stack = '']) { 183 void fail(String messageText, [String stack = '']) {
108 if (result != null) { 184 if (result != null) {
109 if (result == PASS) { 185 if (result == PASS) {
110 error('Test failed after initially passing: $messageText', stack); 186 error('Test failed after initially passing: $messageText', stack);
111 } else if (result == FAIL) { 187 } else if (result == FAIL) {
112 error('Test failed more than once: $messageText', stack); 188 error('Test failed more than once: $messageText', stack);
113 } 189 }
114 } else { 190 } else {
115 result = FAIL; 191 result = FAIL;
116 message = messageText; 192 message = messageText;
117 stackTrace = stack; 193 stackTrace = stack;
118 _complete(); 194 _complete();
119 } 195 }
120 } 196 }
121 197
122 void error(String messageText, [String stack = '']) { 198 void error(String messageText, [String stack = '']) {
123 result = ERROR; 199 result = ERROR;
124 message = messageText; 200 message = messageText;
125 stackTrace = stack; 201 stackTrace = stack;
126 _complete(); 202 _complete();
127 } 203 }
128 } 204 }
OLDNEW
« no previous file with comments | « no previous file | pkg/unittest/lib/unittest.dart » ('j') | pkg/unittest/lib/unittest.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698