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