| 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 part of matcher; | 5 part of matcher; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Returns a matcher that matches empty strings, maps or iterables | 8 * Returns a matcher that matches empty strings, maps or iterables |
| 9 * (including collections). | 9 * (including collections). |
| 10 */ | 10 */ |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 description.add('same instance as ').addDescriptionOf(_expected); | 80 description.add('same instance as ').addDescriptionOf(_expected); |
| 81 } | 81 } |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * Returns a matcher that does a deep recursive match. This only works | 84 * Returns a matcher that does a deep recursive match. This only works |
| 85 * with scalars, Maps and Iterables. To handle cyclic structures a | 85 * with scalars, Maps and Iterables. To handle cyclic structures a |
| 86 * recursion depth [limit] can be provided. The default limit is 100. | 86 * recursion depth [limit] can be provided. The default limit is 100. |
| 87 */ | 87 */ |
| 88 Matcher equals(expected, [limit=100]) => | 88 Matcher equals(expected, [limit=100]) => |
| 89 expected is String | 89 expected is String |
| 90 ? new _StringEqualsMatcher(expected) | 90 ? new _StringEqualsMatcher(expected) |
| 91 : new _DeepMatcher(expected, limit); | 91 : new _DeepMatcher(expected, limit); |
| 92 | 92 |
| 93 class _DeepMatcher extends BaseMatcher { | 93 class _DeepMatcher extends BaseMatcher { |
| 94 final _expected; | 94 final _expected; |
| 95 final int _limit; | 95 final int _limit; |
| 96 var count; | 96 var count; |
| 97 | 97 |
| 98 _DeepMatcher(this._expected, [limit = 1000]) : this._limit = limit; | 98 _DeepMatcher(this._expected, [limit = 1000]) : this._limit = limit; |
| 99 | 99 |
| 100 // Returns a pair (reason, location) | 100 // Returns a pair (reason, location) |
| 101 List _compareIterables(expected, actual, matcher, depth, location) { | 101 List _compareIterables(expected, actual, matcher, depth, location) { |
| 102 if (actual is !Iterable) { | 102 if (actual is !Iterable) { |
| 103 return ['is not Iterable', location]; | 103 return ['is not Iterable', location]; |
| 104 } | 104 } |
| 105 var expectedIterator = expected.iterator; | 105 var expectedIterator = expected.iterator; |
| 106 var actualIterator = actual.iterator; | 106 var actualIterator = actual.iterator; |
| 107 var index = 0; | 107 var index = 0; |
| 108 while (true) { | 108 while (true) { |
| 109 var newLocation = '${location}[${index}]'; |
| 109 if (expectedIterator.moveNext()) { | 110 if (expectedIterator.moveNext()) { |
| 110 var newLocation = '${location}[${index}]'; | |
| 111 if (actualIterator.moveNext()) { | 111 if (actualIterator.moveNext()) { |
| 112 var rp = matcher(expectedIterator.current, | 112 var rp = matcher(expectedIterator.current, |
| 113 actualIterator.current, newLocation, | 113 actualIterator.current, newLocation, |
| 114 depth); | 114 depth); |
| 115 if (rp != null) return rp; | 115 if (rp != null) return rp; |
| 116 ++index; | 116 ++index; |
| 117 } else { | 117 } else { |
| 118 return ['shorter than expected', newLocation]; | 118 return ['shorter than expected', newLocation]; |
| 119 } | 119 } |
| 120 } else if (actualIterator.moveNext()) { | 120 } else if (actualIterator.moveNext()) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 213 |
| 214 bool matches(item, Map matchState) => | 214 bool matches(item, Map matchState) => |
| 215 _match(_expected, item, matchState) == null; | 215 _match(_expected, item, matchState) == null; |
| 216 | 216 |
| 217 Description describe(Description description) => | 217 Description describe(Description description) => |
| 218 description.addDescriptionOf(_expected); | 218 description.addDescriptionOf(_expected); |
| 219 | 219 |
| 220 Description describeMismatch(item, Description mismatchDescription, | 220 Description describeMismatch(item, Description mismatchDescription, |
| 221 Map matchState, bool verbose) { | 221 Map matchState, bool verbose) { |
| 222 var reason = matchState['reason']; | 222 var reason = matchState['reason']; |
| 223 // If we didn't get a good reason, that would normally be a | 223 // If we didn't get a good reason, that would normally be a |
| 224 // simple 'is <value>' message. We only add that if the mismatch | 224 // simple 'is <value>' message. We only add that if the mismatch |
| 225 // description is non empty (so we are supplementing the mismatch | 225 // description is non empty (so we are supplementing the mismatch |
| 226 // description). | 226 // description). |
| 227 if (reason.length == 0 && mismatchDescription.length > 0) { | 227 if (reason.length == 0 && mismatchDescription.length > 0) { |
| 228 mismatchDescription.add('is ').addDescriptionOf(item); | 228 mismatchDescription.add('is ').addDescriptionOf(item); |
| 229 } else { | 229 } else { |
| 230 mismatchDescription.add(reason); | 230 mismatchDescription.add(reason); |
| 231 } | 231 } |
| 232 return mismatchDescription; | 232 return mismatchDescription; |
| 233 } | 233 } |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 var innerDescription = new StringDescription(); | 818 var innerDescription = new StringDescription(); |
| 819 _matcher.describeMismatch(matchState['feature'], innerDescription, | 819 _matcher.describeMismatch(matchState['feature'], innerDescription, |
| 820 matchState['state'], verbose); | 820 matchState['state'], verbose); |
| 821 if (innerDescription.length > 0) { | 821 if (innerDescription.length > 0) { |
| 822 mismatchDescription.add(' which ').add(innerDescription.toString()); | 822 mismatchDescription.add(' which ').add(innerDescription.toString()); |
| 823 } | 823 } |
| 824 return mismatchDescription; | 824 return mismatchDescription; |
| 825 } | 825 } |
| 826 } | 826 } |
| 827 | 827 |
| OLD | NEW |