| 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 library matcher.iterable_matchers; | 5 part of matcher; |
| 6 | 6 |
| 7 import 'core_matchers.dart'; | 7 /** |
| 8 import 'description.dart'; | 8 * Returns a matcher which matches [Iterable]s in which all elements |
| 9 import 'expect.dart'; | 9 * match the given [matcher]. |
| 10 import 'interfaces.dart'; | 10 */ |
| 11 | |
| 12 /// Returns a matcher which matches [Iterable]s in which all elements | |
| 13 /// match the given [matcher]. | |
| 14 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); | 11 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); |
| 15 | 12 |
| 16 class _EveryElement extends _IterableMatcher { | 13 class _EveryElement extends _IterableMatcher { |
| 17 final Matcher _matcher; | 14 Matcher _matcher; |
| 18 | 15 |
| 19 _EveryElement(Matcher this._matcher); | 16 _EveryElement(Matcher this._matcher); |
| 20 | 17 |
| 21 bool matches(item, Map matchState) { | 18 bool matches(item, Map matchState) { |
| 22 if (item is! Iterable) { | 19 if (item is! Iterable) { |
| 23 return false; | 20 return false; |
| 24 } | 21 } |
| 25 var i = 0; | 22 var i = 0; |
| 26 for (var element in item) { | 23 for (var element in item) { |
| 27 if (!_matcher.matches(element, matchState)) { | 24 if (!_matcher.matches(element, matchState)) { |
| 28 addStateInfo(matchState, {'index': i, 'element': element}); | 25 addStateInfo(matchState, {'index': i, 'element': element}); |
| 29 return false; | 26 return false; |
| 30 } | 27 } |
| 31 ++i; | 28 ++i; |
| 32 } | 29 } |
| 33 return true; | 30 return true; |
| 34 } | 31 } |
| 35 | 32 |
| 36 Description describe(Description description) => | 33 Description describe(Description description) => |
| 37 description.add('every element(').addDescriptionOf(_matcher).add(')'); | 34 description.add('every element(').addDescriptionOf(_matcher).add(')'); |
| 38 | 35 |
| 39 Description describeMismatch(item, Description mismatchDescription, | 36 Description describeMismatch(item, Description mismatchDescription, |
| 40 Map matchState, bool verbose) { | 37 Map matchState, bool verbose) { |
| 41 if (matchState['index'] != null) { | 38 if (matchState['index'] != null) { |
| 42 var index = matchState['index']; | 39 var index = matchState['index']; |
| 43 var element = matchState['element']; | 40 var element = matchState['element']; |
| 44 mismatchDescription.add('has value ').addDescriptionOf(element). | 41 mismatchDescription.add('has value ').addDescriptionOf(element). |
| 45 add(' which '); | 42 add(' which '); |
| 46 var subDescription = new StringDescription(); | 43 var subDescription = new StringDescription(); |
| 47 _matcher.describeMismatch(element, subDescription, matchState['state'], | 44 _matcher.describeMismatch(element, subDescription, |
| 48 verbose); | 45 matchState['state'], verbose); |
| 49 if (subDescription.length > 0) { | 46 if (subDescription.length > 0) { |
| 50 mismatchDescription.add(subDescription.toString()); | 47 mismatchDescription.add(subDescription); |
| 51 } else { | 48 } else { |
| 52 mismatchDescription.add("doesn't match "); | 49 mismatchDescription.add("doesn't match "); |
| 53 _matcher.describe(mismatchDescription); | 50 _matcher.describe(mismatchDescription); |
| 54 } | 51 } |
| 55 mismatchDescription.add(' at index $index'); | 52 mismatchDescription.add(' at index $index'); |
| 56 return mismatchDescription; | 53 return mismatchDescription; |
| 57 } | 54 } |
| 58 return super.describeMismatch(item, mismatchDescription, | 55 return super.describeMismatch(item, mismatchDescription, |
| 59 matchState, verbose); | 56 matchState, verbose); |
| 60 } | 57 } |
| 61 } | 58 } |
| 62 | 59 |
| 63 /// Returns a matcher which matches [Iterable]s in which at least one | 60 /** |
| 64 /// element matches the given [matcher]. | 61 * Returns a matcher which matches [Iterable]s in which at least one |
| 62 * element matches the given [matcher]. |
| 63 */ |
| 65 Matcher anyElement(matcher) => new _AnyElement(wrapMatcher(matcher)); | 64 Matcher anyElement(matcher) => new _AnyElement(wrapMatcher(matcher)); |
| 66 | 65 |
| 67 class _AnyElement extends _IterableMatcher { | 66 class _AnyElement extends _IterableMatcher { |
| 68 final Matcher _matcher; | 67 Matcher _matcher; |
| 69 | 68 |
| 70 _AnyElement(this._matcher); | 69 _AnyElement(this._matcher); |
| 71 | 70 |
| 72 bool matches(item, Map matchState) { | 71 bool matches(item, Map matchState) { |
| 73 return item.any((e) => _matcher.matches(e, matchState)); | 72 return item.any((e) => _matcher.matches(e, matchState)); |
| 74 } | 73 } |
| 75 | 74 |
| 76 Description describe(Description description) => | 75 Description describe(Description description) => |
| 77 description.add('some element ').addDescriptionOf(_matcher); | 76 description.add('some element ').addDescriptionOf(_matcher); |
| 78 } | 77 } |
| 79 | 78 |
| 80 /// Returns a matcher which matches [Iterable]s that have the same | 79 /** |
| 81 /// length and the same elements as [expected], and in the same order. | 80 * Returns a matcher which matches [Iterable]s that have the same |
| 82 /// This is equivalent to equals but does not recurse. | 81 * length and the same elements as [expected], and in the same order. |
| 82 * This is equivalent to equals but does not recurse. |
| 83 */ |
| 83 | 84 |
| 84 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); | 85 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); |
| 85 | 86 |
| 86 class _OrderedEquals extends Matcher { | 87 class _OrderedEquals extends Matcher { |
| 87 final Iterable _expected; | 88 final Iterable _expected; |
| 88 Matcher _matcher; | 89 Matcher _matcher; |
| 89 | 90 |
| 90 _OrderedEquals(this._expected) { | 91 _OrderedEquals(this._expected) { |
| 91 _matcher = equals(_expected, 1); | 92 _matcher = equals(_expected, 1); |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool matches(item, Map matchState) => | 95 bool matches(item, Map matchState) => |
| 95 (item is Iterable) && _matcher.matches(item, matchState); | 96 (item is Iterable) && _matcher.matches(item, matchState); |
| 96 | 97 |
| 97 Description describe(Description description) => | 98 Description describe(Description description) => |
| 98 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); | 99 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); |
| 99 | 100 |
| 100 Description describeMismatch(item, Description mismatchDescription, | 101 Description describeMismatch(item, Description mismatchDescription, |
| 101 Map matchState, bool verbose) { | 102 Map matchState, bool verbose) { |
| 102 if (item is !Iterable) { | 103 if (item is !Iterable) { |
| 103 return mismatchDescription.add('is not an Iterable'); | 104 return mismatchDescription.add('is not an Iterable'); |
| 104 } else { | 105 } else { |
| 105 return _matcher.describeMismatch(item, mismatchDescription, | 106 return _matcher.describeMismatch(item, mismatchDescription, |
| 106 matchState, verbose); | 107 matchState, verbose); |
| 107 } | 108 } |
| 108 } | 109 } |
| 109 } | 110 } |
| 110 | 111 |
| 111 /// Returns a matcher which matches [Iterable]s that have the same | 112 /** |
| 112 /// length and the same elements as [expected], but not necessarily in | 113 * Returns a matcher which matches [Iterable]s that have the same |
| 113 /// the same order. Note that this is O(n^2) so should only be used on | 114 * length and the same elements as [expected], but not necessarily in |
| 114 /// small objects. | 115 * the same order. Note that this is O(n^2) so should only be used on |
| 116 * small objects. |
| 117 */ |
| 115 Matcher unorderedEquals(Iterable expected) => new _UnorderedEquals(expected); | 118 Matcher unorderedEquals(Iterable expected) => new _UnorderedEquals(expected); |
| 116 | 119 |
| 117 class _UnorderedEquals extends _UnorderedMatches { | 120 class _UnorderedEquals extends _UnorderedMatches { |
| 118 final List _expectedValues; | 121 final List _expectedValues; |
| 119 | 122 |
| 120 _UnorderedEquals(Iterable expected) | 123 _UnorderedEquals(Iterable expected) |
| 121 : super(expected.map(equals)), | 124 : super(expected.map(equals)), |
| 122 _expectedValues = expected.toList(); | 125 _expectedValues = expected.toList(); |
| 123 | 126 |
| 124 Description describe(Description description) => | 127 Description describe(Description description) => |
| 125 description | 128 description |
| 126 .add('equals ') | 129 .add('equals ') |
| 127 .addDescriptionOf(_expectedValues) | 130 .addDescriptionOf(_expectedValues) |
| 128 .add(' unordered'); | 131 .add(' unordered'); |
| 129 } | 132 } |
| 130 | 133 |
| 131 /// Iterable matchers match against [Iterable]s. We add this intermediate | 134 /** |
| 132 /// class to give better mismatch error messages than the base Matcher class. | 135 * Iterable matchers match against [Iterable]s. We add this intermediate |
| 136 * class to give better mismatch error messages than the base Matcher class. |
| 137 */ |
| 133 abstract class _IterableMatcher extends Matcher { | 138 abstract class _IterableMatcher extends Matcher { |
| 134 const _IterableMatcher(); | 139 const _IterableMatcher(); |
| 135 Description describeMismatch(item, Description mismatchDescription, | 140 Description describeMismatch(item, Description mismatchDescription, |
| 136 Map matchState, bool verbose) { | 141 Map matchState, bool verbose) { |
| 137 if (item is! Iterable) { | 142 if (item is! Iterable) { |
| 138 return mismatchDescription. | 143 return mismatchDescription. |
| 139 addDescriptionOf(item). | 144 addDescriptionOf(item). |
| 140 add(' not an Iterable'); | 145 add(' not an Iterable'); |
| 141 } else { | 146 } else { |
| 142 return super.describeMismatch(item, mismatchDescription, matchState, | 147 return super.describeMismatch(item, mismatchDescription, matchState, |
| 143 verbose); | 148 verbose); |
| 144 } | 149 } |
| 145 } | 150 } |
| 146 } | 151 } |
| 147 | 152 |
| 148 /// Returns a matcher which matches [Iterable]s whose elements match the | 153 /** |
| 149 /// matchers in [expected], but not necessarily in the same order. | 154 * Returns a matcher which matches [Iterable]s whose elements match the matchers |
| 150 /// | 155 * in [expected], but not necessarily in the same order. |
| 151 /// Note that this is `O(n^2)` and so should only be used on small objects. | 156 * |
| 157 * Note that this is `O(n^2)` and so should only be used on small objects. |
| 158 */ |
| 152 Matcher unorderedMatches(Iterable expected) => new _UnorderedMatches(expected); | 159 Matcher unorderedMatches(Iterable expected) => new _UnorderedMatches(expected); |
| 153 | 160 |
| 154 class _UnorderedMatches extends Matcher { | 161 class _UnorderedMatches extends Matcher { |
| 155 final List<Matcher> _expected; | 162 final List<Matcher> _expected; |
| 156 | 163 |
| 157 _UnorderedMatches(Iterable expected) | 164 _UnorderedMatches(Iterable expected) |
| 158 : _expected = expected.map(wrapMatcher).toList(); | 165 : _expected = expected.map(wrapMatcher).toList(); |
| 159 | 166 |
| 160 String _test(item) { | 167 String _test(item) { |
| 161 if (item is! Iterable) return 'not iterable'; | 168 if (item is! Iterable) return 'not iterable'; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 description | 209 description |
| 203 .add('matches ') | 210 .add('matches ') |
| 204 .addAll('[', ', ', ']', _expected) | 211 .addAll('[', ', ', ']', _expected) |
| 205 .add(' unordered'); | 212 .add(' unordered'); |
| 206 | 213 |
| 207 Description describeMismatch(item, Description mismatchDescription, | 214 Description describeMismatch(item, Description mismatchDescription, |
| 208 Map matchState, bool verbose) => | 215 Map matchState, bool verbose) => |
| 209 mismatchDescription.add(_test(item)); | 216 mismatchDescription.add(_test(item)); |
| 210 } | 217 } |
| 211 | 218 |
| 212 /// A pairwise matcher for iterable. You can pass an arbitrary [comparator] | 219 /** |
| 213 /// function that takes an expected and actual argument which will be applied | 220 * A pairwise matcher for iterable. You can pass an arbitrary [comparator] |
| 214 /// to each pair in order. [description] should be a meaningful name for | 221 * function that takes an expected and actual argument which will be applied |
| 215 /// the comparator. | 222 * to each pair in order. [description] should be a meaningful name for |
| 216 Matcher pairwiseCompare(Iterable expected, bool comparator(a, b), | 223 * the comparator. |
| 224 */ |
| 225 Matcher pairwiseCompare(Iterable expected, Function comparator, |
| 217 String description) => | 226 String description) => |
| 218 new _PairwiseCompare(expected, comparator, description); | 227 new _PairwiseCompare(expected, comparator, description); |
| 219 | 228 |
| 220 typedef bool _Comparator(a, b); | |
| 221 | |
| 222 class _PairwiseCompare extends _IterableMatcher { | 229 class _PairwiseCompare extends _IterableMatcher { |
| 223 final Iterable _expected; | 230 Iterable _expected; |
| 224 final _Comparator _comparator; | 231 Function _comparator; |
| 225 final String _description; | 232 String _description; |
| 226 | 233 |
| 227 _PairwiseCompare(this._expected, this._comparator, this._description); | 234 _PairwiseCompare(this._expected, this._comparator, this._description); |
| 228 | 235 |
| 229 bool matches(item, Map matchState) { | 236 bool matches(item, Map matchState) { |
| 230 if (item is! Iterable) return false; | 237 if (item is! Iterable) return false; |
| 231 if (item.length != _expected.length) return false; | 238 if (item.length != _expected.length) return false; |
| 232 var iterator = item.iterator; | 239 var iterator = item.iterator; |
| 233 var i = 0; | 240 var i = 0; |
| 234 for (var e in _expected) { | 241 for (var e in _expected) { |
| 235 iterator.moveNext(); | 242 iterator.moveNext(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 256 } else { | 263 } else { |
| 257 return mismatchDescription. | 264 return mismatchDescription. |
| 258 add('has '). | 265 add('has '). |
| 259 addDescriptionOf(matchState["actual"]). | 266 addDescriptionOf(matchState["actual"]). |
| 260 add(' which is not $_description '). | 267 add(' which is not $_description '). |
| 261 addDescriptionOf(matchState["expected"]). | 268 addDescriptionOf(matchState["expected"]). |
| 262 add(' at index ${matchState["index"]}'); | 269 add(' at index ${matchState["index"]}'); |
| 263 } | 270 } |
| 264 } | 271 } |
| 265 } | 272 } |
| 273 |
| OLD | NEW |