| 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 which matches [Collection]s in which all elements | 8 * Returns a matcher which matches [Iterable]s in which all elements |
| 9 * match the given [matcher]. | 9 * match the given [matcher]. |
| 10 */ | 10 */ |
| 11 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); | 11 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); |
| 12 | 12 |
| 13 class _EveryElement extends _CollectionMatcher { | 13 class _EveryElement extends _IterableMatcher { |
| 14 Matcher _matcher; | 14 Matcher _matcher; |
| 15 | 15 |
| 16 _EveryElement(Matcher this._matcher); | 16 _EveryElement(Matcher this._matcher); |
| 17 | 17 |
| 18 bool matches(item, MatchState matchState) { | 18 bool matches(item, MatchState matchState) { |
| 19 if (item is! Iterable) { | 19 if (item is! Iterable) { |
| 20 return false; | 20 return false; |
| 21 } | 21 } |
| 22 var i = 0; | 22 var i = 0; |
| 23 for (var element in item) { | 23 for (var element in item) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 44 var element = matchState.state['element']; | 44 var element = matchState.state['element']; |
| 45 return _matcher.describeMismatch(element, mismatchDescription, | 45 return _matcher.describeMismatch(element, mismatchDescription, |
| 46 matchState.state['state'], verbose).add(' at position $index'); | 46 matchState.state['state'], verbose).add(' at position $index'); |
| 47 } | 47 } |
| 48 return super.describeMismatch(item, mismatchDescription, | 48 return super.describeMismatch(item, mismatchDescription, |
| 49 matchState, verbose); | 49 matchState, verbose); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Returns a matcher which matches [Collection]s in which at least one | 54 * Returns a matcher which matches [Iterable]s in which at least one |
| 55 * element matches the given [matcher]. | 55 * element matches the given [matcher]. |
| 56 */ | 56 */ |
| 57 Matcher someElement(matcher) => new _SomeElement(wrapMatcher(matcher)); | 57 Matcher someElement(matcher) => new _SomeElement(wrapMatcher(matcher)); |
| 58 | 58 |
| 59 class _SomeElement extends _CollectionMatcher { | 59 class _SomeElement extends _IterableMatcher { |
| 60 Matcher _matcher; | 60 Matcher _matcher; |
| 61 | 61 |
| 62 _SomeElement(this._matcher); | 62 _SomeElement(this._matcher); |
| 63 | 63 |
| 64 bool matches(item, MatchState matchState) { | 64 bool matches(item, MatchState matchState) { |
| 65 return item.any((e) => _matcher.matches(e, matchState)); | 65 return item.any((e) => _matcher.matches(e, matchState)); |
| 66 } | 66 } |
| 67 | 67 |
| 68 Description describe(Description description) => | 68 Description describe(Description description) => |
| 69 description.add('some element ').addDescriptionOf(_matcher); | 69 description.add('some element ').addDescriptionOf(_matcher); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 class _UnorderedEquals extends BaseMatcher { | 113 class _UnorderedEquals extends BaseMatcher { |
| 114 Iterable _expected; | 114 Iterable _expected; |
| 115 | 115 |
| 116 _UnorderedEquals(Iterable this._expected); | 116 _UnorderedEquals(Iterable this._expected); |
| 117 | 117 |
| 118 String _test(item) { | 118 String _test(item) { |
| 119 if (item is !Iterable) { | 119 if (item is !Iterable) { |
| 120 return 'not iterable'; | 120 return 'not iterable'; |
| 121 } | 121 } |
| 122 // Check the lengths are the same. | 122 // Check the lengths are the same. |
| 123 var expectedLength = 0; | 123 var expectedLength = _expected.length; |
| 124 if (_expected is Collection) { | 124 var actualLength = item.length; |
| 125 Collection cast = _expected; // "_expected as Collection" | |
| 126 expectedLength = cast.length; | |
| 127 } else { | |
| 128 for (var element in _expected) { | |
| 129 ++expectedLength; | |
| 130 } | |
| 131 } | |
| 132 var actualLength = 0; | |
| 133 if (item is Collection) { | |
| 134 actualLength = item.length; | |
| 135 } else { | |
| 136 for (var element in item) { | |
| 137 ++actualLength; | |
| 138 } | |
| 139 } | |
| 140 if (expectedLength > actualLength) { | 125 if (expectedLength > actualLength) { |
| 141 return 'has too few elements (${actualLength} < ${expectedLength})'; | 126 return 'has too few elements (${actualLength} < ${expectedLength})'; |
| 142 } else if (expectedLength < actualLength) { | 127 } else if (expectedLength < actualLength) { |
| 143 return 'has too many elements (${actualLength} > ${expectedLength})'; | 128 return 'has too many elements (${actualLength} > ${expectedLength})'; |
| 144 } | 129 } |
| 145 List<bool> matched = new List<bool>(actualLength); | 130 List<bool> matched = new List<bool>(actualLength); |
| 146 for (var i = 0; i < actualLength; i++) { | 131 for (var i = 0; i < actualLength; i++) { |
| 147 matched[i] = false; | 132 matched[i] = false; |
| 148 } | 133 } |
| 149 var expectedPosition = 0; | 134 var expectedPosition = 0; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 175 | 160 |
| 176 Description describe(Description description) => | 161 Description describe(Description description) => |
| 177 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); | 162 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); |
| 178 | 163 |
| 179 Description describeMismatch(item, Description mismatchDescription, | 164 Description describeMismatch(item, Description mismatchDescription, |
| 180 MatchState matchState, bool verbose) => | 165 MatchState matchState, bool verbose) => |
| 181 mismatchDescription.add(_test(item)); | 166 mismatchDescription.add(_test(item)); |
| 182 } | 167 } |
| 183 | 168 |
| 184 /** | 169 /** |
| 185 * Collection matchers match against [Collection]s. We add this intermediate | 170 * Iterable matchers match against [Iterable]s. We add this intermediate |
| 186 * class to give better mismatch error messages than the base Matcher class. | 171 * class to give better mismatch error messages than the base Matcher class. |
| 187 */ | 172 */ |
| 188 abstract class _CollectionMatcher extends BaseMatcher { | 173 abstract class _IterableMatcher extends BaseMatcher { |
| 189 const _CollectionMatcher(); | 174 const _IterableMatcher(); |
| 190 Description describeMismatch(item, Description mismatchDescription, | 175 Description describeMismatch(item, Description mismatchDescription, |
| 191 MatchState matchState, bool verbose) { | 176 MatchState matchState, bool verbose) { |
| 192 if (item is! Iterable) { | 177 if (item is! Iterable) { |
| 193 return mismatchDescription. | 178 return mismatchDescription. |
| 194 addDescriptionOf(item). | 179 addDescriptionOf(item). |
| 195 add(' not an Iterable'); | 180 add(' not an Iterable'); |
| 196 } else { | 181 } else { |
| 197 return super.describeMismatch(item, mismatchDescription, matchState, | 182 return super.describeMismatch(item, mismatchDescription, matchState, |
| 198 verbose); | 183 verbose); |
| 199 } | 184 } |
| 200 } | 185 } |
| 201 } | 186 } |
| OLD | NEW |