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; | 5 library matcher.string_matchers; |
6 | 6 |
7 import 'interfaces.dart'; | 7 import 'interfaces.dart'; |
8 | 8 |
9 /// Returns a matcher which matches if the match argument is a string and | 9 /// Returns a matcher which matches if the match argument is a string and |
10 /// is equal to [value] when compared case-insensitively. | 10 /// is equal to [value] when compared case-insensitively. |
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 final String _matchValue; | 15 final String _matchValue; |
16 | 16 |
17 _IsEqualIgnoringCase(String value) | 17 _IsEqualIgnoringCase(String value) |
18 : _value = value, | 18 : _value = value, |
19 _matchValue = value.toLowerCase(); | 19 _matchValue = value.toLowerCase(); |
20 | 20 |
21 bool matches(item, Map matchState) => | 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 /// Returns a matcher which matches if the match argument is a string and | 28 /// Returns a matcher which matches if the match argument is a string and |
29 /// is equal to [value] when compared with all runs of whitespace | 29 /// is equal to [value], ignoring whitespace. |
30 /// collapsed to single spaces and leading and trailing whitespace removed. | |
31 /// | 30 /// |
32 /// For example, `equalsIgnoringCase("hello world")` will match | 31 /// In this matcher, "ignoring whitespace" means comparing with all runs of |
33 /// "hello world", " hello world" and "hello world ". | 32 /// whitespace collapsed to single space characters and leading and trailing |
34 Matcher equalsIgnoringWhitespace(String string) => | 33 /// whitespace removed. |
35 new _IsEqualIgnoringWhitespace(string); | 34 /// |
35 /// For example, the following will all match successfully: | |
36 /// | |
37 /// expect("hello world", equalsIgnoringCase("hello world")); | |
38 /// expect(" hello world", equalsIgnoringCase("hello world")); | |
39 /// expect("hello world ", equalsIgnoringCase("hello world")); | |
Bob Nystrom
2014/10/06 16:14:28
A negative example might help here too, like "he l
srawlins
2014/10/06 16:19:35
Done.
| |
40 Matcher equalsIgnoringWhitespace(String value) => | |
41 new _IsEqualIgnoringWhitespace(value); | |
36 | 42 |
37 class _IsEqualIgnoringWhitespace extends _StringMatcher { | 43 class _IsEqualIgnoringWhitespace extends _StringMatcher { |
38 final String _value; | 44 final String _value; |
39 final String _matchValue; | 45 final String _matchValue; |
40 | 46 |
41 _IsEqualIgnoringWhitespace(String value) | 47 _IsEqualIgnoringWhitespace(String value) |
42 : _value = value, | 48 : _value = value, |
43 _matchValue = collapseWhitespace(value); | 49 _matchValue = collapseWhitespace(value); |
44 | 50 |
45 bool matches(item, Map matchState) => | 51 bool matches(item, Map matchState) => |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 } | 124 } |
119 return true; | 125 return true; |
120 } | 126 } |
121 | 127 |
122 Description describe(Description description) => | 128 Description describe(Description description) => |
123 description.addAll('a string containing ', ', ', ' in order', | 129 description.addAll('a string containing ', ', ', ' in order', |
124 _substrings); | 130 _substrings); |
125 } | 131 } |
126 | 132 |
127 /// Returns a matcher that matches if the match argument is a string and | 133 /// Returns a matcher that matches if the match argument is a string and |
128 /// matches the regular expression given by [re]. [re] can be a [RegExp] | 134 /// matches the regular expression given by [re]. |
129 /// instance or a [String]; in the latter case it will be used to create | 135 /// |
130 /// a RegExp instance. | 136 /// [re] can be a [RegExp] instance or a [String]; in the latter case it will be |
137 /// used to create a RegExp instance. | |
131 Matcher matches(re) => new _MatchesRegExp(re); | 138 Matcher matches(re) => new _MatchesRegExp(re); |
132 | 139 |
133 class _MatchesRegExp extends _StringMatcher { | 140 class _MatchesRegExp extends _StringMatcher { |
134 RegExp _regexp; | 141 RegExp _regexp; |
135 | 142 |
136 _MatchesRegExp(re) { | 143 _MatchesRegExp(re) { |
137 if (re is String) { | 144 if (re is String) { |
138 _regexp = new RegExp(re); | 145 _regexp = new RegExp(re); |
139 } else if (re is RegExp) { | 146 } else if (re is RegExp) { |
140 _regexp = re; | 147 _regexp = re; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 } else { | 189 } else { |
183 result.write(character); | 190 result.write(character); |
184 skipSpace = false; | 191 skipSpace = false; |
185 } | 192 } |
186 } | 193 } |
187 return result.toString().trim(); | 194 return result.toString().trim(); |
188 } | 195 } |
189 | 196 |
190 bool _isWhitespace(String ch) => | 197 bool _isWhitespace(String ch) => |
191 ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; | 198 ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; |
OLD | NEW |