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.error, e2.error); | 32 Expect.equals(e1.error, e2.error); |
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 Future err2 = new Future.immediateError(new AsyncError("AsyncError"))..catchEr
ror((_){}); | 38 Future err2 = new Future.error(new AsyncError("AsyncError"))..catchError((_){}
); |
39 compare(() => 42); | 39 compare(() => 42); |
40 compare(() => val); | 40 compare(() => val); |
41 compare(() { throw "Flif"; }); | 41 compare(() { throw "Flif"; }); |
42 compare(() { throw new AsyncError("AsyncFlif"); }); | 42 compare(() { throw new AsyncError("AsyncFlif"); }); |
43 compare(() => err1); | 43 compare(() => err1); |
44 compare(() => err2); | 44 compare(() => err2); |
| 45 bool hasExecuted = false; |
| 46 compare(() { |
| 47 hasExecuted = true; |
| 48 return 499; |
| 49 }); |
| 50 Expect.isTrue(hasExecuted); |
45 } | 51 } |
46 | 52 |
47 testNeverComplete() { | 53 testNeverComplete() { |
48 final completer = new Completer<int>(); | 54 final completer = new Completer<int>(); |
49 final future = completer.future; | 55 final future = completer.future; |
50 future.then((v) => Expect.fails("Value not expected")); | 56 future.then((v) => Expect.fails("Value not expected")); |
51 future.catchError((e) => Expect.fails("Value not expected")); | 57 future.catchError((e) => Expect.fails("Value not expected")); |
52 } | 58 } |
53 | 59 |
54 testComplete() { | 60 testComplete() { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 }, | 239 }, |
234 onDone: () { | 240 onDone: () { |
235 Expect.isTrue(gotValue); | 241 Expect.isTrue(gotValue); |
236 port.close(); | 242 port.close(); |
237 }); | 243 }); |
238 } | 244 } |
239 | 245 |
240 testFutureAsStreamCompleteImmediate() { | 246 testFutureAsStreamCompleteImmediate() { |
241 bool gotValue = false; | 247 bool gotValue = false; |
242 var port = new ReceivePort(); | 248 var port = new ReceivePort(); |
243 new Future.immediate("value").asStream().listen( | 249 new Future.value("value").asStream().listen( |
244 (data) { | 250 (data) { |
245 Expect.isFalse(gotValue); | 251 Expect.isFalse(gotValue); |
246 gotValue = true; | 252 gotValue = true; |
247 Expect.equals("value", data); | 253 Expect.equals("value", data); |
248 }, | 254 }, |
249 onDone: () { | 255 onDone: () { |
250 Expect.isTrue(gotValue); | 256 Expect.isTrue(gotValue); |
251 port.close(); | 257 port.close(); |
252 }); | 258 }); |
253 } | 259 } |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 }); | 561 }); |
556 | 562 |
557 completer.completeError(error); | 563 completer.completeError(error); |
558 } | 564 } |
559 | 565 |
560 testChainedFutureValue() { | 566 testChainedFutureValue() { |
561 final completer = new Completer(); | 567 final completer = new Completer(); |
562 final future = completer.future; | 568 final future = completer.future; |
563 var port = new ReceivePort(); | 569 var port = new ReceivePort(); |
564 | 570 |
565 future.then((v) => new Future.immediate(v * 2)) | 571 future.then((v) => new Future.value(v * 2)) |
566 .then((v) { | 572 .then((v) { |
567 Expect.equals(42, v); | 573 Expect.equals(42, v); |
568 port.close(); | 574 port.close(); |
569 }); | 575 }); |
570 completer.complete(21); | 576 completer.complete(21); |
571 } | 577 } |
572 | 578 |
573 testChainedFutureValueDelay() { | 579 testChainedFutureValueDelay() { |
574 final completer = new Completer(); | 580 final completer = new Completer(); |
575 final future = completer.future; | 581 final future = completer.future; |
(...skipping 15 matching lines...) Expand all Loading... |
591 .then((v) { | 597 .then((v) { |
592 Expect.isNull(v); | 598 Expect.isNull(v); |
593 port.close(); | 599 port.close(); |
594 }); | 600 }); |
595 } | 601 } |
596 testChainedFutureError() { | 602 testChainedFutureError() { |
597 final completer = new Completer(); | 603 final completer = new Completer(); |
598 final future = completer.future; | 604 final future = completer.future; |
599 var port = new ReceivePort(); | 605 var port = new ReceivePort(); |
600 | 606 |
601 future.then((v) => new Future.immediateError("Fehler")) | 607 future.then((v) => new Future.error("Fehler")) |
602 .then((v) { Expect.fail("unreachable!"); }, onError: (e) { | 608 .then((v) { Expect.fail("unreachable!"); }, onError: (e) { |
603 Expect.equals("Fehler", e.error); | 609 Expect.equals("Fehler", e.error); |
604 port.close(); | 610 port.close(); |
605 }); | 611 }); |
606 completer.complete(21); | 612 completer.complete(21); |
607 } | 613 } |
608 | 614 |
609 main() { | 615 main() { |
610 testImmediate(); | 616 testValue(); |
611 testOf(); | 617 testSync(); |
612 testNeverComplete(); | 618 testNeverComplete(); |
613 | 619 |
614 testComplete(); | 620 testComplete(); |
615 testCompleteWithSuccessHandlerBeforeComplete(); | 621 testCompleteWithSuccessHandlerBeforeComplete(); |
616 testCompleteWithSuccessHandlerAfterComplete(); | 622 testCompleteWithSuccessHandlerAfterComplete(); |
617 testCompleteManySuccessHandlers(); | 623 testCompleteManySuccessHandlers(); |
618 testCompleteWithAsyncError(); | 624 testCompleteWithAsyncError(); |
619 | 625 |
620 testException(); | 626 testException(); |
621 testExceptionHandler(); | 627 testExceptionHandler(); |
(...skipping 21 matching lines...) Expand all Loading... |
643 testFutureCatchThrowsAsync(); | 649 testFutureCatchThrowsAsync(); |
644 testFutureWhenThrowsAsync(); | 650 testFutureWhenThrowsAsync(); |
645 testFutureCatchRethrowsAsync(); | 651 testFutureCatchRethrowsAsync(); |
646 | 652 |
647 testChainedFutureValue(); | 653 testChainedFutureValue(); |
648 testChainedFutureValueDelay(); | 654 testChainedFutureValueDelay(); |
649 testChainedFutureError(); | 655 testChainedFutureError(); |
650 } | 656 } |
651 | 657 |
652 | 658 |
OLD | NEW |