Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: lib/unittest/string_matchers.dart

Issue 10832058: Improved the way we generate mismatch descriptions. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/unittest/operator_matchers.dart ('k') | tests/lib/unittest/instance_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * Returns a matcher which matches if the match argument is a string and 6 * Returns a matcher which matches if the match argument is a string and
7 * is equal to [value] when compared case-insensitively. 7 * is equal to [value] when compared case-insensitively.
8 */ 8 */
9 Matcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value); 9 Matcher equalsIgnoringCase(String value) => new _IsEqualIgnoringCase(value);
10 10
11 class _IsEqualIgnoringCase extends _StringMatcher { 11 class _IsEqualIgnoringCase extends _StringMatcher {
12 final String _value; 12 final String _value;
13 String _matchValue; 13 String _matchValue;
14 14
15 _IsEqualIgnoringCase(this._value) { 15 _IsEqualIgnoringCase(this._value) {
16 _matchValue = _value.toLowerCase(); 16 _matchValue = _value.toLowerCase();
17 } 17 }
18 18
19 bool matches(item) => item is String && _matchValue == item.toLowerCase(); 19 bool matches(item, MatchState mismatchState) =>
20 item is String && _matchValue == item.toLowerCase();
20 21
21 Description describe(Description description) => 22 Description describe(Description description) =>
22 description.addDescriptionOf(_value).add(' ignoring case'); 23 description.addDescriptionOf(_value).add(' ignoring case');
23 } 24 }
24 25
25 /** 26 /**
26 * Returns a matcher which matches if the match argument is a string and 27 * Returns a matcher which matches if the match argument is a string and
27 * is equal to [value] when compared with all runs of whitespace 28 * is equal to [value] when compared with all runs of whitespace
28 * collapsed to single spaces and leading and trailing whitespace removed. 29 * collapsed to single spaces and leading and trailing whitespace removed.
29 * 30 *
30 * For example, `equalsIgnoringCase("hello world")` will match 31 * For example, `equalsIgnoringCase("hello world")` will match
31 * "hello world", " hello world" and "hello world ". 32 * "hello world", " hello world" and "hello world ".
32 */ 33 */
33 Matcher equalsIgnoringWhitespace(_string) => 34 Matcher equalsIgnoringWhitespace(_string) =>
34 new _IsEqualIgnoringWhitespace(_string); 35 new _IsEqualIgnoringWhitespace(_string);
35 36
36 class _IsEqualIgnoringWhitespace extends _StringMatcher { 37 class _IsEqualIgnoringWhitespace extends _StringMatcher {
37 final String _value; 38 final String _value;
38 String _matchValue; 39 String _matchValue;
39 40
40 _IsEqualIgnoringWhitespace(this._value) { 41 _IsEqualIgnoringWhitespace(this._value) {
41 _matchValue = collapseWhitespace(_value); 42 _matchValue = collapseWhitespace(_value);
42 } 43 }
43 44
44 bool matches(item) => 45 bool matches(item, MatchState matchState) =>
45 item is String && _matchValue == collapseWhitespace(item); 46 item is String && _matchValue == collapseWhitespace(item);
46 47
47 Description describe(Description description) => 48 Description describe(Description description) =>
48 description.addDescriptionOf(_matchValue).add(' ignoring whitespace'); 49 description.addDescriptionOf(_matchValue).add(' ignoring whitespace');
49 50
50 Description describeMismatch(item, Description mismatchDescription) { 51 Description describeMismatch(item, Description mismatchDescription,
52 MatchState matchState, bool verbose) {
51 if (item is String) { 53 if (item is String) {
52 return mismatchDescription.add('was '). 54 return mismatchDescription.add('was ').
53 addDescriptionOf(collapseWhitespace(item)); 55 addDescriptionOf(collapseWhitespace(item));
54 } else { 56 } else {
55 return super.describeMismatch(item, mismatchDescription); 57 return super.describeMismatch(item, mismatchDescription,
58 matchState, verbose);
56 } 59 }
57 } 60 }
58 } 61 }
59 62
60 /** 63 /**
61 * Utility function to collapse whitespace runs to single spaces 64 * Utility function to collapse whitespace runs to single spaces
62 * and strip leading/trailing whitespace. 65 * and strip leading/trailing whitespace.
63 */ 66 */
64 String collapseWhitespace(_string) { 67 String collapseWhitespace(_string) {
65 bool isWhitespace(String ch) => (' \n\r\t'.indexOf(ch) >= 0); 68 bool isWhitespace(String ch) => (' \n\r\t'.indexOf(ch) >= 0);
(...skipping 18 matching lines...) Expand all
84 * Returns a matcher that matches if the match argument is a string and 87 * Returns a matcher that matches if the match argument is a string and
85 * starts with [prefixString]. 88 * starts with [prefixString].
86 */ 89 */
87 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString); 90 Matcher startsWith(String prefixString) => new _StringStartsWith(prefixString);
88 91
89 class _StringStartsWith extends _StringMatcher { 92 class _StringStartsWith extends _StringMatcher {
90 final String _prefix; 93 final String _prefix;
91 94
92 const _StringStartsWith(this._prefix); 95 const _StringStartsWith(this._prefix);
93 96
94 bool matches(item) => item is String && item.startsWith(_prefix); 97 bool matches(item, MatchState matchState) =>
98 item is String && item.startsWith(_prefix);
95 99
96 Description describe(Description description) => 100 Description describe(Description description) =>
97 description.add('a string starting with ').addDescriptionOf(_prefix); 101 description.add('a string starting with ').addDescriptionOf(_prefix);
98 } 102 }
99 103
100 /** 104 /**
101 * Returns a matcher that matches if the match argument is a string and 105 * Returns a matcher that matches if the match argument is a string and
102 * ends with [suffixString]. 106 * ends with [suffixString].
103 */ 107 */
104 Matcher endsWith(String suffixString) => new _StringEndsWith(suffixString); 108 Matcher endsWith(String suffixString) => new _StringEndsWith(suffixString);
105 109
106 class _StringEndsWith extends _StringMatcher { 110 class _StringEndsWith extends _StringMatcher {
107 111
108 final String _suffix; 112 final String _suffix;
109 113
110 const _StringEndsWith(this._suffix); 114 const _StringEndsWith(this._suffix);
111 115
112 bool matches(item) => item is String && item.endsWith(_suffix); 116 bool matches(item, MatchState matchState) =>
117 item is String && item.endsWith(_suffix);
113 118
114 Description describe(Description description) => 119 Description describe(Description description) =>
115 description.add('a string ending with ').addDescriptionOf(_suffix); 120 description.add('a string ending with ').addDescriptionOf(_suffix);
116 } 121 }
117 122
118 /** 123 /**
119 * Returns a matcher that matches if the match argument is a string and 124 * Returns a matcher that matches if the match argument is a string and
120 * contains a given list of [substrings] in relative order. 125 * contains a given list of [substrings] in relative order.
121 * 126 *
122 * For example, `stringContainsInOrder(["a", "e", "i", "o", "u"])` will match 127 * For example, `stringContainsInOrder(["a", "e", "i", "o", "u"])` will match
123 * "abcdefghijklmnopqrstuvwxyz". 128 * "abcdefghijklmnopqrstuvwxyz".
124 */ 129 */
125 Matcher stringContainsInOrder(substrings) => 130 Matcher stringContainsInOrder(substrings) =>
126 new _StringContainsInOrder(substrings); 131 new _StringContainsInOrder(substrings);
127 132
128 class _StringContainsInOrder extends _StringMatcher { 133 class _StringContainsInOrder extends _StringMatcher {
129 134
130 final List<String> _substrings; 135 final List<String> _substrings;
131 136
132 const _StringContainsInOrder(this._substrings); 137 const _StringContainsInOrder(this._substrings);
133 138
134 bool matches(item) { 139 bool matches(item, MatchState matchState) {
135 if (!(item is String)) { 140 if (!(item is String)) {
136 return false; 141 return false;
137 } 142 }
138 var from_index = 0; 143 var from_index = 0;
139 for (var s in _substrings) { 144 for (var s in _substrings) {
140 from_index = item.indexOf(s, from_index); 145 from_index = item.indexOf(s, from_index);
141 if (from_index < 0) 146 if (from_index < 0)
142 return false; 147 return false;
143 } 148 }
144 return true; 149 return true;
(...skipping 18 matching lines...) Expand all
163 _MatchesRegExp(re) { 168 _MatchesRegExp(re) {
164 if (re is String) { 169 if (re is String) {
165 _regexp = new RegExp(re); 170 _regexp = new RegExp(re);
166 } else if (re is RegExp) { 171 } else if (re is RegExp) {
167 _regexp = re; 172 _regexp = re;
168 } else { 173 } else {
169 throw new IllegalArgumentException('matches requires a regexp or string'); 174 throw new IllegalArgumentException('matches requires a regexp or string');
170 } 175 }
171 } 176 }
172 177
173 bool matches(String item) => _regexp.hasMatch(item); 178 bool matches(String item, MatchState matchState) =>
179 _regexp.hasMatch(item);
174 180
175 Description describe(Description description) => 181 Description describe(Description description) =>
176 description.add("match '${_regexp.pattern}'"); 182 description.add("match '${_regexp.pattern}'");
177 } 183 }
178 184
179 // String matchers match against a string. We add this intermediate 185 // String matchers match against a string. We add this intermediate
180 // class to give better mismatch error messages than the base Matcher class. 186 // class to give better mismatch error messages than the base Matcher class.
181 /* abstract */ class _StringMatcher extends BaseMatcher { 187 /* abstract */ class _StringMatcher extends BaseMatcher {
182 const _StringMatcher(); 188 const _StringMatcher();
183 Description describeMismatch(item, Description mismatchDescription) { 189 Description describeMismatch(item, Description mismatchDescription,
190 MatchState matchState, bool verbose) {
184 if (!(item is String)) { 191 if (!(item is String)) {
185 return mismatchDescription. 192 return mismatchDescription.
186 addDescriptionOf(item). 193 addDescriptionOf(item).
187 add(' not a string'); 194 add(' not a string');
188 } else { 195 } else {
189 return super.describeMismatch(item, mismatchDescription); 196 return super.describeMismatch(item, mismatchDescription,
197 matchState, verbose);
190 } 198 }
191 } 199 }
192 } 200 }
OLDNEW
« no previous file with comments | « lib/unittest/operator_matchers.dart ('k') | tests/lib/unittest/instance_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698