| OLD | NEW |
| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 completer.complete(Invoker.current); | 49 completer.complete(Invoker.current); |
| 50 }); | 50 }); |
| 51 }).load(suite); | 51 }).load(suite); |
| 52 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 52 liveTest.onError.listen(expectAsync((_) {}, count: 0)); |
| 53 | 53 |
| 54 expect(liveTest.run(), completes); | 54 expect(liveTest.run(), completes); |
| 55 var invoker = await completer.future; | 55 var invoker = await completer.future; |
| 56 expect(invoker.liveTest, equals(liveTest)); | 56 expect(invoker.liveTest, equals(liveTest)); |
| 57 expect(status, equals(Status.complete)); | 57 expect(status, equals(Status.complete)); |
| 58 }); | 58 }); |
| 59 | |
| 60 test("returns the current invoker in a tearDown body", () async { | |
| 61 var invoker; | |
| 62 var liveTest = _localTest(() {}, tearDown: () { | |
| 63 invoker = Invoker.current; | |
| 64 }).load(suite); | |
| 65 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | |
| 66 | |
| 67 await liveTest.run(); | |
| 68 expect(invoker.liveTest, equals(liveTest)); | |
| 69 }); | |
| 70 | |
| 71 test("returns the current invoker in a tearDown body after the test " | |
| 72 "completes", () async { | |
| 73 var status; | |
| 74 var completer = new Completer(); | |
| 75 var liveTest = _localTest(() {}, tearDown: () { | |
| 76 // Use [new Future] in particular to wait longer than a microtask for | |
| 77 // the test to complete. | |
| 78 new Future(() { | |
| 79 status = Invoker.current.liveTest.state.status; | |
| 80 completer.complete(Invoker.current); | |
| 81 }); | |
| 82 }).load(suite); | |
| 83 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | |
| 84 | |
| 85 expect(liveTest.run(), completes); | |
| 86 var invoker = await completer.future; | |
| 87 expect(invoker.liveTest, equals(liveTest)); | |
| 88 expect(status, equals(Status.complete)); | |
| 89 }); | |
| 90 }); | 59 }); |
| 91 | 60 |
| 92 group("in a successful test,", () { | 61 group("in a successful test,", () { |
| 93 test("the state changes from pending to running to complete", () async { | 62 test("the state changes from pending to running to complete", () async { |
| 94 var stateInTest; | 63 var stateInTest; |
| 95 var stateInTearDown; | |
| 96 var liveTest; | 64 var liveTest; |
| 97 liveTest = _localTest(() { | 65 liveTest = _localTest(() { |
| 98 stateInTest = liveTest.state; | 66 stateInTest = liveTest.state; |
| 99 }, tearDown: () { | |
| 100 stateInTearDown = liveTest.state; | |
| 101 }).load(suite); | 67 }).load(suite); |
| 102 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 68 liveTest.onError.listen(expectAsync((_) {}, count: 0)); |
| 103 | 69 |
| 104 expect(liveTest.state.status, equals(Status.pending)); | 70 expect(liveTest.state.status, equals(Status.pending)); |
| 105 expect(liveTest.state.result, equals(Result.success)); | 71 expect(liveTest.state.result, equals(Result.success)); |
| 106 | 72 |
| 107 var future = liveTest.run(); | 73 var future = liveTest.run(); |
| 108 | 74 |
| 109 expect(liveTest.state.status, equals(Status.running)); | 75 expect(liveTest.state.status, equals(Status.running)); |
| 110 expect(liveTest.state.result, equals(Result.success)); | 76 expect(liveTest.state.result, equals(Result.success)); |
| 111 | 77 |
| 112 await future; | 78 await future; |
| 113 | 79 |
| 114 expect(stateInTest.status, equals(Status.running)); | 80 expect(stateInTest.status, equals(Status.running)); |
| 115 expect(stateInTest.result, equals(Result.success)); | 81 expect(stateInTest.result, equals(Result.success)); |
| 116 | 82 |
| 117 expect(stateInTearDown.status, equals(Status.running)); | |
| 118 expect(stateInTearDown.result, equals(Result.success)); | |
| 119 | |
| 120 expect(liveTest.state.status, equals(Status.complete)); | 83 expect(liveTest.state.status, equals(Status.complete)); |
| 121 expect(liveTest.state.result, equals(Result.success)); | 84 expect(liveTest.state.result, equals(Result.success)); |
| 122 }); | 85 }); |
| 123 | 86 |
| 124 test("onStateChange fires for each state change", () { | 87 test("onStateChange fires for each state change", () { |
| 125 var liveTest = _localTest(() {}).load(suite); | 88 var liveTest = _localTest(() {}).load(suite); |
| 126 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 89 liveTest.onError.listen(expectAsync((_) {}, count: 0)); |
| 127 | 90 |
| 128 var first = true; | 91 var first = true; |
| 129 liveTest.onStateChange.listen(expectAsync((state) { | 92 liveTest.onStateChange.listen(expectAsync((state) { |
| 130 if (first) { | 93 if (first) { |
| 131 expect(state.status, equals(Status.running)); | 94 expect(state.status, equals(Status.running)); |
| 132 first = false; | 95 first = false; |
| 133 } else { | 96 } else { |
| 134 expect(state.status, equals(Status.complete)); | 97 expect(state.status, equals(Status.complete)); |
| 135 } | 98 } |
| 136 expect(state.result, equals(Result.success)); | 99 expect(state.result, equals(Result.success)); |
| 137 }, count: 2, max: 2)); | 100 }, count: 2, max: 2)); |
| 138 | 101 |
| 139 return liveTest.run(); | 102 return liveTest.run(); |
| 140 }); | 103 }); |
| 141 | 104 |
| 142 test("onComplete completes once the test body and tearDown are done", () { | 105 test("onComplete completes once the test body is done", () { |
| 143 var testRun = false; | 106 var testRun = false; |
| 144 var tearDownRun = false; | |
| 145 var liveTest = _localTest(() { | 107 var liveTest = _localTest(() { |
| 146 testRun = true; | 108 testRun = true; |
| 147 }, tearDown: () { | |
| 148 tearDownRun = true; | |
| 149 }).load(suite); | 109 }).load(suite); |
| 150 | 110 |
| 151 expect(liveTest.onComplete.then((_) { | 111 expect(liveTest.onComplete.then((_) { |
| 152 expect(testRun, isTrue); | 112 expect(testRun, isTrue); |
| 153 expect(tearDownRun, isTrue); | |
| 154 }), completes); | 113 }), completes); |
| 155 | 114 |
| 156 return liveTest.run(); | 115 return liveTest.run(); |
| 157 }); | 116 }); |
| 158 }); | 117 }); |
| 159 | 118 |
| 160 group("in a test with failures,", () { | 119 group("in a test with failures,", () { |
| 161 test("a synchronous throw is reported and causes the test to fail", () { | 120 test("a synchronous throw is reported and causes the test to fail", () { |
| 162 var liveTest = _localTest(() { | 121 var liveTest = _localTest(() { |
| 163 throw new TestFailure('oh no'); | 122 throw new TestFailure('oh no'); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 225 |
| 267 expectErrors(liveTest, [(error) { | 226 expectErrors(liveTest, [(error) { |
| 268 expect(lastState, equals(const State(Status.complete, Result.error))); | 227 expect(lastState, equals(const State(Status.complete, Result.error))); |
| 269 expect(error, equals("error")); | 228 expect(error, equals("error")); |
| 270 }, (error) { | 229 }, (error) { |
| 271 expect(error, isTestFailure("fail")); | 230 expect(error, isTestFailure("fail")); |
| 272 }]); | 231 }]); |
| 273 | 232 |
| 274 return liveTest.run(); | 233 return liveTest.run(); |
| 275 }); | 234 }); |
| 276 | |
| 277 test("tearDown is run after an asynchronous failure", () async { | |
| 278 var stateDuringTearDown; | |
| 279 var liveTest; | |
| 280 liveTest = _localTest(() { | |
| 281 Invoker.current.addOutstandingCallback(); | |
| 282 new Future(() => throw new TestFailure("oh no")); | |
| 283 }, tearDown: () { | |
| 284 stateDuringTearDown = liveTest.state; | |
| 285 }).load(suite); | |
| 286 | |
| 287 expectSingleFailure(liveTest); | |
| 288 await liveTest.run(); | |
| 289 expect(stateDuringTearDown, | |
| 290 equals(const State(Status.complete, Result.failure))); | |
| 291 }); | |
| 292 }); | 235 }); |
| 293 | 236 |
| 294 group("in a test with errors,", () { | 237 group("in a test with errors,", () { |
| 295 test("a synchronous throw is reported and causes the test to error", () { | 238 test("a synchronous throw is reported and causes the test to error", () { |
| 296 var liveTest = _localTest(() { | 239 var liveTest = _localTest(() { |
| 297 throw 'oh no'; | 240 throw 'oh no'; |
| 298 }).load(suite); | 241 }).load(suite); |
| 299 | 242 |
| 300 expectSingleError(liveTest); | 243 expectSingleError(liveTest); |
| 301 return liveTest.run(); | 244 return liveTest.run(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 expectErrors(liveTest, [(error) { | 343 expectErrors(liveTest, [(error) { |
| 401 expect(lastState, equals(const State(Status.complete, Result.failure))); | 344 expect(lastState, equals(const State(Status.complete, Result.failure))); |
| 402 expect(error, isTestFailure("fail")); | 345 expect(error, isTestFailure("fail")); |
| 403 }, (error) { | 346 }, (error) { |
| 404 expect(lastState, equals(const State(Status.complete, Result.error))); | 347 expect(lastState, equals(const State(Status.complete, Result.error))); |
| 405 expect(error, equals("error")); | 348 expect(error, equals("error")); |
| 406 }]); | 349 }]); |
| 407 | 350 |
| 408 return liveTest.run(); | 351 return liveTest.run(); |
| 409 }); | 352 }); |
| 410 | |
| 411 test("tearDown is run after an asynchronous error", () async { | |
| 412 var stateDuringTearDown; | |
| 413 var liveTest; | |
| 414 liveTest = _localTest(() { | |
| 415 Invoker.current.addOutstandingCallback(); | |
| 416 new Future(() => throw "oh no"); | |
| 417 }, tearDown: () { | |
| 418 stateDuringTearDown = liveTest.state; | |
| 419 }).load(suite); | |
| 420 | |
| 421 expectSingleError(liveTest); | |
| 422 await liveTest.run(); | |
| 423 expect(stateDuringTearDown, | |
| 424 equals(const State(Status.complete, Result.error))); | |
| 425 }); | |
| 426 | |
| 427 test("an asynchronous error in tearDown causes the test to error", () { | |
| 428 var liveTest = _localTest(() {}, tearDown: () { | |
| 429 Invoker.current.addOutstandingCallback(); | |
| 430 new Future(() => throw "oh no"); | |
| 431 }).load(suite); | |
| 432 | |
| 433 expectSingleError(liveTest); | |
| 434 return liveTest.run(); | |
| 435 }); | |
| 436 | |
| 437 test("an error reported in the test body after tearDown begins running " | |
| 438 "doesn't stop tearDown", () async { | |
| 439 var tearDownComplete = false;; | |
| 440 var completer = new Completer(); | |
| 441 | |
| 442 var liveTest; | |
| 443 liveTest = _localTest(() { | |
| 444 completer.future.then((_) => throw "not again"); | |
| 445 throw "oh no"; | |
| 446 }, tearDown: () { | |
| 447 completer.complete(); | |
| 448 | |
| 449 // Pump the event queue so that we will run the following code after the | |
| 450 // test body has thrown a second error. | |
| 451 Invoker.current.addOutstandingCallback(); | |
| 452 pumpEventQueue().then((_) { | |
| 453 Invoker.current.removeOutstandingCallback(); | |
| 454 tearDownComplete = true; | |
| 455 }); | |
| 456 }).load(suite); | |
| 457 | |
| 458 expectStates(liveTest, [ | |
| 459 const State(Status.running, Result.success), | |
| 460 const State(Status.complete, Result.error) | |
| 461 ]); | |
| 462 | |
| 463 expectErrors(liveTest, [ | |
| 464 (error) { | |
| 465 expect(lastState.status, equals(Status.complete)); | |
| 466 expect(error, equals("oh no")); | |
| 467 }, | |
| 468 (error) => expect(error, equals("not again")) | |
| 469 ]); | |
| 470 | |
| 471 await liveTest.run(); | |
| 472 expect(tearDownComplete, isTrue); | |
| 473 }); | |
| 474 }); | 353 }); |
| 475 | 354 |
| 476 test("a test doesn't complete until there are no outstanding callbacks", | 355 test("a test doesn't complete until there are no outstanding callbacks", |
| 477 () async { | 356 () async { |
| 478 var outstandingCallbackRemoved = false; | 357 var outstandingCallbackRemoved = false; |
| 479 var liveTest = _localTest(() { | 358 var liveTest = _localTest(() { |
| 480 Invoker.current.addOutstandingCallback(); | 359 Invoker.current.addOutstandingCallback(); |
| 481 | 360 |
| 482 // Pump the event queue to make sure the test isn't coincidentally | 361 // Pump the event queue to make sure the test isn't coincidentally |
| 483 // completing after the outstanding callback is removed. | 362 // completing after the outstanding callback is removed. |
| 484 pumpEventQueue().then((_) { | 363 pumpEventQueue().then((_) { |
| 485 outstandingCallbackRemoved = true; | 364 outstandingCallbackRemoved = true; |
| 486 Invoker.current.removeOutstandingCallback(); | 365 Invoker.current.removeOutstandingCallback(); |
| 487 }); | 366 }); |
| 488 }).load(suite); | 367 }).load(suite); |
| 489 | 368 |
| 490 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 369 liveTest.onError.listen(expectAsync((_) {}, count: 0)); |
| 491 | 370 |
| 492 await liveTest.run(); | 371 await liveTest.run(); |
| 493 expect(outstandingCallbackRemoved, isTrue); | 372 expect(outstandingCallbackRemoved, isTrue); |
| 494 }); | 373 }); |
| 495 | 374 |
| 496 test("a test's tearDown isn't run until there are no outstanding callbacks", | |
| 497 () async { | |
| 498 var outstandingCallbackRemoved = false; | |
| 499 var outstandingCallbackRemovedBeforeTeardown = false; | |
| 500 var liveTest = _localTest(() { | |
| 501 Invoker.current.addOutstandingCallback(); | |
| 502 pumpEventQueue().then((_) { | |
| 503 outstandingCallbackRemoved = true; | |
| 504 Invoker.current.removeOutstandingCallback(); | |
| 505 }); | |
| 506 }, tearDown: () { | |
| 507 outstandingCallbackRemovedBeforeTeardown = outstandingCallbackRemoved; | |
| 508 }).load(suite); | |
| 509 | |
| 510 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | |
| 511 | |
| 512 await liveTest.run(); | |
| 513 expect(outstandingCallbackRemovedBeforeTeardown, isTrue); | |
| 514 }); | |
| 515 | |
| 516 test("a test's tearDown doesn't complete until there are no outstanding " | |
| 517 "callbacks", () async { | |
| 518 var outstandingCallbackRemoved = false; | |
| 519 var liveTest = _localTest(() {}, tearDown: () { | |
| 520 Invoker.current.addOutstandingCallback(); | |
| 521 | |
| 522 // Pump the event queue to make sure the test isn't coincidentally | |
| 523 // completing after the outstanding callback is removed. | |
| 524 pumpEventQueue().then((_) { | |
| 525 outstandingCallbackRemoved = true; | |
| 526 Invoker.current.removeOutstandingCallback(); | |
| 527 }); | |
| 528 }).load(suite); | |
| 529 | |
| 530 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | |
| 531 | |
| 532 await liveTest.run(); | |
| 533 expect(outstandingCallbackRemoved, isTrue); | |
| 534 }); | |
| 535 | |
| 536 test("a test body's outstanding callbacks can't complete its tearDown", | |
| 537 () async { | |
| 538 var outstandingCallbackRemoved = false; | |
| 539 var completer = new Completer(); | |
| 540 var liveTest = _localTest(() { | |
| 541 // Once the tearDown runs, remove an outstanding callback to see if it | |
| 542 // causes the tearDown to complete. | |
| 543 completer.future.then((_) { | |
| 544 Invoker.current.removeOutstandingCallback(); | |
| 545 }); | |
| 546 }, tearDown: () { | |
| 547 Invoker.current.addOutstandingCallback(); | |
| 548 | |
| 549 // This will cause the test BODY to remove an outstanding callback, which | |
| 550 // shouldn't cause the test to complete. | |
| 551 completer.complete(); | |
| 552 | |
| 553 pumpEventQueue().then((_) { | |
| 554 outstandingCallbackRemoved = true; | |
| 555 Invoker.current.removeOutstandingCallback(); | |
| 556 }); | |
| 557 }).load(suite); | |
| 558 | |
| 559 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | |
| 560 | |
| 561 await liveTest.run(); | |
| 562 expect(outstandingCallbackRemoved, isTrue); | |
| 563 }); | |
| 564 | |
| 565 test("a test's prints are captured and reported", () { | 375 test("a test's prints are captured and reported", () { |
| 566 expect(() { | 376 expect(() { |
| 567 var liveTest = _localTest(() { | 377 var liveTest = _localTest(() { |
| 568 print("Hello,"); | 378 print("Hello,"); |
| 569 return new Future(() => print("world!")); | 379 return new Future(() => print("world!")); |
| 570 }).load(suite); | 380 }).load(suite); |
| 571 | 381 |
| 572 expect(liveTest.onPrint.take(2).toList(), | 382 expect(liveTest.onPrint.take(2).toList(), |
| 573 completion(equals(["Hello,", "world!"]))); | 383 completion(equals(["Hello,", "world!"]))); |
| 574 | 384 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 var innerFunctionCompleted = false; | 494 var innerFunctionCompleted = false; |
| 685 await Invoker.current.waitForOutstandingCallbacks(() { | 495 await Invoker.current.waitForOutstandingCallbacks(() { |
| 686 Invoker.current.waitForOutstandingCallbacks(() async { | 496 Invoker.current.waitForOutstandingCallbacks(() async { |
| 687 await pumpEventQueue(); | 497 await pumpEventQueue(); |
| 688 innerFunctionCompleted = true; | 498 innerFunctionCompleted = true; |
| 689 }); | 499 }); |
| 690 }); | 500 }); |
| 691 | 501 |
| 692 expect(innerFunctionCompleted, isFalse); | 502 expect(innerFunctionCompleted, isFalse); |
| 693 }); | 503 }); |
| 504 |
| 505 test("forwards errors to the enclosing test but doesn't remove its " |
| 506 "outstanding callbacks", () async { |
| 507 var liveTest = _localTest(() async { |
| 508 Invoker.current.addOutstandingCallback(); |
| 509 await Invoker.current.waitForOutstandingCallbacks(() { |
| 510 throw 'oh no'; |
| 511 }); |
| 512 }).load(suite); |
| 513 |
| 514 expectStates(liveTest, [ |
| 515 const State(Status.running, Result.success), |
| 516 const State(Status.complete, Result.error) |
| 517 ]); |
| 518 |
| 519 var isComplete = false; |
| 520 liveTest.run().then((_) => isComplete = true); |
| 521 await pumpEventQueue(); |
| 522 expect(liveTest.state.status, equals(Status.complete)); |
| 523 expect(isComplete, isFalse); |
| 524 }); |
| 694 }); | 525 }); |
| 695 } | 526 } |
| 696 | 527 |
| 697 LocalTest _localTest(body(), {tearDown(), Metadata metadata}) { | 528 LocalTest _localTest(body(), {Metadata metadata}) { |
| 698 if (metadata == null) metadata = new Metadata(); | 529 if (metadata == null) metadata = new Metadata(); |
| 699 return new LocalTest("test", metadata, body, tearDown: tearDown); | 530 return new LocalTest("test", metadata, body); |
| 700 } | 531 } |
| OLD | NEW |