OLD | NEW |
1 // Copyright (c) 2013, 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 library unittest.expect_async_test; | 5 import 'package:unittest/src/state.dart'; |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:metatest/metatest.dart'; | |
10 import 'package:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
11 | 7 |
12 void main() => initTests(_test); | 8 import 'utils.dart'; |
13 | 9 |
14 void _test(message) { | 10 void main() { |
15 initMetatest(message); | 11 group("supports a function with this many arguments:", () { |
16 | 12 test("0", () { |
17 var count = 0; | 13 var callbackRun = false; |
18 | 14 return runTest(() { |
19 expectTestsPass('expect async test', () { | 15 expectAsync(() { |
20 test('expectAsync zero params', () { | 16 callbackRun = true; |
21 new Future.sync(expectAsync(() { | 17 })(); |
22 ++count; | 18 }).then((liveTest) { |
23 })); | 19 expectTestPassed(liveTest); |
24 }); | 20 expect(callbackRun, isTrue); |
25 | 21 }); |
26 test('expectAsync 1 param', () { | 22 }); |
27 var func = expectAsync((arg) { | 23 |
28 expect(arg, 0); | 24 test("1", () { |
29 ++count; | 25 var callbackRun = false; |
30 }); | 26 return runTest(() { |
31 new Future.sync(() => func(0)); | 27 expectAsync((arg) { |
32 }); | 28 expect(arg, equals(1)); |
33 | 29 callbackRun = true; |
34 test('expectAsync 2 param', () { | 30 })(1); |
35 var func = expectAsync((arg0, arg1) { | 31 }).then((liveTest) { |
36 expect(arg0, 0); | 32 expectTestPassed(liveTest); |
37 expect(arg1, 1); | 33 expect(callbackRun, isTrue); |
38 ++count; | 34 }); |
39 }); | 35 }); |
40 new Future.sync(() => func(0, 1)); | 36 |
41 }); | 37 test("2", () { |
42 | 38 var callbackRun = false; |
43 test('single arg to Future.catchError', () { | 39 return runTest(() { |
44 var func = expectAsync((error) { | 40 expectAsync((arg1, arg2) { |
45 expect(error, isStateError); | 41 expect(arg1, equals(1)); |
46 ++count; | 42 expect(arg2, equals(2)); |
47 }); | 43 callbackRun = true; |
48 | 44 })(1, 2); |
49 new Future(() { | 45 }).then((liveTest) { |
50 throw new StateError('test'); | 46 expectTestPassed(liveTest); |
51 }).catchError(func); | 47 expect(callbackRun, isTrue); |
52 }); | 48 }); |
53 | 49 }); |
54 test('2 args to Future.catchError', () { | 50 |
55 var func = expectAsync((error, stack) { | 51 test("3", () { |
56 expect(error, isStateError); | 52 var callbackRun = false; |
57 expect(stack is StackTrace, isTrue); | 53 return runTest(() { |
58 ++count; | 54 expectAsync((arg1, arg2, arg3) { |
59 }); | 55 expect(arg1, equals(1)); |
60 | 56 expect(arg2, equals(2)); |
61 new Future(() { | 57 expect(arg3, equals(3)); |
62 throw new StateError('test'); | 58 callbackRun = true; |
63 }).catchError(func); | 59 })(1, 2, 3); |
64 }); | 60 }).then((liveTest) { |
65 | 61 expectTestPassed(liveTest); |
66 test('zero of two optional positional args', () { | 62 expect(callbackRun, isTrue); |
67 var func = expectAsync(([arg0 = true, arg1 = true]) { | 63 }); |
68 expect(arg0, isTrue); | 64 }); |
69 expect(arg1, isTrue); | 65 |
70 ++count; | 66 test("4", () { |
71 }); | 67 var callbackRun = false; |
72 | 68 return runTest(() { |
73 new Future.sync(() => func()); | 69 expectAsync((arg1, arg2, arg3, arg4) { |
74 }); | 70 expect(arg1, equals(1)); |
75 | 71 expect(arg2, equals(2)); |
76 test('one of two optional positional args', () { | 72 expect(arg3, equals(3)); |
77 var func = expectAsync(([arg0 = true, arg1 = true]) { | 73 expect(arg4, equals(4)); |
78 expect(arg0, isFalse); | 74 callbackRun = true; |
79 expect(arg1, isTrue); | 75 })(1, 2, 3, 4); |
80 ++count; | 76 }).then((liveTest) { |
81 }); | 77 expectTestPassed(liveTest); |
82 | 78 expect(callbackRun, isTrue); |
83 new Future.sync(() => func(false)); | 79 }); |
84 }); | 80 }); |
85 | 81 |
86 test('two of two optional positional args', () { | 82 test("5", () { |
87 var func = expectAsync(([arg0 = true, arg1 = true]) { | 83 var callbackRun = false; |
88 expect(arg0, isFalse); | 84 return runTest(() { |
89 expect(arg1, isNull); | 85 expectAsync((arg1, arg2, arg3, arg4, arg5) { |
90 ++count; | 86 expect(arg1, equals(1)); |
91 }); | 87 expect(arg2, equals(2)); |
92 | 88 expect(arg3, equals(3)); |
93 new Future.sync(() => func(false, null)); | 89 expect(arg4, equals(4)); |
94 }); | 90 expect(arg5, equals(5)); |
95 | 91 callbackRun = true; |
96 test('verify count', () { | 92 })(1, 2, 3, 4, 5); |
97 expect(count, 8); | 93 }).then((liveTest) { |
| 94 expectTestPassed(liveTest); |
| 95 expect(callbackRun, isTrue); |
| 96 }); |
| 97 }); |
| 98 |
| 99 test("6", () { |
| 100 var callbackRun = false; |
| 101 return runTest(() { |
| 102 expectAsync((arg1, arg2, arg3, arg4, arg5, arg6) { |
| 103 expect(arg1, equals(1)); |
| 104 expect(arg2, equals(2)); |
| 105 expect(arg3, equals(3)); |
| 106 expect(arg4, equals(4)); |
| 107 expect(arg5, equals(5)); |
| 108 expect(arg6, equals(6)); |
| 109 callbackRun = true; |
| 110 })(1, 2, 3, 4, 5, 6); |
| 111 }).then((liveTest) { |
| 112 expectTestPassed(liveTest); |
| 113 expect(callbackRun, isTrue); |
| 114 }); |
| 115 }); |
| 116 }); |
| 117 |
| 118 group("with optional arguments", () { |
| 119 test("allows them to be passed", () { |
| 120 var callbackRun = false; |
| 121 return runTest(() { |
| 122 expectAsync(([arg = 1]) { |
| 123 expect(arg, equals(2)); |
| 124 callbackRun = true; |
| 125 })(2); |
| 126 }).then((liveTest) { |
| 127 expectTestPassed(liveTest); |
| 128 expect(callbackRun, isTrue); |
| 129 }); |
| 130 }); |
| 131 |
| 132 test("allows them not to be passed", () { |
| 133 var callbackRun = false; |
| 134 return runTest(() { |
| 135 expectAsync(([arg = 1]) { |
| 136 expect(arg, equals(1)); |
| 137 callbackRun = true; |
| 138 })(); |
| 139 }).then((liveTest) { |
| 140 expectTestPassed(liveTest); |
| 141 expect(callbackRun, isTrue); |
| 142 }); |
| 143 }); |
| 144 }); |
| 145 |
| 146 test("doesn't support a function with 7 arguments", () { |
| 147 expect(() => expectAsync((_1, _2, _3, _4, _5, _6, _7) {}), |
| 148 throwsArgumentError); |
| 149 }); |
| 150 |
| 151 group("by default", () { |
| 152 test("won't allow the test to complete until it's called", () { |
| 153 return expectTestBlocks( |
| 154 () => expectAsync(() {}), |
| 155 (callback) => callback()); |
| 156 }); |
| 157 |
| 158 test("may only be called once", () { |
| 159 return runTest(() { |
| 160 var callback = expectAsync(() {}); |
| 161 callback(); |
| 162 callback(); |
| 163 }).then((liveTest) { |
| 164 expectTestFailed(liveTest, |
| 165 "Callback called more times than expected (1)."); |
| 166 }); |
| 167 }); |
| 168 }); |
| 169 |
| 170 group("with count", () { |
| 171 test("won't allow the test to complete until it's called at least that " |
| 172 "many times", () { |
| 173 var liveTest; |
| 174 var future; |
| 175 liveTest = createTest(() { |
| 176 var callback = expectAsync(() {}, count: 3); |
| 177 future = pumpEventQueue().then((_) { |
| 178 expect(liveTest.state.status, equals(Status.running)); |
| 179 callback(); |
| 180 return pumpEventQueue(); |
| 181 }).then((_) { |
| 182 expect(liveTest.state.status, equals(Status.running)); |
| 183 callback(); |
| 184 return pumpEventQueue(); |
| 185 }).then((_) { |
| 186 expect(liveTest.state.status, equals(Status.running)); |
| 187 callback(); |
| 188 }); |
| 189 }); |
| 190 |
| 191 return liveTest.run().then((_) { |
| 192 expectTestPassed(liveTest); |
| 193 // Ensure that the outer test doesn't complete until the inner future |
| 194 // completes. |
| 195 return future; |
| 196 }); |
| 197 }); |
| 198 |
| 199 test("will throw an error if it's called more than that many times", () { |
| 200 return runTest(() { |
| 201 var callback = expectAsync(() {}, count: 3); |
| 202 callback(); |
| 203 callback(); |
| 204 callback(); |
| 205 callback(); |
| 206 }).then((liveTest) { |
| 207 expectTestFailed( |
| 208 liveTest, "Callback called more times than expected (3)."); |
| 209 }); |
| 210 }); |
| 211 |
| 212 group("0,", () { |
| 213 test("won't block the test's completion", () { |
| 214 expectAsync(() {}, count: 0); |
| 215 }); |
| 216 |
| 217 test("will throw an error if it's ever called", () { |
| 218 return runTest(() { |
| 219 expectAsync(() {}, count: 0)(); |
| 220 }).then((liveTest) { |
| 221 expectTestFailed( |
| 222 liveTest, "Callback called more times than expected (0)."); |
| 223 }); |
| 224 }); |
| 225 }); |
| 226 }); |
| 227 |
| 228 group("with max", () { |
| 229 test("will allow the callback to be called that many times", () { |
| 230 var callback = expectAsync(() {}, max: 3); |
| 231 callback(); |
| 232 callback(); |
| 233 callback(); |
| 234 }); |
| 235 |
| 236 test("will allow the callback to be called fewer than that many times", () { |
| 237 var callback = expectAsync(() {}, max: 3); |
| 238 callback(); |
| 239 }); |
| 240 |
| 241 test("will throw an error if it's called more than that many times", () { |
| 242 return runTest(() { |
| 243 var callback = expectAsync(() {}, max: 3); |
| 244 callback(); |
| 245 callback(); |
| 246 callback(); |
| 247 callback(); |
| 248 }).then((liveTest) { |
| 249 expectTestFailed( |
| 250 liveTest, "Callback called more times than expected (3)."); |
| 251 }); |
| 252 }); |
| 253 |
| 254 test("-1, will allow the callback to be called any number of times", () { |
| 255 var callback = expectAsync(() {}, max: -1); |
| 256 for (var i = 0; i < 20; i++) { |
| 257 callback(); |
| 258 } |
| 259 }); |
| 260 }); |
| 261 |
| 262 test("will throw an error if max is less than count", () { |
| 263 expect(() => expectAsync(() {}, max: 1, count: 2), |
| 264 throwsArgumentError); |
| 265 }); |
| 266 |
| 267 group("expectAsyncUntil()", () { |
| 268 test("won't allow the test to complete until isDone returns true", () { |
| 269 var liveTest; |
| 270 var future; |
| 271 liveTest = createTest(() { |
| 272 var done = false; |
| 273 var callback = expectAsyncUntil(() {}, () => done); |
| 274 |
| 275 future = pumpEventQueue().then((_) { |
| 276 expect(liveTest.state.status, equals(Status.running)); |
| 277 callback(); |
| 278 return pumpEventQueue(); |
| 279 }).then((_) { |
| 280 expect(liveTest.state.status, equals(Status.running)); |
| 281 done = true; |
| 282 callback(); |
| 283 }); |
| 284 }); |
| 285 |
| 286 return liveTest.run().then((_) { |
| 287 expectTestPassed(liveTest); |
| 288 // Ensure that the outer test doesn't complete until the inner future |
| 289 // completes. |
| 290 return future; |
| 291 }); |
| 292 }); |
| 293 |
| 294 test("doesn't call isDone until after the callback is called", () { |
| 295 var callbackRun = false; |
| 296 expectAsyncUntil(() => callbackRun = true, () { |
| 297 expect(callbackRun, isTrue); |
| 298 return true; |
| 299 })(); |
| 300 }); |
| 301 }); |
| 302 |
| 303 group("with errors", () { |
| 304 test("reports them to the current test", () { |
| 305 return runTest(() { |
| 306 expectAsync(() => throw new TestFailure('oh no'))(); |
| 307 }).then((liveTest) { |
| 308 expectTestFailed(liveTest, 'oh no'); |
| 309 }); |
| 310 }); |
| 311 |
| 312 test("swallows them and returns null", () { |
| 313 var returnValue; |
| 314 var caughtError = false; |
| 315 return runTest(() { |
| 316 try { |
| 317 returnValue = expectAsync(() => throw new TestFailure('oh no'))(); |
| 318 } on TestFailure catch (_) { |
| 319 caughtError = true; |
| 320 } |
| 321 }).then((liveTest) { |
| 322 expectTestFailed(liveTest, 'oh no'); |
| 323 expect(returnValue, isNull); |
| 324 expect(caughtError, isFalse); |
| 325 }); |
98 }); | 326 }); |
99 }); | 327 }); |
100 } | 328 } |
OLD | NEW |