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

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

Issue 11788028: Fix future_test.dart and remove future2_test.dart. (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/future2_test.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_test.dart
diff --git a/tests/lib/async/future_test.dart b/tests/lib/async/future_test.dart
index 99386b279eb724eb195ec0488ff3df2965ae927f..a86bb5bacd2e702957d01f5e86c791ab6130628c 100644
--- a/tests/lib/async/future_test.dart
+++ b/tests/lib/async/future_test.dart
@@ -7,6 +7,161 @@ library future_test;
import 'dart:async';
import 'dart:isolate';
+testImmediate() {
+ final future = new Future<String>.immediate("42");
+ future.then((x) => Expect.equals("42", x));
Lasse Reichstein Nielsen 2013/01/08 09:06:52 Need a port to guarantee that the then is run,
Anders Johnsen 2013/01/08 09:27:01 Done.
+}
+
+testNeverComplete() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ future.then((v) => Except.fails("Value not expected"));
+ future.catchError((e) => Except.fails("Value not expected"));
Lasse Reichstein Nielsen 2013/01/08 09:06:52 This might actually succeede even if you complete
Anders Johnsen 2013/01/08 09:27:01 Discussed offline.
+}
+
+testComplete() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+
+ completer.complete(3);
+
+ future.then((v) => Expect.equals(3, v));
+}
+
+// Tests for [then]
+
+testCompleteWithSuccessHandlerBeforeComplete() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+
+ int before;
+ future.then((int v) { before = v; });
+ Expect.isNull(before);
+ completer.complete(3);
+
+ Expect.equals(3, before);
Lasse Reichstein Nielsen 2013/01/08 09:06:52 'before' isn't a good name here :) This one doesn
Anders Johnsen 2013/01/08 09:27:01 Done.
+}
+
+testCompleteWithSuccessHandlerAfterComplete() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+
+ int after;
+ completer.complete(3);
+ Expect.isNull(after);
+
+ var port = new ReceivePort();
+ future.then((int v) { after = v; })
+ .then((_) {
+ Expect.equals(3, after);
+ port.close();
+ });
+}
+
+testCompleteManySuccessHandlers() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ int before;
+ int after1;
+ int after2;
+
+ var futures = [];
+ futures.add(future.then((int v) { before = v; }));
+ completer.complete(3);
+ futures.add(future.then((int v) { after1 = v; }));
+ futures.add(future.then((int v) { after2 = v; }));
+
+ var port = new ReceivePort();
+ new Future.wait(futures).then((_) {
+ Expect.equals(3, before);
+ Expect.equals(3, after1);
+ Expect.equals(3, after2);
+ port.close();
+ });
+}
+
+// Tests for [catchError]
+
+testException() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ final ex = new Exception();
+ future.then((v) {print(v);})
+ .catchError((e) => Expect.equals(e.error, ex));
Lasse Reichstein Nielsen 2013/01/08 09:06:52 This one works, but it probably should be using th
Anders Johnsen 2013/01/08 09:27:01 Done.
+ completer.completeError(ex);
+}
+
+testExceptionNoSuccessListeners() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ final ex = new Exception();
+ completer.completeException(ex); // future.then is not called, so no exception
+}
+
+testExceptionHandler() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ final ex = new Exception();
+
+ var ex2;
+ var done = future.catchError((e) { ex2 = e.error; });
+ completer.completeError(ex);
+
+ var port = new ReceivePort();
+ done.then((_) {
+ Expect.equals(ex, ex2);
+ port.close();
+ });
+}
+
+testExceptionHandlerReturnsTrue() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ final ex = new Exception();
+
+ bool reached = false;
+ future.catchError((e) { });
+ future.catchError((e) { reached = true; }, test: (e) => false)
+ .catchError((e) {});
+ completer.completeError(ex);
+ Expect.isFalse(reached);
+}
+
+testExceptionHandlerReturnsTrue2() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ final ex = new Exception();
+
+ bool reached = false;
+ var done = future
+ .catchError((e) { }, test: (e) => false)
+ .catchError((e) { reached = true; });
+ completer.completeError(ex);
+
+ var port = new ReceivePort();
+ done.then((_) {
+ Expect.isTrue(reached);
+ port.close();
+ });
+}
+
+testExceptionHandlerReturnsFalse() {
+ final completer = new Completer<int>();
+ final future = completer.future;
+ final ex = new Exception();
+
+ bool reached = false;
+
+ future.catchError((e) { });
+
+ future.catchError((e) { reached = true; }, test: (e) => false)
+ .catchError((e) { });
+
+ completer.completeError(ex);
+
+ Expect.isFalse(reached);
+}
+
testFutureAsStreamCompleteAfter() {
var completer = new Completer();
bool gotValue = false;
@@ -184,6 +339,20 @@ testFutureWhenCompletePreValue() {
}
main() {
+ testImmediate();
+ testNeverComplete();
+
+ testComplete();
+ testCompleteWithSuccessHandlerBeforeComplete();
+ testCompleteWithSuccessHandlerAfterComplete();
+ testCompleteManySuccessHandlers();
+
+ testException();
+ testExceptionHandler();
+ testExceptionHandlerReturnsTrue();
+ testExceptionHandlerReturnsTrue2();
+ testExceptionHandlerReturnsFalse();
+
testFutureAsStreamCompleteAfter();
testFutureAsStreamCompleteBefore();
testFutureAsStreamCompleteImmediate();
« no previous file with comments | « tests/lib/async/future2_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698