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", equalsIgnoringCase("hello world")); |
| 38 /// expect(" hello world", equalsIgnoringCase("hello world")); |
| 39 /// expect("hello world ", equalsIgnoringCase("hello world")); |
| 40 /// |
| 41 /// The following will not match: |
| 42 /// |
| 43 /// expect("helloworld", equalsIgnoringCase("hello world")); |
| 44 /// expect("he llo world", equalsIgnoringCase("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(item, Description mismatchDescription, |
| 63 Map matchState, bool verbose) { |
| 64 if (item is String) { |
| 65 return mismatchDescription.add('is '). |
| 66 addDescriptionOf(collapseWhitespace(item)). |
| 67 add(' with whitespace compressed'); |
| 68 } else { |
| 69 return super.describeMismatch(item, mismatchDescription, |
| 70 matchState, verbose); |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 /// Returns a matcher that matches if the match argument is a string and |
| 76 /// starts with [prefixString]. |
| 77 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString); |
| 78 |
| 79 class _StringStartsWith extends _StringMatcher { |
| 80 final String _prefix; |
| 81 |
| 82 const _StringStartsWith(this._prefix); |
| 83 |
| 84 bool matches(item, Map matchState) => |
| 85 item is String && item.startsWith(_prefix); |
| 86 |
| 87 Description describe(Description description) => |
| 88 description.add('a string starting with ').addDescriptionOf(_prefix); |
| 89 } |
| 90 |
| 91 /// Returns a matcher that matches if the match argument is a string and |
| 92 /// ends with [suffixString]. |
| 93 Matcher endsWith(String suffixString) => new _StringEndsWith(suffixString); |
| 94 |
| 95 class _StringEndsWith extends _StringMatcher { |
| 96 final String _suffix; |
| 97 |
| 98 const _StringEndsWith(this._suffix); |
| 99 |
| 100 bool matches(item, Map matchState) => |
| 101 item is String && item.endsWith(_suffix); |
| 102 |
| 103 Description describe(Description description) => |
| 104 description.add('a string ending with ').addDescriptionOf(_suffix); |
| 105 } |
| 106 |
| 107 /// Returns a matcher that matches if the match argument is a string and |
| 108 /// contains a given list of [substrings] in relative order. |
| 109 /// |
| 110 /// For example, `stringContainsInOrder(["a", "e", "i", "o", "u"])` will match |
| 111 /// "abcdefghijklmnopqrstuvwxyz". |
| 112 |
| 113 Matcher stringContainsInOrder(List<String> substrings) => |
| 114 new _StringContainsInOrder(substrings); |
| 115 |
| 116 class _StringContainsInOrder extends _StringMatcher { |
| 117 final List<String> _substrings; |
| 118 |
| 119 const _StringContainsInOrder(this._substrings); |
| 120 |
| 121 bool matches(item, Map matchState) { |
| 122 if (!(item is String)) { |
| 123 return false; |
| 124 } |
| 125 var from_index = 0; |
| 126 for (var s in _substrings) { |
| 127 from_index = item.indexOf(s, from_index); |
| 128 if (from_index < 0) return false; |
| 129 } |
| 130 return true; |
| 131 } |
| 132 |
| 133 Description describe(Description description) => |
| 134 description.addAll('a string containing ', ', ', ' in order', |
| 135 _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(item, Description mismatchDescription, |
| 170 Map matchState, bool verbose) { |
| 171 if (!(item is String)) { |
| 172 return mismatchDescription. |
| 173 addDescriptionOf(item). |
| 174 add(' not a string'); |
| 175 } else { |
| 176 return super.describeMismatch(item, mismatchDescription, |
| 177 matchState, verbose); |
| 178 } |
| 179 } |
| 180 } |
| 181 |
| 182 /// Utility function to collapse whitespace runs to single spaces |
| 183 /// and strip leading/trailing whitespace. |
| 184 String collapseWhitespace(String string) { |
| 185 var result = new StringBuffer(); |
| 186 var skipSpace = true; |
| 187 for (var i = 0; i < string.length; i++) { |
| 188 var character = string[i]; |
| 189 if (_isWhitespace(character)) { |
| 190 if (!skipSpace) { |
| 191 result.write(' '); |
| 192 skipSpace = true; |
| 193 } |
| 194 } else { |
| 195 result.write(character); |
| 196 skipSpace = false; |
| 197 } |
| 198 } |
| 199 return result.toString().trim(); |
| 200 } |
| 201 |
| 202 bool _isWhitespace(String ch) => |
| 203 ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; |
OLD | NEW |