| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 // TODO(ajohnsen): This test needs to be updated. | |
| 6 // Can Dart2JS on V8 somehow run it? | |
| 7 | |
| 8 // Tests for Future.immediate | |
| 9 import 'dart:async'; | |
| 10 import 'dart:isolate'; | |
| 11 | |
| 12 testImmediate() { | |
| 13 final future = new Future<String>.immediate("42"); | |
| 14 future.then((x) => Expect.equals("42", x)); | |
| 15 } | |
| 16 | |
| 17 // Tests for getters (value, exception, isComplete, isValue) | |
| 18 | |
| 19 testNeverComplete() { | |
| 20 final completer = new Completer<int>(); | |
| 21 final future = completer.future; | |
| 22 future.then((v) => Except.fails("Value not expected")); | |
| 23 future.catchError((e) => Except.fails("Value not expected")); | |
| 24 } | |
| 25 | |
| 26 testComplete() { | |
| 27 final completer = new Completer<int>(); | |
| 28 final future = completer.future; | |
| 29 | |
| 30 completer.complete(3); | |
| 31 | |
| 32 future.then((v) => Expect.equals(3, v)); | |
| 33 } | |
| 34 | |
| 35 // Tests for [then] | |
| 36 | |
| 37 testCompleteWithSuccessHandlerBeforeComplete() { | |
| 38 final completer = new Completer<int>(); | |
| 39 final future = completer.future; | |
| 40 | |
| 41 int before; | |
| 42 future.then((int v) { before = v; }); | |
| 43 Expect.isNull(before); | |
| 44 completer.complete(3); | |
| 45 | |
| 46 Expect.equals(3, before); | |
| 47 } | |
| 48 | |
| 49 testCompleteWithSuccessHandlerAfterComplete() { | |
| 50 final completer = new Completer<int>(); | |
| 51 final future = completer.future; | |
| 52 | |
| 53 int after; | |
| 54 completer.complete(3); | |
| 55 Expect.isNull(after); | |
| 56 | |
| 57 future.then((int v) { after = v; }); | |
| 58 | |
| 59 Expect.equals(3, after); | |
| 60 } | |
| 61 | |
| 62 testCompleteManySuccessHandlers() { | |
| 63 final completer = new Completer<int>(); | |
| 64 final future = completer.future; | |
| 65 int before; | |
| 66 int after1; | |
| 67 int after2; | |
| 68 | |
| 69 future.then((int v) { before = v; }); | |
| 70 completer.complete(3); | |
| 71 future.then((int v) { after1 = v; }); | |
| 72 future.then((int v) { after2 = v; }); | |
| 73 | |
| 74 Expect.equals(3, before); | |
| 75 Expect.equals(3, after1); | |
| 76 Expect.equals(3, after2); | |
| 77 } | |
| 78 | |
| 79 // Tests for [handleException] | |
| 80 | |
| 81 testException() { | |
| 82 final completer = new Completer<int>(); | |
| 83 final future = completer.future; | |
| 84 final ex = new Exception(); | |
| 85 // future.catchError((e) => print("got error"));//Expect.equals(e, ex)); | |
| 86 future.then((v) {print(v);}) | |
| 87 .catchError((e) => Expect.equals(e.error, ex)); | |
| 88 completer.completeError(ex); | |
| 89 } | |
| 90 | |
| 91 testExceptionNoSuccessListeners() { | |
| 92 final completer = new Completer<int>(); | |
| 93 final future = completer.future; | |
| 94 final ex = new Exception(); | |
| 95 completer.completeException(ex); // future.then is not called, so no exception | |
| 96 } | |
| 97 | |
| 98 testExceptionHandler() { | |
| 99 final completer = new Completer<int>(); | |
| 100 final future = completer.future; | |
| 101 final ex = new Exception(); | |
| 102 | |
| 103 var ex2; | |
| 104 future.catchError((e) { ex2 = e.error; }); | |
| 105 completer.completeError(ex); | |
| 106 Expect.equals(ex, ex2); | |
| 107 } | |
| 108 | |
| 109 testExceptionHandlerReturnsTrue() { | |
| 110 final completer = new Completer<int>(); | |
| 111 final future = completer.future; | |
| 112 final ex = new Exception(); | |
| 113 | |
| 114 bool reached = false; | |
| 115 future.catchError((e) { }); | |
| 116 future.catchError((e) { reached = true; }, test: (e) => false) | |
| 117 .catchError((e) {}); | |
| 118 completer.completeError(ex); | |
| 119 Expect.isFalse(reached); | |
| 120 } | |
| 121 | |
| 122 testExceptionHandlerReturnsTrue2() { | |
| 123 final completer = new Completer<int>(); | |
| 124 final future = completer.future; | |
| 125 final ex = new Exception(); | |
| 126 | |
| 127 bool reached = false; | |
| 128 future.catchError((e) { }, test: (e) => false) | |
| 129 .catchError((e) { reached = true; }); | |
| 130 completer.completeError(ex); | |
| 131 Expect.isTrue(reached); | |
| 132 } | |
| 133 | |
| 134 testExceptionHandlerReturnsFalse() { | |
| 135 final completer = new Completer<int>(); | |
| 136 final future = completer.future; | |
| 137 final ex = new Exception(); | |
| 138 | |
| 139 bool reached = false; | |
| 140 | |
| 141 future.catchError((e) { }); | |
| 142 | |
| 143 future.catchError((e) { reached = true; }, test: (e) => false) | |
| 144 .catchError((e) { }); | |
| 145 | |
| 146 completer.completeError(ex); | |
| 147 | |
| 148 Expect.isFalse(reached); | |
| 149 } | |
| 150 | |
| 151 testExceptionHandlerReturnsFalse2() { | |
| 152 final completer = new Completer<int>(); | |
| 153 final future = completer.future; | |
| 154 final ex = new Exception(); | |
| 155 | |
| 156 bool reached = false; | |
| 157 future.handleException((e) { return false; }); | |
| 158 future.handleException((e) { reached = true; return false; }); // overshadowed | |
| 159 completer.completeException(ex); // future.then is not called, so no exception | |
| 160 Expect.isTrue(reached); | |
| 161 } | |
| 162 | |
| 163 testExceptionHandlerAfterCompleteThenNotCalled() { | |
| 164 final completer = new Completer<int>(); | |
| 165 final future = completer.future; | |
| 166 final ex = new Exception(); | |
| 167 | |
| 168 var ex2; | |
| 169 completer.completeException(ex); | |
| 170 future.handleException((e) { ex2 = e; return true; }); | |
| 171 future.then((e) { }); | |
| 172 Expect.equals(ex, ex2); | |
| 173 } | |
| 174 | |
| 175 testExceptionHandlerAfterCompleteReturnsFalseThenThrows() { | |
| 176 final completer = new Completer<int>(); | |
| 177 final future = completer.future; | |
| 178 final ex = new Exception(); | |
| 179 | |
| 180 var ex2; | |
| 181 completer.completeException(ex); | |
| 182 future.handleException((e) { ex2 = e; return false; }); | |
| 183 Expect.throws(() { future.then((e) { }); }); | |
| 184 Expect.equals(ex, ex2); | |
| 185 } | |
| 186 | |
| 187 // Tests for accessing the exception call stack. | |
| 188 | |
| 189 testCallStackThrowsIfNotComplete() { | |
| 190 var exception; | |
| 191 try { | |
| 192 new Completer().future.stackTrace; | |
| 193 } catch (ex) { | |
| 194 exception = ex; | |
| 195 } | |
| 196 | |
| 197 Expect.isTrue(exception is FutureNotCompleteException); | |
| 198 } | |
| 199 | |
| 200 testCallStackIsNullIfCompletedSuccessfully() { | |
| 201 Expect.isNull(new Future.immediate('blah').stackTrace); | |
| 202 } | |
| 203 | |
| 204 testCallStackReturnsCallstackPassedToCompleteException() { | |
| 205 final completer = new Completer(); | |
| 206 final future = completer.future; | |
| 207 | |
| 208 final stackTrace = 'fake stack trace'; | |
| 209 completer.completeException(new Exception(), stackTrace); | |
| 210 Expect.equals(stackTrace, future.stackTrace); | |
| 211 } | |
| 212 | |
| 213 testCallStackIsCapturedIfTransformCallbackThrows() { | |
| 214 final completer = new Completer(); | |
| 215 final transformed = completer.future.then((_) { | |
| 216 throw 'whoops!'; | |
| 217 }); | |
| 218 | |
| 219 final stackTrace = 'fake stack trace'; | |
| 220 completer.complete('blah'); | |
| 221 Expect.isNotNull(transformed.stackTrace); | |
| 222 } | |
| 223 | |
| 224 testCallStackIsCapturedIfChainCallbackThrows() { | |
| 225 final completer = new Completer(); | |
| 226 final chained = completer.future.chain((_) { | |
| 227 throw 'whoops!'; | |
| 228 }); | |
| 229 | |
| 230 final stackTrace = 'fake stack trace'; | |
| 231 completer.complete('blah'); | |
| 232 Expect.isNotNull(chained.stackTrace); | |
| 233 } | |
| 234 | |
| 235 testCallStackIsPreservedIfExceptionIsRethrownInTransformException() { | |
| 236 final completer = new Completer(); | |
| 237 var chained = completer.future.chain((_) { | |
| 238 throw 'whoops!'; | |
| 239 }); | |
| 240 var transformed = chained.transformException((e) { | |
| 241 throw e; | |
| 242 }); | |
| 243 | |
| 244 completer.complete('blah'); | |
| 245 Expect.equals(transformed.stackTrace, chained.stackTrace); | |
| 246 } | |
| 247 | |
| 248 // Tests for mixed usage of [onComplete], [then], and [handleException] | |
| 249 | |
| 250 testCompleteWithCompletionAndSuccessHandlers() { | |
| 251 final completer = new Completer<int>(); | |
| 252 final future = completer.future; | |
| 253 | |
| 254 var valueFromSuccessHandler; | |
| 255 var valueFromCompletionHandler; | |
| 256 future.onComplete((f) { | |
| 257 Expect.isNotNull(valueFromSuccessHandler); | |
| 258 valueFromCompletionHandler = f.value; | |
| 259 }); | |
| 260 future.then((v) { | |
| 261 Expect.isNull(valueFromCompletionHandler); | |
| 262 valueFromSuccessHandler = v; | |
| 263 }); | |
| 264 completer.complete(42); | |
| 265 Expect.equals(42, valueFromSuccessHandler); | |
| 266 Expect.equals(42, valueFromCompletionHandler); | |
| 267 Expect.equals(42, future.value); | |
| 268 } | |
| 269 | |
| 270 testExceptionWithCompletionAndSuccessHandlers() { | |
| 271 final completer = new Completer<int>(); | |
| 272 final future = completer.future; | |
| 273 final ex = new Exception(); | |
| 274 | |
| 275 var exceptionFromCompleteHandler; | |
| 276 future.onComplete((f) { | |
| 277 Expect.equals(future, f); | |
| 278 Expect.isFalse(f.hasValue); | |
| 279 exceptionFromCompleteHandler = f.exception; | |
| 280 }); | |
| 281 future.then((v) => Expect.fail("Should not succeed")); | |
| 282 Expect.throws(() => completer.completeException(ex), (e) => e.source == ex); | |
| 283 Expect.equals(ex, exceptionFromCompleteHandler); | |
| 284 } | |
| 285 | |
| 286 testExceptionWithCompletionAndSuccessAndExceptionHandlers() { | |
| 287 final completer = new Completer<int>(); | |
| 288 final future = completer.future; | |
| 289 final ex = new Exception(); | |
| 290 | |
| 291 var exceptionFromCompleteHandler; | |
| 292 var exceptionFromExceptionHandler; | |
| 293 future.onComplete((f) { | |
| 294 Expect.equals(future, f); | |
| 295 Expect.isFalse(f.hasValue); | |
| 296 exceptionFromCompleteHandler = f.exception; | |
| 297 }); | |
| 298 future.handleException((e) { | |
| 299 exceptionFromExceptionHandler = e; | |
| 300 return true; | |
| 301 }); | |
| 302 future.then((v) => Expect.fail("Should not succeed")); | |
| 303 completer.completeException(ex); | |
| 304 Expect.equals(ex, exceptionFromCompleteHandler); | |
| 305 Expect.equals(ex, exceptionFromExceptionHandler); | |
| 306 } | |
| 307 | |
| 308 // Tests for Future.transform | |
| 309 | |
| 310 testTransformSuccess() { | |
| 311 final completer = new Completer<String>(); | |
| 312 final transformedFuture = completer.future.then((x) => "** $x **"); | |
| 313 Expect.isFalse(transformedFuture.isComplete); | |
| 314 completer.complete("42"); | |
| 315 Expect.equals("** 42 **", transformedFuture.value); | |
| 316 } | |
| 317 | |
| 318 testTransformFutureFails() { | |
| 319 final completer = new Completer<String>(); | |
| 320 final error = new Exception("Oh no!"); | |
| 321 final transformedFuture = completer.future.then((x) { | |
| 322 Expect.fail("transformer shouldn't be called"); | |
| 323 }); | |
| 324 Expect.isFalse(transformedFuture.isComplete); | |
| 325 completer.completeException(error); | |
| 326 Expect.equals(error, transformedFuture.exception); | |
| 327 } | |
| 328 | |
| 329 testTransformTransformerFails() { | |
| 330 final completer = new Completer<String>(); | |
| 331 final error = new Exception("Oh no!"); | |
| 332 final transformedFuture = completer.future.then((x) { throw error; }); | |
| 333 Expect.isFalse(transformedFuture.isComplete); | |
| 334 transformedFuture.then((v) => null); | |
| 335 Expect.throws(() => completer.complete("42"), (e) => e.source == error); | |
| 336 Expect.equals(error, transformedFuture.exception); | |
| 337 } | |
| 338 | |
| 339 // Tests for Future.chain | |
| 340 | |
| 341 testChainSuccess() { | |
| 342 final completerA = new Completer<String>(); | |
| 343 final completerB = new Completer<String>(); | |
| 344 final chainedFuture = completerA.future.chain((x) { | |
| 345 Expect.equals("42", x); | |
| 346 return completerB.future; | |
| 347 }); | |
| 348 Expect.isFalse(chainedFuture.isComplete); | |
| 349 completerA.complete("42"); | |
| 350 Expect.isFalse(chainedFuture.isComplete); | |
| 351 completerB.complete("43"); | |
| 352 Expect.equals("43", chainedFuture.value); | |
| 353 } | |
| 354 | |
| 355 testChainFirstFutureFails() { | |
| 356 final completerA = new Completer<String>(); | |
| 357 final error = new Exception("Oh no!"); | |
| 358 final chainedFuture = completerA.future.chain((x) { | |
| 359 Expect.fail("transformer shouldn't be called"); | |
| 360 }); | |
| 361 Expect.isFalse(chainedFuture.isComplete); | |
| 362 completerA.completeException(error); | |
| 363 Expect.equals(error, chainedFuture.exception); | |
| 364 } | |
| 365 | |
| 366 testChainTransformerFails() { | |
| 367 final completerA = new Completer<String>(); | |
| 368 final error = new Exception("Oh no!"); | |
| 369 final chainedFuture = completerA.future.chain((x) { | |
| 370 Expect.equals("42", x); | |
| 371 throw error; | |
| 372 }); | |
| 373 chainedFuture.then((v) => null); | |
| 374 Expect.isFalse(chainedFuture.isComplete); | |
| 375 Expect.throws(() => completerA.complete("42"), (e) => e.source == error); | |
| 376 Expect.equals(error, chainedFuture.exception); | |
| 377 } | |
| 378 | |
| 379 testChainSecondFutureFails() { | |
| 380 final completerA = new Completer<String>(); | |
| 381 final completerB = new Completer<String>(); | |
| 382 final error = new Exception("Oh no!"); | |
| 383 final chainedFuture = completerA.future.chain((x) { | |
| 384 Expect.equals("42", x); | |
| 385 return completerB.future; | |
| 386 }); | |
| 387 Expect.isFalse(chainedFuture.isComplete); | |
| 388 completerA.complete("42"); | |
| 389 Expect.isFalse(chainedFuture.isComplete); | |
| 390 completerB.completeException(error); | |
| 391 Expect.equals(error, chainedFuture.exception); | |
| 392 } | |
| 393 | |
| 394 // Tests for Future.transformException | |
| 395 | |
| 396 testTransformExceptionCompletesNormally() { | |
| 397 final completer = new Completer<String>(); | |
| 398 var called = false; | |
| 399 | |
| 400 final transformedFuture = completer.future.transformException((ex) { | |
| 401 Expect.fail("should not get here"); | |
| 402 }); | |
| 403 | |
| 404 completer.complete("value"); | |
| 405 Expect.isTrue(transformedFuture.isComplete); | |
| 406 Expect.equals("value", transformedFuture.value); | |
| 407 } | |
| 408 | |
| 409 testTransformExceptionThrows() { | |
| 410 final completer = new Completer<String>(); | |
| 411 var called = false; | |
| 412 | |
| 413 final transformedFuture = completer.future.transformException((ex) { | |
| 414 Expect.equals("original error", ex); | |
| 415 called = true; | |
| 416 throw "transformed error"; | |
| 417 }); | |
| 418 | |
| 419 completer.completeException("original error"); | |
| 420 Expect.isTrue(called); | |
| 421 Expect.isTrue(transformedFuture.isComplete); | |
| 422 Expect.equals("transformed error", transformedFuture.exception); | |
| 423 } | |
| 424 | |
| 425 testTransformExceptionReturns() { | |
| 426 final completer = new Completer<String>(); | |
| 427 var called = false; | |
| 428 | |
| 429 final transformedFuture = completer.future.transformException((ex) { | |
| 430 Expect.equals("original error", ex); | |
| 431 called = true; | |
| 432 return "transformed value"; | |
| 433 }); | |
| 434 | |
| 435 completer.completeException("original error"); | |
| 436 Expect.isTrue(called); | |
| 437 Expect.isTrue(transformedFuture.isComplete); | |
| 438 Expect.equals("transformed value", transformedFuture.value); | |
| 439 } | |
| 440 | |
| 441 testTransformExceptionReturnsAFuture() { | |
| 442 final completer = new Completer<String>(); | |
| 443 var called = false; | |
| 444 | |
| 445 final returnedCompleter = new Completer<String>(); | |
| 446 | |
| 447 final transformedFuture = completer.future.transformException((ex) { | |
| 448 Expect.equals("original error", ex); | |
| 449 called = true; | |
| 450 return returnedCompleter.future; | |
| 451 }); | |
| 452 | |
| 453 completer.completeException("original error"); | |
| 454 Expect.isTrue(called); | |
| 455 Expect.isFalse(transformedFuture.isComplete); | |
| 456 | |
| 457 returnedCompleter.complete("transformed value"); | |
| 458 Expect.isTrue(transformedFuture.isComplete); | |
| 459 Expect.equals("transformed value", transformedFuture.value); | |
| 460 } | |
| 461 | |
| 462 // Tests for branching exceptions | |
| 463 | |
| 464 testExceptionTravelsAlongBothBranches() { | |
| 465 var results = <int>[]; | |
| 466 | |
| 467 var completer = new Completer(); | |
| 468 var branch1 = completer.future.then((_) => null); | |
| 469 var branch2 = completer.future.then((_) => null); | |
| 470 | |
| 471 branch1.handleException((e) { | |
| 472 results.add(1); | |
| 473 return true; | |
| 474 }); | |
| 475 | |
| 476 branch2.handleException((e) { | |
| 477 results.add(2); | |
| 478 return true; | |
| 479 }); | |
| 480 | |
| 481 completer.completeException("error"); | |
| 482 Expect.setEquals([1, 2], results); | |
| 483 } | |
| 484 | |
| 485 testExceptionTravelsAlongBothBranchesAfterComplete() { | |
| 486 var results = <int>[]; | |
| 487 | |
| 488 var completer = new Completer(); | |
| 489 completer.completeException("error"); | |
| 490 | |
| 491 var branch1 = completer.future.then((_) => null); | |
| 492 var branch2 = completer.future.then((_) => null); | |
| 493 | |
| 494 branch1.handleException((e) { | |
| 495 results.add(1); | |
| 496 return true; | |
| 497 }); | |
| 498 | |
| 499 branch2.handleException((e) { | |
| 500 results.add(2); | |
| 501 return true; | |
| 502 }); | |
| 503 | |
| 504 Expect.setEquals([1, 2], results); | |
| 505 } | |
| 506 | |
| 507 testExceptionIsHandledInBaseAndBranch() { | |
| 508 var results = <String>[]; | |
| 509 | |
| 510 var completer = new Completer(); | |
| 511 var branch = completer.future.then((_) => null); | |
| 512 | |
| 513 completer.future.handleException((e) { | |
| 514 results.add("base"); | |
| 515 return true; | |
| 516 }); | |
| 517 | |
| 518 branch.handleException((e) { | |
| 519 results.add("branch"); | |
| 520 return true; | |
| 521 }); | |
| 522 | |
| 523 completer.completeException("error"); | |
| 524 Expect.setEquals(["base", "branch"], results); | |
| 525 } | |
| 526 | |
| 527 testExceptionIsHandledInBaseAndBranchAfterComplete() { | |
| 528 var results = <String>[]; | |
| 529 | |
| 530 var completer = new Completer(); | |
| 531 completer.completeException("error"); | |
| 532 | |
| 533 var branch = completer.future.then((_) => null); | |
| 534 | |
| 535 completer.future.handleException((e) { | |
| 536 results.add("base"); | |
| 537 return true; | |
| 538 }); | |
| 539 | |
| 540 branch.handleException((e) { | |
| 541 results.add("branch"); | |
| 542 return true; | |
| 543 }); | |
| 544 | |
| 545 Expect.setEquals(["base", "branch"], results); | |
| 546 } | |
| 547 | |
| 548 main() { | |
| 549 // /* | |
| 550 testImmediate(); | |
| 551 testNeverComplete(); | |
| 552 testComplete(); | |
| 553 testCompleteWithSuccessHandlerBeforeComplete(); | |
| 554 testCompleteWithSuccessHandlerAfterComplete(); | |
| 555 testCompleteManySuccessHandlers(); | |
| 556 testException(); | |
| 557 testExceptionHandler(); | |
| 558 testExceptionHandlerReturnsTrue(); | |
| 559 testExceptionHandlerReturnsTrue2(); | |
| 560 testExceptionHandlerReturnsFalse(); | |
| 561 // */ | |
| 562 /* | |
| 563 */ | |
| 564 /* | |
| 565 testExceptionHandlerReturnsFalse2(); | |
| 566 testExceptionHandlerAfterCompleteThenNotCalled(); | |
| 567 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); | |
| 568 testCallStackThrowsIfNotComplete(); | |
| 569 testCallStackIsNullIfCompletedSuccessfully(); | |
| 570 testCallStackReturnsCallstackPassedToCompleteException(); | |
| 571 testCallStackIsCapturedIfTransformCallbackThrows(); | |
| 572 testCallStackIsCapturedIfChainCallbackThrows(); | |
| 573 testCallStackIsPreservedIfExceptionIsRethrownInTransformException(); | |
| 574 testCompleteWithCompletionAndSuccessHandlers(); | |
| 575 testExceptionWithCompletionAndSuccessHandlers(); | |
| 576 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); | |
| 577 testTransformSuccess(); | |
| 578 testTransformFutureFails(); | |
| 579 testTransformTransformerFails(); | |
| 580 testChainSuccess(); | |
| 581 testChainFirstFutureFails(); | |
| 582 testChainTransformerFails(); | |
| 583 testChainSecondFutureFails(); | |
| 584 testTransformExceptionCompletesNormally(); | |
| 585 testTransformExceptionThrows(); | |
| 586 testTransformExceptionReturns(); | |
| 587 testTransformExceptionReturnsAFuture(); | |
| 588 testExceptionTravelsAlongBothBranches(); | |
| 589 testExceptionTravelsAlongBothBranchesAfterComplete(); | |
| 590 testExceptionIsHandledInBaseAndBranch(); | |
| 591 testExceptionIsHandledInBaseAndBranchAfterComplete(); | |
| 592 */ | |
| 593 } | |
| OLD | NEW |