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

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

Issue 13843031: pkg/unittest: more cleanup pulled from a defunct CL (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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
« no previous file with comments | « no previous file | 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 * ## Installing ## 8 * ## Installing ##
9 * 9 *
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` 10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml`
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 : this.callback = callback, 378 : this.callback = callback,
379 minExpectedCalls = minExpected, 379 minExpectedCalls = minExpected,
380 maxExpectedCalls = (maxExpected == 0 && minExpected > 0) 380 maxExpectedCalls = (maxExpected == 0 && minExpected > 0)
381 ? minExpected 381 ? minExpected
382 : maxExpected, 382 : maxExpected,
383 this.isDone = isDone, 383 this.isDone = isDone,
384 this.testCase = currentTestCase, 384 this.testCase = currentTestCase,
385 this.id = _makeCallbackId(id, callback) { 385 this.id = _makeCallbackId(id, callback) {
386 ensureInitialized(); 386 ensureInitialized();
387 if (testCase == null) { 387 if (testCase == null) {
388 print("No valid test, did you forget to run your test inside a call " 388 throw new StateError("No valid test. Did you forget to run your test "
389 "to test()?"); 389 "inside a call to test()?");
390 } 390 }
391 assert(testCase != null);
392 391
393 if (isDone != null || minExpected > 0) { 392 if (isDone != null || minExpected > 0) {
394 testCase._callbackFunctionsOutstanding++; 393 testCase._callbackFunctionsOutstanding++;
395 complete = false; 394 complete = false;
396 } else { 395 } else {
397 complete = true; 396 complete = true;
398 } 397 }
399 } 398 }
400 399
401 static _makeCallbackId(String id, Function callback) { 400 static String _makeCallbackId(String id, Function callback) {
402 // Try to create a reasonable id. 401 // Try to create a reasonable id.
403 if (id != null) { 402 if (id != null) {
404 return "$id "; 403 return "$id ";
405 } else { 404 } else {
406 // If the callback is not an anonymous closure, try to get the 405 // If the callback is not an anonymous closure, try to get the
407 // name. 406 // name.
408 var fname = callback.toString(); 407 var fname = callback.toString();
409 var prefix = "Function '"; 408 var prefix = "Function '";
410 var pos = fname.indexOf(prefix); 409 var pos = fname.indexOf(prefix);
411 if (pos > 0) { 410 if (pos > 0) {
412 pos += prefix.length; 411 pos += prefix.length;
413 var epos = fname.indexOf("'", pos); 412 var epos = fname.indexOf("'", pos);
414 if (epos > 0) { 413 if (epos > 0) {
415 return "${fname.substring(pos, epos)} "; 414 return "${fname.substring(pos, epos)} ";
416 } 415 }
417 } 416 }
418 } 417 }
419 return ''; 418 return '';
420 } 419 }
421 420
422 shouldCallBack() { 421 bool shouldCallBack() {
423 ++actualCalls; 422 ++actualCalls;
424 if (testCase.isComplete) { 423 if (testCase.isComplete) {
425 // Don't run if the test is done. We don't throw here as this is not 424 // Don't run if the test is done. We don't throw here as this is not
426 // the current test, but we do mark the old test as having an error 425 // the current test, but we do mark the old test as having an error
427 // if it previously passed. 426 // if it previously passed.
428 if (testCase.result == PASS) { 427 if (testCase.result == PASS) {
429 testCase.error( 428 testCase.error(
430 'Callback ${id}called ($actualCalls) after test case ' 429 'Callback ${id}called ($actualCalls) after test case '
431 '${testCase.description} has already been marked as ' 430 '${testCase.description} has already been marked as '
432 '${testCase.result}.', ''); 431 '${testCase.result}.', '');
433 } 432 }
434 return false; 433 return false;
435 } else if (maxExpectedCalls >= 0 && actualCalls > maxExpectedCalls) { 434 } else if (maxExpectedCalls >= 0 && actualCalls > maxExpectedCalls) {
436 throw new TestFailure('Callback ${id}called more times than expected ' 435 throw new TestFailure('Callback ${id}called more times than expected '
437 '($maxExpectedCalls).'); 436 '($maxExpectedCalls).');
438 } 437 }
439 return true; 438 return true;
440 } 439 }
441 440
442 after() { 441 void after() {
443 if (!complete) { 442 if (!complete) {
444 if (minExpectedCalls > 0 && actualCalls < minExpectedCalls) return; 443 if (minExpectedCalls > 0 && actualCalls < minExpectedCalls) return;
445 if (isDone != null && !isDone()) return; 444 if (isDone != null && !isDone()) return;
446 445
447 // Mark this callback as complete and remove it from the testcase 446 // Mark this callback as complete and remove it from the testcase
448 // oustanding callback count; if that hits zero the testcase is done. 447 // oustanding callback count; if that hits zero the testcase is done.
449 complete = true; 448 complete = true;
450 testCase._markCallbackComplete(); 449 testCase._markCallbackComplete();
451 } 450 }
452 } 451 }
453 452
454 invoke([arg0 = sentinel, arg1 = sentinel, arg2 = sentinel,
455 arg3 = sentinel, arg4 = sentinel]) {
456 return _guardAsync(() {
457 if (!shouldCallBack()) {
458 return;
459 } else if (arg0 == sentinel) {
460 return callback();
461 } else if (arg1 == sentinel) {
462 return callback(arg0);
463 } else if (arg2 == sentinel) {
464 return callback(arg0, arg1);
465 } else if (arg3 == sentinel) {
466 return callback(arg0, arg1, arg2);
467 } else if (arg4 == sentinel) {
468 return callback(arg0, arg1, arg2, arg3);
469 } else {
470 testCase.error(
471 'unittest lib does not support callbacks with more than'
472 ' 4 arguments.',
473 '');
474 }
475 },
476 after, testCase);
477 }
478
479 invoke0() { 453 invoke0() {
480 return _guardAsync( 454 return _guardAsync(
481 () { 455 () {
482 if (shouldCallBack()) { 456 if (shouldCallBack()) {
483 return callback(); 457 return callback();
484 } 458 }
485 }, 459 },
486 after, testCase); 460 after, testCase);
487 } 461 }
488 462
(...skipping 15 matching lines...) Expand all
504 } 478 }
505 }, 479 },
506 after, testCase); 480 after, testCase);
507 } 481 }
508 } 482 }
509 483
510 /** 484 /**
511 * Indicate that [callback] is expected to be called a [count] number of times 485 * Indicate that [callback] is expected to be called a [count] number of times
512 * (by default 1). The unittest framework will wait for the callback to run the 486 * (by default 1). The unittest framework will wait for the callback to run the
513 * specified [count] times before it continues with the following test. Using 487 * specified [count] times before it continues with the following test. Using
514 * [_expectAsync] will also ensure that errors that occur within [callback] are
515 * tracked and reported. [callback] should take between 0 and 4 positional
516 * arguments (named arguments are not supported here). [id] can be used
517 * to provide more descriptive error messages if the callback is called more
518 * often than expected.
519 */
520 Function _expectAsync(Function callback,
521 {int count: 1, int max: 0, String id}) {
522 return new _SpreadArgsHelper(callback, count, max, null, id).invoke;
523 }
524
525 /**
526 * Indicate that [callback] is expected to be called a [count] number of times
527 * (by default 1). The unittest framework will wait for the callback to run the
528 * specified [count] times before it continues with the following test. Using
529 * [expectAsync0] will also ensure that errors that occur within [callback] are 488 * [expectAsync0] will also ensure that errors that occur within [callback] are
530 * tracked and reported. [callback] should take 0 positional arguments (named 489 * tracked and reported. [callback] should take 0 positional arguments (named
531 * arguments are not supported). [id] can be used to provide more 490 * arguments are not supported). [id] can be used to provide more
532 * descriptive error messages if the callback is called more often than 491 * descriptive error messages if the callback is called more often than
533 * expected. [max] can be used to specify an upper bound on the number of 492 * expected. [max] can be used to specify an upper bound on the number of
534 * calls; if this is exceeded the test will fail (or be marked as in error if 493 * calls; if this is exceeded the test will fail (or be marked as in error if
535 * it was already complete). A value of 0 for [max] (the default) will set 494 * it was already complete). A value of 0 for [max] (the default) will set
536 * the upper bound to the same value as [count]; i.e. the callback should be 495 * the upper bound to the same value as [count]; i.e. the callback should be
537 * called exactly [count] times. A value of -1 for [max] will mean no upper 496 * called exactly [count] times. A value of -1 for [max] will mean no upper
538 * bound. 497 * bound.
(...skipping 13 matching lines...) Expand all
552 511
553 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ 512 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */
554 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 513 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
555 Function expectAsync2(Function callback, 514 Function expectAsync2(Function callback,
556 {int count: 1, int max: 0, String id}) { 515 {int count: 1, int max: 0, String id}) {
557 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; 516 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2;
558 } 517 }
559 518
560 /** 519 /**
561 * Indicate that [callback] is expected to be called until [isDone] returns 520 * Indicate that [callback] is expected to be called until [isDone] returns
562 * true. The unittest framework checks [isDone] after each callback and only
563 * when it returns true will it continue with the following test. Using
564 * [expectAsyncUntil] will also ensure that errors that occur within
565 * [callback] are tracked and reported. [callback] should take between 0 and
566 * 4 positional arguments (named arguments are not supported). [id] can be
567 * used to identify the callback in error messages (for example if it is called
568 * after the test case is complete).
569 */
570 Function _expectAsyncUntil(Function callback, Function isDone, {String id}) {
571 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke;
572 }
573
574 /**
575 * Indicate that [callback] is expected to be called until [isDone] returns
576 * true. The unittest framework check [isDone] after each callback and only 521 * true. The unittest framework check [isDone] after each callback and only
577 * when it returns true will it continue with the following test. Using 522 * when it returns true will it continue with the following test. Using
578 * [expectAsyncUntil0] will also ensure that errors that occur within 523 * [expectAsyncUntil0] will also ensure that errors that occur within
579 * [callback] are tracked and reported. [callback] should take 0 positional 524 * [callback] are tracked and reported. [callback] should take 0 positional
580 * arguments (named arguments are not supported). [id] can be used to 525 * arguments (named arguments are not supported). [id] can be used to
581 * identify the callback in error messages (for example if it is called 526 * identify the callback in error messages (for example if it is called
582 * after the test case is complete). 527 * after the test case is complete).
583 */ 528 */
584 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 529 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
585 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) { 530 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) {
(...skipping 14 matching lines...) Expand all
600 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 545 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
601 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { 546 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) {
602 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; 547 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2;
603 } 548 }
604 549
605 /** 550 /**
606 * Wraps the [callback] in a new function and returns that function. The new 551 * Wraps the [callback] in a new function and returns that function. The new
607 * function will be able to handle exceptions by directing them to the correct 552 * function will be able to handle exceptions by directing them to the correct
608 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that 553 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that
609 * might optionally be called but may never be called during the test. 554 * might optionally be called but may never be called during the test.
610 * [callback] should take between 0 and 4 positional arguments (named arguments
611 * are not supported). [id] can be used to identify the callback in error
612 * messages (for example if it is called after the test case is complete).
613 */
614 Function _protectAsync(Function callback, {String id}) {
615 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke;
616 }
617
618 /**
619 * Wraps the [callback] in a new function and returns that function. The new
620 * function will be able to handle exceptions by directing them to the correct
621 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that
622 * might optionally be called but may never be called during the test.
623 * [callback] should take 0 positional arguments (named arguments are not 555 * [callback] should take 0 positional arguments (named arguments are not
624 * supported). [id] can be used to identify the callback in error 556 * supported). [id] can be used to identify the callback in error
625 * messages (for example if it is called after the test case is complete). 557 * messages (for example if it is called after the test case is complete).
626 */ 558 */
627 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 559 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
628 Function protectAsync0(Function callback, {String id}) { 560 Function protectAsync0(Function callback, {String id}) {
629 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke0; 561 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke0;
630 } 562 }
631 563
632 /** 564 /**
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 } 825 }
894 826
895 /** Enable a test by ID. */ 827 /** Enable a test by ID. */
896 void enableTest(int testId) => _setTestEnabledState(testId, true); 828 void enableTest(int testId) => _setTestEnabledState(testId, true);
897 829
898 /** Disable a test by ID. */ 830 /** Disable a test by ID. */
899 void disableTest(int testId) => _setTestEnabledState(testId, false); 831 void disableTest(int testId) => _setTestEnabledState(testId, false);
900 832
901 /** Signature for a test function. */ 833 /** Signature for a test function. */
902 typedef dynamic TestFunction(); 834 typedef dynamic TestFunction();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698