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