| 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 Configuration _config = null; |
| 6 |
| 5 /** | 7 /** |
| 6 * Description text of the current test group. If multiple groups are nested, | 8 * Description text of the current test group. If multiple groups are nested, |
| 7 * this will contain all of their text concatenated. | 9 * this will contain all of their text concatenated. |
| 8 */ | 10 */ |
| 9 String _currentGroup = ''; | 11 String _currentGroup = ''; |
| 10 | 12 |
| 11 /** Tests executed in this suite. */ | 13 /** Tests executed in this suite. */ |
| 12 List<TestCase> _tests; | 14 List<TestCase> _tests; |
| 13 | 15 |
| 14 /** | 16 /** |
| 15 * Callback used to run tests. Entrypoints can replace this with their own | 17 * Callback used to run tests. Entrypoints can replace this with their own |
| 16 * if they want. | 18 * if they want. |
| 17 */ | 19 */ |
| 18 Function _testRunner; | 20 Function _testRunner; |
| 19 | 21 |
| 20 /** Whether this is run within dartium layout tests. */ | |
| 21 bool _isLayoutTest = false; | |
| 22 | |
| 23 /** Current test being executed. */ | 22 /** Current test being executed. */ |
| 24 int _currentTest = 0; | 23 int _currentTest = 0; |
| 25 | 24 |
| 26 /** Total number of callbacks that have been executed in the current test. */ | 25 /** Total number of callbacks that have been executed in the current test. */ |
| 27 int _callbacksCalled = 0; | 26 int _callbacksCalled = 0; |
| 28 | 27 |
| 29 final _UNINITIALIZED = 0; | 28 final _UNINITIALIZED = 0; |
| 30 final _READY = 1; | 29 final _READY = 1; |
| 31 final _RUNNING_TEST = 2; | 30 final _RUNNING_TEST = 2; |
| 32 | 31 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 final testCase = new TestCase( | 78 final testCase = new TestCase( |
| 80 _tests.length + 1, _fullSpec(spec), body, callbacks); | 79 _tests.length + 1, _fullSpec(spec), body, callbacks); |
| 81 _tests.add(testCase); | 80 _tests.add(testCase); |
| 82 | 81 |
| 83 if (callbacks < 1) { | 82 if (callbacks < 1) { |
| 84 testCase.error( | 83 testCase.error( |
| 85 'Async tests must wait for at least one callback ', ''); | 84 'Async tests must wait for at least one callback ', ''); |
| 86 } | 85 } |
| 87 } | 86 } |
| 88 | 87 |
| 89 void serialInvokeAsync(List closures) { | |
| 90 final length = closures.length; | |
| 91 if (length > 0) { | |
| 92 int i = 0; | |
| 93 void invokeNext() { | |
| 94 closures[i](); | |
| 95 i++; | |
| 96 if (i < length) { | |
| 97 window.setTimeout(invokeNext, 0); | |
| 98 } | |
| 99 } | |
| 100 window.setTimeout(invokeNext, 0); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 /** | 88 /** |
| 105 * Creates a new named group of tests. Calls to group() or test() within the | 89 * Creates a new named group of tests. Calls to group() or test() within the |
| 106 * body of the function passed to this will inherit this group's description. | 90 * body of the function passed to this will inherit this group's description. |
| 107 */ | 91 */ |
| 108 void group(String description, void body()) { | 92 void group(String description, void body()) { |
| 109 _ensureInitialized(); | 93 _ensureInitialized(); |
| 110 | 94 |
| 111 // Concatenate the new group. | 95 // Concatenate the new group. |
| 112 final oldGroup = _currentGroup; | 96 final oldGroup = _currentGroup; |
| 113 if (_currentGroup != '') { | 97 if (_currentGroup != '') { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 141 + 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); | 125 + 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); |
| 142 _state = _UNCAUGHT_ERROR; | 126 _state = _UNCAUGHT_ERROR; |
| 143 } else if ((_callbacksCalled == testCase.callbacks) && | 127 } else if ((_callbacksCalled == testCase.callbacks) && |
| 144 (_state != _RUNNING_TEST)) { | 128 (_state != _RUNNING_TEST)) { |
| 145 testCase.pass(); | 129 testCase.pass(); |
| 146 _currentTest++; | 130 _currentTest++; |
| 147 _testRunner(); | 131 _testRunner(); |
| 148 } | 132 } |
| 149 } | 133 } |
| 150 | 134 |
| 151 void forLayoutTests() { | 135 /** Runs [callback] at the end of the event loop. */ |
| 152 _isLayoutTest = true; | 136 _defer(void callback()) { |
| 137 // Exploit isolate ports as a platform-independent mechanism to queue a |
| 138 // message at the end of the event loop. |
| 139 // TODO(sigmund): expose this functionality somewhere in our libraries. |
| 140 final port = new ReceivePort(); |
| 141 port.receive((msg, reply) { |
| 142 callback(); |
| 143 port.close(); |
| 144 }); |
| 145 port.toSendPort().send(null, null); |
| 153 } | 146 } |
| 154 | 147 |
| 155 /** Runs all queued tests, one at a time. */ | 148 /** Runs all queued tests, one at a time. */ |
| 156 _runTests() { | 149 _runTests() { |
| 157 _platformStartTests(); | 150 _config.onStart(); |
| 158 | 151 |
| 159 _platformDefer(() { | 152 _defer(() { |
| 160 assert (_currentTest == 0); | 153 assert (_currentTest == 0); |
| 161 _testRunner(); | 154 _testRunner(); |
| 162 }); | 155 }); |
| 163 } | 156 } |
| 164 | 157 |
| 165 /** Runs a single test. */ | 158 /** Runs a single test. */ |
| 166 _runTest(TestCase testCase) { | 159 _runTest(TestCase testCase) { |
| 167 try { | 160 try { |
| 168 _callbacksCalled = 0; | 161 _callbacksCalled = 0; |
| 169 _state = _RUNNING_TEST; | 162 _state = _RUNNING_TEST; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 int testsErrors_ = 0; | 212 int testsErrors_ = 0; |
| 220 | 213 |
| 221 for (TestCase t in _tests) { | 214 for (TestCase t in _tests) { |
| 222 switch (t.result) { | 215 switch (t.result) { |
| 223 case _PASS: testsPassed_++; break; | 216 case _PASS: testsPassed_++; break; |
| 224 case _FAIL: testsFailed_++; break; | 217 case _FAIL: testsFailed_++; break; |
| 225 case _ERROR: testsErrors_++; break; | 218 case _ERROR: testsErrors_++; break; |
| 226 } | 219 } |
| 227 } | 220 } |
| 228 | 221 |
| 229 _platformCompleteTests(testsPassed_, testsFailed_, testsErrors_); | 222 _config.onDone(testsPassed_, testsFailed_, testsErrors_, _tests); |
| 230 } | 223 } |
| 231 | 224 |
| 232 String _fullSpec(String spec) { | 225 String _fullSpec(String spec) { |
| 233 if (spec === null) return '$_currentGroup'; | 226 if (spec === null) return '$_currentGroup'; |
| 234 return _currentGroup != '' ? '$_currentGroup $spec' : spec; | 227 return _currentGroup != '' ? '$_currentGroup $spec' : spec; |
| 235 } | 228 } |
| 236 | 229 |
| 237 /** | 230 /** |
| 238 * Lazily initializes the test library if not already initialized. | 231 * Lazily initializes the test library if not already initialized. |
| 239 */ | 232 */ |
| 240 _ensureInitialized() { | 233 _ensureInitialized() { |
| 241 if (_state != _UNINITIALIZED) return; | 234 if (_state != _UNINITIALIZED) return; |
| 242 | 235 |
| 243 _tests = <TestCase>[]; | 236 _tests = <TestCase>[]; |
| 244 _currentGroup = ''; | 237 _currentGroup = ''; |
| 245 _state = _READY; | 238 _state = _READY; |
| 246 _testRunner = _nextBatch; | 239 _testRunner = _nextBatch; |
| 247 | 240 |
| 248 _platformInitialize(); | 241 if (_config == null) { |
| 242 _config = _defaultConfig(); |
| 243 } |
| 244 _config.onInit(); |
| 249 | 245 |
| 250 // Immediately queue the suite up. It will run after a timeout (i.e. after | 246 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 251 // main() has returned). | 247 // main() has returned). |
| 252 _platformDefer(_runTests); | 248 _defer(_runTests); |
| 253 } | 249 } |
| 254 | 250 |
| 255 /** | 251 /** |
| 256 * Wraps an value and provides an "==" operator that can be used to verify that | 252 * Wraps an value and provides an "==" operator that can be used to verify that |
| 257 * the value matches a given expectation. | 253 * the value matches a given expectation. |
| 258 */ | 254 */ |
| 259 class Expectation { | 255 class Expectation { |
| 260 final _value; | 256 final _value; |
| 261 | 257 |
| 262 Expectation(this._value); | 258 Expectation(this._value); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 } | 360 } |
| 365 | 361 |
| 366 void error(String message_, String stackTrace_) { | 362 void error(String message_, String stackTrace_) { |
| 367 result = _ERROR; | 363 result = _ERROR; |
| 368 this.message = message_; | 364 this.message = message_; |
| 369 this.stackTrace = stackTrace_; | 365 this.stackTrace = stackTrace_; |
| 370 } | 366 } |
| 371 } | 367 } |
| 372 | 368 |
| 373 typedef void TestFunction(); | 369 typedef void TestFunction(); |
| OLD | NEW |