Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: test/result/result_captureAll_test.dart

Issue 2996143002: Add methods to Result. Bump version to 2.0 and remove deprecated features. (Closed)
Patch Set: Address comments, run dartfmt on tests too. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pubspec.yaml ('k') | test/result/result_flattenAll_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 import "dart:async";
6 import "dart:math" show Random;
7 import "package:async/async.dart";
8 import "package:test/test.dart";
9
10 final someStack = StackTrace.current;
11 Result<int> res(int n) => new Result<int>.value(n);
12 Result err(n) => new ErrorResult("$n", someStack);
13
14 /// Helper function creating an iterable of futures.
15 Iterable<Future<int>> futures(int count, {bool throwWhen(int index)}) sync* {
16 for (int i = 0; i < count; i++) {
17 if (throwWhen != null && throwWhen(i)) {
18 yield new Future<int>.error("$i", someStack);
19 } else {
20 yield new Future<int>.value(i);
21 }
22 }
23 }
24
25 main() {
26 test("empty", () async {
27 var all = await Result.captureAll<int>(futures(0));
28 expect(all, []);
29 });
30
31 group("futures only,", () {
32 test("single", () async {
33 var all = await Result.captureAll<int>(futures(1));
34 expect(all, [res(0)]);
35 });
36
37 test("multiple", () async {
38 var all = await Result.captureAll<int>(futures(3));
39 expect(all, [res(0), res(1), res(2)]);
40 });
41
42 test("error only", () async {
43 var all =
44 await Result.captureAll<int>(futures(1, throwWhen: (_) => true));
45 expect(all, [err(0)]);
46 });
47
48 test("multiple error only", () async {
49 var all =
50 await Result.captureAll<int>(futures(3, throwWhen: (_) => true));
51 expect(all, [err(0), err(1), err(2)]);
52 });
53
54 test("mixed error and value", () async {
55 var all =
56 await Result.captureAll<int>(futures(4, throwWhen: (x) => x.isOdd));
57 expect(all, [res(0), err(1), res(2), err(3)]);
58 });
59
60 test("completion permutation 1-2-3", () async {
61 var cs = new List.generate(3, (_) => new Completer<int>());
62 var all = Result.captureAll<int>(cs.map((c) => c.future));
63 expect(all, completion([res(1), res(2), err(3)]));
64 await 0;
65 cs[0].complete(1);
66 await 0;
67 cs[1].complete(2);
68 await 0;
69 cs[2].completeError("3", someStack);
70 });
71
72 test("completion permutation 1-3-2", () async {
73 var cs = new List.generate(3, (_) => new Completer<int>());
74 var all = Result.captureAll<int>(cs.map((c) => c.future));
75 expect(all, completion([res(1), res(2), err(3)]));
76 await 0;
77 cs[0].complete(1);
78 await 0;
79 cs[2].completeError("3", someStack);
80 await 0;
81 cs[1].complete(2);
82 });
83
84 test("completion permutation 2-1-3", () async {
85 var cs = new List.generate(3, (_) => new Completer<int>());
86 var all = Result.captureAll<int>(cs.map((c) => c.future));
87 expect(all, completion([res(1), res(2), err(3)]));
88 await 0;
89 cs[1].complete(2);
90 await 0;
91 cs[0].complete(1);
92 await 0;
93 cs[2].completeError("3", someStack);
94 });
95
96 test("completion permutation 2-3-1", () async {
97 var cs = new List.generate(3, (_) => new Completer<int>());
98 var all = Result.captureAll<int>(cs.map((c) => c.future));
99 expect(all, completion([res(1), res(2), err(3)]));
100 await 0;
101 cs[1].complete(2);
102 await 0;
103 cs[2].completeError("3", someStack);
104 await 0;
105 cs[0].complete(1);
106 });
107
108 test("completion permutation 3-1-2", () async {
109 var cs = new List.generate(3, (_) => new Completer<int>());
110 var all = Result.captureAll<int>(cs.map((c) => c.future));
111 expect(all, completion([res(1), res(2), err(3)]));
112 await 0;
113 cs[2].completeError("3", someStack);
114 await 0;
115 cs[0].complete(1);
116 await 0;
117 cs[1].complete(2);
118 });
119
120 test("completion permutation 3-2-1", () async {
121 var cs = new List.generate(3, (_) => new Completer<int>());
122 var all = Result.captureAll<int>(cs.map((c) => c.future));
123 expect(all, completion([res(1), res(2), err(3)]));
124 await 0;
125 cs[2].completeError("3", someStack);
126 await 0;
127 cs[1].complete(2);
128 await 0;
129 cs[0].complete(1);
130 });
131
132 var seed = new Random().nextInt(0x100000000);
133 int n = 25; // max 32, otherwise rnd.nextInt(1<<n) won't work.
134 test("randomized #$n seed:${seed.toRadixString(16)}", () async {
135 var cs = new List.generate(n, (_) => new Completer<int>());
136 var all = Result.captureAll<int>(cs.map((c) => c.future));
137 var rnd = new Random(seed);
138 var throwFlags = rnd.nextInt(1 << n); // Bit-flag for throwing.
139 bool throws(index) => (throwFlags & (1 << index)) != 0;
140 var expected = new List.generate(n, (x) => throws(x) ? err(x) : res(x));
141
142 expect(all, completion(expected));
143
144 var completeFunctions = new List<Function()>.generate(n, (i) {
145 var c = cs[i];
146 return () =>
147 throws(i) ? c.completeError("$i", someStack) : c.complete(i);
148 });
149 completeFunctions.shuffle(rnd);
150 for (int i = 0; i < n; i++) {
151 await 0;
152 completeFunctions[i]();
153 }
154 });
155 });
156 group("values only,", () {
157 test("single", () async {
158 var all = await Result.captureAll<int>(<int>[1]);
159 expect(all, [res(1)]);
160 });
161 test("multiple", () async {
162 var all = await Result.captureAll<int>(<int>[1, 2, 3]);
163 expect(all, [res(1), res(2), res(3)]);
164 });
165 });
166 group("mixed futures and values,", () {
167 test("no error", () async {
168 var all = await Result.captureAll<int>(<FutureOr<int>>[
169 1,
170 new Future<int>(() => 2),
171 3,
172 new Future<int>.value(4),
173 ]);
174 expect(all, [res(1), res(2), res(3), res(4)]);
175 });
176 test("error", () async {
177 var all = await Result.captureAll<int>(<FutureOr<int>>[
178 1,
179 new Future<int>(() => 2),
180 3,
181 new Future<int>(() async => await new Future.error("4", someStack)),
182 new Future<int>.value(5)
183 ]);
184 expect(all, [res(1), res(2), res(3), err(4), res(5)]);
185 });
186 });
187 }
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | test/result/result_flattenAll_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698