Chromium Code Reviews| 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 /** | 5 /** |
| 6 * Returns a matcher which matches [Collection]s in which all elements | 6 * Returns a matcher which matches [Collection]s in which all elements |
| 7 * match the given [matcher]. | 7 * match the given [matcher]. |
| 8 */ | 8 */ |
| 9 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); | 9 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); |
| 10 | 10 |
| 11 class _EveryElement extends _CollectionMatcher { | 11 class _EveryElement extends _CollectionMatcher { |
| 12 Matcher _matcher; | 12 Matcher _matcher; |
| 13 | 13 |
| 14 _EveryElement(Matcher this._matcher); | 14 _EveryElement(Matcher this._matcher); |
| 15 | 15 |
| 16 bool matches(item) { | 16 bool matches(item, MatchState matchState) { |
| 17 return item.every((e) => _matcher.matches(e)); | 17 if (item is! Iterable) { |
| 18 return false; | |
| 19 } | |
| 20 var i = 0; | |
| 21 for (var element in item) { | |
| 22 if (!_matcher.matches(element, matchState)) { | |
| 23 matchState.state = { | |
| 24 'index': i, | |
| 25 'element': element, | |
| 26 'state': matchState.state | |
|
Siggi Cherem (dart-lang)
2012/07/31 17:16:40
so state is a list of states?
gram
2012/07/31 17:56:01
No; we fail fast, so it has the mismatch state jus
| |
| 27 }; | |
| 28 return false; | |
| 29 } | |
| 30 ++i; | |
| 31 } | |
| 32 return true; | |
| 18 } | 33 } |
| 19 | 34 |
| 20 Description describe(Description description) => | 35 Description describe(Description description) => |
| 21 description.add('every element ').addDescriptionOf(_matcher); | 36 description.add('every element ').addDescriptionOf(_matcher); |
| 37 | |
| 38 Description describeMismatch(item, Description mismatchDescription, | |
| 39 MatchState matchState, bool verbose) { | |
| 40 if (matchState.state != null) { | |
| 41 var index = matchState.state['index']; | |
| 42 var element = matchState.state['element']; | |
| 43 return _matcher.describeMismatch(element, mismatchDescription, | |
| 44 matchState.state['state'], verbose).add(' at position $index'); | |
| 45 } | |
| 46 return super.describeMismatch(item, mismatchDescription, | |
| 47 matchState, verbose); | |
| 48 } | |
| 22 } | 49 } |
| 23 | 50 |
| 24 /** | 51 /** |
| 25 * Returns a matcher which matches [Collection]s in which at least one | 52 * Returns a matcher which matches [Collection]s in which at least one |
| 26 * element matches the given [matcher]. | 53 * element matches the given [matcher]. |
| 27 */ | 54 */ |
| 28 Matcher someElement(matcher) => new _SomeElement(wrapMatcher(matcher)); | 55 Matcher someElement(matcher) => new _SomeElement(wrapMatcher(matcher)); |
| 29 | 56 |
| 30 class _SomeElement extends _CollectionMatcher { | 57 class _SomeElement extends _CollectionMatcher { |
| 31 Matcher _matcher; | 58 Matcher _matcher; |
| 32 | 59 |
| 33 _SomeElement(this._matcher); | 60 _SomeElement(this._matcher); |
| 34 | 61 |
| 35 bool matches(item) { | 62 bool matches(item, MatchState matchState) { |
| 36 return item.some( (e) => _matcher.matches(e) ); | 63 return item.some( (e) => _matcher.matches(e, matchState) ); |
| 37 } | 64 } |
| 38 | 65 |
| 39 Description describe(Description description) => | 66 Description describe(Description description) => |
| 40 description.add('some element ').addDescriptionOf(_matcher); | 67 description.add('some element ').addDescriptionOf(_matcher); |
| 41 } | 68 } |
| 42 | 69 |
| 43 /** | 70 /** |
| 44 * Returns a matcher which matches [Iterable]s that have the same | 71 * Returns a matcher which matches [Iterable]s that have the same |
| 45 * length and the same elements as [expected], and in the same order. | 72 * length and the same elements as [expected], and in the same order. |
| 46 * This is equivalent to equals but does not recurse. | 73 * This is equivalent to equals but does not recurse. |
| 47 */ | 74 */ |
| 48 | 75 |
| 49 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); | 76 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); |
| 50 | 77 |
| 51 class _OrderedEquals extends BaseMatcher { | 78 class _OrderedEquals extends BaseMatcher { |
| 52 final Iterable _expected; | 79 final Iterable _expected; |
| 53 Matcher _matcher; | 80 Matcher _matcher; |
| 54 | 81 |
| 55 _OrderedEquals(this._expected) { | 82 _OrderedEquals(this._expected) { |
| 56 _matcher = equals(_expected, 1); | 83 _matcher = equals(_expected, 1); |
| 57 } | 84 } |
| 58 | 85 |
| 59 bool matches(item) => (item is Iterable) && _matcher.matches(item); | 86 bool matches(item, MatchState matchState) => |
| 87 (item is Iterable) && _matcher.matches(item, matchState); | |
| 60 | 88 |
| 61 Description describe(Description description) => | 89 Description describe(Description description) => |
| 62 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); | 90 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); |
| 63 | 91 |
| 64 Description describeMismatch(item, Description mismatchDescription) { | 92 Description describeMismatch(item, Description mismatchDescription, |
| 93 MatchState matchState, bool verbose) { | |
| 65 if (item is !Iterable) { | 94 if (item is !Iterable) { |
| 66 return mismatchDescription.add('not an Iterable'); | 95 return mismatchDescription.add('not an Iterable'); |
| 67 } else { | 96 } else { |
| 68 return _matcher.describeMismatch(item, mismatchDescription); | 97 return _matcher.describeMismatch(item, mismatchDescription, |
| 98 matchState, verbose); | |
| 69 } | 99 } |
| 70 } | 100 } |
| 71 } | 101 } |
| 72 /** | 102 /** |
| 73 * Returns a matcher which matches [Iterable]s that have the same | 103 * Returns a matcher which matches [Iterable]s that have the same |
| 74 * length and the same elements as [expected], but not necessarily in | 104 * length and the same elements as [expected], but not necessarily in |
| 75 * the same order. Note that this is O(n^2) so should only be used on | 105 * the same order. Note that this is O(n^2) so should only be used on |
| 76 * small objects. | 106 * small objects. |
| 77 */ | 107 */ |
| 78 Matcher unorderedEquals(Iterable expected) => | 108 Matcher unorderedEquals(Iterable expected) => |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 reason.add('has no match for element '). | 162 reason.add('has no match for element '). |
| 133 addDescriptionOf(expectedElement). | 163 addDescriptionOf(expectedElement). |
| 134 add(' at position ${expectedPosition}'); | 164 add(' at position ${expectedPosition}'); |
| 135 return reason.toString(); | 165 return reason.toString(); |
| 136 } | 166 } |
| 137 ++expectedPosition; | 167 ++expectedPosition; |
| 138 } | 168 } |
| 139 return null; | 169 return null; |
| 140 } | 170 } |
| 141 | 171 |
| 142 bool matches(item) => (_test(item) == null); | 172 bool matches(item, MatchState mismatchState) => (_test(item) == null); |
| 143 | 173 |
| 144 Description describe(Description description) => | 174 Description describe(Description description) => |
| 145 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); | 175 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); |
| 146 | 176 |
| 147 Description describeMismatch(item, Description mismatchDescription) => | 177 Description describeMismatch(item, Description mismatchDescription, |
| 178 MatchState matchState, bool verbose) => | |
| 148 mismatchDescription.add(_test(item)); | 179 mismatchDescription.add(_test(item)); |
| 149 } | 180 } |
| 150 | 181 |
| 151 /** | 182 /** |
| 152 * Collection matchers match against [Collection]s. We add this intermediate | 183 * Collection matchers match against [Collection]s. We add this intermediate |
| 153 * class to give better mismatch error messages than the base Matcher class. | 184 * class to give better mismatch error messages than the base Matcher class. |
| 154 */ | 185 */ |
| 155 /* abstract */ class _CollectionMatcher extends BaseMatcher { | 186 /* abstract */ class _CollectionMatcher extends BaseMatcher { |
| 156 const _CollectionMatcher(); | 187 const _CollectionMatcher(); |
| 157 Description describeMismatch(item, Description mismatchDescription) { | 188 Description describeMismatch(item, Description mismatchDescription, |
| 189 MatchState matchState, bool verbose) { | |
| 158 if (item is !Collection) { | 190 if (item is !Collection) { |
| 159 return mismatchDescription. | 191 return mismatchDescription. |
| 160 addDescriptionOf(item). | 192 addDescriptionOf(item). |
| 161 add(' not a collection'); | 193 add(' not a collection'); |
| 162 } else { | 194 } else { |
| 163 return super.describeMismatch(item, mismatchDescription); | 195 return super.describeMismatch(item, mismatchDescription, matchState, |
| 196 verbose); | |
| 164 } | 197 } |
| 165 } | 198 } |
| 166 } | 199 } |
| OLD | NEW |