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