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