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

Unified Diff: tests/lib/async/future_foreach_test.dart

Issue 2851443002: Fix bug in Future.forEach. Add tests. (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « sdk/lib/async/future.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/async/future_foreach_test.dart
diff --git a/tests/lib/async/future_foreach_test.dart b/tests/lib/async/future_foreach_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7416b9411d1c56414d0cd4983225f857cfd71759
--- /dev/null
+++ b/tests/lib/async/future_foreach_test.dart
@@ -0,0 +1,111 @@
+// 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.
+
+library future_foreach_test;
+
+import 'package:async_helper/async_helper.dart';
+import "package:expect/expect.dart";
+import 'dart:async';
+
+main() {
+ asyncStart();
+
+ testForeach(<int>[]);
+ testForeach(<int>[0]);
+ testForeach(<int>[0, 1, 2, 3]);
+
+ testForeachIterableThrows(
+ new Iterable<int>.generate(5, (i) => i < 4 ? i : throw "ERROR"),
+ 4,
+ "ERROR");
+
+ testForeachFunctionThrows(new Iterable<int>.generate(5, (x) => x), 4);
+
+ asyncEnd();
+}
+
+void testForeach(Iterable<int> elements) {
+ for (var delay in [0, 1, 2]) {
+ asyncStart();
+ int length = elements.length;
+ int count = 0;
+ int nesting = 0;
+ Future.forEach<int>(elements, (int value) {
+ Expect.isTrue(nesting == 0, "overlapping calls detected");
+ if (delay == 0) return "something-$delay";
+ nesting++;
+ var future;
+ if (delay == 1) {
+ future = new Future(() => null);
+ } else {
+ future = new Future.microtask(() => null);
+ }
+ return future.then<String>((_) {
+ Expect.equals(1, nesting);
+ nesting--;
+ return "something-$delay";
+ });
+ }).then((_) {
+ asyncEnd();
+ }).catchError((e) {
+ Expect.fail("Throws: $e");
+ });
+ }
+}
+
+void testForeachIterableThrows(Iterable<int> elements, n, error) {
+ for (var delay in [0, 1, 2]) {
+ asyncStart();
+ int count = 0;
+ Future.forEach<int>(elements, (_) {
+ count++;
+ Expect.isTrue(n >= 0);
+ switch (delay) {
+ case 1:
+ return new Future<String>(() {});
+ case 2:
+ return new Future<String>.microtask(() {});
+ }
+ }).then((_) {
+ Expect.fail("Did not throw");
+ }, onError: (e) {
+ Expect.equals(n, count);
+ Expect.equals(error, e);
+ asyncEnd();
+ });
+ }
+}
+
+void testForeachFunctionThrows(Iterable<int> elements, n) {
+ for (var delay in [0, 1, 2]) {
+ asyncStart();
+ Future.forEach<int>(elements, (v) {
+ if (v == n) {
+ switch (delay) {
+ case 0:
+ throw "ERROR";
+ case 1:
+ return new Future<String>(() {
+ throw "ERROR";
+ });
+ case 2:
+ return new Future<String>.microtask(() {
+ throw "ERROR";
+ });
+ }
+ }
+ switch (delay) {
+ case 1:
+ return new Future(() {});
+ case 2:
+ return new Future.microtask(() {});
+ }
+ }).then((_) {
+ Expect.fail("Did not throw");
+ }, onError: (e) {
+ Expect.equals("ERROR", e);
+ asyncEnd();
+ });
+ }
+}
« no previous file with comments | « sdk/lib/async/future.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698