OLD | NEW |
---|---|
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, use the pub package manager. | 8 * To import this library, use the pub package manager. |
9 * Create a pubspec.yaml file in your project and add | 9 * Create a pubspec.yaml file in your project and add |
10 * a dependency on unittest with the following lines: | 10 * a dependency on unittest with the following lines: |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 /** Sentinel value for [_SpreadArgsHelper]. */ | 281 /** Sentinel value for [_SpreadArgsHelper]. */ |
282 class _Sentinel { | 282 class _Sentinel { |
283 const _Sentinel(); | 283 const _Sentinel(); |
284 } | 284 } |
285 | 285 |
286 /** Simulates spread arguments using named arguments. */ | 286 /** Simulates spread arguments using named arguments. */ |
287 // TODO(sigmund): remove this class and simply use a closure with named | 287 // TODO(sigmund): remove this class and simply use a closure with named |
288 // arguments (if still applicable). | 288 // arguments (if still applicable). |
289 class _SpreadArgsHelper { | 289 class _SpreadArgsHelper { |
290 Function _callback; | 290 Function _callback; |
291 int _expectedCalls; | 291 int _minExpectedCalls; |
Siggi Cherem (dart-lang)
2013/02/28 19:29:23
Now that we don't have _init, can we make some of
gram
2013/02/28 22:49:50
Done.
| |
292 int _maxExpectedCalls; | |
292 int _actualCalls = 0; | 293 int _actualCalls = 0; |
293 int _testNum; | 294 int _testNum; |
294 TestCase _testCase; | 295 TestCase _testCase; |
295 Function _shouldCallBack; | |
296 Function _isDone; | 296 Function _isDone; |
297 String _id; | 297 String _id; |
298 bool _complete; | |
Siggi Cherem (dart-lang)
2013/02/28 19:29:23
since this class is private and we don't return it
gram
2013/02/28 22:49:50
Done.
| |
298 static const _sentinel = const _Sentinel(); | 299 static const _sentinel = const _Sentinel(); |
299 | 300 |
300 _init(Function callback, Function shouldCallBack, Function isDone, | 301 _SpreadArgsHelper(Function callback, int minExpected, int maxExpected, |
Siggi Cherem (dart-lang)
2013/02/28 19:29:23
more precisely:
_SpreadArgsHelper(Function callbac
gram
2013/02/28 22:49:50
Done.
| |
301 [expectedCalls = 0]) { | 302 Function isDone, String id) { |
302 ensureInitialized(); | 303 ensureInitialized(); |
303 if (!(_currentTest >= 0 && | 304 if (!(_currentTest >= 0 && |
304 _currentTest < _tests.length && | 305 _currentTest < _tests.length && |
305 _tests[_currentTest] != null)) { | 306 _tests[_currentTest] != null)) { |
306 print("No valid test, did you forget to run your test inside a call " | 307 print("No valid test, did you forget to run your test inside a call " |
307 "to test()?"); | 308 "to test()?"); |
308 } | 309 } |
309 assert(_currentTest >= 0 && | 310 assert(_currentTest >= 0 && |
310 _currentTest < _tests.length && | 311 _currentTest < _tests.length && |
311 _tests[_currentTest] != null); | 312 _tests[_currentTest] != null); |
312 _callback = callback; | 313 _callback = callback; |
313 _shouldCallBack = shouldCallBack; | 314 _minExpectedCalls = minExpected; |
315 if (maxExpected == 0 && minExpected > 0) { | |
316 _maxExpectedCalls = minExpected; | |
317 } else { | |
318 _maxExpectedCalls = maxExpected; | |
319 } | |
314 _isDone = isDone; | 320 _isDone = isDone; |
315 _expectedCalls = expectedCalls; | |
316 _testNum = _currentTest; | 321 _testNum = _currentTest; |
317 _testCase = _tests[_currentTest]; | 322 _testCase = _tests[_currentTest]; |
318 if (expectedCalls > 0) { | 323 if (isDone != null || minExpected > 0) { |
319 _testCase.callbackFunctionsOutstanding++; | 324 _testCase.callbackFunctionsOutstanding++; |
325 _complete = false; | |
326 } else { | |
327 _complete = true; | |
320 } | 328 } |
329 | |
330 // Try to create a reasonable id. | |
321 _id = ''; | 331 _id = ''; |
322 // If the callback is not an anonymous closure, try to get the | 332 if (id != null) { |
323 // name. | 333 _id = "$id "; |
324 var fname = callback.toString(); | 334 } else { |
325 var prefix = "Function '"; | 335 // If the callback is not an anonymous closure, try to get the |
326 var pos = fname.indexOf(prefix); | 336 // name. |
327 if (pos > 0) { | 337 var fname = callback.toString(); |
328 pos += prefix.length; | 338 var prefix = "Function '"; |
329 var epos = fname.indexOf("'", pos); | 339 var pos = fname.indexOf(prefix); |
330 if (epos > 0) { | 340 if (pos > 0) { |
331 _id = "${fname.substring(pos, epos)} "; | 341 pos += prefix.length; |
342 var epos = fname.indexOf("'", pos); | |
343 if (epos > 0) { | |
344 _id = "${fname.substring(pos, epos)} "; | |
345 } | |
332 } | 346 } |
333 } | 347 } |
334 } | 348 } |
335 | 349 |
336 _SpreadArgsHelper(callback, shouldCallBack, isDone) { | 350 _shouldCallBack() { |
337 _init(callback, shouldCallBack, isDone); | 351 ++_actualCalls; |
352 if (_testCase.isComplete) { | |
353 // Don't run if the test is done. We don't throw here as this is not | |
354 // the current test, but we do mark the old test as having an error | |
355 // if it previously passed. | |
356 if (_testCase.result == PASS) { | |
357 _testCase.error( | |
358 'Callback ${_id}called after test case ${_testCase.description} ' | |
359 'has already been marked as done.', | |
360 ''); | |
361 } | |
362 return false; | |
363 } else if (_maxExpectedCalls >= 0 && _actualCalls > _maxExpectedCalls) { | |
364 throw new TestFailure('Callback ${_id}called more times than expected ' | |
365 '($_maxExpectedCalls).'); | |
366 } | |
367 return true; | |
338 } | 368 } |
339 | 369 |
340 _SpreadArgsHelper.fixedCallCount(callback, expectedCalls, id) { | |
341 _init(callback, _checkCallCount, _allCallsDone, expectedCalls); | |
342 if (id != null) { | |
343 _id = "$id "; | |
344 } | |
345 } | |
346 | |
347 _SpreadArgsHelper.variableCallCount(callback, isDone) { | |
348 _init(callback, _always, isDone, 1); | |
349 } | |
350 | |
351 _SpreadArgsHelper.optionalCalls(callback) { | |
352 _init(callback, _always, () => false, 0); | |
353 } | |
354 | |
355 _after() { | 370 _after() { |
356 if (_isDone()) { | 371 if (!_complete) { |
357 _handleCallbackFunctionComplete(_testNum, _id); | 372 if (_minExpectedCalls > 0 && _actualCalls < _minExpectedCalls) { |
358 } | 373 return; |
359 } | 374 } |
360 | 375 if (_isDone != null && !_isDone()) { |
361 _allCallsDone() => _actualCalls == _expectedCalls; | 376 return; |
362 | 377 } |
363 _always() { | 378 // Mark this callback as complete and remove it from the testcase |
364 // Always run except if the test is done. | 379 // oustanding callback count; if that hits zero the testcase is done. |
365 if (_testCase.isComplete) { | 380 _complete = true; |
366 _testCase.error( | 381 if (--_testCase.callbackFunctionsOutstanding == 0 && |
367 'Callback ${_id}called after already being marked ' | 382 !_testCase.isComplete) { |
368 'as done ($_actualCalls).', | 383 _testCase.pass(); |
369 ''); | 384 } |
370 return false; | |
371 } else { | |
372 return true; | |
373 } | 385 } |
374 } | 386 } |
375 | 387 |
376 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, | 388 invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel, |
377 arg3 = _sentinel, arg4 = _sentinel]) { | 389 arg3 = _sentinel, arg4 = _sentinel]) { |
378 return guardAsync(() { | 390 return guardAsync(() { |
379 ++_actualCalls; | |
380 if (!_shouldCallBack()) { | 391 if (!_shouldCallBack()) { |
381 return; | 392 return; |
382 } else if (arg0 == _sentinel) { | 393 } else if (arg0 == _sentinel) { |
383 return _callback(); | 394 return _callback(); |
384 } else if (arg1 == _sentinel) { | 395 } else if (arg1 == _sentinel) { |
385 return _callback(arg0); | 396 return _callback(arg0); |
386 } else if (arg2 == _sentinel) { | 397 } else if (arg2 == _sentinel) { |
387 return _callback(arg0, arg1); | 398 return _callback(arg0, arg1); |
388 } else if (arg3 == _sentinel) { | 399 } else if (arg3 == _sentinel) { |
389 return _callback(arg0, arg1, arg2); | 400 return _callback(arg0, arg1, arg2); |
390 } else if (arg4 == _sentinel) { | 401 } else if (arg4 == _sentinel) { |
391 return _callback(arg0, arg1, arg2, arg3); | 402 return _callback(arg0, arg1, arg2, arg3); |
392 } else { | 403 } else { |
393 _testCase.error( | 404 _testCase.error( |
394 'unittest lib does not support callbacks with more than' | 405 'unittest lib does not support callbacks with more than' |
395 ' 4 arguments.', | 406 ' 4 arguments.', |
396 ''); | 407 ''); |
397 } | 408 } |
398 }, | 409 }, |
399 _after, _testNum); | 410 _after, _testNum); |
400 } | 411 } |
401 | 412 |
402 invoke0() { | 413 invoke0() { |
403 return guardAsync( | 414 return guardAsync( |
404 () { | 415 () { |
405 ++_actualCalls; | |
406 if (_shouldCallBack()) { | 416 if (_shouldCallBack()) { |
407 return _callback(); | 417 return _callback(); |
408 } | 418 } |
409 }, | 419 }, |
410 _after, _testNum); | 420 _after, _testNum); |
411 } | 421 } |
412 | 422 |
413 invoke1(arg1) { | 423 invoke1(arg1) { |
414 return guardAsync( | 424 return guardAsync( |
415 () { | 425 () { |
416 ++_actualCalls; | |
417 if (_shouldCallBack()) { | 426 if (_shouldCallBack()) { |
418 return _callback(arg1); | 427 return _callback(arg1); |
419 } | 428 } |
420 }, | 429 }, |
421 _after, _testNum); | 430 _after, _testNum); |
422 } | 431 } |
423 | 432 |
424 invoke2(arg1, arg2) { | 433 invoke2(arg1, arg2) { |
425 return guardAsync( | 434 return guardAsync( |
426 () { | 435 () { |
427 ++_actualCalls; | |
428 if (_shouldCallBack()) { | 436 if (_shouldCallBack()) { |
429 return _callback(arg1, arg2); | 437 return _callback(arg1, arg2); |
430 } | 438 } |
431 }, | 439 }, |
432 _after, _testNum); | 440 _after, _testNum); |
433 } | 441 } |
434 | |
435 /** Returns false if we exceded the number of expected calls. */ | |
436 bool _checkCallCount() { | |
437 if (_actualCalls > _expectedCalls) { | |
438 _testCase.error('Callback ${_id}called more times than expected ' | |
439 '($_actualCalls > $_expectedCalls).', ''); | |
440 return false; | |
441 } | |
442 return true; | |
443 } | |
444 } | 442 } |
445 | 443 |
446 /** | 444 /** |
447 * Indicate that [callback] is expected to be called a [count] number of times | 445 * Indicate that [callback] is expected to be called a [count] number of times |
448 * (by default 1). The unittest framework will wait for the callback to run the | 446 * (by default 1). The unittest framework will wait for the callback to run the |
449 * specified [count] times before it continues with the following test. Using | 447 * specified [count] times before it continues with the following test. Using |
450 * [_expectAsync] will also ensure that errors that occur within [callback] are | 448 * [_expectAsync] will also ensure that errors that occur within [callback] are |
451 * tracked and reported. [callback] should take between 0 and 4 positional | 449 * tracked and reported. [callback] should take between 0 and 4 positional |
452 * arguments (named arguments are not supported here). [id] can be used | 450 * arguments (named arguments are not supported here). [id] can be used |
453 * to provide more descriptive error messages if the callback is called more | 451 * to provide more descriptive error messages if the callback is called more |
454 * often than expected. | 452 * often than expected. |
455 */ | 453 */ |
456 Function _expectAsync(Function callback, {int count: 1, String id}) { | 454 Function _expectAsync(Function callback, |
457 return new _SpreadArgsHelper. | 455 {int count: 1, int max: 0, String id}) { |
458 fixedCallCount(callback, count, id).invoke; | 456 return new _SpreadArgsHelper(callback, count, max, null, id).invoke; |
459 } | 457 } |
460 | 458 |
461 /** | 459 /** |
462 * Indicate that [callback] is expected to be called a [count] number of times | 460 * Indicate that [callback] is expected to be called a [count] number of times |
463 * (by default 1). The unittest framework will wait for the callback to run the | 461 * (by default 1). The unittest framework will wait for the callback to run the |
464 * specified [count] times before it continues with the following test. Using | 462 * specified [count] times before it continues with the following test. Using |
465 * [expectAsync0] will also ensure that errors that occur within [callback] are | 463 * [expectAsync0] will also ensure that errors that occur within [callback] are |
466 * tracked and reported. [callback] should take 0 positional arguments (named | 464 * tracked and reported. [callback] should take 0 positional arguments (named |
467 * arguments are not supported). [id] can be used to provide more | 465 * arguments are not supported). [id] can be used to provide more |
468 * descriptive error messages if the callback is called more often than | 466 * descriptive error messages if the callback is called more often than |
469 * expected. | 467 * expected. [max] can be used to specify an upper bound on the number of |
468 * calls; if this is exceeded the test will fail (or be marked as in error if | |
469 * it was already complete). A value of 0 for [max] (the default) will set | |
470 * the upper bound to the same value as [count]; i.e. the callback should be | |
471 * called exactly [count] times. A value of -1 for [max] will mean no upper | |
472 * bound. | |
470 */ | 473 */ |
471 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 474 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
472 Function expectAsync0(Function callback, {int count: 1, String id}) { | 475 Function expectAsync0(Function callback, |
473 return new _SpreadArgsHelper. | 476 {int count: 1, int max: 0, String id}) { |
474 fixedCallCount(callback, count, id).invoke0; | 477 return new _SpreadArgsHelper(callback, count, max, null, id).invoke0; |
475 } | 478 } |
476 | 479 |
477 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ | 480 /** Like [expectAsync0] but [callback] should take 1 positional argument. */ |
478 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 481 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
479 Function expectAsync1(Function callback, {int count: 1, String id}) { | 482 Function expectAsync1(Function callback, |
480 return new _SpreadArgsHelper. | 483 {int count: 1, int max: 0, String id}) { |
481 fixedCallCount(callback, count, id).invoke1; | 484 return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; |
482 } | 485 } |
483 | 486 |
484 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ | 487 /** Like [expectAsync0] but [callback] should take 2 positional arguments. */ |
485 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 488 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
486 Function expectAsync2(Function callback, {int count: 1, String id}) { | 489 Function expectAsync2(Function callback, |
487 return new _SpreadArgsHelper. | 490 {int count: 1, int max: 0, String id}) { |
488 fixedCallCount(callback, count, id).invoke2; | 491 return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; |
489 } | 492 } |
490 | 493 |
491 /** | 494 /** |
492 * Indicate that [callback] is expected to be called until [isDone] returns | 495 * Indicate that [callback] is expected to be called until [isDone] returns |
493 * true. The unittest framework checks [isDone] after each callback and only | 496 * true. The unittest framework checks [isDone] after each callback and only |
494 * when it returns true will it continue with the following test. Using | 497 * when it returns true will it continue with the following test. Using |
495 * [expectAsyncUntil] will also ensure that errors that occur within | 498 * [expectAsyncUntil] will also ensure that errors that occur within |
496 * [callback] are tracked and reported. [callback] should take between 0 and | 499 * [callback] are tracked and reported. [callback] should take between 0 and |
497 * 4 positional arguments (named arguments are not supported). | 500 * 4 positional arguments (named arguments are not supported). [id] can be |
501 * used to identify the callback in error messages (for example if it is called | |
502 * after the test case is complete). | |
498 */ | 503 */ |
499 Function _expectAsyncUntil(Function callback, Function isDone) { | 504 Function _expectAsyncUntil(Function callback, Function isDone, {String id}) { |
500 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke; | 505 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke; |
Siggi Cherem (dart-lang)
2013/02/28 19:29:23
strange - I was expecting that max was always >= 0
gram
2013/02/28 22:49:50
-1 means unlimited
| |
501 } | 506 } |
502 | 507 |
503 /** | 508 /** |
504 * Indicate that [callback] is expected to be called until [isDone] returns | 509 * Indicate that [callback] is expected to be called until [isDone] returns |
505 * true. The unittest framework check [isDone] after each callback and only | 510 * true. The unittest framework check [isDone] after each callback and only |
506 * when it returns true will it continue with the following test. Using | 511 * when it returns true will it continue with the following test. Using |
507 * [expectAsyncUntil0] will also ensure that errors that occur within | 512 * [expectAsyncUntil0] will also ensure that errors that occur within |
508 * [callback] are tracked and reported. [callback] should take 0 positional | 513 * [callback] are tracked and reported. [callback] should take 0 positional |
509 * arguments (named arguments are not supported). | 514 * arguments (named arguments are not supported). [id] can be used to |
515 * identify the callback in error messages (for example if it is called | |
516 * after the test case is complete). | |
510 */ | 517 */ |
511 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 518 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
512 Function expectAsyncUntil0(Function callback, Function isDone) { | 519 Function expectAsyncUntil0(Function callback, Function isDone, {String id}) { |
513 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke0; | 520 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; |
514 } | 521 } |
515 | 522 |
516 /** | 523 /** |
517 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument. | 524 * Like [expectAsyncUntil0] but [callback] should take 1 positional argument. |
518 */ | 525 */ |
519 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 526 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
520 Function expectAsyncUntil1(Function callback, Function isDone) { | 527 Function expectAsyncUntil1(Function callback, Function isDone, {String id}) { |
521 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke1; | 528 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; |
522 } | 529 } |
523 | 530 |
524 /** | 531 /** |
525 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. | 532 * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments. |
526 */ | 533 */ |
527 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 534 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
528 Function expectAsyncUntil2(Function callback, Function isDone) { | 535 Function expectAsyncUntil2(Function callback, Function isDone, {String id}) { |
529 return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke2; | 536 return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; |
530 } | 537 } |
531 | 538 |
532 /** | 539 /** |
533 * Wraps the [callback] in a new function and returns that function. The new | 540 * Wraps the [callback] in a new function and returns that function. The new |
534 * function will be able to handle exceptions by directing them to the correct | 541 * function will be able to handle exceptions by directing them to the correct |
535 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that | 542 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that |
536 * might optionally be called but may never be called during the test. | 543 * might optionally be called but may never be called during the test. |
537 * [callback] should take between 0 and 4 positional arguments (named arguments | 544 * [callback] should take between 0 and 4 positional arguments (named arguments |
538 * are not supported). | 545 * are not supported). [id] can be used to identify the callback in error |
546 * messages (for example if it is called after the test case is complete). | |
539 */ | 547 */ |
540 Function _protectAsync(Function callback) { | 548 Function _protectAsync(Function callback, {String id}) { |
541 return new _SpreadArgsHelper.optionalCalls(callback).invoke; | 549 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke; |
542 } | 550 } |
543 | 551 |
544 /** | 552 /** |
545 * Wraps the [callback] in a new function and returns that function. The new | 553 * Wraps the [callback] in a new function and returns that function. The new |
546 * function will be able to handle exceptions by directing them to the correct | 554 * function will be able to handle exceptions by directing them to the correct |
547 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that | 555 * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that |
548 * might optionally be called but may never be called during the test. | 556 * might optionally be called but may never be called during the test. |
549 * [callback] should take 0 positional arguments (named arguments are not | 557 * [callback] should take 0 positional arguments (named arguments are not |
550 * supported). | 558 * supported). [id] can be used to identify the callback in error |
559 * messages (for example if it is called after the test case is complete). | |
551 */ | 560 */ |
552 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 561 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
553 Function protectAsync0(Function callback) { | 562 Function protectAsync0(Function callback, {String id}) { |
554 return new _SpreadArgsHelper.optionalCalls(callback).invoke0; | 563 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke0; |
555 } | 564 } |
556 | 565 |
557 /** | 566 /** |
558 * Like [protectAsync0] but [callback] should take 1 positional argument. | 567 * Like [protectAsync0] but [callback] should take 1 positional argument. |
559 */ | 568 */ |
560 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 569 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
561 Function protectAsync1(Function callback) { | 570 Function protectAsync1(Function callback, {String id}) { |
562 return new _SpreadArgsHelper.optionalCalls(callback).invoke1; | 571 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke1; |
563 } | 572 } |
564 | 573 |
565 /** | 574 /** |
566 * Like [protectAsync0] but [callback] should take 2 positional arguments. | 575 * Like [protectAsync0] but [callback] should take 2 positional arguments. |
567 */ | 576 */ |
568 // TODO(sigmund): deprecate this API when issue 2706 is fixed. | 577 // TODO(sigmund): deprecate this API when issue 2706 is fixed. |
569 Function protectAsync2(Function callback) { | 578 Function protectAsync2(Function callback, {String id}) { |
570 return new _SpreadArgsHelper.optionalCalls(callback).invoke2; | 579 return new _SpreadArgsHelper(callback, 0, -1, null, id).invoke2; |
571 } | 580 } |
572 | 581 |
573 /** | 582 /** |
574 * Creates a new named group of tests. Calls to group() or test() within the | 583 * Creates a new named group of tests. Calls to group() or test() within the |
575 * body of the function passed to this will inherit this group's description. | 584 * body of the function passed to this will inherit this group's description. |
576 */ | 585 */ |
577 void group(String description, void body()) { | 586 void group(String description, void body()) { |
578 ensureInitialized(); | 587 ensureInitialized(); |
579 // Concatenate the new group. | 588 // Concatenate the new group. |
580 final parentGroup = _currentGroup; | 589 final parentGroup = _currentGroup; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 * be called after each test in the group is run. Note that if groups | 632 * be called after each test in the group is run. Note that if groups |
624 * are nested only the most locally scoped [teardownTest] function will be run. | 633 * are nested only the most locally scoped [teardownTest] function will be run. |
625 * [setUp] and [tearDown] should be called within the [group] before any | 634 * [setUp] and [tearDown] should be called within the [group] before any |
626 * calls to [test]. The [teardownTest] function can be asynchronous; in this | 635 * calls to [test]. The [teardownTest] function can be asynchronous; in this |
627 * case it must return a [Future]. | 636 * case it must return a [Future]. |
628 */ | 637 */ |
629 void tearDown(Function teardownTest) { | 638 void tearDown(Function teardownTest) { |
630 _testTeardown = teardownTest; | 639 _testTeardown = teardownTest; |
631 } | 640 } |
632 | 641 |
633 /** | |
634 * Called when one of the callback functions is done with all expected | |
635 * calls. | |
636 */ | |
637 void _handleCallbackFunctionComplete(testNum, [id = '']) { | |
638 // TODO (gram): we defer this to give the nextBatch recursive | |
639 // stack a chance to unwind. This is a temporary hack but | |
640 // really a bunch of code here needs to be fixed. We have a | |
641 // single array that is being iterated through by nextBatch(), | |
642 // which is recursively invoked in the case of async tests that | |
643 // run synchronously. Bad things can then happen. | |
644 _defer(() { | |
645 if (_currentTest != testNum) { | |
646 if (_tests[testNum].result == PASS) { | |
647 _tests[testNum].error("${id}Unexpected extra callbacks", ''); | |
648 } | |
649 } else if (_currentTest < _tests.length) { | |
650 final testCase = _tests[_currentTest]; | |
651 --testCase.callbackFunctionsOutstanding; | |
652 if (testCase.callbackFunctionsOutstanding < 0) { | |
653 // TODO(gram): Check: Can this even happen? | |
654 testCase.error( | |
655 'More calls to _handleCallbackFunctionComplete() than expected.', | |
656 ''); | |
657 } else if (testCase.callbackFunctionsOutstanding == 0 && | |
658 !testCase.isComplete) { | |
659 testCase.pass(); | |
660 } | |
661 } | |
662 }); | |
663 } | |
664 | |
665 /** Advance to the next test case. */ | 642 /** Advance to the next test case. */ |
666 void _nextTestCase() { | 643 void _nextTestCase() { |
667 _defer(() { | 644 _defer(() { |
668 _currentTest++; | 645 _currentTest++; |
669 _testRunner(); | 646 _testRunner(); |
670 }); | 647 }); |
671 } | 648 } |
672 | 649 |
673 /** | 650 /** |
674 * Temporary hack: expose old API. | |
675 * TODO(gram) remove this when WebKit tests are working with new framework | |
676 */ | |
677 void callbackDone() { | |
678 _handleCallbackFunctionComplete(_currentTest); | |
679 } | |
680 | |
681 /** | |
682 * Utility function that can be used to notify the test framework that an | 651 * Utility function that can be used to notify the test framework that an |
683 * error was caught outside of this library. | 652 * error was caught outside of this library. |
684 */ | 653 */ |
685 void _reportTestError(String msg, String trace) { | 654 void _reportTestError(String msg, String trace) { |
686 if (_currentTest < _tests.length) { | 655 if (_currentTest < _tests.length) { |
687 final testCase = _tests[_currentTest]; | 656 final testCase = _tests[_currentTest]; |
688 testCase.error(msg, trace); | 657 testCase.error(msg, trace); |
689 } else { | 658 } else { |
690 _uncaughtErrorMessage = "$msg: $trace"; | 659 _uncaughtErrorMessage = "$msg: $trace"; |
691 } | 660 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
765 */ | 734 */ |
766 registerException(e, [trace]) { | 735 registerException(e, [trace]) { |
767 _registerException(_currentTest, e, trace); | 736 _registerException(_currentTest, e, trace); |
768 } | 737 } |
769 | 738 |
770 /** | 739 /** |
771 * Registers that an exception was caught for the current test. | 740 * Registers that an exception was caught for the current test. |
772 */ | 741 */ |
773 _registerException(testNum, e, [trace]) { | 742 _registerException(testNum, e, [trace]) { |
774 trace = trace == null ? '' : trace.toString(); | 743 trace = trace == null ? '' : trace.toString(); |
744 String message = (e is TestFailure) ? e.message : 'Caught $e'; | |
775 if (_tests[testNum].result == null) { | 745 if (_tests[testNum].result == null) { |
776 String message = (e is TestFailure) ? e.message : 'Caught $e'; | |
777 _tests[testNum].fail(message, trace); | 746 _tests[testNum].fail(message, trace); |
778 } else { | 747 } else { |
779 _tests[testNum].error('Caught $e', trace); | 748 _tests[testNum].error(message, trace); |
780 } | 749 } |
781 } | 750 } |
782 | 751 |
783 /** | 752 /** |
784 * Runs a batch of tests, yielding whenever an asynchronous test starts | 753 * Runs a batch of tests, yielding whenever an asynchronous test starts |
785 * running. Tests will resume executing when such asynchronous test calls | 754 * running. Tests will resume executing when such asynchronous test calls |
786 * [done] or if it fails with an exception. | 755 * [done] or if it fails with an exception. |
787 */ | 756 */ |
788 _nextBatch() { | 757 _nextBatch() { |
789 while (true) { | 758 while (true) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
881 } | 850 } |
882 | 851 |
883 /** Enable a test by ID. */ | 852 /** Enable a test by ID. */ |
884 void enableTest(int testId) => _setTestEnabledState(testId, true); | 853 void enableTest(int testId) => _setTestEnabledState(testId, true); |
885 | 854 |
886 /** Disable a test by ID. */ | 855 /** Disable a test by ID. */ |
887 void disableTest(int testId) => _setTestEnabledState(testId, false); | 856 void disableTest(int testId) => _setTestEnabledState(testId, false); |
888 | 857 |
889 /** Signature for a test function. */ | 858 /** Signature for a test function. */ |
890 typedef dynamic TestFunction(); | 859 typedef dynamic TestFunction(); |
OLD | NEW |