| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Tests for Future.immediate | 5 // Tests for Future.immediate |
| 6 | 6 |
| 7 testImmediate() { | 7 testImmediate() { |
| 8 final future = new Future<String>.immediate("42"); | 8 final future = new Future<String>.immediate("42"); |
| 9 Expect.isTrue(future.isComplete); | 9 Expect.isTrue(future.isComplete); |
| 10 Expect.isTrue(future.hasValue); | 10 Expect.isTrue(future.hasValue); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Tests for [handleException] | 201 // Tests for [handleException] |
| 202 | 202 |
| 203 testException() { | 203 testException() { |
| 204 final completer = new Completer<int>(); | 204 final completer = new Completer<int>(); |
| 205 final future = completer.future; | 205 final future = completer.future; |
| 206 final ex = new Exception(); | 206 final ex = new Exception(); |
| 207 future.then((_) {}); // exception is thrown if we plan to use the value | 207 future.then((_) {}); // exception is thrown if we plan to use the value |
| 208 Expect.throws( | 208 Expect.throws( |
| 209 () { completer.completeException(ex); }, | 209 () { completer.completeException(ex); }, |
| 210 check: (e) => e == ex); | 210 check: (e) => e == ex); |
| 211 } | 211 } |
| 212 | 212 |
| 213 testExceptionNoSuccessListeners() { | 213 testExceptionNoSuccessListeners() { |
| 214 final completer = new Completer<int>(); | 214 final completer = new Completer<int>(); |
| 215 final future = completer.future; | 215 final future = completer.future; |
| 216 final ex = new Exception(); | 216 final ex = new Exception(); |
| 217 completer.completeException(ex); // future.then is not called, so no exception | 217 completer.completeException(ex); // future.then is not called, so no exception |
| 218 } | 218 } |
| 219 | 219 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 testExceptionHandlerReturnsFalse() { | 255 testExceptionHandlerReturnsFalse() { |
| 256 final completer = new Completer<int>(); | 256 final completer = new Completer<int>(); |
| 257 final future = completer.future; | 257 final future = completer.future; |
| 258 final ex = new Exception(); | 258 final ex = new Exception(); |
| 259 | 259 |
| 260 bool reached = false; | 260 bool reached = false; |
| 261 future.then((_) {}); // ensure exception is thrown... | 261 future.then((_) {}); // ensure exception is thrown... |
| 262 future.handleException((e) { return false; }); | 262 future.handleException((e) { return false; }); |
| 263 future.handleException((e) { reached = true; return false; }); // overshadowed | 263 future.handleException((e) { reached = true; return false; }); // overshadowed |
| 264 Expect.throws( | 264 Expect.throws( |
| 265 () { completer.completeException(ex); }, | 265 () { completer.completeException(ex); }, |
| 266 check: (e) => e == ex); | 266 check: (e) => e == ex); |
| 267 Expect.isTrue(reached); | 267 Expect.isTrue(reached); |
| 268 } | 268 } |
| 269 | 269 |
| 270 testExceptionHandlerReturnsFalse2() { | 270 testExceptionHandlerReturnsFalse2() { |
| 271 final completer = new Completer<int>(); | 271 final completer = new Completer<int>(); |
| 272 final future = completer.future; | 272 final future = completer.future; |
| 273 final ex = new Exception(); | 273 final ex = new Exception(); |
| 274 | 274 |
| 275 bool reached = false; | 275 bool reached = false; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 296 final future = completer.future; | 296 final future = completer.future; |
| 297 final ex = new Exception(); | 297 final ex = new Exception(); |
| 298 | 298 |
| 299 var ex2; | 299 var ex2; |
| 300 completer.completeException(ex); | 300 completer.completeException(ex); |
| 301 future.handleException((e) { ex2 = e; return false; }); | 301 future.handleException((e) { ex2 = e; return false; }); |
| 302 Expect.throws(() { future.then((e) { }); }); | 302 Expect.throws(() { future.then((e) { }); }); |
| 303 Expect.equals(ex, ex2); | 303 Expect.equals(ex, ex2); |
| 304 } | 304 } |
| 305 | 305 |
| 306 // Tests for accessing the exception call stack. |
| 307 |
| 308 testCallStackThrowsIfNotComplete() { |
| 309 var exception; |
| 310 try { |
| 311 new Completer().future.callStack; |
| 312 } catch (var ex) { |
| 313 exception = ex; |
| 314 } |
| 315 |
| 316 Expect.isTrue(exception is FutureNotCompleteException); |
| 317 } |
| 318 |
| 319 testCallStackIsNullIfCompletedSuccessfully() { |
| 320 Expect.isNull(new Future.immediate('blah').callStack); |
| 321 } |
| 322 |
| 323 testCallStackReturnsCallstackPassedToCompleteException() { |
| 324 final completer = new Completer(); |
| 325 final future = completer.future; |
| 326 |
| 327 final callstack = 'fake call stack'; |
| 328 completer.completeException(new Exception(), callstack); |
| 329 Expect.equals(callstack, future.callStack); |
| 330 } |
| 331 |
| 332 testCallStackIsCapturedIfTransformCallbackThrows() { |
| 333 final completer = new Completer(); |
| 334 final transformed = completer.future.transform((_) { |
| 335 throw 'whoops!'; |
| 336 }); |
| 337 |
| 338 final callstack = 'fake call stack'; |
| 339 completer.complete('blah'); |
| 340 Expect.isNotNull(transformed.callStack); |
| 341 } |
| 342 |
| 343 testCallStackIsCapturedIfChainCallbackThrows() { |
| 344 final completer = new Completer(); |
| 345 final chained = completer.future.chain((_) { |
| 346 throw 'whoops!'; |
| 347 }); |
| 348 |
| 349 final callstack = 'fake call stack'; |
| 350 completer.complete('blah'); |
| 351 Expect.isNotNull(chained.callStack); |
| 352 } |
| 353 |
| 306 // Tests for mixed usage of [onComplete], [then], and [handleException] | 354 // Tests for mixed usage of [onComplete], [then], and [handleException] |
| 307 | 355 |
| 308 testCompleteWithCompletionAndSuccessHandlers() { | 356 testCompleteWithCompletionAndSuccessHandlers() { |
| 309 final completer = new Completer<int>(); | 357 final completer = new Completer<int>(); |
| 310 final future = completer.future; | 358 final future = completer.future; |
| 311 | 359 |
| 312 var valueFromSuccessHandler; | 360 var valueFromSuccessHandler; |
| 313 var valueFromCompletionHandler; | 361 var valueFromCompletionHandler; |
| 314 future.onComplete((f) { | 362 future.onComplete((f) { |
| 315 Expect.isNotNull(valueFromSuccessHandler); | 363 Expect.isNotNull(valueFromSuccessHandler); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 testCompleteWithSuccessHandlerAfterComplete(); | 511 testCompleteWithSuccessHandlerAfterComplete(); |
| 464 testCompleteManySuccessHandlers(); | 512 testCompleteManySuccessHandlers(); |
| 465 testException(); | 513 testException(); |
| 466 testExceptionHandler(); | 514 testExceptionHandler(); |
| 467 testExceptionHandlerReturnsTrue(); | 515 testExceptionHandlerReturnsTrue(); |
| 468 testExceptionHandlerReturnsTrue2(); | 516 testExceptionHandlerReturnsTrue2(); |
| 469 testExceptionHandlerReturnsFalse(); | 517 testExceptionHandlerReturnsFalse(); |
| 470 testExceptionHandlerReturnsFalse2(); | 518 testExceptionHandlerReturnsFalse2(); |
| 471 testExceptionHandlerAfterCompleteThenNotCalled(); | 519 testExceptionHandlerAfterCompleteThenNotCalled(); |
| 472 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); | 520 testExceptionHandlerAfterCompleteReturnsFalseThenThrows(); |
| 521 testCallStackThrowsIfNotComplete(); |
| 522 testCallStackIsNullIfCompletedSuccessfully(); |
| 523 testCallStackReturnsCallstackPassedToCompleteException(); |
| 524 testCallStackIsCapturedIfTransformCallbackThrows(); |
| 525 testCallStackIsCapturedIfChainCallbackThrows(); |
| 473 testCompleteWithCompletionAndSuccessHandlers(); | 526 testCompleteWithCompletionAndSuccessHandlers(); |
| 474 testExceptionWithCompletionAndSuccessHandlers(); | 527 testExceptionWithCompletionAndSuccessHandlers(); |
| 475 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); | 528 testExceptionWithCompletionAndSuccessAndExceptionHandlers(); |
| 476 testTransformSuccess(); | 529 testTransformSuccess(); |
| 477 testTransformFutureFails(); | 530 testTransformFutureFails(); |
| 478 testTransformTransformerFails(); | 531 testTransformTransformerFails(); |
| 479 testChainSuccess(); | 532 testChainSuccess(); |
| 480 testChainFirstFutureFails(); | 533 testChainFirstFutureFails(); |
| 481 testChainTransformerFails(); | 534 testChainTransformerFails(); |
| 482 testChainSecondFutureFails(); | 535 testChainSecondFutureFails(); |
| 483 } | 536 } |
| OLD | NEW |