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 library future_test; | 5 library future_test; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
11 const Duration MS = const Duration(milliseconds: 1); | 11 const Duration MS = const Duration(milliseconds: 1); |
12 | 12 |
13 testImmediate() { | 13 testValue() { |
14 final future = new Future<String>.immediate("42"); | 14 final future = new Future<String>.value("42"); |
15 var port = new ReceivePort(); | 15 var port = new ReceivePort(); |
16 future.then((x) { | 16 future.then((x) { |
17 Expect.equals("42", x); | 17 Expect.equals("42", x); |
18 port.close(); | 18 port.close(); |
19 }); | 19 }); |
20 } | 20 } |
21 | 21 |
22 testOf() { | 22 testSync() { |
23 compare(func) { | 23 compare(func) { |
24 // Compare the results of the following two futures. | 24 // Compare the results of the following two futures. |
25 Future f1 = new Future.of(func); | 25 Future f1 = new Future.sync(func); |
26 Future f2 = new Future.immediate(null).then((_) => func()); | 26 Future f2 = new Future.value().then((_) => func()); |
27 f2.catchError((_){}); // I'll get the error later. | 27 f2.catchError((_){}); // I'll get the error later. |
28 f1.then((v1) { f2.then((v2) { Expect.equals(v1, v2); }); }, | 28 f1.then((v1) { f2.then((v2) { Expect.equals(v1, v2); }); }, |
29 onError: (e1) { | 29 onError: (e1) { |
30 f2.then((_) { Expect.fail("Expected error"); }, | 30 f2.then((_) { Expect.fail("Expected error"); }, |
31 onError: (e2) { | 31 onError: (e2) { |
32 Expect.equals(e1, e2); | 32 Expect.equals(e1, e2); |
33 }); | 33 }); |
34 }); | 34 }); |
35 } | 35 } |
36 Future val = new Future.immediate(42); | 36 Future val = new Future.value(42); |
37 Future err1 = new Future.immediateError("Error")..catchError((_){}); | 37 Future err1 = new Future.error("Error")..catchError((_){}); |
38 try { | 38 try { |
39 throw new List(0); | 39 throw new List(0); |
40 } catch (e, st) { | 40 } catch (e, st) { |
41 Future err2 = new Future.immediateError(e, st)..catchError((_){}); | 41 Future err2 = new Future.error(e, st)..catchError((_){}); |
42 } | 42 } |
43 compare(() => 42); | 43 compare(() => 42); |
44 compare(() => val); | 44 compare(() => val); |
45 compare(() { throw "Flif"; }); | 45 compare(() { throw "Flif"; }); |
46 compare(() => err1); | 46 compare(() => err1); |
| 47 bool hasExecuted = false; |
| 48 compare(() { |
| 49 hasExecuted = true; |
| 50 return 499; |
| 51 }); |
| 52 Expect.isTrue(hasExecuted); |
47 } | 53 } |
48 | 54 |
49 testNeverComplete() { | 55 testNeverComplete() { |
50 final completer = new Completer<int>(); | 56 final completer = new Completer<int>(); |
51 final future = completer.future; | 57 final future = completer.future; |
52 future.then((v) => Expect.fails("Value not expected")); | 58 future.then((v) => Expect.fails("Value not expected")); |
53 future.catchError((e) => Expect.fails("Value not expected")); | 59 future.catchError((e) => Expect.fails("Value not expected")); |
54 } | 60 } |
55 | 61 |
56 testComplete() { | 62 testComplete() { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 }, | 241 }, |
236 onDone: () { | 242 onDone: () { |
237 Expect.isTrue(gotValue); | 243 Expect.isTrue(gotValue); |
238 port.close(); | 244 port.close(); |
239 }); | 245 }); |
240 } | 246 } |
241 | 247 |
242 testFutureAsStreamCompleteImmediate() { | 248 testFutureAsStreamCompleteImmediate() { |
243 bool gotValue = false; | 249 bool gotValue = false; |
244 var port = new ReceivePort(); | 250 var port = new ReceivePort(); |
245 new Future.immediate("value").asStream().listen( | 251 new Future.value("value").asStream().listen( |
246 (data) { | 252 (data) { |
247 Expect.isFalse(gotValue); | 253 Expect.isFalse(gotValue); |
248 gotValue = true; | 254 gotValue = true; |
249 Expect.equals("value", data); | 255 Expect.equals("value", data); |
250 }, | 256 }, |
251 onDone: () { | 257 onDone: () { |
252 Expect.isTrue(gotValue); | 258 Expect.isTrue(gotValue); |
253 port.close(); | 259 port.close(); |
254 }); | 260 }); |
255 } | 261 } |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 }); | 563 }); |
558 | 564 |
559 completer.completeError(error); | 565 completer.completeError(error); |
560 } | 566 } |
561 | 567 |
562 testChainedFutureValue() { | 568 testChainedFutureValue() { |
563 final completer = new Completer(); | 569 final completer = new Completer(); |
564 final future = completer.future; | 570 final future = completer.future; |
565 var port = new ReceivePort(); | 571 var port = new ReceivePort(); |
566 | 572 |
567 future.then((v) => new Future.immediate(v * 2)) | 573 future.then((v) => new Future.value(v * 2)) |
568 .then((v) { | 574 .then((v) { |
569 Expect.equals(42, v); | 575 Expect.equals(42, v); |
570 port.close(); | 576 port.close(); |
571 }); | 577 }); |
572 completer.complete(21); | 578 completer.complete(21); |
573 } | 579 } |
574 | 580 |
575 testChainedFutureValueDelay() { | 581 testChainedFutureValueDelay() { |
576 final completer = new Completer(); | 582 final completer = new Completer(); |
577 final future = completer.future; | 583 final future = completer.future; |
(...skipping 15 matching lines...) Expand all Loading... |
593 .then((v) { | 599 .then((v) { |
594 Expect.isNull(v); | 600 Expect.isNull(v); |
595 port.close(); | 601 port.close(); |
596 }); | 602 }); |
597 } | 603 } |
598 testChainedFutureError() { | 604 testChainedFutureError() { |
599 final completer = new Completer(); | 605 final completer = new Completer(); |
600 final future = completer.future; | 606 final future = completer.future; |
601 var port = new ReceivePort(); | 607 var port = new ReceivePort(); |
602 | 608 |
603 future.then((v) => new Future.immediateError("Fehler")) | 609 future.then((v) => new Future.error("Fehler")) |
604 .then((v) { Expect.fail("unreachable!"); }, onError: (error) { | 610 .then((v) { Expect.fail("unreachable!"); }, onError: (error) { |
605 Expect.equals("Fehler", error); | 611 Expect.equals("Fehler", error); |
606 port.close(); | 612 port.close(); |
607 }); | 613 }); |
608 completer.complete(21); | 614 completer.complete(21); |
609 } | 615 } |
610 | 616 |
611 main() { | 617 main() { |
612 testImmediate(); | 618 testValue(); |
613 testOf(); | 619 testSync(); |
614 testNeverComplete(); | 620 testNeverComplete(); |
615 | 621 |
616 testComplete(); | 622 testComplete(); |
617 testCompleteWithSuccessHandlerBeforeComplete(); | 623 testCompleteWithSuccessHandlerBeforeComplete(); |
618 testCompleteWithSuccessHandlerAfterComplete(); | 624 testCompleteWithSuccessHandlerAfterComplete(); |
619 testCompleteManySuccessHandlers(); | 625 testCompleteManySuccessHandlers(); |
620 testCompleteWithError(); | 626 testCompleteWithError(); |
621 | 627 |
622 testException(); | 628 testException(); |
623 testExceptionHandler(); | 629 testExceptionHandler(); |
(...skipping 21 matching lines...) Expand all Loading... |
645 testFutureCatchThrowsAsync(); | 651 testFutureCatchThrowsAsync(); |
646 testFutureWhenThrowsAsync(); | 652 testFutureWhenThrowsAsync(); |
647 testFutureCatchRethrowsAsync(); | 653 testFutureCatchRethrowsAsync(); |
648 | 654 |
649 testChainedFutureValue(); | 655 testChainedFutureValue(); |
650 testChainedFutureValueDelay(); | 656 testChainedFutureValueDelay(); |
651 testChainedFutureError(); | 657 testChainedFutureError(); |
652 } | 658 } |
653 | 659 |
654 | 660 |
OLD | NEW |