| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'package:unittest/src/backend/state.dart'; | |
| 6 import 'package:unittest/unittest.dart'; | |
| 7 | |
| 8 import 'utils.dart'; | |
| 9 | |
| 10 void main() { | |
| 11 group("supports a function with this many arguments:", () { | |
| 12 test("0", () { | |
| 13 var callbackRun = false; | |
| 14 return runTest(() { | |
| 15 expectAsync(() { | |
| 16 callbackRun = true; | |
| 17 })(); | |
| 18 }).then((liveTest) { | |
| 19 expectTestPassed(liveTest); | |
| 20 expect(callbackRun, isTrue); | |
| 21 }); | |
| 22 }); | |
| 23 | |
| 24 test("1", () { | |
| 25 var callbackRun = false; | |
| 26 return runTest(() { | |
| 27 expectAsync((arg) { | |
| 28 expect(arg, equals(1)); | |
| 29 callbackRun = true; | |
| 30 })(1); | |
| 31 }).then((liveTest) { | |
| 32 expectTestPassed(liveTest); | |
| 33 expect(callbackRun, isTrue); | |
| 34 }); | |
| 35 }); | |
| 36 | |
| 37 test("2", () { | |
| 38 var callbackRun = false; | |
| 39 return runTest(() { | |
| 40 expectAsync((arg1, arg2) { | |
| 41 expect(arg1, equals(1)); | |
| 42 expect(arg2, equals(2)); | |
| 43 callbackRun = true; | |
| 44 })(1, 2); | |
| 45 }).then((liveTest) { | |
| 46 expectTestPassed(liveTest); | |
| 47 expect(callbackRun, isTrue); | |
| 48 }); | |
| 49 }); | |
| 50 | |
| 51 test("3", () { | |
| 52 var callbackRun = false; | |
| 53 return runTest(() { | |
| 54 expectAsync((arg1, arg2, arg3) { | |
| 55 expect(arg1, equals(1)); | |
| 56 expect(arg2, equals(2)); | |
| 57 expect(arg3, equals(3)); | |
| 58 callbackRun = true; | |
| 59 })(1, 2, 3); | |
| 60 }).then((liveTest) { | |
| 61 expectTestPassed(liveTest); | |
| 62 expect(callbackRun, isTrue); | |
| 63 }); | |
| 64 }); | |
| 65 | |
| 66 test("4", () { | |
| 67 var callbackRun = false; | |
| 68 return runTest(() { | |
| 69 expectAsync((arg1, arg2, arg3, arg4) { | |
| 70 expect(arg1, equals(1)); | |
| 71 expect(arg2, equals(2)); | |
| 72 expect(arg3, equals(3)); | |
| 73 expect(arg4, equals(4)); | |
| 74 callbackRun = true; | |
| 75 })(1, 2, 3, 4); | |
| 76 }).then((liveTest) { | |
| 77 expectTestPassed(liveTest); | |
| 78 expect(callbackRun, isTrue); | |
| 79 }); | |
| 80 }); | |
| 81 | |
| 82 test("5", () { | |
| 83 var callbackRun = false; | |
| 84 return runTest(() { | |
| 85 expectAsync((arg1, arg2, arg3, arg4, arg5) { | |
| 86 expect(arg1, equals(1)); | |
| 87 expect(arg2, equals(2)); | |
| 88 expect(arg3, equals(3)); | |
| 89 expect(arg4, equals(4)); | |
| 90 expect(arg5, equals(5)); | |
| 91 callbackRun = true; | |
| 92 })(1, 2, 3, 4, 5); | |
| 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 }); | |
| 326 }); | |
| 327 }); | |
| 328 } | |
| OLD | NEW |