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:test/src/backend/declarer.dart'; | 7 import 'package:test/src/backend/declarer.dart'; |
| 8 import 'package:test/src/backend/invoker.dart'; |
8 import 'package:test/src/backend/suite.dart'; | 9 import 'package:test/src/backend/suite.dart'; |
9 import 'package:test/src/frontend/timeout.dart'; | 10 import 'package:test/src/frontend/timeout.dart'; |
10 import 'package:test/test.dart'; | 11 import 'package:test/test.dart'; |
11 | 12 |
| 13 import '../utils.dart'; |
| 14 |
12 Declarer _declarer; | 15 Declarer _declarer; |
13 Suite _suite; | 16 Suite _suite; |
14 | 17 |
15 void main() { | 18 void main() { |
16 setUp(() { | 19 setUp(() { |
17 _declarer = new Declarer(); | 20 _declarer = new Declarer(); |
18 _suite = new Suite([]); | 21 _suite = new Suite([]); |
19 }); | 22 }); |
20 | 23 |
21 group(".test()", () { | 24 group(".test()", () { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 _declarer.test("description 2", expectAsync(() { | 98 _declarer.test("description 2", expectAsync(() { |
96 expect(tearDownRun, isFalse); | 99 expect(tearDownRun, isFalse); |
97 }, max: 1)); | 100 }, max: 1)); |
98 | 101 |
99 await _runTest(0); | 102 await _runTest(0); |
100 expect(tearDownRun, isTrue); | 103 expect(tearDownRun, isTrue); |
101 await _runTest(1); | 104 await _runTest(1); |
102 expect(tearDownRun, isTrue); | 105 expect(tearDownRun, isTrue); |
103 }); | 106 }); |
104 | 107 |
| 108 test("is run after an out-of-band failure", () async { |
| 109 var tearDownRun; |
| 110 _declarer.setUp(() => tearDownRun = false); |
| 111 _declarer.tearDown(() => tearDownRun = true); |
| 112 |
| 113 _declarer.test("description 1", expectAsync(() { |
| 114 Invoker.current.addOutstandingCallback(); |
| 115 new Future(() => throw new TestFailure("oh no")); |
| 116 }, max: 1)); |
| 117 |
| 118 await _runTest(0, shouldFail: true); |
| 119 expect(tearDownRun, isTrue); |
| 120 }); |
| 121 |
105 test("can return a Future", () async { | 122 test("can return a Future", () async { |
106 var tearDownRun = false; | 123 var tearDownRun = false; |
107 _declarer.tearDown(() { | 124 _declarer.tearDown(() { |
108 return new Future(() => tearDownRun = true); | 125 return new Future(() => tearDownRun = true); |
109 }); | 126 }); |
110 | 127 |
111 _declarer.test("description", expectAsync(() { | 128 _declarer.test("description", expectAsync(() { |
112 expect(tearDownRun, isFalse); | 129 expect(tearDownRun, isFalse); |
113 }, max: 1)); | 130 }, max: 1)); |
114 | 131 |
115 await _runTest(0); | 132 await _runTest(0); |
116 expect(tearDownRun, isTrue); | 133 expect(tearDownRun, isTrue); |
117 }); | 134 }); |
118 | 135 |
| 136 test("isn't run until there are no outstanding callbacks", () async { |
| 137 var outstandingCallbackRemoved = false; |
| 138 var outstandingCallbackRemovedBeforeTeardown = false; |
| 139 _declarer.tearDown(() { |
| 140 outstandingCallbackRemovedBeforeTeardown = outstandingCallbackRemoved; |
| 141 }); |
| 142 |
| 143 _declarer.test("description", () { |
| 144 Invoker.current.addOutstandingCallback(); |
| 145 pumpEventQueue().then((_) { |
| 146 outstandingCallbackRemoved = true; |
| 147 Invoker.current.removeOutstandingCallback(); |
| 148 }); |
| 149 }); |
| 150 |
| 151 await _runTest(0); |
| 152 expect(outstandingCallbackRemovedBeforeTeardown, isTrue); |
| 153 }); |
| 154 |
| 155 test("doesn't complete until there are no outstanding callbacks", () async { |
| 156 var outstandingCallbackRemoved = false; |
| 157 _declarer.tearDown(() { |
| 158 Invoker.current.addOutstandingCallback(); |
| 159 pumpEventQueue().then((_) { |
| 160 outstandingCallbackRemoved = true; |
| 161 Invoker.current.removeOutstandingCallback(); |
| 162 }); |
| 163 }); |
| 164 |
| 165 _declarer.test("description", () {}); |
| 166 |
| 167 await _runTest(0); |
| 168 expect(outstandingCallbackRemoved, isTrue); |
| 169 }); |
| 170 |
119 test("can't be called multiple times", () { | 171 test("can't be called multiple times", () { |
120 _declarer.tearDown(() {}); | 172 _declarer.tearDown(() {}); |
121 expect(() => _declarer.tearDown(() {}), throwsStateError); | 173 expect(() => _declarer.tearDown(() {}), throwsStateError); |
122 }); | 174 }); |
123 }); | 175 }); |
124 | 176 |
125 group("in a group,", () { | 177 group("in a group,", () { |
126 test("tests inherit the group's description", () { | 178 test("tests inherit the group's description", () { |
127 _declarer.group("group", () { | 179 _declarer.group("group", () { |
128 _declarer.test("description", () {}); | 180 _declarer.test("description", () {}); |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 }); | 396 }); |
345 }); | 397 }); |
346 }); | 398 }); |
347 }); | 399 }); |
348 } | 400 } |
349 | 401 |
350 /// Runs the test at [index] defined on [_declarer]. | 402 /// Runs the test at [index] defined on [_declarer]. |
351 /// | 403 /// |
352 /// This automatically sets up an `onError` listener to ensure that the test | 404 /// This automatically sets up an `onError` listener to ensure that the test |
353 /// doesn't throw any invisible exceptions. | 405 /// doesn't throw any invisible exceptions. |
354 Future _runTest(int index) { | 406 Future _runTest(int index, {bool shouldFail: false}) { |
355 var liveTest = _declarer.tests[index].load(_suite); | 407 var liveTest = _declarer.tests[index].load(_suite); |
356 liveTest.onError.listen(expectAsync((_) {}, | 408 |
357 count: 0, reason: "No errors expected for test #$index.")); | 409 liveTest.onError.listen(shouldFail |
| 410 ? expectAsync((_) {}) |
| 411 : expectAsync((_) {}, |
| 412 count: 0, reason: "No errors expected for test #$index.")); |
| 413 |
358 return liveTest.run(); | 414 return liveTest.run(); |
359 } | 415 } |
OLD | NEW |