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

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

Issue 10544167: Some unit test fixes (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 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 | « lib/unittest/expect.dart ('k') | tests/html/audiocontext_test.dart » ('j') | 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) 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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 class _Sentinel { 287 class _Sentinel {
288 const _Sentinel(); 288 const _Sentinel();
289 } 289 }
290 290
291 // TODO(sigmund): make a singleton const field when frog supports passing those 291 // TODO(sigmund): make a singleton const field when frog supports passing those
292 // as default values to named arguments. 292 // as default values to named arguments.
293 final _sentinel = const _Sentinel(); 293 final _sentinel = const _Sentinel();
294 294
295 /** Simulates spread arguments using named arguments. */ 295 /** Simulates spread arguments using named arguments. */
296 // TODO(sigmund): remove this class and simply use a closure with named 296 // TODO(sigmund): remove this class and simply use a closure with named
297 // arguments inside [_expectAsync], once bug 282 is fixed or frog is replaced by 297 // arguments (if still applicable).
298 // dart2js.
299 class _SpreadArgsHelper { 298 class _SpreadArgsHelper {
300 Function callback; 299 Function _callback;
301 int expectedCalls; 300 int _expectedCalls;
302 int calls = 0; 301 int _calls = 0;
303 TestCase testCase; 302 TestCase _testCase;
304 _SpreadArgsHelper(this.callback, this.expectedCalls) { 303 Function _shouldCallBack;
305 Expect.isTrue(_currentTest < _tests.length); 304 Function _isDone;
306 testCase = _tests[_currentTest]; 305
307 testCase.callbacks++; 306 _init(Function callback, Function shouldCallBack, Function isDone,
307 [expectedCalls = 0]) {
308 assert(_currentTest < _tests.length);
309 _callback = callback;
310 _shouldCallBack = shouldCallBack;
311 _isDone = isDone;
312 _expectedCalls = expectedCalls;
313 _testCase = _tests[_currentTest];
314 if (expectedCalls > 0) {
315 _testCase.callbacks++;
316 }
317 }
318
319 _SpreadArgsHelper(callback, shouldCallBack, isDone) {
320 _init(callback, shouldCallBack, isDone);
321 }
322
323 _SpreadArgsHelper.fixedCallCount(callback, expectedCalls) {
324 _init(callback, _checkCallCount, _allCallsDone, expectedCalls);
325 }
326
327 _SpreadArgsHelper.variableCallCount(callback, isDone) {
328 _init(callback, _always, isDone);
329 }
330
331 _after() {
332 if (_isDone()) {
333 _handleAllCallbacksDone();
334 }
335 }
336
337 _allCallsDone() => _calls == _expectedCalls;
338
339 _always() {
340 // Always run except if the test is done.
341 if (_testCase.isComplete) {
342 _testCase.error(
343 'Callback called after already being marked as done ($_calls)',
344 '');
345 _state = _UNCAUGHT_ERROR;
346 return false;
347 } else {
348 return true;
349 }
308 } 350 }
309 351
310 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, 352 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel,
311 arg3 = _sentinel, arg4 = _sentinel]) { 353 arg3 = _sentinel, arg4 = _sentinel]) {
312 return guardAsync(() { 354 return guardAsync(() {
313 if (!_incrementCall()) { 355 ++_calls;
356 if (!_shouldCallBack()) {
314 return; 357 return;
315 } else if (arg0 == _sentinel) { 358 } else if (arg0 == _sentinel) {
316 return callback(); 359 return _callback();
317 } else if (arg1 == _sentinel) { 360 } else if (arg1 == _sentinel) {
318 return callback(arg0); 361 return _callback(arg0);
319 } else if (arg2 == _sentinel) { 362 } else if (arg2 == _sentinel) {
320 return callback(arg0, arg1); 363 return _callback(arg0, arg1);
321 } else if (arg3 == _sentinel) { 364 } else if (arg3 == _sentinel) {
322 return callback(arg0, arg1, arg2); 365 return _callback(arg0, arg1, arg2);
323 } else if (arg4 == _sentinel) { 366 } else if (arg4 == _sentinel) {
324 return callback(arg0, arg1, arg2, arg3); 367 return _callback(arg0, arg1, arg2, arg3);
325 } else { 368 } else {
326 testCase.error( 369 _testCase.error(
327 'unittest lib does not support callbacks with more than 4 arguments', 370 'unittest lib does not support callbacks with more than 4 arguments',
328 ''); 371 '');
329 _state = _UNCAUGHT_ERROR; 372 _state = _UNCAUGHT_ERROR;
330 } 373 }
331 }, () { if (calls == expectedCalls) callbackDone(); }); 374 },
375 _after);
332 } 376 }
333 377
334 invoke0() { 378 invoke0() {
335 return guardAsync( 379 return guardAsync(
336 () => _incrementCall() ? callback() : null, 380 () {
337 () { if (calls == expectedCalls) callbackDone(); }); 381 ++_calls;
382 if (_shouldCallBack()) {
383 return _callback();
384 }
385 },
386 _after);
338 } 387 }
339 388
340 invoke1(arg1) { 389 invoke1(arg1) {
341 return guardAsync( 390 return guardAsync(
342 () => _incrementCall() ? callback(arg1) : null, 391 () {
343 () { if (calls == expectedCalls) callbackDone(); }); 392 ++_calls;
393 if (_shouldCallBack()) {
394 return _callback(arg1);
395 }
396 },
397 _after);
344 } 398 }
345 399
346 invoke2(arg1, arg2) { 400 invoke2(arg1, arg2) {
347 return guardAsync( 401 return guardAsync(
348 () => _incrementCall() ? callback(arg1, arg2) : null, 402 () {
349 () { if (calls == expectedCalls) callbackDone(); }); 403 ++_calls;
404 if (_shouldCallBack()) {
405 return _callback(arg1, arg2);
406 }
407 },
408 _after);
350 } 409 }
351 410
352 /** Returns false if we exceded the number of expected calls. */ 411 /** Returns false if we exceded the number of expected calls. */
353 bool _incrementCall() { 412 bool _checkCallCount() {
354 calls++; 413 if (_calls > _expectedCalls) {
355 if (calls > expectedCalls) { 414 _testCase.error(
356 testCase.error( 415 'Callback called more times than expected '
357 'Callback called more times than expected ($calls > $expectedCalls)', 416 '($_calls > $_expectedCalls)',
358 ''); 417 '');
359 _state = _UNCAUGHT_ERROR; 418 _state = _UNCAUGHT_ERROR;
360 return false; 419 return false;
361 } 420 }
362 return true; 421 return true;
363 } 422 }
364 } 423 }
365 424
366 /** 425 /**
367 * Indicate that [callback] is expected to be called a [count] number of times 426 * Indicate that [callback] is expected to be called a [count] number of times
368 * (by default 1). The unittest framework will wait for the callback to run the 427 * (by default 1). The unittest framework will wait for the callback to run the
369 * specified [count] times before it continues with the following test. Using 428 * specified [count] times before it continues with the following test. Using
370 * [_expectAsync] will also ensure that errors that occur within [callback] are 429 * [_expectAsync] will also ensure that errors that occur within [callback] are
371 * tracked and reported. [callback] should take between 0 and 4 positional 430 * tracked and reported. [callback] should take between 0 and 4 positional
372 * arguments (named arguments are not supported here). 431 * arguments (named arguments are not supported here).
373 */ 432 */
374 Function _expectAsync(Function callback, [int count = 1]) { 433 Function _expectAsync(Function callback, [int count = 1]) {
375 return new _SpreadArgsHelper(callback, count).invoke; 434 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke;
376 } 435 }
377 436
378 /** 437 /**
379 * Indicate that [callback] is expected to be called a [count] number of times 438 * Indicate that [callback] is expected to be called a [count] number of times
380 * (by default 1). The unittest framework will wait for the callback to run the 439 * (by default 1). The unittest framework will wait for the callback to run the
381 * specified [count] times before it continues with the following test. Using 440 * specified [count] times before it continues with the following test. Using
382 * [expectAsync0] will also ensure that errors that occur within [callback] are 441 * [expectAsync0] will also ensure that errors that occur within [callback] are
383 * tracked and reported. [callback] should take 0 positional arguments (named 442 * tracked and reported. [callback] should take 0 positional arguments (named
384 * arguments are not supported). 443 * arguments are not supported).
385 */ 444 */
386 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 445 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
387 Function expectAsync0(Function callback, [int count = 1]) { 446 Function expectAsync0(Function callback, [int count = 1]) {
388 return new _SpreadArgsHelper(callback, count).invoke0; 447 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke0;
389 } 448 }
390 449
391 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ 450 /** Like [expectAsync0] but [callback] should take 1 positional argument. */
392 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 451 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
393 Function expectAsync1(Function callback, [int count = 1]) { 452 Function expectAsync1(Function callback, [int count = 1]) {
394 return new _SpreadArgsHelper(callback, count).invoke1; 453 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke1;
395 } 454 }
396 455
397 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ 456 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */
398 // TODO(sigmund): deprecate this API when issue 2706 is fixed. 457 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
399 Function expectAsync2(Function callback, [int count = 1]) { 458 Function expectAsync2(Function callback, [int count = 1]) {
400 return new _SpreadArgsHelper(callback, count).invoke2; 459 return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke2;
460 }
461
462 /**
463 * Indicate that [callback] is expected to be called until [isDone] returns
464 * true. The unittest framework checks [isDone] after each callback and only
465 * when it returns true will it continue with the following test. Using
466 * [expectAsyncUntil] will also ensure that errors that occur within
467 * [callback] are tracked and reported. [callback] should take between 0 and
468 * 4 positional arguments (named arguments are not supported).
469 */
470 Function _expectAsyncUntil(Function callback, Function isDone) {
471 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke;
472 }
473
474 /**
475 * Indicate that [callback] is expected to be called until [isDone] returns
476 * true. The unittest framework check [isDone] after each callback and only
477 * when it returns true will it continue with the following test. Using
478 * [expectAsyncUntil0] will also ensure that errors that occur within
479 * [callback] are tracked and reported. [callback] should take 0 positional
480 * arguments (named arguments are not supported).
481 */
482 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
483 Function expectAsyncUntil0(Function callback, Function isDone) {
484 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke0;
485 }
486
487 /**
488 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument.
489 */
490 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
491 Function expectAsyncUntil1(Function callback, Function isDone) {
492 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke1;
493 }
494
495 /**
496 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments.
497 */
498 // TODO(sigmund): deprecate this API when issue 2706 is fixed.
499 Function expectAsyncUntil2(Function callback, Function isDone) {
500 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2;
401 } 501 }
402 502
403 /** 503 /**
404 * Creates a new named group of tests. Calls to group() or test() within the 504 * Creates a new named group of tests. Calls to group() or test() within the
405 * body of the function passed to this will inherit this group's description. 505 * body of the function passed to this will inherit this group's description.
406 */ 506 */
407 void group(String description, void body()) { 507 void group(String description, void body()) {
408 ensureInitialized(); 508 ensureInitialized();
409 509
410 // Concatenate the new group. 510 // Concatenate the new group.
411 final oldGroup = _currentGroup; 511 final oldGroup = _currentGroup;
412 if (_currentGroup != '') { 512 if (_currentGroup != '') {
413 // Add a space. 513 // Add a space.
414 _currentGroup = '$_currentGroup $description'; 514 _currentGroup = '$_currentGroup $description';
415 } else { 515 } else {
416 // The first group. 516 // The first group.
417 _currentGroup = description; 517 _currentGroup = description;
418 } 518 }
419 519
420 try { 520 try {
421 body(); 521 body();
422 } finally { 522 } finally {
423 // Now that the group is over, restore the previous one. 523 // Now that the group is over, restore the previous one.
424 _currentGroup = oldGroup; 524 _currentGroup = oldGroup;
425 } 525 }
426 } 526 }
427 527
428 /** Called by subclasses to indicate that an asynchronous test completed. */ 528 /** Called by subclasses to indicate that an asynchronous test completed. */
429 void callbackDone() { 529 void _handleAllCallbacksDone() {
430 // TODO (gram): we defer this to give the nextBatch recursive 530 // TODO (gram): we defer this to give the nextBatch recursive
431 // stack a chance to unwind. This is a temporary hack but 531 // stack a chance to unwind. This is a temporary hack but
432 // really a bunch of code here needs to be fixed. We have a 532 // really a bunch of code here needs to be fixed. We have a
433 // single array that is being iterated through by nextBatch(), 533 // single array that is being iterated through by nextBatch(),
434 // which is recursively invoked in the case of async tests that 534 // which is recursively invoked in the case of async tests that
435 // run synchronously. Bad things can then happen. 535 // run synchronously. Bad things can then happen.
436 _defer(() { 536 _defer(() {
437 _callbacksCalled++; 537 _callbacksCalled++;
438 if (_currentTest < _tests.length) { 538 if (_currentTest < _tests.length) {
439 final testCase = _tests[_currentTest]; 539 final testCase = _tests[_currentTest];
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 * Runs a batch of tests, yielding whenever an asynchronous test starts 635 * Runs a batch of tests, yielding whenever an asynchronous test starts
536 * running. Tests will resume executing when such asynchronous test calls 636 * running. Tests will resume executing when such asynchronous test calls
537 * [done] or if it fails with an exception. 637 * [done] or if it fails with an exception.
538 */ 638 */
539 _nextBatch() { 639 _nextBatch() {
540 while (_currentTest < _tests.length) { 640 while (_currentTest < _tests.length) {
541 final testCase = _tests[_currentTest]; 641 final testCase = _tests[_currentTest];
542 guardAsync(() { 642 guardAsync(() {
543 _callbacksCalled = 0; 643 _callbacksCalled = 0;
544 _state = _RUNNING_TEST; 644 _state = _RUNNING_TEST;
545
546 testCase.test(); 645 testCase.test();
547 646
548 if (_state != _UNCAUGHT_ERROR) { 647 if (_state != _UNCAUGHT_ERROR) {
549 if (testCase.callbacks == _callbacksCalled) { 648 if (testCase.callbacks == _callbacksCalled) {
550 testCase.pass(); 649 testCase.pass();
551 } 650 }
552 } 651 }
553 }); 652 });
554 653
555 if (!testCase.isComplete && testCase.callbacks > 0) return; 654 if (!testCase.isComplete && testCase.callbacks > 0) return;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 throw new ExpectException(message); 688 throw new ExpectException(message);
590 } 689 }
591 690
592 /** 691 /**
593 * Lazily initializes the test library if not already initialized. 692 * Lazily initializes the test library if not already initialized.
594 */ 693 */
595 ensureInitialized() { 694 ensureInitialized() {
596 if (_state != _UNINITIALIZED) return; 695 if (_state != _UNINITIALIZED) return;
597 696
598 _tests = <TestCase>[]; 697 _tests = <TestCase>[];
698 _uncaughtErrorMessage = null;
699 _currentTest = 0;
599 _currentGroup = ''; 700 _currentGroup = '';
600 _state = _READY; 701 _state = _READY;
601 _testRunner = _nextBatch; 702 _testRunner = _nextBatch;
602 703
603 if (_config == null) { 704 if (_config == null) {
604 _config = new Configuration(); 705 _config = new Configuration();
605 } 706 }
606 _config.onInit(); 707 _config.onInit();
607 708
608 // Immediately queue the suite up. It will run after a timeout (i.e. after 709 // Immediately queue the suite up. It will run after a timeout (i.e. after
609 // main() has returned). 710 // main() has returned).
610 _defer(_runTests); 711 _defer(_runTests);
611 } 712 }
612 713
613 /** Signature for a test function. */ 714 /** Signature for a test function. */
614 typedef void TestFunction(); 715 typedef void TestFunction();
716
OLDNEW
« no previous file with comments | « lib/unittest/expect.dart ('k') | tests/html/audiocontext_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698