Index: pkg/async_helper/lib/async_helper.dart |
diff --git a/pkg/async_helper/lib/async_helper.dart b/pkg/async_helper/lib/async_helper.dart |
index 84df557a811d6ce40f3643430a3477d7f420260a..1b444815fa10fe3e0ee924e3380cb46b80bcc382 100644 |
--- a/pkg/async_helper/lib/async_helper.dart |
+++ b/pkg/async_helper/lib/async_helper.dart |
@@ -32,6 +32,7 @@ Exception _buildException(String msg) { |
return new Exception('Fatal: $msg. This is most likely a bug in your test.'); |
} |
+/// Call this method before an asynchronous test is created. |
void asyncStart() { |
if (_initialized && _asyncLevel == 0) { |
throw _buildException('asyncStart() was called even though we are done ' |
@@ -45,6 +46,7 @@ void asyncStart() { |
_asyncLevel++; |
} |
+/// Call this after an asynchronous test has ended successfully. |
void asyncEnd() { |
if (_asyncLevel <= 0) { |
if (!_initialized) { |
@@ -62,7 +64,26 @@ void asyncEnd() { |
} |
} |
kustermann
2013/09/09 12:41:54
In general I've noticed that people misuse these t
|
+/** |
+ * Call this after an asynchronous test has ended successfully. This is a helper |
+ * for calling [asyncEnd]. |
+ * |
+ * This method intentionally has a signature that matches [:Future.then:] as a |
+ * convenience for calling [asyncEnd] when a [:Future:] completes without error, |
+ * like this: |
+ * |
+ * asyncStart(); |
+ * Future result = test(); |
+ * result.then(asyncSuccess); |
+ */ |
+void asyncSuccess(_) => asyncEnd(); |
+ |
+/** |
+ * Helper method for performing asynchronous tests involving [:Future:]. |
+ * |
+ * [f] must return a [:Future:] for the test computation. |
+ */ |
void asyncTest(f()) { |
kustermann
2013/09/09 12:41:54
Either document this on the top of the file or at
Johnni Winther
2013/09/13 07:34:59
Added a top comment as well.
|
asyncStart(); |
- f().whenComplete(() => asyncEnd()); |
+ f().then(asyncSuccess); |
} |