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

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

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 | « tests/lib/async/future_delayed_error_test.dart ('k') | tests/lib/async/merge_stream_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/async/future_test.dart
diff --git a/tests/lib/async/future_test.dart b/tests/lib/async/future_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..0826db3af0dde12d3ca691a4856bd07e3a327ef3
--- /dev/null
+++ b/tests/lib/async/future_test.dart
@@ -0,0 +1,196 @@
+// Copyright (c) 2012, 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:isolate';
+
+testFutureAsStreamCompleteAfter() {
+ var completer = new Completer();
+ bool gotValue = false;
+ var port = new ReceivePort();
+ completer.future.asStream().listen(
+ (data) {
+ Expect.isFalse(gotValue);
+ gotValue = true;
+ Expect.equals("value", data);
+ },
+ onDone: () {
+ Expect.isTrue(gotValue);
+ port.close();
+ });
+ completer.complete("value");
+}
+
+testFutureAsStreamCompleteBefore() {
+ var completer = new Completer();
+ bool gotValue = false;
+ var port = new ReceivePort();
+ completer.complete("value");
+ completer.future.asStream().listen(
+ (data) {
+ Expect.isFalse(gotValue);
+ gotValue = true;
+ Expect.equals("value", data);
+ },
+ onDone: () {
+ Expect.isTrue(gotValue);
+ port.close();
+ });
+}
+
+testFutureAsStreamCompleteImmediate() {
+ bool gotValue = false;
+ var port = new ReceivePort();
+ new Future.immediate("value").asStream().listen(
+ (data) {
+ Expect.isFalse(gotValue);
+ gotValue = true;
+ Expect.equals("value", data);
+ },
+ onDone: () {
+ Expect.isTrue(gotValue);
+ port.close();
+ });
+}
+
+testFutureAsStreamCompleteErrorAfter() {
+ var completer = new Completer();
+ bool gotError = false;
+ var port = new ReceivePort();
+ completer.future.asStream().listen(
+ (data) {
+ Expect.fail("Unexpected data");
+ },
+ onError: (error) {
+ Expect.isFalse(gotError);
+ gotError = true;
+ Expect.equals("error", error.error);
+ },
+ onDone: () {
+ Expect.isTrue(gotError);
+ port.close();
+ });
+ completer.completeError("error");
+}
+
+testFutureAsStreamWrapper() {
+ var completer = new Completer();
+ bool gotValue = false;
+ var port = new ReceivePort();
+ completer.complete("value");
+ completer.future
+ .catchError((_) { throw "not possible"; }) // Returns a future wrapper.
+ .asStream().listen(
+ (data) {
+ Expect.isFalse(gotValue);
+ gotValue = true;
+ Expect.equals("value", data);
+ },
+ onDone: () {
+ Expect.isTrue(gotValue);
+ port.close();
+ });
+}
+
+testFutureWhenCompleteValue() {
+ var port = new ReceivePort();
+ int counter = 2;
+ countDown() {
+ if (--counter == 0) port.close();
+ }
+ var completer = new Completer();
+ Future future = completer.future;
+ Future later = future.whenComplete(countDown);
+ later.then((v) {
+ Expect.equals(42, v);
+ countDown();
+ });
+ completer.complete(42);
+}
+
+testFutureWhenCompleteError() {
+ var port = new ReceivePort();
+ int counter = 2;
+ countDown() {
+ if (--counter == 0) port.close();
+ }
+ var completer = new Completer();
+ Future future = completer.future;
+ Future later = future.whenComplete(countDown);
+ later.catchError((AsyncError e) {
+ Expect.equals("error", e.error);
+ countDown();
+ });
+ completer.completeError("error");
+}
+
+testFutureWhenCompleteValueNewError() {
+ var port = new ReceivePort();
+ int counter = 2;
+ countDown() {
+ if (--counter == 0) port.close();
+ }
+ var completer = new Completer();
+ Future future = completer.future;
+ Future later = future.whenComplete(() {
+ countDown();
+ throw "new error";
+ });
+ later.catchError((AsyncError e) {
+ Expect.equals("new error", e.error);
+ countDown();
+ });
+ completer.complete(42);
+}
+
+testFutureWhenCompleteErrorNewError() {
+ var port = new ReceivePort();
+ int counter = 2;
+ countDown() {
+ if (--counter == 0) port.close();
+ }
+ var completer = new Completer();
+ Future future = completer.future;
+ Future later = future.whenComplete(() {
+ countDown();
+ throw "new error";
+ });
+ later.catchError((AsyncError e) {
+ Expect.equals("new error", e.error);
+ countDown();
+ });
+ completer.completeError("error");
+}
+
+testFutureWhenCompletePreValue() {
+ var port = new ReceivePort();
+ int counter = 2;
+ countDown() {
+ if (--counter == 0) port.close();
+ }
+ var completer = new Completer();
+ Future future = completer.future;
+ completer.complete(42);
+ new Timer(0, () {
+ Future later = future.whenComplete(countDown);
+ later.then((v) {
+ Expect.equals(42, v);
+ countDown();
+ });
+ });
+}
+
+main() {
+ testFutureAsStreamCompleteAfter();
+ testFutureAsStreamCompleteBefore();
+ testFutureAsStreamCompleteImmediate();
+ testFutureAsStreamCompleteErrorAfter();
+ testFutureAsStreamWrapper();
+
+ testFutureWhenCompleteValue();
+ testFutureWhenCompleteError();
+ testFutureWhenCompleteValueNewError();
+ testFutureWhenCompleteErrorNewError();
+}
+
« no previous file with comments | « tests/lib/async/future_delayed_error_test.dart ('k') | tests/lib/async/merge_stream_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698