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 library matcher.string_matchers; | |
6 | |
7 import 'interfaces.dart'; | 5 import 'interfaces.dart'; |
8 | 6 |
9 /// Returns a matcher which matches if the match argument is a string and | 7 /// Returns a matcher which matches if the match argument is a string and |
10 /// is equal to [value] when compared case-insensitively. | 8 /// is equal to [value] when compared case-insensitively. |
11 Matcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value); | 9 Matcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value); |
12 | 10 |
13 class _IsEqualIgnoringCase extends _StringMatcher { | 11 class _IsEqualIgnoringCase extends _StringMatcher { |
14 final String _value; | 12 final String _value; |
15 final String _matchValue; | 13 final String _matchValue; |
16 | 14 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 description.addDescriptionOf(_matchValue).add(' ignoring whitespace'); | 58 description.addDescriptionOf(_matchValue).add(' ignoring whitespace'); |
61 | 59 |
62 Description describeMismatch( | 60 Description describeMismatch( |
63 item, Description mismatchDescription, Map matchState, bool verbose) { | 61 item, Description mismatchDescription, Map matchState, bool verbose) { |
64 if (item is String) { | 62 if (item is String) { |
65 return mismatchDescription | 63 return mismatchDescription |
66 .add('is ') | 64 .add('is ') |
67 .addDescriptionOf(collapseWhitespace(item)) | 65 .addDescriptionOf(collapseWhitespace(item)) |
68 .add(' with whitespace compressed'); | 66 .add(' with whitespace compressed'); |
69 } else { | 67 } else { |
70 return super.describeMismatch( | 68 return super |
71 item, mismatchDescription, matchState, verbose); | 69 .describeMismatch(item, mismatchDescription, matchState, verbose); |
72 } | 70 } |
73 } | 71 } |
74 } | 72 } |
75 | 73 |
76 /// Returns a matcher that matches if the match argument is a string and | 74 /// Returns a matcher that matches if the match argument is a string and |
77 /// starts with [prefixString]. | 75 /// starts with [prefixString]. |
78 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString); | 76 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString); |
79 | 77 |
80 class _StringStartsWith extends _StringMatcher { | 78 class _StringStartsWith extends _StringMatcher { |
81 final String _prefix; | 79 final String _prefix; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 | 162 |
165 // String matchers match against a string. We add this intermediate | 163 // String matchers match against a string. We add this intermediate |
166 // class to give better mismatch error messages than the base Matcher class. | 164 // class to give better mismatch error messages than the base Matcher class. |
167 abstract class _StringMatcher extends Matcher { | 165 abstract class _StringMatcher extends Matcher { |
168 const _StringMatcher(); | 166 const _StringMatcher(); |
169 Description describeMismatch( | 167 Description describeMismatch( |
170 item, Description mismatchDescription, Map matchState, bool verbose) { | 168 item, Description mismatchDescription, Map matchState, bool verbose) { |
171 if (!(item is String)) { | 169 if (!(item is String)) { |
172 return mismatchDescription.addDescriptionOf(item).add(' not a string'); | 170 return mismatchDescription.addDescriptionOf(item).add(' not a string'); |
173 } else { | 171 } else { |
174 return super.describeMismatch( | 172 return super |
175 item, mismatchDescription, matchState, verbose); | 173 .describeMismatch(item, mismatchDescription, matchState, verbose); |
176 } | 174 } |
177 } | 175 } |
178 } | 176 } |
179 | 177 |
180 /// Utility function to collapse whitespace runs to single spaces | 178 /// Utility function to collapse whitespace runs to single spaces |
181 /// and strip leading/trailing whitespace. | 179 /// and strip leading/trailing whitespace. |
182 String collapseWhitespace(String string) { | 180 String collapseWhitespace(String string) { |
183 var result = new StringBuffer(); | 181 var result = new StringBuffer(); |
184 var skipSpace = true; | 182 var skipSpace = true; |
185 for (var i = 0; i < string.length; i++) { | 183 for (var i = 0; i < string.length; i++) { |
186 var character = string[i]; | 184 var character = string[i]; |
187 if (_isWhitespace(character)) { | 185 if (_isWhitespace(character)) { |
188 if (!skipSpace) { | 186 if (!skipSpace) { |
189 result.write(' '); | 187 result.write(' '); |
190 skipSpace = true; | 188 skipSpace = true; |
191 } | 189 } |
192 } else { | 190 } else { |
193 result.write(character); | 191 result.write(character); |
194 skipSpace = false; | 192 skipSpace = false; |
195 } | 193 } |
196 } | 194 } |
197 return result.toString().trim(); | 195 return result.toString().trim(); |
198 } | 196 } |
199 | 197 |
200 bool _isWhitespace(String ch) => | 198 bool _isWhitespace(String ch) => |
201 ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; | 199 ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; |
OLD | NEW |