OLD | NEW |
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 import 'package:stack_trace/stack_trace.dart'; | 5 import 'package:stack_trace/stack_trace.dart'; |
6 | 6 |
7 import 'description.dart'; | 7 import 'description.dart'; |
8 import 'interfaces.dart'; | 8 import 'interfaces.dart'; |
9 import 'util.dart'; | 9 import 'util.dart'; |
10 | 10 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 /// If [expected] is a [Matcher], then it matches using that. Otherwise it tests | 103 /// If [expected] is a [Matcher], then it matches using that. Otherwise it tests |
104 /// for equality using `==` on the expected value. | 104 /// for equality using `==` on the expected value. |
105 /// | 105 /// |
106 /// For [Iterable]s and [Map]s, this will recursively match the elements. To | 106 /// For [Iterable]s and [Map]s, this will recursively match the elements. To |
107 /// handle cyclic structures a recursion depth [limit] can be provided. The | 107 /// handle cyclic structures a recursion depth [limit] can be provided. The |
108 /// default limit is 100. [Set]s will be compared order-independently. | 108 /// default limit is 100. [Set]s will be compared order-independently. |
109 Matcher equals(expected, [int limit = 100]) => expected is String | 109 Matcher equals(expected, [int limit = 100]) => expected is String |
110 ? new _StringEqualsMatcher(expected) | 110 ? new _StringEqualsMatcher(expected) |
111 : new _DeepMatcher(expected, limit); | 111 : new _DeepMatcher(expected, limit); |
112 | 112 |
| 113 typedef _RecursiveMatcher = List<String> Function( |
| 114 dynamic, dynamic, String, int); |
| 115 |
113 class _DeepMatcher extends Matcher { | 116 class _DeepMatcher extends Matcher { |
114 final _expected; | 117 final _expected; |
115 final int _limit; | 118 final int _limit; |
116 var count; | |
117 | 119 |
118 _DeepMatcher(this._expected, [int limit = 1000]) : this._limit = limit; | 120 _DeepMatcher(this._expected, [int limit = 1000]) : this._limit = limit; |
119 | 121 |
120 // Returns a pair (reason, location) | 122 // Returns a pair (reason, location) |
121 List _compareIterables(expected, actual, matcher, depth, location) { | 123 List<String> _compareIterables(Iterable expected, Object actual, |
122 if (actual is! Iterable) return ['is not Iterable', location]; | 124 _RecursiveMatcher matcher, int depth, String location) { |
| 125 if (actual is Iterable) { |
| 126 var expectedIterator = expected.iterator; |
| 127 var actualIterator = actual.iterator; |
| 128 for (var index = 0;; index++) { |
| 129 // Advance in lockstep. |
| 130 var expectedNext = expectedIterator.moveNext(); |
| 131 var actualNext = actualIterator.moveNext(); |
123 | 132 |
124 var expectedIterator = expected.iterator; | 133 // If we reached the end of both, we succeeded. |
125 var actualIterator = actual.iterator; | 134 if (!expectedNext && !actualNext) return null; |
126 for (var index = 0;; index++) { | |
127 // Advance in lockstep. | |
128 var expectedNext = expectedIterator.moveNext(); | |
129 var actualNext = actualIterator.moveNext(); | |
130 | 135 |
131 // If we reached the end of both, we succeeded. | 136 // Fail if their lengths are different. |
132 if (!expectedNext && !actualNext) return null; | 137 var newLocation = '$location[$index]'; |
| 138 if (!expectedNext) return ['longer than expected', newLocation]; |
| 139 if (!actualNext) return ['shorter than expected', newLocation]; |
133 | 140 |
134 // Fail if their lengths are different. | 141 // Match the elements. |
135 var newLocation = '${location}[${index}]'; | 142 var rp = matcher(expectedIterator.current, actualIterator.current, |
136 if (!expectedNext) return ['longer than expected', newLocation]; | 143 newLocation, depth); |
137 if (!actualNext) return ['shorter than expected', newLocation]; | 144 if (rp != null) return rp; |
138 | 145 } |
139 // Match the elements. | 146 } else { |
140 var rp = matcher( | 147 return ['is not Iterable', location]; |
141 expectedIterator.current, actualIterator.current, newLocation, depth); | |
142 if (rp != null) return rp; | |
143 } | 148 } |
144 } | 149 } |
145 | 150 |
146 List _compareSets(Set expected, actual, matcher, depth, location) { | 151 List<String> _compareSets(Set expected, Object actual, |
147 if (actual is! Iterable) return ['is not Iterable', location]; | 152 _RecursiveMatcher matcher, int depth, String location) { |
148 actual = actual.toSet(); | 153 if (actual is Iterable) { |
| 154 Set other = actual.toSet(); |
149 | 155 |
150 for (var expectedElement in expected) { | 156 for (var expectedElement in expected) { |
151 if (actual.every((actualElement) => | 157 if (other.every((actualElement) => |
152 matcher(expectedElement, actualElement, location, depth) != null)) { | 158 matcher(expectedElement, actualElement, location, depth) != null)) { |
153 return ['does not contain $expectedElement', location]; | 159 return ['does not contain $expectedElement', location]; |
| 160 } |
154 } | 161 } |
155 } | |
156 | 162 |
157 if (actual.length > expected.length) { | 163 if (other.length > expected.length) { |
158 return ['larger than expected', location]; | 164 return ['larger than expected', location]; |
159 } else if (actual.length < expected.length) { | 165 } else if (other.length < expected.length) { |
160 return ['smaller than expected', location]; | 166 return ['smaller than expected', location]; |
| 167 } else { |
| 168 return null; |
| 169 } |
161 } else { | 170 } else { |
162 return null; | 171 return ['is not Iterable', location]; |
163 } | 172 } |
164 } | 173 } |
165 | 174 |
166 List _recursiveMatch(expected, actual, String location, int depth) { | 175 List<String> _recursiveMatch( |
| 176 Object expected, Object actual, String location, int depth) { |
167 // If the expected value is a matcher, try to match it. | 177 // If the expected value is a matcher, try to match it. |
168 if (expected is Matcher) { | 178 if (expected is Matcher) { |
169 var matchState = {}; | 179 var matchState = {}; |
170 if (expected.matches(actual, matchState)) return null; | 180 if (expected.matches(actual, matchState)) return null; |
171 | 181 |
172 var description = new StringDescription(); | 182 var description = new StringDescription(); |
173 expected.describe(description); | 183 expected.describe(description); |
174 return ['does not match $description', location]; | 184 return ['does not match $description', location]; |
175 } else { | 185 } else { |
176 // Otherwise, test for equality. | 186 // Otherwise, test for equality. |
(...skipping 10 matching lines...) Expand all Loading... |
187 // If _limit is 1 we can only recurse one level into object. | 197 // If _limit is 1 we can only recurse one level into object. |
188 if (depth == 0 || _limit > 1) { | 198 if (depth == 0 || _limit > 1) { |
189 if (expected is Set) { | 199 if (expected is Set) { |
190 return _compareSets( | 200 return _compareSets( |
191 expected, actual, _recursiveMatch, depth + 1, location); | 201 expected, actual, _recursiveMatch, depth + 1, location); |
192 } else if (expected is Iterable) { | 202 } else if (expected is Iterable) { |
193 return _compareIterables( | 203 return _compareIterables( |
194 expected, actual, _recursiveMatch, depth + 1, location); | 204 expected, actual, _recursiveMatch, depth + 1, location); |
195 } else if (expected is Map) { | 205 } else if (expected is Map) { |
196 if (actual is! Map) return ['expected a map', location]; | 206 if (actual is! Map) return ['expected a map', location]; |
197 | 207 var map = (actual as Map); |
198 var err = (expected.length == actual.length) | 208 var err = |
199 ? '' | 209 (expected.length == map.length) ? '' : 'has different length and '; |
200 : 'has different length and '; | |
201 for (var key in expected.keys) { | 210 for (var key in expected.keys) { |
202 if (!actual.containsKey(key)) { | 211 if (!map.containsKey(key)) { |
203 return ["${err}is missing map key '$key'", location]; | 212 return ["${err}is missing map key '$key'", location]; |
204 } | 213 } |
205 } | 214 } |
206 | 215 |
207 for (var key in actual.keys) { | 216 for (var key in map.keys) { |
208 if (!expected.containsKey(key)) { | 217 if (!expected.containsKey(key)) { |
209 return ["${err}has extra map key '$key'", location]; | 218 return ["${err}has extra map key '$key'", location]; |
210 } | 219 } |
211 } | 220 } |
212 | 221 |
213 for (var key in expected.keys) { | 222 for (var key in expected.keys) { |
214 var rp = _recursiveMatch( | 223 var rp = _recursiveMatch( |
215 expected[key], actual[key], "${location}['${key}']", depth + 1); | 224 expected[key], map[key], "$location['$key']", depth + 1); |
216 if (rp != null) return rp; | 225 if (rp != null) return rp; |
217 } | 226 } |
218 | 227 |
219 return null; | 228 return null; |
220 } | 229 } |
221 } | 230 } |
222 | 231 |
223 var description = new StringDescription(); | 232 var description = new StringDescription(); |
224 | 233 |
225 // If we have recursed, show the expected value too; if not, expect() will | 234 // If we have recursed, show the expected value too; if not, expect() will |
226 // show it for us. | 235 // show it for us. |
227 if (depth > 0) { | 236 if (depth > 0) { |
228 description | 237 description |
229 .add('was ') | 238 .add('was ') |
230 .addDescriptionOf(actual) | 239 .addDescriptionOf(actual) |
231 .add(' instead of ') | 240 .add(' instead of ') |
232 .addDescriptionOf(expected); | 241 .addDescriptionOf(expected); |
233 return [description.toString(), location]; | 242 return [description.toString(), location]; |
234 } | 243 } |
235 | 244 |
236 // We're not adding any value to the actual value. | 245 // We're not adding any value to the actual value. |
237 return ["", location]; | 246 return ["", location]; |
238 } | 247 } |
239 | 248 |
240 String _match(expected, actual, Map matchState) { | 249 String _match(expected, actual, Map matchState) { |
241 var rp = _recursiveMatch(expected, actual, '', 0); | 250 var rp = _recursiveMatch(expected, actual, '', 0); |
242 if (rp == null) return null; | 251 if (rp == null) return null; |
243 var reason; | 252 String reason; |
244 if (rp[0].length > 0) { | 253 if (rp[0].length > 0) { |
245 if (rp[1].length > 0) { | 254 if (rp[1].length > 0) { |
246 reason = "${rp[0]} at location ${rp[1]}"; | 255 reason = "${rp[0]} at location ${rp[1]}"; |
247 } else { | 256 } else { |
248 reason = rp[0]; | 257 reason = rp[0]; |
249 } | 258 } |
250 } else { | 259 } else { |
251 reason = ''; | 260 reason = ''; |
252 } | 261 } |
253 // Cache the failure reason in the matchState. | 262 // Cache the failure reason in the matchState. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 _writeLeading(buff, escapedValue, start); | 332 _writeLeading(buff, escapedValue, start); |
324 _writeTrailing(buff, escapedValue, start); | 333 _writeTrailing(buff, escapedValue, start); |
325 buff.write('\n Actual: '); | 334 buff.write('\n Actual: '); |
326 _writeLeading(buff, escapedItem, start); | 335 _writeLeading(buff, escapedItem, start); |
327 _writeTrailing(buff, escapedItem, start); | 336 _writeTrailing(buff, escapedItem, start); |
328 buff.write('\n '); | 337 buff.write('\n '); |
329 for (int i = (start > 10 ? 14 : start); i > 0; i--) buff.write(' '); | 338 for (int i = (start > 10 ? 14 : start); i > 0; i--) buff.write(' '); |
330 buff.write('^\n Differ at offset $start'); | 339 buff.write('^\n Differ at offset $start'); |
331 } | 340 } |
332 | 341 |
333 return mismatchDescription.replace(buff.toString()); | 342 return mismatchDescription.add(buff.toString()); |
334 } | 343 } |
335 } | 344 } |
336 | 345 |
337 static void _writeLeading(StringBuffer buff, String s, int start) { | 346 static void _writeLeading(StringBuffer buff, String s, int start) { |
338 if (start > 10) { | 347 if (start > 10) { |
339 buff.write('... '); | 348 buff.write('... '); |
340 buff.write(s.substring(start - 10, start)); | 349 buff.write(s.substring(start - 10, start)); |
341 } else { | 350 } else { |
342 buff.write(s.substring(0, start)); | 351 buff.write(s.substring(0, start)); |
343 } | 352 } |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 Description describe(Description description) => | 570 Description describe(Description description) => |
562 description.add('is in ').addDescriptionOf(_expected); | 571 description.add('is in ').addDescriptionOf(_expected); |
563 } | 572 } |
564 | 573 |
565 /// Returns a matcher that uses an arbitrary function that returns | 574 /// Returns a matcher that uses an arbitrary function that returns |
566 /// true or false for the actual value. | 575 /// true or false for the actual value. |
567 /// | 576 /// |
568 /// For example: | 577 /// For example: |
569 /// | 578 /// |
570 /// expect(v, predicate((x) => ((x % 2) == 0), "is even")) | 579 /// expect(v, predicate((x) => ((x % 2) == 0), "is even")) |
571 Matcher predicate(bool f(value), [String description = 'satisfies function']) => | 580 Matcher predicate<T>(bool f(T value), |
| 581 [String description = 'satisfies function']) => |
572 new _Predicate(f, description); | 582 new _Predicate(f, description); |
573 | 583 |
574 typedef bool _PredicateFunction(value); | 584 typedef bool _PredicateFunction<T>(T value); |
575 | 585 |
576 class _Predicate extends Matcher { | 586 class _Predicate<T> extends Matcher { |
577 final _PredicateFunction _matcher; | 587 final _PredicateFunction<T> _matcher; |
578 final String _description; | 588 final String _description; |
579 | 589 |
580 const _Predicate(this._matcher, this._description); | 590 _Predicate(this._matcher, this._description); |
581 | 591 |
582 bool matches(item, Map matchState) => _matcher(item); | 592 bool matches(item, Map matchState) => _matcher(item as T); |
583 | 593 |
584 Description describe(Description description) => | 594 Description describe(Description description) => |
585 description.add(_description); | 595 description.add(_description); |
586 } | 596 } |
587 | 597 |
588 /// A useful utility class for implementing other matchers through inheritance. | 598 /// A useful utility class for implementing other matchers through inheritance. |
589 /// Derived classes should call the base constructor with a feature name and | 599 /// Derived classes should call the base constructor with a feature name and |
590 /// description, and an instance matcher, and should implement the | 600 /// description, and an instance matcher, and should implement the |
591 /// [featureValueOf] abstract method. | 601 /// [featureValueOf] abstract method. |
592 /// | 602 /// |
593 /// The feature description will typically describe the item and the feature, | 603 /// The feature description will typically describe the item and the feature, |
594 /// while the feature name will just name the feature. For example, we may | 604 /// while the feature name will just name the feature. For example, we may |
595 /// have a Widget class where each Widget has a price; we could make a | 605 /// have a Widget class where each Widget has a price; we could make a |
596 /// [CustomMatcher] that can make assertions about prices with: | 606 /// [CustomMatcher] that can make assertions about prices with: |
597 /// | 607 /// |
598 /// class HasPrice extends CustomMatcher { | 608 /// ```dart |
599 /// const HasPrice(matcher) : | 609 /// class HasPrice extends CustomMatcher { |
600 /// super("Widget with price that is", "price", matcher); | 610 /// HasPrice(matcher) : super("Widget with price that is", "price", matcher); |
601 /// featureValueOf(actual) => actual.price; | 611 /// featureValueOf(actual) => actual.price; |
602 /// } | 612 /// } |
| 613 /// ``` |
603 /// | 614 /// |
604 /// and then use this for example like: | 615 /// and then use this for example like: |
605 /// | 616 /// |
606 /// expect(inventoryItem, new HasPrice(greaterThan(0))); | 617 /// ```dart |
| 618 /// expect(inventoryItem, new HasPrice(greaterThan(0))); |
| 619 /// ``` |
607 class CustomMatcher extends Matcher { | 620 class CustomMatcher extends Matcher { |
608 final String _featureDescription; | 621 final String _featureDescription; |
609 final String _featureName; | 622 final String _featureName; |
610 final Matcher _matcher; | 623 final Matcher _matcher; |
611 | 624 |
612 CustomMatcher(this._featureDescription, this._featureName, matcher) | 625 CustomMatcher(this._featureDescription, this._featureName, matcher) |
613 : this._matcher = wrapMatcher(matcher); | 626 : this._matcher = wrapMatcher(matcher); |
614 | 627 |
615 /// Override this to extract the interesting feature. | 628 /// Override this to extract the interesting feature. |
616 featureValueOf(actual) => actual; | 629 featureValueOf(actual) => actual; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 | 672 |
660 _matcher.describeMismatch(matchState['custom.feature'], innerDescription, | 673 _matcher.describeMismatch(matchState['custom.feature'], innerDescription, |
661 matchState['state'], verbose); | 674 matchState['state'], verbose); |
662 | 675 |
663 if (innerDescription.length > 0) { | 676 if (innerDescription.length > 0) { |
664 mismatchDescription.add(' which ').add(innerDescription.toString()); | 677 mismatchDescription.add(' which ').add(innerDescription.toString()); |
665 } | 678 } |
666 return mismatchDescription; | 679 return mismatchDescription; |
667 } | 680 } |
668 } | 681 } |
OLD | NEW |