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/group.dart'; | 8 import 'package:test/src/backend/group.dart'; |
9 import 'package:test/src/backend/invoker.dart'; | 9 import 'package:test/src/backend/invoker.dart'; |
10 import 'package:test/src/backend/message.dart'; | 10 import 'package:test/src/backend/message.dart'; |
(...skipping 15 matching lines...) Expand all Loading... |
26 var invoker = Invoker.current; | 26 var invoker = Invoker.current; |
27 test("returns null outside of a test body", () { | 27 test("returns null outside of a test body", () { |
28 expect(invoker, isNull); | 28 expect(invoker, isNull); |
29 }); | 29 }); |
30 | 30 |
31 test("returns the current invoker in a test body", () async { | 31 test("returns the current invoker in a test body", () async { |
32 var invoker; | 32 var invoker; |
33 var liveTest = _localTest(() { | 33 var liveTest = _localTest(() { |
34 invoker = Invoker.current; | 34 invoker = Invoker.current; |
35 }).load(suite); | 35 }).load(suite); |
36 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 36 liveTest.onError.listen(expectAsync1((_) {}, count: 0)); |
37 | 37 |
38 await liveTest.run(); | 38 await liveTest.run(); |
39 expect(invoker.liveTest, equals(liveTest)); | 39 expect(invoker.liveTest, equals(liveTest)); |
40 }); | 40 }); |
41 | 41 |
42 test("returns the current invoker in a test body after the test completes", | 42 test("returns the current invoker in a test body after the test completes", |
43 () async { | 43 () async { |
44 var status; | 44 var status; |
45 var completer = new Completer(); | 45 var completer = new Completer(); |
46 var liveTest = _localTest(() { | 46 var liveTest = _localTest(() { |
47 // Use [new Future] in particular to wait longer than a microtask for | 47 // Use [new Future] in particular to wait longer than a microtask for |
48 // the test to complete. | 48 // the test to complete. |
49 new Future(() { | 49 new Future(() { |
50 status = Invoker.current.liveTest.state.status; | 50 status = Invoker.current.liveTest.state.status; |
51 completer.complete(Invoker.current); | 51 completer.complete(Invoker.current); |
52 }); | 52 }); |
53 }).load(suite); | 53 }).load(suite); |
54 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 54 liveTest.onError.listen(expectAsync1((_) {}, count: 0)); |
55 | 55 |
56 expect(liveTest.run(), completes); | 56 expect(liveTest.run(), completes); |
57 var invoker = await completer.future; | 57 var invoker = await completer.future; |
58 expect(invoker.liveTest, equals(liveTest)); | 58 expect(invoker.liveTest, equals(liveTest)); |
59 expect(status, equals(Status.complete)); | 59 expect(status, equals(Status.complete)); |
60 }); | 60 }); |
61 }); | 61 }); |
62 | 62 |
63 group("in a successful test,", () { | 63 group("in a successful test,", () { |
64 test("the state changes from pending to running to complete", () async { | 64 test("the state changes from pending to running to complete", () async { |
65 var stateInTest; | 65 var stateInTest; |
66 var liveTest; | 66 var liveTest; |
67 liveTest = _localTest(() { | 67 liveTest = _localTest(() { |
68 stateInTest = liveTest.state; | 68 stateInTest = liveTest.state; |
69 }).load(suite); | 69 }).load(suite); |
70 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 70 liveTest.onError.listen(expectAsync1((_) {}, count: 0)); |
71 | 71 |
72 expect(liveTest.state.status, equals(Status.pending)); | 72 expect(liveTest.state.status, equals(Status.pending)); |
73 expect(liveTest.state.result, equals(Result.success)); | 73 expect(liveTest.state.result, equals(Result.success)); |
74 | 74 |
75 var future = liveTest.run(); | 75 var future = liveTest.run(); |
76 | 76 |
77 expect(liveTest.state.status, equals(Status.running)); | 77 expect(liveTest.state.status, equals(Status.running)); |
78 expect(liveTest.state.result, equals(Result.success)); | 78 expect(liveTest.state.result, equals(Result.success)); |
79 | 79 |
80 await future; | 80 await future; |
81 | 81 |
82 expect(stateInTest.status, equals(Status.running)); | 82 expect(stateInTest.status, equals(Status.running)); |
83 expect(stateInTest.result, equals(Result.success)); | 83 expect(stateInTest.result, equals(Result.success)); |
84 | 84 |
85 expect(liveTest.state.status, equals(Status.complete)); | 85 expect(liveTest.state.status, equals(Status.complete)); |
86 expect(liveTest.state.result, equals(Result.success)); | 86 expect(liveTest.state.result, equals(Result.success)); |
87 }); | 87 }); |
88 | 88 |
89 test("onStateChange fires for each state change", () { | 89 test("onStateChange fires for each state change", () { |
90 var liveTest = _localTest(() {}).load(suite); | 90 var liveTest = _localTest(() {}).load(suite); |
91 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 91 liveTest.onError.listen(expectAsync1((_) {}, count: 0)); |
92 | 92 |
93 var first = true; | 93 var first = true; |
94 liveTest.onStateChange.listen(expectAsync((state) { | 94 liveTest.onStateChange.listen(expectAsync1((state) { |
95 if (first) { | 95 if (first) { |
96 expect(state.status, equals(Status.running)); | 96 expect(state.status, equals(Status.running)); |
97 first = false; | 97 first = false; |
98 } else { | 98 } else { |
99 expect(state.status, equals(Status.complete)); | 99 expect(state.status, equals(Status.complete)); |
100 } | 100 } |
101 expect(state.result, equals(Result.success)); | 101 expect(state.result, equals(Result.success)); |
102 }, count: 2, max: 2)); | 102 }, count: 2, max: 2)); |
103 | 103 |
104 return liveTest.run(); | 104 return liveTest.run(); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 Invoker.current.addOutstandingCallback(); | 361 Invoker.current.addOutstandingCallback(); |
362 | 362 |
363 // Pump the event queue to make sure the test isn't coincidentally | 363 // Pump the event queue to make sure the test isn't coincidentally |
364 // completing after the outstanding callback is removed. | 364 // completing after the outstanding callback is removed. |
365 pumpEventQueue().then((_) { | 365 pumpEventQueue().then((_) { |
366 outstandingCallbackRemoved = true; | 366 outstandingCallbackRemoved = true; |
367 Invoker.current.removeOutstandingCallback(); | 367 Invoker.current.removeOutstandingCallback(); |
368 }); | 368 }); |
369 }).load(suite); | 369 }).load(suite); |
370 | 370 |
371 liveTest.onError.listen(expectAsync((_) {}, count: 0)); | 371 liveTest.onError.listen(expectAsync1((_) {}, count: 0)); |
372 | 372 |
373 await liveTest.run(); | 373 await liveTest.run(); |
374 expect(outstandingCallbackRemoved, isTrue); | 374 expect(outstandingCallbackRemoved, isTrue); |
375 }); | 375 }); |
376 | 376 |
377 test("a test's prints are captured and reported", () { | 377 test("a test's prints are captured and reported", () { |
378 expect(() { | 378 expect(() { |
379 var liveTest = _localTest(() { | 379 var liveTest = _localTest(() { |
380 print("Hello,"); | 380 print("Hello,"); |
381 return new Future(() => print("world!")); | 381 return new Future(() => print("world!")); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 functionCompleted = true; | 481 functionCompleted = true; |
482 }); | 482 }); |
483 | 483 |
484 expect(functionCompleted, isTrue); | 484 expect(functionCompleted, isTrue); |
485 }); | 485 }); |
486 | 486 |
487 test("waits for registered callbacks in the wrapped function to run", | 487 test("waits for registered callbacks in the wrapped function to run", |
488 () async { | 488 () async { |
489 var callbackRun = false; | 489 var callbackRun = false; |
490 await Invoker.current.waitForOutstandingCallbacks(() { | 490 await Invoker.current.waitForOutstandingCallbacks(() { |
491 pumpEventQueue().then(expectAsync((_) { | 491 pumpEventQueue().then(expectAsync1((_) { |
492 callbackRun = true; | 492 callbackRun = true; |
493 })); | 493 })); |
494 }); | 494 }); |
495 | 495 |
496 expect(callbackRun, isTrue); | 496 expect(callbackRun, isTrue); |
497 }); | 497 }); |
498 | 498 |
499 test("doesn't automatically block the enclosing context", () async { | 499 test("doesn't automatically block the enclosing context", () async { |
500 var innerFunctionCompleted = false; | 500 var innerFunctionCompleted = false; |
501 await Invoker.current.waitForOutstandingCallbacks(() { | 501 await Invoker.current.waitForOutstandingCallbacks(() { |
(...skipping 26 matching lines...) Expand all Loading... |
528 expect(liveTest.state.status, equals(Status.complete)); | 528 expect(liveTest.state.status, equals(Status.complete)); |
529 expect(isComplete, isFalse); | 529 expect(isComplete, isFalse); |
530 }); | 530 }); |
531 }); | 531 }); |
532 } | 532 } |
533 | 533 |
534 LocalTest _localTest(body(), {Metadata metadata}) { | 534 LocalTest _localTest(body(), {Metadata metadata}) { |
535 if (metadata == null) metadata = new Metadata(); | 535 if (metadata == null) metadata = new Metadata(); |
536 return new LocalTest("test", metadata, body); | 536 return new LocalTest("test", metadata, body); |
537 } | 537 } |
OLD | NEW |