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

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

Powered by Google App Engine
This is Rietveld 408576698