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

Unified Diff: lib/unittest/future_matchers.dart

Issue 10548005: Add matchers for unit testing futures: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 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: lib/unittest/future_matchers.dart
diff --git a/lib/unittest/future_matchers.dart b/lib/unittest/future_matchers.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5926e90972a5852789fbc762b378c47a1ef38186
--- /dev/null
+++ b/lib/unittest/future_matchers.dart
@@ -0,0 +1,52 @@
+// 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.
+
+/**
+ * Matches a [Future] that completes successfully with a value. Note that this
+ * creates an asynchronous expectation. The call to `expect()` that includes
+ * this will return immediately and execution will continue. Later, when the
+ * future completes, the actual expectation will run.
+ *
+ * To test that a Future completes with an exception, you can use [throws] and
+ * [throwsA].
+ */
+Matcher completes = const _Completes(null);
Siggi Cherem (dart-lang) 2012/06/13 00:04:39 completes => completesWithValue or completesNormal
Bob Nystrom 2012/06/13 20:22:06 Hmm. I see the ambiguity with the current name sin
+
+/**
+ * Matches a [Future] that completes succesfully with a value that matches
+ * [matcher]. Note that this creates an asynchronous expectation. The call to
+ * `expect()` that includes this will return immediately and execution will
+ * continue. Later, when the future completes, the actual expectation will run.
+ *
+ * To test that a Future completes with an exception, you can use [throws] and
+ * [throwsA].
+ */
+Matcher completion(matcher) => new _Completes(wrapMatcher(matcher));
Siggi Cherem (dart-lang) 2012/06/13 00:04:39 I'm trying to think if there are other names that
Bob Nystrom 2012/06/13 20:22:06 Right, this is what I was thinking too. "completio
Siggi Cherem (dart-lang) 2012/06/13 20:27:20 I guess I like 'completion' less because it is a n
Bob Nystrom 2012/06/13 20:35:51 Yeah, but I think that's consistent with other mat
+
+class _Completes extends BaseMatcher {
+ final Matcher _matcher;
+
+ const _Completes(this._matcher);
+
+ bool matches(item) {
+ if (item is! Future) return false;
Siggi Cherem (dart-lang) 2012/06/13 00:04:39 should this by a synchronous expectation (e.g.
Bob Nystrom 2012/06/13 20:22:06 That feels a little confusing since this will be r
+
+ item.onComplete(expectAsync1((future) {
+ expect(future.hasValue,
+ 'Expected future to complete successfully, but it failed');
+ if (_matcher != null) expect(future.value, _matcher);
+ }));
+
+ return true;
+ }
+
+ Description describe(Description description) {
+ if (_matcher == null) {
+ description.add('completes successfully');
+ } else {
+ description.add('completes to a value that ').addDescriptionOf(_matcher);
+ }
+ return description;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698