CustomMatcher class
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)));
class CustomMatcher extends BaseMatcher { final String _featureDescription; final String _featureName; final Matcher _matcher; const CustomMatcher(this._featureDescription, this._featureName, this._matcher); /** Override this to extract the interesting feature.*/ featureValueOf(actual) => 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; } }
Extends
BaseMatcher > CustomMatcher
Constructors
Methods
Description describe(Description description) #
Creates a textual description of a matcher,
by appending to mismatchDescription
.
Description describe(Description description) => description.add(_featureDescription).add(' ').addDescriptionOf(_matcher);
Description describeMismatch(item, Description mismatchDescription, MatchState matchState, bool verbose) #
Generates a description of the matcher failed for a particular item, by appending the description to mismatchDescription. It does not check whether the item fails the match, as it is only called after a failed match. There may be additional info about the mismatch in matchState.
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; }
dynamicfeatureValueOf(actual) #
Override this to extract the interesting feature.
featureValueOf(actual) => actual;
bool matches(item, MatchState matchState) #
Tests the matcher against a given item and return true if the match succeeds; false otherwise. matchState may be used to return additional info for the use of describeMismatch.
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; }