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 part of unittest.matcher; | 5 part of matcher; |
6 | 6 |
7 /** | 7 /** |
8 * Returns a matcher that matches empty strings, maps or iterables | 8 * Returns a matcher that matches empty strings, maps or iterables |
9 * (including collections). | 9 * (including collections). |
10 */ | 10 */ |
11 const Matcher isEmpty = const _Empty(); | 11 const Matcher isEmpty = const _Empty(); |
12 | 12 |
13 class _Empty extends Matcher { | 13 class _Empty extends Matcher { |
14 const _Empty(); | 14 const _Empty(); |
15 bool matches(item, Map matchState) { | 15 bool matches(item, Map matchState) { |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 final Matcher _matcher; | 693 final Matcher _matcher; |
694 const _HasLength([Matcher matcher = null]): this._matcher = matcher; | 694 const _HasLength([Matcher matcher = null]): this._matcher = matcher; |
695 | 695 |
696 bool matches(item, Map matchState) { | 696 bool matches(item, Map matchState) { |
697 try { | 697 try { |
698 // This is harmless code that will throw if no length property | 698 // This is harmless code that will throw if no length property |
699 // but subtle enough that an optimizer shouldn't strip it out. | 699 // but subtle enough that an optimizer shouldn't strip it out. |
700 if (item.length * item.length >= 0) { | 700 if (item.length * item.length >= 0) { |
701 return _matcher.matches(item.length, matchState); | 701 return _matcher.matches(item.length, matchState); |
702 } | 702 } |
703 } catch (e) { | 703 } catch (e) {} |
704 return false; | 704 return false; |
705 } | |
706 } | 705 } |
707 | 706 |
708 Description describe(Description description) => | 707 Description describe(Description description) => |
709 description.add('an object with length of '). | 708 description.add('an object with length of '). |
710 addDescriptionOf(_matcher); | 709 addDescriptionOf(_matcher); |
711 | 710 |
712 Description describeMismatch(item, Description mismatchDescription, | 711 Description describeMismatch(item, Description mismatchDescription, |
713 Map matchState, bool verbose) { | 712 Map matchState, bool verbose) { |
714 try { | 713 try { |
715 // We want to generate a different description if there is no length | 714 // We want to generate a different description if there is no length |
716 // property; we use the same trick as in matches(). | 715 // property; we use the same trick as in matches(). |
717 if (item.length * item.length >= 0) { | 716 if (item.length * item.length >= 0) { |
718 return mismatchDescription.add('has length of '). | 717 return mismatchDescription.add('has length of '). |
719 addDescriptionOf(item.length); | 718 addDescriptionOf(item.length); |
720 } | 719 } |
721 } catch (e) { | 720 } catch (e) {} |
722 return mismatchDescription.add('has no length property'); | 721 return mismatchDescription.add('has no length property'); |
723 } | |
724 } | 722 } |
725 } | 723 } |
726 | 724 |
727 /** | 725 /** |
728 * Returns a matcher that matches if the match argument contains | 726 * Returns a matcher that matches if the match argument contains |
729 * the expected value. For [String]s this means substring matching; | 727 * the expected value. For [String]s this means substring matching; |
730 * for [Map]s it means the map has the key, and for [Iterable]s | 728 * for [Map]s it means the map has the key, and for [Iterable]s |
731 * (including [Iterable]s) it means the iterable has a matching | 729 * (including [Iterable]s) it means the iterable has a matching |
732 * element. In the case of iterables, [expected] can itself be a | 730 * element. In the case of iterables, [expected] can itself be a |
733 * matcher. | 731 * matcher. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
866 addDescriptionOf(matchState['feature']); | 864 addDescriptionOf(matchState['feature']); |
867 var innerDescription = new StringDescription(); | 865 var innerDescription = new StringDescription(); |
868 _matcher.describeMismatch(matchState['feature'], innerDescription, | 866 _matcher.describeMismatch(matchState['feature'], innerDescription, |
869 matchState['state'], verbose); | 867 matchState['state'], verbose); |
870 if (innerDescription.length > 0) { | 868 if (innerDescription.length > 0) { |
871 mismatchDescription.add(' which ').add(innerDescription.toString()); | 869 mismatchDescription.add(' which ').add(innerDescription.toString()); |
872 } | 870 } |
873 return mismatchDescription; | 871 return mismatchDescription; |
874 } | 872 } |
875 } | 873 } |
OLD | NEW |