| 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 matcher; | 5 part of matcher; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Returns a matcher which matches if the match argument is a string and | 8 * Returns a matcher which matches if the match argument is a string and |
| 9 * is equal to [value] when compared case-insensitively. | 9 * is equal to [value] when compared case-insensitively. |
| 10 */ | 10 */ |
| 11 Matcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value); | 11 Matcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value); |
| 12 | 12 |
| 13 class _IsEqualIgnoringCase extends _StringMatcher { | 13 class _IsEqualIgnoringCase extends _StringMatcher { |
| 14 final String _value; | 14 final String _value; |
| 15 String _matchValue; | 15 String _matchValue; |
| 16 | 16 |
| 17 _IsEqualIgnoringCase(this._value) { | 17 _IsEqualIgnoringCase(this._value) { |
| 18 _matchValue = _value.toLowerCase(); | 18 _matchValue = _value.toLowerCase(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 bool matches(item, MatchState mismatchState) => | 21 bool matches(item, Map matchState) => |
| 22 item is String && _matchValue == item.toLowerCase(); | 22 item is String && _matchValue == item.toLowerCase(); |
| 23 | 23 |
| 24 Description describe(Description description) => | 24 Description describe(Description description) => |
| 25 description.addDescriptionOf(_value).add(' ignoring case'); | 25 description.addDescriptionOf(_value).add(' ignoring case'); |
| 26 } | 26 } |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Returns a matcher which matches if the match argument is a string and | 29 * Returns a matcher which matches if the match argument is a string and |
| 30 * is equal to [value] when compared with all runs of whitespace | 30 * is equal to [value] when compared with all runs of whitespace |
| 31 * collapsed to single spaces and leading and trailing whitespace removed. | 31 * collapsed to single spaces and leading and trailing whitespace removed. |
| 32 * | 32 * |
| 33 * For example, `equalsIgnoringCase("hello world")` will match | 33 * For example, `equalsIgnoringCase("hello world")` will match |
| 34 * "hello world", " hello world" and "hello world ". | 34 * "hello world", " hello world" and "hello world ". |
| 35 */ | 35 */ |
| 36 Matcher equalsIgnoringWhitespace(_string) => | 36 Matcher equalsIgnoringWhitespace(_string) => |
| 37 new _IsEqualIgnoringWhitespace(_string); | 37 new _IsEqualIgnoringWhitespace(_string); |
| 38 | 38 |
| 39 class _IsEqualIgnoringWhitespace extends _StringMatcher { | 39 class _IsEqualIgnoringWhitespace extends _StringMatcher { |
| 40 final String _value; | 40 final String _value; |
| 41 String _matchValue; | 41 String _matchValue; |
| 42 | 42 |
| 43 _IsEqualIgnoringWhitespace(this._value) { | 43 _IsEqualIgnoringWhitespace(this._value) { |
| 44 _matchValue = collapseWhitespace(_value); | 44 _matchValue = collapseWhitespace(_value); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool matches(item, MatchState matchState) => | 47 bool matches(item, Map matchState) => |
| 48 item is String && _matchValue == collapseWhitespace(item); | 48 item is String && _matchValue == collapseWhitespace(item); |
| 49 | 49 |
| 50 Description describe(Description description) => | 50 Description describe(Description description) => |
| 51 description.addDescriptionOf(_matchValue).add(' ignoring whitespace'); | 51 description.addDescriptionOf(_matchValue).add(' ignoring whitespace'); |
| 52 | 52 |
| 53 Description describeMismatch(item, Description mismatchDescription, | 53 Description describeMismatch(item, Description mismatchDescription, |
| 54 MatchState matchState, bool verbose) { | 54 Map matchState, bool verbose) { |
| 55 if (item is String) { | 55 if (item is String) { |
| 56 return mismatchDescription.add('was '). | 56 return mismatchDescription.add('is '). |
| 57 addDescriptionOf(collapseWhitespace(item)); | 57 addDescriptionOf(collapseWhitespace(item)). |
| 58 add(' with whitespace compressed'); |
| 58 } else { | 59 } else { |
| 59 return super.describeMismatch(item, mismatchDescription, | 60 return super.describeMismatch(item, mismatchDescription, |
| 60 matchState, verbose); | 61 matchState, verbose); |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 } | 64 } |
| 64 | 65 |
| 65 /** | 66 /** |
| 66 * Utility function to collapse whitespace runs to single spaces | 67 * Utility function to collapse whitespace runs to single spaces |
| 67 * and strip leading/trailing whitespace. | 68 * and strip leading/trailing whitespace. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 89 * Returns a matcher that matches if the match argument is a string and | 90 * Returns a matcher that matches if the match argument is a string and |
| 90 * starts with [prefixString]. | 91 * starts with [prefixString]. |
| 91 */ | 92 */ |
| 92 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString); | 93 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString); |
| 93 | 94 |
| 94 class _StringStartsWith extends _StringMatcher { | 95 class _StringStartsWith extends _StringMatcher { |
| 95 final String _prefix; | 96 final String _prefix; |
| 96 | 97 |
| 97 const _StringStartsWith(this._prefix); | 98 const _StringStartsWith(this._prefix); |
| 98 | 99 |
| 99 bool matches(item, MatchState matchState) => | 100 bool matches(item, Map matchState) => |
| 100 item is String && item.startsWith(_prefix); | 101 item is String && item.startsWith(_prefix); |
| 101 | 102 |
| 102 Description describe(Description description) => | 103 Description describe(Description description) => |
| 103 description.add('a string starting with ').addDescriptionOf(_prefix); | 104 description.add('a string starting with ').addDescriptionOf(_prefix); |
| 104 } | 105 } |
| 105 | 106 |
| 106 /** | 107 /** |
| 107 * Returns a matcher that matches if the match argument is a string and | 108 * Returns a matcher that matches if the match argument is a string and |
| 108 * ends with [suffixString]. | 109 * ends with [suffixString]. |
| 109 */ | 110 */ |
| 110 Matcher endsWith(String suffixString) => new _StringEndsWith(suffixString); | 111 Matcher endsWith(String suffixString) => new _StringEndsWith(suffixString); |
| 111 | 112 |
| 112 class _StringEndsWith extends _StringMatcher { | 113 class _StringEndsWith extends _StringMatcher { |
| 113 | 114 |
| 114 final String _suffix; | 115 final String _suffix; |
| 115 | 116 |
| 116 const _StringEndsWith(this._suffix); | 117 const _StringEndsWith(this._suffix); |
| 117 | 118 |
| 118 bool matches(item, MatchState matchState) => | 119 bool matches(item, Map matchState) => |
| 119 item is String && item.endsWith(_suffix); | 120 item is String && item.endsWith(_suffix); |
| 120 | 121 |
| 121 Description describe(Description description) => | 122 Description describe(Description description) => |
| 122 description.add('a string ending with ').addDescriptionOf(_suffix); | 123 description.add('a string ending with ').addDescriptionOf(_suffix); |
| 123 } | 124 } |
| 124 | 125 |
| 125 /** | 126 /** |
| 126 * Returns a matcher that matches if the match argument is a string and | 127 * Returns a matcher that matches if the match argument is a string and |
| 127 * contains a given list of [substrings] in relative order. | 128 * contains a given list of [substrings] in relative order. |
| 128 * | 129 * |
| 129 * For example, `stringContainsInOrder(["a", "e", "i", "o", "u"])` will match | 130 * For example, `stringContainsInOrder(["a", "e", "i", "o", "u"])` will match |
| 130 * "abcdefghijklmnopqrstuvwxyz". | 131 * "abcdefghijklmnopqrstuvwxyz". |
| 131 */ | 132 */ |
| 132 | 133 |
| 133 Matcher stringContainsInOrder(substrings) => | 134 Matcher stringContainsInOrder(substrings) => |
| 134 new _StringContainsInOrder(substrings); | 135 new _StringContainsInOrder(substrings); |
| 135 | 136 |
| 136 class _StringContainsInOrder extends _StringMatcher { | 137 class _StringContainsInOrder extends _StringMatcher { |
| 137 | 138 |
| 138 final List<String> _substrings; | 139 final List<String> _substrings; |
| 139 | 140 |
| 140 const _StringContainsInOrder(this._substrings); | 141 const _StringContainsInOrder(this._substrings); |
| 141 | 142 |
| 142 bool matches(item, MatchState matchState) { | 143 bool matches(item, Map matchState) { |
| 143 if (!(item is String)) { | 144 if (!(item is String)) { |
| 144 return false; | 145 return false; |
| 145 } | 146 } |
| 146 var from_index = 0; | 147 var from_index = 0; |
| 147 for (var s in _substrings) { | 148 for (var s in _substrings) { |
| 148 from_index = item.indexOf(s, from_index); | 149 from_index = item.indexOf(s, from_index); |
| 149 if (from_index < 0) | 150 if (from_index < 0) |
| 150 return false; | 151 return false; |
| 151 } | 152 } |
| 152 return true; | 153 return true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 _MatchesRegExp(re) { | 172 _MatchesRegExp(re) { |
| 172 if (re is String) { | 173 if (re is String) { |
| 173 _regexp = new RegExp(re); | 174 _regexp = new RegExp(re); |
| 174 } else if (re is RegExp) { | 175 } else if (re is RegExp) { |
| 175 _regexp = re; | 176 _regexp = re; |
| 176 } else { | 177 } else { |
| 177 throw new ArgumentError('matches requires a regexp or string'); | 178 throw new ArgumentError('matches requires a regexp or string'); |
| 178 } | 179 } |
| 179 } | 180 } |
| 180 | 181 |
| 181 bool matches(item, MatchState matchState) => | 182 bool matches(item, Map matchState) => |
| 182 item is String ? _regexp.hasMatch(item) : false; | 183 item is String ? _regexp.hasMatch(item) : false; |
| 183 | 184 |
| 184 Description describe(Description description) => | 185 Description describe(Description description) => |
| 185 description.add("match '${_regexp.pattern}'"); | 186 description.add("match '${_regexp.pattern}'"); |
| 186 } | 187 } |
| 187 | 188 |
| 188 // String matchers match against a string. We add this intermediate | 189 // String matchers match against a string. We add this intermediate |
| 189 // class to give better mismatch error messages than the base Matcher class. | 190 // class to give better mismatch error messages than the base Matcher class. |
| 190 abstract class _StringMatcher extends BaseMatcher { | 191 abstract class _StringMatcher extends BaseMatcher { |
| 191 const _StringMatcher(); | 192 const _StringMatcher(); |
| 192 Description describeMismatch(item, Description mismatchDescription, | 193 Description describeMismatch(item, Description mismatchDescription, |
| 193 MatchState matchState, bool verbose) { | 194 Map matchState, bool verbose) { |
| 194 if (!(item is String)) { | 195 if (!(item is String)) { |
| 195 return mismatchDescription. | 196 return mismatchDescription. |
| 196 addDescriptionOf(item). | 197 addDescriptionOf(item). |
| 197 add(' not a string'); | 198 add(' not a string'); |
| 198 } else { | 199 } else { |
| 199 return super.describeMismatch(item, mismatchDescription, | 200 return super.describeMismatch(item, mismatchDescription, |
| 200 matchState, verbose); | 201 matchState, verbose); |
| 201 } | 202 } |
| 202 } | 203 } |
| 203 } | 204 } |
| OLD | NEW |