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