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

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

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

Powered by Google App Engine
This is Rietveld 408576698