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

Unified Diff: pkg/unittest/core_matchers.dart

Issue 11189040: Added a new Matcher, FeatureMatcher, that is a useful abstract base class (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 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 | « no previous file | pkg/unittest/test/matchers_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/unittest/core_matchers.dart
===================================================================
--- pkg/unittest/core_matchers.dart (revision 13752)
+++ pkg/unittest/core_matchers.dart (working copy)
@@ -625,3 +625,54 @@
Description describe(Description description) =>
description.add(_description);
}
+
+/**
+ * A useful utility class for implementing other matchers through inheritance.
+ * Derived classes should call the base constructor with a feature name and
+ * description, and an instance matcher, and should implement the
+ * [featureValueOf] abstract method.
+ *
+ * The feature description will typically describe the item and the feature,
+ * while the feature name will just name the feature. For example, we may
+ * have a Widget class where each Widget has a price; we could make a
+ * FeatureMatcher that can make assertions about prices with:
+ *
+ * class HasPrice extends FeatureMatcher {
+ * const HasPrice(matcher) :
+ * super("Widget with price that is", "price", matcher);
+ * featureValueOf(actual) => actual.price;
+ * }
+ *
+ * and then use this for example like:
+ *
+ * expect(inventoryItem, new HasPrice(greaterThan(0)));
+ */
+abstract class CustomMatcher extends BaseMatcher {
+ final String _featureDescription;
+ final String _featureName;
+ final Matcher _matcher;
+
+ const CustomMatcher(this._featureDescription, this._featureName,
+ this._matcher);
+
+ /** Implement this to extract the interesting feature.*/
+ featureValueOf(actual);
+
+ bool matches(item, MatchState matchState) {
+ var f = featureValueOf(item);
+ if (_matcher.matches(f, matchState)) return true;
+ matchState.state = { 'innerState': matchState.state, 'feature': f };
+ return false;
+ }
+
+ Description describe(Description description) =>
+ description.add(_featureDescription).add(' ').addDescriptionOf(_matcher);
+
+ Description describeMismatch(item, Description mismatchDescription,
+ MatchState matchState, bool verbose) {
+ mismatchDescription.add(_featureName).add(' ');
+ _matcher.describeMismatch(matchState.state['feature'], mismatchDescription,
+ matchState.state['innerState'], verbose);
+ return mismatchDescription;
+ }
+}
« no previous file with comments | « no previous file | pkg/unittest/test/matchers_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698