Chromium Code Reviews| 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 /** Returns a matcher which matches strings that are empty. */ | |
| 6 IMatcher isEmptyString() => new _Empty(); | |
|
Bob Nystrom
2012/05/30 23:23:51
Does this add a lot of value over just doing:
exp
| |
| 7 | |
| 8 class _Empty extends _StringMatcher { | |
| 9 | |
| 10 _Empty(); | |
| 11 | |
| 12 bool matches(item) => (item is String && item.length == 0); | |
| 13 | |
| 14 IDescription describe(IDescription description) => | |
| 15 description.append('empty string'); | |
| 16 } | |
| 17 | |
| 18 /** | |
| 19 * Returns a matcher which matches if the interpolated string form of the | |
| 20 * match argument matches the given [matcher]. That is: | |
| 21 * | |
| 22 * expect(foo, isInterpolated(bar)) | |
| 23 * | |
| 24 * is equivalent to: | |
| 25 * | |
| 26 * expect("${foo}", bar) | |
| 27 */ | |
| 28 IMatcher isInterpolated(matcher) => new _IsInterpolated(wrapMatcher(matcher)); | |
|
Bob Nystrom
2012/05/30 23:23:51
I think this is equivalent to toString(). Do we ne
gram
2012/06/01 17:33:15
Done.
| |
| 29 | |
| 30 class _IsInterpolated extends Matcher { | |
| 31 Matcher _matcher; | |
| 32 | |
| 33 _IsInterpolated(this._matcher) { | |
| 34 if (!(_matcher is Matcher)) { | |
| 35 throw new IllegalArgumentException('isInterpolated expects Matcher'); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 bool matches(item) => _matcher.matches("${item}"); | |
| 40 | |
| 41 IDescription describe(IDescription description) => | |
| 42 description.appendDescriptionOf(_matcher).append(' interpolated'); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Returns a matcher which matches if the match argument is a string and | |
| 47 * is equal to [value] when compared case-insensitively. | |
| 48 */ | |
| 49 IMatcher equalToIgnoringCase(String value) => new _IsEqualIgnoringCase(value); | |
|
Bob Nystrom
2012/05/30 23:23:51
"equalTo" -> "equals"
gram
2012/06/01 17:33:15
Done.
| |
| 50 | |
| 51 class _IsEqualIgnoringCase extends _StringMatcher { | |
| 52 | |
| 53 String _value; | |
| 54 String _matchValue; | |
| 55 | |
| 56 _IsEqualIgnoringCase(String this._value) { | |
| 57 if (!(_value is String)) { | |
|
Bob Nystrom
2012/05/30 23:23:51
Remove this check.
gram
2012/06/01 17:33:15
Done.
| |
| 58 throw new IllegalArgumentException('equalToIgnoringCase requires string'); | |
| 59 } | |
| 60 _matchValue = _value.toLowerCase(); | |
| 61 } | |
| 62 | |
| 63 bool matches(item) => | |
| 64 (item is String && _matchValue == item.toLowerCase()); | |
| 65 | |
| 66 IDescription describe(IDescription description) => | |
| 67 description.appendDescriptionOf(_value).append(' ignoring case'); | |
| 68 } | |
| 69 | |
| 70 //--------------------------------------------------------------------- | |
| 71 // Utility functions | |
|
Bob Nystrom
2012/05/30 23:23:51
Remove this comment?
gram
2012/06/01 17:33:15
I removed the --- at least.
| |
| 72 | |
| 73 bool isWhiteSpace(String ch) => (' \n\r\t'.indexOf(ch) >= 0); | |
|
Bob Nystrom
2012/05/30 23:23:51
There is some debate here, but I think this should
gram
2012/06/01 17:33:15
Done.
| |
| 74 | |
| 75 String _stripspace(_string) { | |
|
Bob Nystrom
2012/05/30 23:23:51
"_normalizeWhitespace"?
gram
2012/06/01 17:33:15
Done.
gram
2012/06/01 17:33:15
Done.
| |
| 76 StringBuffer result = new StringBuffer(); | |
| 77 bool skipSpace = true; | |
| 78 for (var i = 0; i < _string.length; i++) { | |
| 79 var character = _string[i]; | |
| 80 if (isWhiteSpace(character)) { | |
| 81 if (!skipSpace) { | |
| 82 result.add(' '); | |
| 83 skipSpace = true; | |
| 84 } | |
| 85 } else { | |
| 86 result.add(character); | |
| 87 skipSpace = false; | |
| 88 } | |
| 89 } | |
| 90 return result.toString().trim(); | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * Returns a matcher which matches if the match argument is a string and | |
| 95 * is equal to [value] when compared with all runs of white space | |
|
Bob Nystrom
2012/05/30 23:23:51
"whitespace"
gram
2012/06/01 17:33:15
Done.
| |
| 96 * collapsed to single spaces and leading and trailing whitespace removed. | |
| 97 * | |
| 98 * For example, equalToIgnoringCase("hello world") will match | |
|
Bob Nystrom
2012/05/30 23:23:51
Put inline code in backticks: `equalToIgnoringWhit
gram
2012/06/01 17:33:15
Done.
| |
| 99 * "hello world", " hello world" and "hello world ". | |
| 100 */ | |
| 101 IMatcher equalToIgnoringWhitespace(_string) => | |
|
Bob Nystrom
2012/05/30 23:23:51
"equalTo" -> "equals"
gram
2012/06/01 17:33:15
Done.
| |
| 102 new _IsEqualIgnoringWhiteSpace(_string); | |
| 103 | |
| 104 class _IsEqualIgnoringWhiteSpace extends _StringMatcher { | |
| 105 | |
| 106 String _value; | |
| 107 String _matchValue; | |
| 108 | |
| 109 _IsEqualIgnoringWhiteSpace(this._value) { | |
| 110 if (!(_value is String)) { | |
|
Bob Nystrom
2012/05/30 23:23:51
Remove check.
gram
2012/06/01 17:33:15
Done.
| |
| 111 throw new | |
| 112 IllegalArgumentException('equalToIgnoringWhitespace requires string'); | |
| 113 } | |
| 114 _matchValue = _stripspace(_value); | |
| 115 } | |
| 116 | |
| 117 bool matches(item) => (item is String && _matchValue == _stripspace(item)); | |
|
Bob Nystrom
2012/05/30 23:23:51
Unneeded ().
gram
2012/06/01 17:33:15
Done.
| |
| 118 | |
| 119 IDescription describe(IDescription description) => | |
| 120 description.appendDescriptionOf(_matchValue). | |
|
Bob Nystrom
2012/05/30 23:23:51
I would use a {} here. If not, +2 indent.
gram
2012/06/01 17:33:15
Done.
| |
| 121 append(' ignoring whitespace'); | |
| 122 | |
| 123 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 124 if (item is String) { | |
| 125 return mismatchDescription.append('was '). | |
| 126 appendDescriptionOf(_stripspace(item)); | |
| 127 } else { | |
| 128 return super.describeMismatch(item, mismatchDescription); | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 /** | |
| 134 * Returns a matcher that matches if the match argument is a string and | |
| 135 * starts with [value]. | |
| 136 */ | |
| 137 IMatcher startsWith(substring) => new _StringStartsWith(substring); | |
| 138 | |
| 139 class _StringStartsWith extends _StringMatcher { | |
| 140 | |
| 141 String _substring; | |
| 142 | |
| 143 _StringStartsWith(String this._substring) { | |
| 144 if (!(_substring is String)) { | |
| 145 throw new | |
| 146 IllegalArgumentException('startsWith requires a string'); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 bool matches(item) => (item is String && item.startsWith(_substring)); | |
| 151 | |
| 152 IDescription describe(IDescription description) => | |
| 153 description.append('a string starting with '). | |
| 154 appendDescriptionOf(_substring); | |
| 155 } | |
| 156 | |
| 157 /** | |
| 158 * Returns a matcher that matches if the match argument is a string and | |
| 159 * ends with [value]. | |
| 160 */ | |
| 161 IMatcher endsWith(value) => new _StringEndsWith(value); | |
| 162 | |
| 163 class _StringEndsWith extends _StringMatcher { | |
| 164 | |
| 165 String _substring; | |
| 166 | |
| 167 _StringEndsWith(String this._substring) { | |
| 168 if (!(_substring is String)) { | |
| 169 throw new | |
| 170 IllegalArgumentException('endsWith requires a string'); | |
|
Bob Nystrom
2012/05/30 23:23:51
Indent +2.
gram
2012/06/01 17:33:15
Done.
| |
| 171 } | |
| 172 } | |
| 173 | |
| 174 bool matches(item) => (item is String && item.endsWith(_substring)); | |
| 175 | |
| 176 IDescription describe(IDescription description) => | |
| 177 description.append('a string ending with '). | |
| 178 appendDescriptionOf(_substring); | |
| 179 } | |
| 180 | |
| 181 /** | |
| 182 * Returns a matcher that matches if the match argument is a string and | |
| 183 * contains [substring]. | |
| 184 */ | |
| 185 IMatcher containsString(substring) => new _StringContains(substring); | |
|
Bob Nystrom
2012/05/30 23:23:51
Can we do a single unified contains() that works w
gram
2012/06/01 17:33:15
Done.
| |
| 186 | |
| 187 class _StringContains extends _StringMatcher { | |
| 188 | |
| 189 String _substring; | |
| 190 | |
| 191 _StringContains(String this._substring) { | |
| 192 if (!(_substring is String)) { | |
| 193 throw new | |
| 194 IllegalArgumentException('containsString requires a string'); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 bool matches(item) => (item is String && item.indexOf(_substring) >= 0); | |
| 199 | |
| 200 IDescription describe(IDescription description) => | |
| 201 description.append('a string containing '). | |
| 202 appendDescriptionOf(_substring); | |
| 203 } | |
| 204 | |
| 205 /** | |
| 206 * Returns a matcher that matches if the match argument is a string and | |
| 207 * contains a given list of [substrings] in relative order. | |
| 208 * | |
| 209 * For example, stringContainsInOrder(["a", "e", "i", "o", "u"]) will match | |
| 210 * "abcdefghijklmnopqrstuvwxyz". | |
| 211 */ | |
| 212 IMatcher stringContainsInOrder(substrings) => | |
| 213 new _StringContainsInOrder(substrings); | |
| 214 | |
| 215 class _StringContainsInOrder extends _StringMatcher { | |
| 216 | |
| 217 List<String> _substrings; | |
| 218 | |
| 219 _StringContainsInOrder(List<String> this._substrings) { | |
| 220 if (!(_substrings is List)) { | |
| 221 throw new IllegalArgumentException( | |
| 222 'stringContainsInOrder requires a list of strings'); | |
| 223 } | |
| 224 for (var s in _substrings) { | |
| 225 if (!(s is String)) { | |
| 226 throw new IllegalArgumentException( | |
| 227 'stringContainsInOrder requires a list of strings'); | |
| 228 } | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 bool matches(item) { | |
| 233 if (!(item is String)) { | |
| 234 return false; | |
| 235 } | |
| 236 var from_index = 0; | |
| 237 for (var s in _substrings) { | |
| 238 from_index = item.indexOf(s, from_index); | |
| 239 if (from_index < 0) | |
| 240 return false; | |
| 241 } | |
| 242 return true; | |
| 243 } | |
| 244 | |
| 245 IDescription describe(IDescription description) => | |
| 246 description.appendList('a string containing ', ', ', ' in order', | |
| 247 _substrings); | |
| 248 } | |
| 249 | |
| 250 /** | |
| 251 * Returns a matcher that matches if the match argument is a string and | |
| 252 * matches the regular expression given by [re]. [re] can be a RegExp | |
| 253 * instance or a string; in the latter case it will be used to create | |
| 254 * a RegExp instance. | |
| 255 */ | |
| 256 IMatcher matches(re) => new _MatchesRegExp(re); | |
| 257 | |
| 258 class _MatchesRegExp extends _StringMatcher { | |
| 259 RegExp _regexp; | |
| 260 | |
| 261 _MatchesRegExp(re) { | |
| 262 if (re is String) { | |
| 263 _regexp = new RegExp(re); | |
| 264 } else if (re is RegExp) { | |
| 265 _regexp = re; | |
| 266 } else { | |
| 267 throw new IllegalArgumentException('matches requires a regexp or string'); | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 bool matches(String item) => _regexp.hasMatch(item); | |
| 272 | |
| 273 IDescription describe(IDescription description) => | |
|
Bob Nystrom
2012/05/30 23:23:51
Extra space.
gram
2012/06/01 17:33:15
Done.
| |
| 274 description.append("match '${_regexp.pattern}'"); | |
| 275 } | |
| 276 | |
| 277 // String matchers match against a string. We add this intermediate | |
| 278 // class to give better mismatch error messages than the base Matcher class. | |
| 279 class _StringMatcher extends Matcher { | |
| 280 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 281 if (!(item is String)) { | |
| 282 return mismatchDescription. | |
| 283 appendDescriptionOf(item). | |
| 284 append(' not a string'); | |
| 285 } else { | |
| 286 return super.describeMismatch(item, mismatchDescription); | |
| 287 } | |
| 288 } | |
| 289 } | |
| OLD | NEW |