| 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 [Iterable]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 */ |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 if (item is! Iterable) { | 177 if (item is! Iterable) { |
| 178 return mismatchDescription. | 178 return mismatchDescription. |
| 179 addDescriptionOf(item). | 179 addDescriptionOf(item). |
| 180 add(' not an Iterable'); | 180 add(' not an Iterable'); |
| 181 } else { | 181 } else { |
| 182 return super.describeMismatch(item, mismatchDescription, matchState, | 182 return super.describeMismatch(item, mismatchDescription, matchState, |
| 183 verbose); | 183 verbose); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 |
| 188 /** |
| 189 * A pairwise matcher for iterable. You can pass an arbitrary [comparator] |
| 190 * function that takes an expected and actual argument which will be applied |
| 191 * to each pair in order. [description] should be a meaningful name for |
| 192 * the comparator. |
| 193 */ |
| 194 Matcher pairwiseCompare(Iterable expected, Function comparator, |
| 195 String description) => |
| 196 new _PairwiseCompare(expected, comparator, description); |
| 197 |
| 198 class _PairwiseCompare extends _IterableMatcher { |
| 199 Iterable _expected; |
| 200 Function _comparator; |
| 201 String _description; |
| 202 |
| 203 _PairwiseCompare(this._expected, this._comparator, this._description); |
| 204 |
| 205 bool matches(item, MatchState matchState) { |
| 206 if (item is! Iterable) return false; |
| 207 if (item.length != _expected.length) return false; |
| 208 var iterator = item.iterator; |
| 209 var i = 0; |
| 210 for (var e in _expected) { |
| 211 iterator.moveNext(); |
| 212 if (!_comparator(e, iterator.current)) { |
| 213 matchState.state = { |
| 214 'index': i, |
| 215 'expected': e, |
| 216 'actual' : iterator.current, |
| 217 'state': matchState.state |
| 218 }; |
| 219 return false; |
| 220 } |
| 221 i++; |
| 222 } |
| 223 return true; |
| 224 } |
| 225 |
| 226 Description describe(Description description) => |
| 227 description.add('pairwise $_description ').addDescriptionOf(_expected); |
| 228 |
| 229 Description describeMismatch(item, Description mismatchDescription, |
| 230 MatchState matchState, bool verbose) { |
| 231 if (item is !Iterable) { |
| 232 return mismatchDescription.add('not an Iterable'); |
| 233 } else if (item.length != _expected.length) { |
| 234 return mismatchDescription. |
| 235 add('length was ${item.length} instead of ${_expected.length}'); |
| 236 } else { |
| 237 return mismatchDescription. |
| 238 addDescriptionOf(matchState.state["actual"]). |
| 239 add(' not $_description '). |
| 240 addDescriptionOf(matchState.state["expected"]). |
| 241 add(' at position ${matchState.state["index"]}'); |
| 242 } |
| 243 } |
| 244 } |
| 245 |
| OLD | NEW |