| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * Returns a matcher that matches empty strings, maps or collections. | 7 * Returns a matcher that matches empty strings, maps or collections. |
| 8 */ | 8 */ |
| 9 const Matcher isEmpty = const _Empty(); | 9 const Matcher isEmpty = const _Empty(); |
| 10 | 10 |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 final _matcher; | 618 final _matcher; |
| 619 final String _description; | 619 final String _description; |
| 620 | 620 |
| 621 const _Predicate(this._matcher, this._description); | 621 const _Predicate(this._matcher, this._description); |
| 622 | 622 |
| 623 bool matches(item, MatchState matchState) => _matcher(item); | 623 bool matches(item, MatchState matchState) => _matcher(item); |
| 624 | 624 |
| 625 Description describe(Description description) => | 625 Description describe(Description description) => |
| 626 description.add(_description); | 626 description.add(_description); |
| 627 } | 627 } |
| 628 |
| 629 /** |
| 630 * A useful utility class for implementing other matchers through inheritance. |
| 631 * Derived classes should call the base constructor with a feature name and |
| 632 * description, and an instance matcher, and should implement the |
| 633 * [featureValueOf] abstract method. |
| 634 * |
| 635 * The feature description will typically describe the item and the feature, |
| 636 * while the feature name will just name the feature. For example, we may |
| 637 * have a Widget class where each Widget has a price; we could make a |
| 638 * FeatureMatcher that can make assertions about prices with: |
| 639 * |
| 640 * class HasPrice extends FeatureMatcher { |
| 641 * const HasPrice(matcher) : |
| 642 * super("Widget with price that is", "price", matcher); |
| 643 * featureValueOf(actual) => actual.price; |
| 644 * } |
| 645 * |
| 646 * and then use this for example like: |
| 647 * |
| 648 * expect(inventoryItem, new HasPrice(greaterThan(0))); |
| 649 */ |
| 650 abstract class CustomMatcher extends BaseMatcher { |
| 651 final String _featureDescription; |
| 652 final String _featureName; |
| 653 final Matcher _matcher; |
| 654 |
| 655 const CustomMatcher(this._featureDescription, this._featureName, |
| 656 this._matcher); |
| 657 |
| 658 /** Implement this to extract the interesting feature.*/ |
| 659 featureValueOf(actual); |
| 660 |
| 661 bool matches(item, MatchState matchState) { |
| 662 var f = featureValueOf(item); |
| 663 if (_matcher.matches(f, matchState)) return true; |
| 664 matchState.state = { 'innerState': matchState.state, 'feature': f }; |
| 665 return false; |
| 666 } |
| 667 |
| 668 Description describe(Description description) => |
| 669 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher); |
| 670 |
| 671 Description describeMismatch(item, Description mismatchDescription, |
| 672 MatchState matchState, bool verbose) { |
| 673 mismatchDescription.add(_featureName).add(' '); |
| 674 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription, |
| 675 matchState.state['innerState'], verbose); |
| 676 return mismatchDescription; |
| 677 } |
| 678 } |
| OLD | NEW |