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

Unified 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: Dartformat. Created 3 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: test/result/result_captureAll_test.dart
diff --git a/test/result/result_captureAll_test.dart b/test/result/result_captureAll_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ebdfc375f87781e22aaaec20f9384b8b1c73ec6e
--- /dev/null
+++ b/test/result/result_captureAll_test.dart
@@ -0,0 +1,185 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:async";
+import "dart:math" show Random;
+import "package:async/async.dart";
+import "package:test/test.dart";
+
+final someStack = StackTrace.current;
+Result<int> res(int n) => new Result<int>.value(n);
+Result err(n) => new ErrorResult("$n", someStack);
+
+main() {
+ test("empty", () async {
+ var all = await Result.captureAll<int>(futures(0));
+ expect(all, []);
+ });
+
+ group("futures only,", () {
+ test("single", () async {
+ var all = await Result.captureAll<int>(futures(1));
+ expect(all, [res(0)]);
+ });
+
+ test("multiple", () async {
+ var all = await Result.captureAll<int>(futures(3));
+ expect(all, [res(0), res(1), res(2)]);
+ });
+
+ test("error only", () async {
+ var all = await Result.captureAll<int>(futures(1, throwWhen: (_) => true));
floitsch 2017/08/24 17:46:39 run darfmt on this file.
+ expect(all, [err(0)]);
+ });
+
+ test("multiple error only", () async {
+ var all = await Result.captureAll<int>(futures(3, throwWhen: (_) => true));
+ expect(all, [err(0), err(1), err(2)]);
+ });
+
+ test("mixed error and value", () async {
+ var all = await Result.captureAll<int>(
+ futures(4, throwWhen: (x) => x.isOdd));
+ expect(all, [res(0), err(1), res(2), err(3)]);
+ });
+
+ test("completion permutation 1-2-3", () async {
+ var cs = new List.generate(3, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ expect(all, completion([res(1), res(2), err(3)]));
+ await 0;
+ cs[0].complete(1);
+ await 0;
+ cs[1].complete(2);
+ await 0;
+ cs[2].completeError("3", someStack);
+ });
+
+ test("completion permutation 1-3-2", () async {
+ var cs = new List.generate(3, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ expect(all, completion([res(1), res(2), err(3)]));
+ await 0;
+ cs[0].complete(1);
+ await 0;
+ cs[2].completeError("3", someStack);
+ await 0;
+ cs[1].complete(2);
+ });
+
+ test("completion permutation 2-1-3", () async {
+ var cs = new List.generate(3, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ expect(all, completion([res(1), res(2), err(3)]));
+ await 0;
+ cs[1].complete(2);
+ await 0;
+ cs[0].complete(1);
+ await 0;
+ cs[2].completeError("3", someStack);
+ });
+
+ test("completion permutation 2-3-1", () async {
+ var cs = new List.generate(3, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ expect(all, completion([res(1), res(2), err(3)]));
+ await 0;
+ cs[1].complete(2);
+ await 0;
+ cs[2].completeError("3", someStack);
+ await 0;
+ cs[0].complete(1);
+ });
+
+ test("completion permutation 3-1-2", () async {
+ var cs = new List.generate(3, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ expect(all, completion([res(1), res(2), err(3)]));
+ await 0;
+ cs[2].completeError("3", someStack);
+ await 0;
+ cs[0].complete(1);
+ await 0;
+ cs[1].complete(2);
+ });
+
+ test("completion permutation 3-2-1", () async {
+ var cs = new List.generate(3, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ expect(all, completion([res(1), res(2), err(3)]));
+ await 0;
+ cs[2].completeError("3", someStack);
+ await 0;
+ cs[1].complete(2);
+ await 0;
+ cs[0].complete(1);
+ });
+
+ var seed = new Random().nextInt(0x100000000);
+ int n = 25; // max 32, otherwise rnd.nextInt(1<<n) won't work.
+ test("randomized #$n seed:${seed.toRadixString(16)}", () async {
+ var cs = new List.generate(n, (_) => new Completer<int>());
+ var all = Result.captureAll<int>(cs.map((c) => c.future));
+ var rnd = new Random(seed);
+ var throwFlags = rnd.nextInt(1 << n); // Bit-flag for throwing.
+ bool throws(index) => (throwFlags & (1 << index)) != 0;
+ var expected =
+ new List.generate(n, (x) => throws(x) ? err(x) : res(x));
+
+ expect(all, completion(expected));
+
+ var completeFunctions = new List<Function()>.generate(n, (i) {
+ var c = cs[i];
+ return () => throws(i) ? c.completeError("$i", someStack) : c.complete(i);
+ });
+ completeFunctions.shuffle(rnd);
+ for (int i = 0; i < n; i++) {
+ await 0;
+ completeFunctions[i]();
+ }
+ });
+ });
+ group("values only,", () {
+ test("single", () async {
+ var all = await Result.captureAll<int>(<int>[1]);
+ expect(all, [res(1)]);
+ });
+ test("multiple", () async {
+ var all = await Result.captureAll<int>(<int>[1, 2, 3]);
+ expect(all, [res(1), res(2), res(3)]);
+ });
+ });
+ group("mixed futures and values,", () {
+ test("no error", () async {
+ var all = await Result.captureAll<int>(<FutureOr<int>>[
+ 1,
+ new Future<int>(() => 2),
+ 3,
+ new Future<int>.value(4),
+ ]);
+ expect(all, [res(1), res(2), res(3), res(4)]);
+ });
+ test("error", () async {
+ var all = await Result.captureAll<int>(<FutureOr<int>>[
+ 1,
+ new Future<int>(() => 2),
+ 3,
+ new Future<int>(() async => await new Future.error("4", someStack)),
+ new Future<int>.value(5)
+ ]);
+ expect(all, [res(1), res(2), res(3), err(4), res(5)]);
+ });
+
+ });
+}
+
+Iterable<Future<int>> futures(int count, { bool throwWhen(int index) }) sync* {
floitsch 2017/08/24 17:46:39 Move this function to the top.
+ for (int i = 0; i < count; i++) {
+ if (throwWhen != null && throwWhen(i)) {
+ yield new Future<int>.error("$i", someStack);
+ } else {
+ yield new Future<int>.value(i);
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698