Chromium Code Reviews| 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 // TODO(rnystrom): This code is gradually moving from a Java/JUnit style to | 5 // TODO(rnystrom): This code is gradually moving from a Java/JUnit style to |
| 6 // something closer to JS/Jasmine. Eventually, the UnitTestSuite class can go | 6 // something closer to JS/Jasmine. Eventually, the UnitTestSuite class can go |
| 7 // away completely (or become private to this library) and the only exposed API | 7 // away completely (or become private to this library) and the only exposed API |
| 8 // will be group()/test()/expect(). Until then, both ways are supported, which | 8 // will be group()/test()/expect(). Until then, both ways are supported, which |
| 9 // is why things look a bit weird in here. | 9 // is why things look a bit weird in here. |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 /** | 34 /** |
| 35 * Whether an undetected error occurred while running the last test. This | 35 * Whether an undetected error occurred while running the last test. This |
| 36 * errors are commonly caused by DOM callbacks that were not guarded in a | 36 * errors are commonly caused by DOM callbacks that were not guarded in a |
| 37 * try-catch block. | 37 * try-catch block. |
| 38 */ | 38 */ |
| 39 bool _uncaughtError; | 39 bool _uncaughtError; |
| 40 EventListener _onErrorClosure; | 40 EventListener _onErrorClosure; |
| 41 | 41 |
| 42 bool _queuedToRun = false; | 42 bool _queuedToRun = false; |
| 43 | 43 |
| 44 /** | |
| 45 * Whether test is currently being executed by [:runTest:]. | |
|
Siggi Cherem (dart-lang)
2011/10/19 16:57:41
(3 minor nits):
- "Whether test..." => "Whether a
| |
| 46 */ | |
| 47 bool _testIsRunning = false; | |
| 48 | |
| 44 // TODO(sigmund): remove isLayoutTest argument after converting all DOM tests | 49 // TODO(sigmund): remove isLayoutTest argument after converting all DOM tests |
| 45 // to use the named constructor below. | 50 // to use the named constructor below. |
| 46 // TODO(vsm): remove the ignoredWindow parameter once all tests are fixed. | 51 // TODO(vsm): remove the ignoredWindow parameter once all tests are fixed. |
| 47 UnitTestSuite([var ignoredWindow = null, bool isLayoutTest = false]) | 52 UnitTestSuite([var ignoredWindow = null, bool isLayoutTest = false]) |
| 48 : _isLayoutTest = isLayoutTest, | 53 : _isLayoutTest = isLayoutTest, |
| 49 _tests = new List<TestCase>(), | 54 _tests = new List<TestCase>(), |
| 50 _currentTest = 0, | 55 _currentTest = 0, |
| 51 _callbacksCalled = 0 { | 56 _callbacksCalled = 0 { |
| 52 _onErrorClosure = (e) { _onError(e); }; | 57 _onErrorClosure = (e) { _onError(e); }; |
| 53 if (_currentSuite != null) { | 58 if (_currentSuite != null) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 "Can't call callbackDone() on a synchronous test", ''); | 157 "Can't call callbackDone() on a synchronous test", ''); |
| 153 _uncaughtError = true; | 158 _uncaughtError = true; |
| 154 } else if (_callbacksCalled > testCase.callbacks) { | 159 } else if (_callbacksCalled > testCase.callbacks) { |
| 155 final expected = testCase.callbacks; | 160 final expected = testCase.callbacks; |
| 156 testCase.recordError( | 161 testCase.recordError( |
| 157 'More calls to callbackDone() than expected. ' | 162 'More calls to callbackDone() than expected. ' |
| 158 + 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); | 163 + 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); |
| 159 _uncaughtError = true; | 164 _uncaughtError = true; |
| 160 } else if (_callbacksCalled == testCase.callbacks) { | 165 } else if (_callbacksCalled == testCase.callbacks) { |
| 161 testCase.recordSuccess(); | 166 testCase.recordSuccess(); |
| 162 _currentTest++; | 167 if (!_testIsRunning) { |
| 163 _nextBatch(); | 168 _currentTest++; |
| 169 _nextBatch(); | |
| 170 } | |
| 164 } | 171 } |
| 165 } | 172 } |
| 166 | 173 |
| 167 /** | 174 /** |
| 168 * Runs a batch of tests, yielding whenever an asynchronous test starts | 175 * Runs a batch of tests, yielding whenever an asynchronous test starts |
| 169 * running. Tests will resume executing when such asynchronous test calls | 176 * running. Tests will resume executing when such asynchronous test calls |
| 170 * [done] or if it fails with an exception. | 177 * [done] or if it fails with an exception. |
| 171 */ | 178 */ |
| 172 void _nextBatch() { | 179 void _nextBatch() { |
| 173 while (_currentTest < _tests.length) { | 180 while (_currentTest < _tests.length) { |
| 174 final testCase = _tests[_currentTest]; | 181 final testCase = _tests[_currentTest]; |
| 175 runTest(testCase); | 182 runTest(testCase); |
| 176 if (!testCase.isComplete() && testCase.callbacks > 0) { | 183 if (!testCase.isComplete() && testCase.callbacks > 0) { |
| 177 return; | 184 return; |
| 178 } | 185 } |
| 179 _currentTest++; | 186 _currentTest++; |
| 180 } | 187 } |
| 181 _completeTests(); | 188 _completeTests(); |
| 182 } | 189 } |
| 183 | 190 |
| 184 /** Runs a single test. */ | 191 /** Runs a single test. */ |
| 185 void runTest(TestCase testCase) { | 192 void runTest(TestCase testCase) { |
| 186 // TODO(sigmund): remove this declaration once dartc supports trapping error | 193 // TODO(sigmund): remove this declaration once dartc supports trapping error |
| 187 // traces. | 194 // traces. |
| 188 var trace = ''; | 195 var trace = ''; |
| 189 _uncaughtError = false; | 196 _uncaughtError = false; |
| 190 _callbacksCalled = 0; | 197 _callbacksCalled = 0; |
| 191 try { | 198 try { |
| 199 _testIsRunning = true; | |
| 192 (testCase.test)(); | 200 (testCase.test)(); |
| 193 if (!_uncaughtError) { | 201 if (!_uncaughtError) { |
| 194 if (testCase.callbacks == _callbacksCalled) { | 202 if (testCase.callbacks == _callbacksCalled) { |
| 195 testCase.recordSuccess(); | 203 testCase.recordSuccess(); |
| 196 } | 204 } |
| 197 } | 205 } |
| 198 } catch (ExpectException e, var trace) { | 206 } catch (ExpectException e, var trace) { |
| 199 if (!_uncaughtError) { | 207 if (!_uncaughtError) { |
| 200 testCase.recordFail(e.message, trace.toString()); | 208 testCase.recordFail(e.message, trace.toString()); |
| 201 } | 209 } |
| 202 } catch (var e, var trace) { | 210 } catch (var e, var trace) { |
| 203 if (!_uncaughtError) { | 211 if (!_uncaughtError) { |
| 204 testCase.recordError('Caught ${e}', trace.toString()); | 212 testCase.recordError('Caught ${e}', trace.toString()); |
| 205 } | 213 } |
| 214 } finally { | |
| 215 _testIsRunning = false; | |
| 206 } | 216 } |
| 207 } | 217 } |
| 208 | 218 |
| 209 /** Publish results on the page and notify controller. */ | 219 /** Publish results on the page and notify controller. */ |
| 210 void _completeTests() { | 220 void _completeTests() { |
| 211 try { | 221 try { |
| 212 window.dynamic.on.error.remove(_onErrorClosure); | 222 window.dynamic.on.error.remove(_onErrorClosure); |
| 213 } catch (var e) { | 223 } catch (var e) { |
| 214 // TODO(jacobr): remove this horrible hack to work around dartc bugs. | 224 // TODO(jacobr): remove this horrible hack to work around dartc bugs. |
| 215 window.dynamic.onerror = null; | 225 window.dynamic.onerror = null; |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 </tr>"""; | 528 </tr>"""; |
| 519 if (stackTrace != null) { | 529 if (stackTrace != null) { |
| 520 message += | 530 message += |
| 521 "<tr><td></td><td colspan='2'><pre>${stackTrace}</pre></td></tr>"; | 531 "<tr><td></td><td colspan='2'><pre>${stackTrace}</pre></td></tr>"; |
| 522 } | 532 } |
| 523 fail = true; | 533 fail = true; |
| 524 } | 534 } |
| 525 } | 535 } |
| 526 | 536 |
| 527 typedef void TestFunction(); | 537 typedef void TestFunction(); |
| OLD | NEW |