Chromium Code Reviews| Index: lib/unittest/string_matchers.dart |
| =================================================================== |
| --- lib/unittest/string_matchers.dart (revision 0) |
| +++ lib/unittest/string_matchers.dart (revision 0) |
| @@ -0,0 +1,190 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is a string and |
| + * is equal to [value] when compared case-insensitively. |
| + */ |
| +IMatcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value); |
| + |
| +class _IsEqualIgnoringCase extends _StringMatcher { |
| + final String _value; |
| + String _matchValue; |
| + |
| + _IsEqualIgnoringCase(this._value) { |
| + _matchValue = _value.toLowerCase(); |
| + } |
| + |
| + bool matches(item) => item is String && _matchValue == item.toLowerCase(); |
| + |
| + IDescription describe(IDescription description) => |
| + description.addDescriptionOf(_value).add(' ignoring case'); |
| +} |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is a string and |
| + * is equal to [value] when compared with all runs of whitespace |
| + * collapsed to single spaces and leading and trailing whitespace removed. |
| + * |
| + * For example, `equalsIgnoringCase("hello world")` will match |
| + * "hello world", " hello world" and "hello world ". |
| + */ |
| +IMatcher equalsIgnoringWhitespace(_string) => |
| + new _IsEqualIgnoringWhiteSpace(_string); |
| + |
| +class _IsEqualIgnoringWhiteSpace extends _StringMatcher { |
|
Bob Nystrom
2012/06/01 18:22:23
This is private so it matters less, but for consis
gram
2012/06/01 22:22:00
Done.
|
| + final String _value; |
| + String _matchValue; |
| + |
| + _IsEqualIgnoringWhiteSpace(this._value) { |
| + _matchValue = _collapseWhitespace(_value); |
| + } |
| + |
| + bool matches(item) => |
| + item is String && _matchValue == _collapseWhitespace(item); |
| + |
| + IDescription describe(IDescription description) => |
| + description.addDescriptionOf(_matchValue).add(' ignoring whitespace'); |
| + |
| + IDescription describeMismatch(item, IDescription mismatchDescription) { |
| + if (item is String) { |
| + return mismatchDescription.add('was '). |
| + addDescriptionOf(_collapseWhitespace(item)); |
| + } else { |
| + return super.describeMismatch(item, mismatchDescription); |
| + } |
| + } |
| + |
| + // Utility function to collapse whitespace runs |
|
Bob Nystrom
2012/06/01 18:22:23
Doc comment?
gram
2012/06/01 22:22:00
Done.
|
| + |
| + String _collapseWhitespace(_string) { |
| + bool isWhitespace(String ch) => (' \n\r\t'.indexOf(ch) >= 0); |
| + StringBuffer result = new StringBuffer(); |
| + bool skipSpace = true; |
| + for (var i = 0; i < _string.length; i++) { |
| + var character = _string[i]; |
| + if (isWhitespace(character)) { |
| + if (!skipSpace) { |
| + result.add(' '); |
| + skipSpace = true; |
| + } |
| + } else { |
| + result.add(character); |
| + skipSpace = false; |
| + } |
| + } |
| + return result.toString().trim(); |
| + } |
| +} |
| + |
| +/** |
| + * Returns a matcher that matches if the match argument is a string and |
| + * starts with [prefixString]. |
| + */ |
| +IMatcher startsWith(String prefixString) => new _StringStartsWith(prefixString); |
| + |
| +class _StringStartsWith extends _StringMatcher { |
| + final String _prefix; |
| + |
| + const _StringStartsWith(this._prefix); |
| + |
| + bool matches(item) => item is String && item.startsWith(_prefix); |
| + |
| + IDescription describe(IDescription description) => |
| + description.add('a string starting with ').addDescriptionOf(_prefix); |
| +} |
| + |
| +/** |
| + * Returns a matcher that matches if the match argument is a string and |
| + * ends with [suffixString]. |
| + */ |
| +IMatcher endsWith(String suffixString) => new _StringEndsWith(suffixString); |
| + |
| +class _StringEndsWith extends _StringMatcher { |
| + |
| + final String _suffix; |
| + |
| + const _StringEndsWith(this._suffix); |
| + |
| + bool matches(item) => item is String && item.endsWith(_suffix); |
| + |
| + IDescription describe(IDescription description) => |
| + description.add('a string ending with ').addDescriptionOf(_suffix); |
| +} |
| + |
| +/** |
| + * Returns a matcher that matches if the match argument is a string and |
| + * contains a given list of [substrings] in relative order. |
| + * |
| + * For example, `stringContainsInOrder(["a", "e", "i", "o", "u"])` will match |
|
Bob Nystrom
2012/06/01 18:22:23
Yay markdown!
gram
2012/06/01 22:22:00
Done.
|
| + * "abcdefghijklmnopqrstuvwxyz". |
| + */ |
| +IMatcher stringContainsInOrder(substrings) => |
| + new _StringContainsInOrder(substrings); |
| + |
| +class _StringContainsInOrder extends _StringMatcher { |
| + |
| + final List<String> _substrings; |
| + |
| + const _StringContainsInOrder(this._substrings); |
| + |
| + bool matches(item) { |
| + if (!(item is String)) { |
| + return false; |
| + } |
| + var from_index = 0; |
| + for (var s in _substrings) { |
| + from_index = item.indexOf(s, from_index); |
| + if (from_index < 0) |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + IDescription describe(IDescription description) => |
| + description.addList('a string containing ', ', ', ' in order', |
| + _substrings); |
| +} |
| + |
| +/** |
| + * Returns a matcher that matches if the match argument is a string and |
| + * matches the regular expression given by [re]. [re] can be a RegExp |
| + * instance or a string; in the latter case it will be used to create |
| + * a RegExp instance. |
| + */ |
| +IMatcher matches(re) => new _MatchesRegExp(re); |
| + |
| +class _MatchesRegExp extends _StringMatcher { |
| + RegExp _regexp; |
| + |
| + _MatchesRegExp(re) { |
| + if (re is String) { |
| + _regexp = new RegExp(re); |
| + } else if (re is RegExp) { |
| + _regexp = re; |
| + } else { |
| + throw new IllegalArgumentException('matches requires a regexp or string'); |
| + } |
| + } |
| + |
| + bool matches(String item) => _regexp.hasMatch(item); |
| + |
| + IDescription describe(IDescription description) => |
| + description.add("match '${_regexp.pattern}'"); |
| +} |
| + |
| +// String matchers match against a string. We add this intermediate |
| +// class to give better mismatch error messages than the base Matcher class. |
| +/*abstract*/ class _StringMatcher extends Matcher { |
| + const _StringMatcher(); |
| + IDescription describeMismatch(item, IDescription mismatchDescription) { |
| + if (!(item is String)) { |
| + return mismatchDescription. |
| + addDescriptionOf(item). |
| + add(' not a string'); |
| + } else { |
| + return super.describeMismatch(item, mismatchDescription); |
| + } |
| + } |
| +} |