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

Side by Side Diff: pkg/unittest/test/unittest_test.dart

Issue 12310123: Added ability for test cases to return Futures. (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 | « pkg/unittest/lib/unittest.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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(gram): 5 // TODO(gram):
6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g.
7 // insufficient callbacks, because the timeout is controlled externally 7 // insufficient callbacks, because the timeout is controlled externally
8 // (test.dart?), and we would need to use a shorter timeout for the inner tests 8 // (test.dart?), and we would need to use a shorter timeout for the inner tests
9 // so the outer timeout doesn't fire. So I removed all such tests. 9 // so the outer timeout doesn't fire. So I removed all such tests.
10 // I'd like to revisit this at some point. 10 // I'd like to revisit this at some point.
11 11
12 library unittestTest; 12 library unittestTest;
13 import 'dart:isolate'; 13 import 'dart:isolate';
14 import 'dart:async'; 14 import 'dart:async';
15 import 'package:unittest/unittest.dart'; 15 import 'package:unittest/unittest.dart';
16 16
17 var tests; // array of test names 17 var tests; // array of test names
18 var expected; // array of test expected results (from buildStatusString) 18 var expected; // array of test expected results (from buildStatusString)
19 var actual; // actual test results (from buildStatusString in config.onDone) 19 var actual; // actual test results (from buildStatusString in config.onDone)
20 var _testconfig; // test configuration to capture onDone 20 var _testconfig; // test configuration to capture onDone
21 21
22 _defer(void fn()) { 22 _defer(void fn()) {
23 // Exploit isolate ports as a platform-independent mechanism to queue a 23 return (new Future.immediate(null)).then((_) => guardAsync(fn));
24 // message at the end of the event loop. Stolen from unittest.dart.
25 final port = new ReceivePort();
26 port.receive((msg, reply) {
27 fn();
28 port.close();
29 });
30 port.toSendPort().send(null, null);
31 } 24 }
32 25
33 String buildStatusString(int passed, int failed, int errors, 26 String buildStatusString(int passed, int failed, int errors,
34 var results, 27 var results,
35 {int count: 0, 28 {int count: 0,
36 String setup: '', String teardown: '', 29 String setup: '', String teardown: '',
37 String uncaughtError: null, 30 String uncaughtError: null,
38 String message: ''}) { 31 String message: ''}) {
39 var totalTests = 0; 32 var totalTests = 0;
40 String testDetails = ''; 33 String testDetails = '';
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 completer.completeError( 220 completer.completeError(
228 new AsyncError("Failed to complete tearDown")); 221 new AsyncError("Failed to complete tearDown"));
229 }); 222 });
230 return completer.future; 223 return completer.future;
231 }); 224 });
232 test('foo4', (){}); 225 test('foo4', (){});
233 }); 226 });
234 // The next test is just to make sure we make steady progress 227 // The next test is just to make sure we make steady progress
235 // through the tests. 228 // through the tests.
236 test('post groups', () {}); 229 test('post groups', () {});
230 } else if (testName == 'test returning future') {
231 test("successful", () {
232 return _defer(() {
233 expect(true, true);
234 });
235 });
236 // We repeat the fail and error tests, because during development
237 // I had a situation where either worked fine on their own, and
238 // error/fail worked, but fail/error would time out.
239 test("error1", () {
240 var callback = expectAsync0((){});
241 var excesscallback = expectAsync0((){});
242 return _defer(() {
243 excesscallback();
244 excesscallback();
245 excesscallback();
246 callback();
247 });
248 });
249 test("fail1", () {
250 return _defer(() {
251 expect(true, false);
252 });
253 });
254 test("error2", () {
255 var callback = expectAsync0((){});
256 var excesscallback = expectAsync0((){});
257 return _defer(() {
258 excesscallback();
259 excesscallback();
260 callback();
261 });
262 });
263 test("fail2", () {
264 return _defer(() {
265 expect(false, true);
266 });
267 });
268 test('foo5', () {
269 });
270 } else if (testName == 'test returning future using Timer') {
271 test("successful", () {
272 return _defer(() {
273 Timer.run(() {
274 guardAsync(() {
275 expect(true, true);
276 });
277 });
278 });
279 });
280 test("fail1", () {
281 var callback = expectAsync0((){});
282 return _defer(() {
283 Timer.run(() {
284 guardAsync(() {
285 expect(true, false);
286 callback();
287 });
288 });
289 });
290 });
291 test('error1', () {
292 var callback = expectAsync0((){});
293 var excesscallback = expectAsync0((){});
294 return _defer(() {
295 Timer.run(() {
296 guardAsync(() {
297 excesscallback();
298 excesscallback();
299 excesscallback();
300 callback();
301 });
302 });
303 });
304 });
305 test("fail2", () {
306 var callback = expectAsync0((){});
307 return _defer(() {
308 Timer.run(() {
309 guardAsync(() {
310 expect(false, true);
311 callback();
312 });
313 });
314 });
315 });
316 test('error2', () {
317 var callback = expectAsync0((){});
318 var excesscallback = expectAsync0((){});
319 return _defer(() {
320 Timer.run(() {
321 guardAsync(() {
322 excesscallback();
323 excesscallback();
324 callback();
325 });
326 });
327 });
328 });
329 test('foo6', () {
330 });
237 } 331 }
238 }); 332 });
239 } 333 }
240 334
241 void nextTest(int testNum) { 335 void nextTest(int testNum) {
242 SendPort sport = spawnFunction(runTest); 336 SendPort sport = spawnFunction(runTest);
243 sport.call(tests[testNum]).then((msg) { 337 sport.call(tests[testNum]).then((msg) {
244 actual.add(msg); 338 actual.add(msg);
245 if (actual.length == expected.length) { 339 if (actual.length == expected.length) {
246 for (var i = 0; i < tests.length; i++) { 340 for (var i = 0; i < tests.length; i++) {
(...skipping 13 matching lines...) Expand all
260 'group name test', 354 'group name test',
261 'setup test', 355 'setup test',
262 'teardown test', 356 'teardown test',
263 'setup and teardown test', 357 'setup and teardown test',
264 'correct callback test', 358 'correct callback test',
265 'excess callback test', 359 'excess callback test',
266 'completion test', 360 'completion test',
267 'async exception test', 361 'async exception test',
268 'late exception test', 362 'late exception test',
269 'middle exception test', 363 'middle exception test',
270 'async setup/teardown test' 364 'async setup/teardown test',
365 'test returning future',
366 'test returning future using Timer'
271 ]; 367 ];
272 368
273 expected = [ 369 expected = [
274 buildStatusString(1, 0, 0, tests[0]), 370 buildStatusString(1, 0, 0, tests[0]),
275 buildStatusString(0, 1, 0, tests[1], 371 buildStatusString(0, 1, 0, tests[1],
276 message: 'Expected: <5> but: was <4>.'), 372 message: 'Expected: <5> but: was <4>.'),
277 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: Fail.'), 373 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: Fail.'),
278 buildStatusString(2, 0, 0, 'a a::a b b'), 374 buildStatusString(2, 0, 0, 'a a::a b b'),
279 buildStatusString(1, 0, 0, 'a ${tests[4]}', count: 0, setup: 'setup'), 375 buildStatusString(1, 0, 0, 'a ${tests[4]}', count: 0, setup: 'setup'),
280 buildStatusString(1, 0, 0, 'a ${tests[5]}', count: 0, setup: '', 376 buildStatusString(1, 0, 0, 'a ${tests[5]}', count: 0, setup: '',
(...skipping 11 matching lines...) Expand all
292 buildStatusString(2, 1, 0, 388 buildStatusString(2, 1, 0,
293 'testOne::testTwo:Expected: false but: was <true>.:testThree'), 389 'testOne::testTwo:Expected: false but: was <true>.:testThree'),
294 buildStatusString(2, 0, 3, 390 buildStatusString(2, 0, 3,
295 'good setup/good teardown foo1::' 391 'good setup/good teardown foo1::'
296 'good setup/bad teardown foo2:good setup/bad teardown ' 392 'good setup/bad teardown foo2:good setup/bad teardown '
297 'foo2: Test teardown failed: Failed to complete tearDown:' 393 'foo2: Test teardown failed: Failed to complete tearDown:'
298 'bad setup/good teardown foo3:bad setup/good teardown ' 394 'bad setup/good teardown foo3:bad setup/good teardown '
299 'foo3: Test setup failed: Failed to complete setUp:' 395 'foo3: Test setup failed: Failed to complete setUp:'
300 'bad setup/bad teardown foo4:bad setup/bad teardown ' 396 'bad setup/bad teardown foo4:bad setup/bad teardown '
301 'foo4: Test teardown failed: Failed to complete tearDown:' 397 'foo4: Test teardown failed: Failed to complete tearDown:'
302 'post groups') 398 'post groups'),
399 buildStatusString(2, 2, 2,
400 'successful::'
401 'error1:Callback called more times than expected (3 > 1).:'
402 'fail1:Expected: <false> but: was <true>.:'
403 'error2:Callback called more times than expected (2 > 1).:'
404 'fail2:Expected: <true> but: was <false>.:'
405 'foo5'),
406 buildStatusString(2, 2, 2,
407 'successful::'
408 'fail1:Expected: <false> but: was <true>.:'
409 'error1:Callback called more times than expected (3 > 1).:'
410 'fail2:Expected: <true> but: was <false>.:'
411 'error2:Callback called more times than expected (2 > 1).:'
412 'foo6'),
303 ]; 413 ];
304 414
305 actual = []; 415 actual = [];
306 416
307 nextTest(0); 417 nextTest(0);
308 } 418 }
309 419
OLDNEW
« no previous file with comments | « pkg/unittest/lib/unittest.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698