OLD | NEW |
| (Empty) |
1 // Copyright (c) 2017, 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 library future_foreach_test; | |
6 | |
7 import 'package:async_helper/async_helper.dart'; | |
8 import "package:expect/expect.dart"; | |
9 import 'dart:async'; | |
10 | |
11 main() { | |
12 asyncStart(); | |
13 | |
14 testForeach(<int>[]); | |
15 testForeach(<int>[0]); | |
16 testForeach(<int>[0, 1, 2, 3]); | |
17 | |
18 testForeachIterableThrows( | |
19 new Iterable<int>.generate(5, (i) => i < 4 ? i : throw "ERROR"), | |
20 4, | |
21 "ERROR"); | |
22 | |
23 testForeachFunctionThrows(new Iterable<int>.generate(5, (x) => x), 4); | |
24 | |
25 asyncEnd(); | |
26 } | |
27 | |
28 void testForeach(Iterable<int> elements) { | |
29 for (var delay in [0, 1, 2]) { | |
30 asyncStart(); | |
31 int length = elements.length; | |
32 int count = 0; | |
33 int nesting = 0; | |
34 Future.forEach<int>(elements, (int value) { | |
35 Expect.isTrue(nesting == 0, "overlapping calls detected"); | |
36 if (delay == 0) return "something-$delay"; | |
37 nesting++; | |
38 var future; | |
39 if (delay == 1) { | |
40 future = new Future(() => null); | |
41 } else { | |
42 future = new Future.microtask(() => null); | |
43 } | |
44 return future.then<String>((_) { | |
45 Expect.equals(1, nesting); | |
46 nesting--; | |
47 return "something-$delay"; | |
48 }); | |
49 }).then((_) { | |
50 asyncEnd(); | |
51 }).catchError((e) { | |
52 Expect.fail("Throws: $e"); | |
53 }); | |
54 } | |
55 } | |
56 | |
57 void testForeachIterableThrows(Iterable<int> elements, n, error) { | |
58 for (var delay in [0, 1, 2]) { | |
59 asyncStart(); | |
60 int count = 0; | |
61 Future.forEach<int>(elements, (_) { | |
62 count++; | |
63 Expect.isTrue(n >= 0); | |
64 switch (delay) { | |
65 case 1: | |
66 return new Future<String>(() {}); | |
67 case 2: | |
68 return new Future<String>.microtask(() {}); | |
69 } | |
70 }).then((_) { | |
71 Expect.fail("Did not throw"); | |
72 }, onError: (e) { | |
73 Expect.equals(n, count); | |
74 Expect.equals(error, e); | |
75 asyncEnd(); | |
76 }); | |
77 } | |
78 } | |
79 | |
80 void testForeachFunctionThrows(Iterable<int> elements, n) { | |
81 for (var delay in [0, 1, 2]) { | |
82 asyncStart(); | |
83 Future.forEach<int>(elements, (v) { | |
84 if (v == n) { | |
85 switch (delay) { | |
86 case 0: | |
87 throw "ERROR"; | |
88 case 1: | |
89 return new Future<String>(() { | |
90 throw "ERROR"; | |
91 }); | |
92 case 2: | |
93 return new Future<String>.microtask(() { | |
94 throw "ERROR"; | |
95 }); | |
96 } | |
97 } | |
98 switch (delay) { | |
99 case 1: | |
100 return new Future(() {}); | |
101 case 2: | |
102 return new Future.microtask(() {}); | |
103 } | |
104 }).then((_) { | |
105 Expect.fail("Did not throw"); | |
106 }, onError: (e) { | |
107 Expect.equals("ERROR", e); | |
108 asyncEnd(); | |
109 }); | |
110 } | |
111 } | |
OLD | NEW |