| 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 library matcher.iterable_matchers; |
| 6 | 6 |
| 7 import 'core_matchers.dart'; | 7 import 'core_matchers.dart'; |
| 8 import 'description.dart'; | 8 import 'description.dart'; |
| 9 import 'interfaces.dart'; | 9 import 'interfaces.dart'; |
| 10 import 'util.dart'; | 10 import 'util.dart'; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 bool matches(item, Map matchState) { | 72 bool matches(item, Map matchState) { |
| 73 return item.any((e) => _matcher.matches(e, matchState)); | 73 return item.any((e) => _matcher.matches(e, matchState)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 Description describe(Description description) => | 76 Description describe(Description description) => |
| 77 description.add('some element ').addDescriptionOf(_matcher); | 77 description.add('some element ').addDescriptionOf(_matcher); |
| 78 } | 78 } |
| 79 | 79 |
| 80 /// Returns a matcher which matches [Iterable]s that have the same | 80 /// Returns a matcher which matches [Iterable]s that have the same |
| 81 /// length and the same elements as [expected], and in the same order. | 81 /// length and the same elements as [expected], in the same order. |
| 82 /// This is equivalent to equals but does not recurse. | 82 /// |
| 83 | 83 /// This is equivalent to [equals] but does not recurse. |
| 84 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); | 84 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); |
| 85 | 85 |
| 86 class _OrderedEquals extends Matcher { | 86 class _OrderedEquals extends Matcher { |
| 87 final Iterable _expected; | 87 final Iterable _expected; |
| 88 Matcher _matcher; | 88 Matcher _matcher; |
| 89 | 89 |
| 90 _OrderedEquals(this._expected) { | 90 _OrderedEquals(this._expected) { |
| 91 _matcher = equals(_expected, 1); | 91 _matcher = equals(_expected, 1); |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool matches(item, Map matchState) => | 94 bool matches(item, Map matchState) => |
| 95 (item is Iterable) && _matcher.matches(item, matchState); | 95 (item is Iterable) && _matcher.matches(item, matchState); |
| 96 | 96 |
| 97 Description describe(Description description) => | 97 Description describe(Description description) => |
| 98 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); | 98 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); |
| 99 | 99 |
| 100 Description describeMismatch(item, Description mismatchDescription, | 100 Description describeMismatch(item, Description mismatchDescription, |
| 101 Map matchState, bool verbose) { | 101 Map matchState, bool verbose) { |
| 102 if (item is !Iterable) { | 102 if (item is !Iterable) { |
| 103 return mismatchDescription.add('is not an Iterable'); | 103 return mismatchDescription.add('is not an Iterable'); |
| 104 } else { | 104 } else { |
| 105 return _matcher.describeMismatch(item, mismatchDescription, | 105 return _matcher.describeMismatch(item, mismatchDescription, |
| 106 matchState, verbose); | 106 matchState, verbose); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 /// Returns a matcher which matches [Iterable]s that have the same | 111 /// Returns a matcher which matches [Iterable]s that have the same length and |
| 112 /// length and the same elements as [expected], but not necessarily in | 112 /// the same elements as [expected], but not necessarily in the same order. |
| 113 /// the same order. Note that this is O(n^2) so should only be used on | 113 /// |
| 114 /// small objects. | 114 /// Note that this is O(n^2) so should only be used on small objects. |
| 115 Matcher unorderedEquals(Iterable expected) => new _UnorderedEquals(expected); | 115 Matcher unorderedEquals(Iterable expected) => new _UnorderedEquals(expected); |
| 116 | 116 |
| 117 class _UnorderedEquals extends _UnorderedMatches { | 117 class _UnorderedEquals extends _UnorderedMatches { |
| 118 final List _expectedValues; | 118 final List _expectedValues; |
| 119 | 119 |
| 120 _UnorderedEquals(Iterable expected) | 120 _UnorderedEquals(Iterable expected) |
| 121 : super(expected.map(equals)), | 121 : super(expected.map(equals)), |
| 122 _expectedValues = expected.toList(); | 122 _expectedValues = expected.toList(); |
| 123 | 123 |
| 124 Description describe(Description description) => | 124 Description describe(Description description) => |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 description | 202 description |
| 203 .add('matches ') | 203 .add('matches ') |
| 204 .addAll('[', ', ', ']', _expected) | 204 .addAll('[', ', ', ']', _expected) |
| 205 .add(' unordered'); | 205 .add(' unordered'); |
| 206 | 206 |
| 207 Description describeMismatch(item, Description mismatchDescription, | 207 Description describeMismatch(item, Description mismatchDescription, |
| 208 Map matchState, bool verbose) => | 208 Map matchState, bool verbose) => |
| 209 mismatchDescription.add(_test(item)); | 209 mismatchDescription.add(_test(item)); |
| 210 } | 210 } |
| 211 | 211 |
| 212 /// A pairwise matcher for iterable. You can pass an arbitrary [comparator] | 212 /// A pairwise matcher for [Iterable]s. |
| 213 /// function that takes an expected and actual argument which will be applied | 213 /// |
| 214 /// to each pair in order. [description] should be a meaningful name for | 214 /// The [comparator] function, taking an expected and an actual argument, and |
| 215 /// the comparator. | 215 /// returning whether they match, will be applied to each pair in order. |
| 216 /// [description] should be a meaningful name for the comparator. |
| 216 Matcher pairwiseCompare(Iterable expected, bool comparator(a, b), | 217 Matcher pairwiseCompare(Iterable expected, bool comparator(a, b), |
| 217 String description) => | 218 String description) => |
| 218 new _PairwiseCompare(expected, comparator, description); | 219 new _PairwiseCompare(expected, comparator, description); |
| 219 | 220 |
| 220 typedef bool _Comparator(a, b); | 221 typedef bool _Comparator(a, b); |
| 221 | 222 |
| 222 class _PairwiseCompare extends _IterableMatcher { | 223 class _PairwiseCompare extends _IterableMatcher { |
| 223 final Iterable _expected; | 224 final Iterable _expected; |
| 224 final _Comparator _comparator; | 225 final _Comparator _comparator; |
| 225 final String _description; | 226 final String _description; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 256 } else { | 257 } else { |
| 257 return mismatchDescription. | 258 return mismatchDescription. |
| 258 add('has '). | 259 add('has '). |
| 259 addDescriptionOf(matchState["actual"]). | 260 addDescriptionOf(matchState["actual"]). |
| 260 add(' which is not $_description '). | 261 add(' which is not $_description '). |
| 261 addDescriptionOf(matchState["expected"]). | 262 addDescriptionOf(matchState["expected"]). |
| 262 add(' at index ${matchState["index"]}'); | 263 add(' at index ${matchState["index"]}'); |
| 263 } | 264 } |
| 264 } | 265 } |
| 265 } | 266 } |
| OLD | NEW |