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

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

Issue 10412015: aChange all asyncTest/callbackDone style tests to use the new expectAsync/ (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 7 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 * A library for writing dart unit tests. 6 * A library for writing dart unit tests.
7 * 7 *
8 * To import this library, specify the relative path to 8 * To import this library, specify the relative path to
9 * lib/unittest/unittest.dart. 9 * lib/unittest/unittest.dart.
10 * 10 *
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 Function expectAsync0(Function callback, [int count = 1]) { 367 Function expectAsync0(Function callback, [int count = 1]) {
368 return new _SpreadArgsHelper(callback, count).invoke0; 368 return new _SpreadArgsHelper(callback, count).invoke0;
369 } 369 }
370 370
371 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ 371 /** Like [expectAsync0] but [callback] should take 1 positional argument. */
372 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 372 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
373 Function expectAsync1(Function callback, [int count = 1]) { 373 Function expectAsync1(Function callback, [int count = 1]) {
374 return new _SpreadArgsHelper(callback, count).invoke1; 374 return new _SpreadArgsHelper(callback, count).invoke1;
375 } 375 }
376 376
377 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ 377 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */
378 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 378 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
379 Function expectAsync2(Function callback, [int count = 1]) { 379 Function expectAsync2(Function callback, [int count = 1]) {
380 return new _SpreadArgsHelper(callback, count).invoke2; 380 return new _SpreadArgsHelper(callback, count).invoke2;
381 } 381 }
382 382
383 /** 383 /**
384 * Creates a new named group of tests. Calls to group() or test() within the 384 * Creates a new named group of tests. Calls to group() or test() within the
385 * body of the function passed to this will inherit this group's description. 385 * body of the function passed to this will inherit this group's description.
386 */ 386 */
387 void group(String description, void body()) { 387 void group(String description, void body()) {
(...skipping 12 matching lines...) Expand all
400 try { 400 try {
401 body(); 401 body();
402 } finally { 402 } finally {
403 // Now that the group is over, restore the previous one. 403 // Now that the group is over, restore the previous one.
404 _currentGroup = oldGroup; 404 _currentGroup = oldGroup;
405 } 405 }
406 } 406 }
407 407
408 /** Called by subclasses to indicate that an asynchronous test completed. */ 408 /** Called by subclasses to indicate that an asynchronous test completed. */
409 void callbackDone() { 409 void callbackDone() {
410 _callbacksCalled++; 410 // TODO (gram): we defer this to give the nextBatch recursive
411 if (_currentTest < _tests.length) { 411 // stack a chance to unwind. This is a temporary hack but
412 final testCase = _tests[_currentTest]; 412 // really a bunch of code here needs to be fixed. We have a
413 if (_callbacksCalled > testCase.callbacks) { 413 // single array that is being iterated through by a function
414 final expected = testCase.callbacks; 414 // that is recursively invoked in the case of async tests.
Siggi Cherem (dart-lang) 2012/05/21 22:20:47 can we clarify here why recursive calls happen. In
gram 2012/05/22 17:36:36 Done.
415 testCase.error( 415 // Bad things can happen.
416 'More calls to callbackDone() than expected. ' 416 _defer(() {
417 'Actual: ${_callbacksCalled}, expected: ${expected}', ''); 417 _callbacksCalled++;
418 _state = _UNCAUGHT_ERROR; 418 if (_currentTest < _tests.length) {
419 } else if ((_callbacksCalled == testCase.callbacks) && 419 final testCase = _tests[_currentTest];
420 (_state != _RUNNING_TEST)) { 420 if (_callbacksCalled > testCase.callbacks) {
421 if (testCase.result == null) testCase.pass(); 421 final expected = testCase.callbacks;
422 _currentTest++; 422 testCase.error(
423 _testRunner(); 423 'More calls to callbackDone() than expected. '
424 'Actual: ${_callbacksCalled}, expected: ${expected}', '');
425 _state = _UNCAUGHT_ERROR;
426 } else if ((_callbacksCalled == testCase.callbacks) &&
427 (_state != _RUNNING_TEST)) {
428 if (testCase.result == null) testCase.pass();
429 _currentTest++;
430 _testRunner();
431 }
424 } 432 }
425 } 433 });
426 } 434 }
427 435
428 /** Menchanism to notify that an error was caught outside of this library. */ 436 /** Menchanism to notify that an error was caught outside of this library. */
429 void reportTestError(String msg, String trace) { 437 void reportTestError(String msg, String trace) {
430 if (_currentTest < _tests.length) { 438 if (_currentTest < _tests.length) {
431 final testCase = _tests[_currentTest]; 439 final testCase = _tests[_currentTest];
432 testCase.error(msg, trace); 440 testCase.error(msg, trace);
433 _state = _UNCAUGHT_ERROR; 441 _state = _UNCAUGHT_ERROR;
434 if (testCase.callbacks > 0) { 442 if (testCase.callbacks > 0) {
435 _currentTest++; 443 _currentTest++;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 } 477 }
470 478
471 /** 479 /**
472 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update 480 * Run [tryBody] guarded in a try-catch block. If an exception is thrown, update
473 * the [_currentTest] status accordingly. 481 * the [_currentTest] status accordingly.
474 */ 482 */
475 guardAsync(tryBody, [finallyBody]) { 483 guardAsync(tryBody, [finallyBody]) {
476 try { 484 try {
477 return tryBody(); 485 return tryBody();
478 } catch (ExpectException e, var trace) { 486 } catch (ExpectException e, var trace) {
487 Expect.isTrue(_currentTest < _tests.length);
479 if (_state != _UNCAUGHT_ERROR) { 488 if (_state != _UNCAUGHT_ERROR) {
480 _tests[_currentTest].fail(e.message, 489 _tests[_currentTest].fail(e.message,
481 trace == null ? '' : trace.toString()); 490 trace == null ? '' : trace.toString());
482 } 491 }
483 } catch (var e, var trace) { 492 } catch (var e, var trace) {
484 if (_state == _RUNNING_TEST) { 493 if (_state == _RUNNING_TEST) {
485 // If a random exception is thrown from within a test, we consider that 494 // If a random exception is thrown from within a test, we consider that
486 // a test failure too. A test case implicitly has an expectation that it 495 // a test failure too. A test case implicitly has an expectation that it
487 // will run to completion without an uncaught exception being thrown. 496 // will run to completion without an uncaught exception being thrown.
488 _tests[_currentTest].fail('Caught $e', 497 _tests[_currentTest].fail('Caught $e',
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 } 580 }
572 _config.onInit(); 581 _config.onInit();
573 582
574 // Immediately queue the suite up. It will run after a timeout (i.e. after 583 // Immediately queue the suite up. It will run after a timeout (i.e. after
575 // main() has returned). 584 // main() has returned).
576 _defer(_runTests); 585 _defer(_runTests);
577 } 586 }
578 587
579 /** Signature for a test function. */ 588 /** Signature for a test function. */
580 typedef void TestFunction(); 589 typedef void TestFunction();
OLDNEW
« no previous file with comments | « no previous file | tests/benchmark_smoke/benchmark_smoke_test.dart » ('j') | tests/dom/async_window_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698