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

Side by Side Diff: test/backend/invoker_test.dart

Issue 1116443002: Support future matchers and expectAsync in tearDowns. (Closed) Base URL: git@github.com:dart-lang/unittest.git@master
Patch Set: Created 5 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:fake_async/fake_async.dart'; 7 import 'package:fake_async/fake_async.dart';
8 import 'package:test/src/backend/invoker.dart'; 8 import 'package:test/src/backend/invoker.dart';
9 import 'package:test/src/backend/metadata.dart'; 9 import 'package:test/src/backend/metadata.dart';
10 import 'package:test/src/backend/state.dart'; 10 import 'package:test/src/backend/state.dart';
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 }, tearDown: () { 422 }, tearDown: () {
423 stateDuringTearDown = liveTest.state; 423 stateDuringTearDown = liveTest.state;
424 }).load(suite); 424 }).load(suite);
425 425
426 expectSingleError(liveTest); 426 expectSingleError(liveTest);
427 return liveTest.run().then((_) { 427 return liveTest.run().then((_) {
428 expect(stateDuringTearDown, 428 expect(stateDuringTearDown,
429 equals(const State(Status.complete, Result.error))); 429 equals(const State(Status.complete, Result.error)));
430 }); 430 });
431 }); 431 });
432
433 test("an asynchronous error in tearDown causes the test to error", () {
434 var liveTest = _localTest(() {}, tearDown: () {
435 Invoker.current.addOutstandingCallback();
436 new Future(() => throw "oh no");
437 }).load(suite);
438
439 expectSingleError(liveTest);
440 return liveTest.run();
441 });
442
443 test("an error reported in the test body after tearDown begins running "
444 "doesn't stop tearDown", () {
445 var tearDownComplete = false;;
446 var completer = new Completer();
447
448 var liveTest;
449 liveTest = _localTest(() {
450 completer.future.then((_) => throw "not again");
451 throw "oh no";
452 }, tearDown: () {
453 completer.complete();
454
455 // Pump the event queue so that we will run the following code after the
456 // test body has thrown a second error.
457 Invoker.current.addOutstandingCallback();
458 pumpEventQueue().then((_) {
459 Invoker.current.removeOutstandingCallback();
460 tearDownComplete = true;
461 });
462 }).load(suite);
463
464 expectStates(liveTest, [
465 const State(Status.running, Result.success),
466 const State(Status.complete, Result.error)
467 ]);
468
469 expectErrors(liveTest, [
470 (error) {
471 expect(lastState.status, equals(Status.complete));
472 expect(error, equals("oh no"));
473 },
474 (error) => expect(error, equals("not again"))
475 ]);
476
477 return liveTest.run().then((_) {
478 expect(tearDownComplete, isTrue);
479 });
480 });
432 }); 481 });
433 482
434 test("a test doesn't complete until there are no outstanding callbacks", 483 test("a test doesn't complete until there are no outstanding callbacks",
435 () { 484 () {
436 var outstandingCallbackRemoved = false; 485 var outstandingCallbackRemoved = false;
437 var liveTest = _localTest(() { 486 var liveTest = _localTest(() {
438 Invoker.current.addOutstandingCallback(); 487 Invoker.current.addOutstandingCallback();
439 488
440 // Pump the event queue to make sure the test isn't coincidentally 489 // Pump the event queue to make sure the test isn't coincidentally
441 // completing after the outstanding callback is removed. 490 // completing after the outstanding callback is removed.
(...skipping 24 matching lines...) Expand all
466 outstandingCallbackRemovedBeforeTeardown = outstandingCallbackRemoved; 515 outstandingCallbackRemovedBeforeTeardown = outstandingCallbackRemoved;
467 }).load(suite); 516 }).load(suite);
468 517
469 liveTest.onError.listen(expectAsync((_) {}, count: 0)); 518 liveTest.onError.listen(expectAsync((_) {}, count: 0));
470 519
471 return liveTest.run().then((_) { 520 return liveTest.run().then((_) {
472 expect(outstandingCallbackRemovedBeforeTeardown, isTrue); 521 expect(outstandingCallbackRemovedBeforeTeardown, isTrue);
473 }); 522 });
474 }); 523 });
475 524
525 test("a test's tearDown doesn't complete until there are no outstanding "
526 "callbacks", () {
527 var outstandingCallbackRemoved = false;
528 var liveTest = _localTest(() {}, tearDown: () {
529 Invoker.current.addOutstandingCallback();
530
531 // Pump the event queue to make sure the test isn't coincidentally
532 // completing after the outstanding callback is removed.
533 pumpEventQueue().then((_) {
534 outstandingCallbackRemoved = true;
535 Invoker.current.removeOutstandingCallback();
536 });
537 }).load(suite);
538
539 liveTest.onError.listen(expectAsync((_) {}, count: 0));
540
541 return liveTest.run().then((_) {
542 expect(outstandingCallbackRemoved, isTrue);
543 });
544 });
545
546 test("a test body's outstanding callbacks can't complete its tearDown", () {
547 var outstandingCallbackRemoved = false;
548 var completer = new Completer();
549 var liveTest = _localTest(() {
550 // Once the tearDown runs, remove an outstanding callback to see if it
551 // causes the tearDown to complete.
552 completer.future.then((_) {
553 Invoker.current.removeOutstandingCallback();
554 });
555 }, tearDown: () {
556 Invoker.current.addOutstandingCallback();
557
558 // This will cause the test BODY to remove an outstanding callback, which
559 // shouldn't cause the test to complete.
560 completer.complete();
561
562 pumpEventQueue().then((_) {
563 outstandingCallbackRemoved = true;
564 Invoker.current.removeOutstandingCallback();
565 });
566 }).load(suite);
567
568 liveTest.onError.listen(expectAsync((_) {}, count: 0));
569
570 return liveTest.run().then((_) {
571 expect(outstandingCallbackRemoved, isTrue);
572 });
573 });
574
476 test("a test's prints are captured and reported", () { 575 test("a test's prints are captured and reported", () {
477 expect(() { 576 expect(() {
478 var liveTest = _localTest(() { 577 var liveTest = _localTest(() {
479 print("Hello,"); 578 print("Hello,");
480 return new Future(() => print("world!")); 579 return new Future(() => print("world!"));
481 }).load(suite); 580 }).load(suite);
482 581
483 expect(liveTest.onPrint.take(2).toList(), 582 expect(liveTest.onPrint.take(2).toList(),
484 completion(equals(["Hello,", "world!"]))); 583 completion(equals(["Hello,", "world!"])));
485 584
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 async.elapse(new Duration(seconds: 15)); 651 async.elapse(new Duration(seconds: 15));
553 }); 652 });
554 }); 653 });
555 }); 654 });
556 } 655 }
557 656
558 LocalTest _localTest(body(), {tearDown(), Metadata metadata}) { 657 LocalTest _localTest(body(), {tearDown(), Metadata metadata}) {
559 if (metadata == null) metadata = new Metadata(); 658 if (metadata == null) metadata = new Metadata();
560 return new LocalTest("test", metadata, body, tearDown: tearDown); 659 return new LocalTest("test", metadata, body, tearDown: tearDown);
561 } 660 }
OLDNEW
« lib/src/backend/outstanding_callback_counter.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698