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 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 36 return item.some( (e) => _matcher.matches(e) ); | 36 return item.some( (e) => _matcher.matches(e) ); |
| 37 } | 37 } |
| 38 | 38 |
| 39 Description describe(Description description) => | 39 Description describe(Description description) => |
| 40 description.add('some element ').addDescriptionOf(_matcher); | 40 description.add('some element ').addDescriptionOf(_matcher); |
| 41 } | 41 } |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * Returns a matcher which matches [Iterable]s that have the same | 44 * Returns a matcher which matches [Iterable]s that have the same |
| 45 * length and the same elements as [expected], and in the same order. | 45 * length and the same elements as [expected], and in the same order. |
| 46 * This is equivalent to equals but does not recurse. | |
| 46 */ | 47 */ |
| 48 | |
| 47 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); | 49 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); |
| 48 | 50 |
| 49 class _OrderedEquals extends BaseMatcher { | 51 class _OrderedEquals extends BaseMatcher { |
| 50 Iterable _expected; | 52 Iterable _expected; |
| 53 Matcher _matcher; | |
|
Bob Nystrom
2012/06/19 23:38:33
Can you make these final?
gram
2012/06/20 17:44:14
_expected yes, _matcher no
| |
| 51 | 54 |
| 52 _OrderedEquals(this._expected); | 55 _OrderedEquals(this._expected) { |
| 53 | 56 _matcher = equals(_expected, 1); |
| 54 String _test(item) { | |
| 55 return _compareIterables(_expected, item, | |
| 56 (expected, actual, location) => expected == actual? null : location); | |
| 57 } | 57 } |
| 58 | 58 |
| 59 bool matches(item) => (_test(item) == null); | 59 bool matches(item) { |
|
Bob Nystrom
2012/06/19 23:38:33
=> (item is Iterable) && _matcher.matches(item);
gram
2012/06/20 17:44:14
Done.
| |
| 60 if (item is !Iterable) { | |
| 61 return false; | |
| 62 } | |
| 63 return _matcher.matches(item); | |
| 64 } | |
| 60 | 65 |
| 61 Description describe(Description description) => | 66 Description describe(Description description) => |
| 62 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); | 67 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); |
| 63 | 68 |
| 64 Description describeMismatch(item, Description mismatchDescription) => | 69 Description describeMismatch(item, Description mismatchDescription) { |
| 65 mismatchDescription.add(_test(item)); | 70 if (item is !Iterable) { |
| 71 return mismatchDescription.add('not an Iterable'); | |
| 72 } else { | |
| 73 return _matcher.describeMismatch(item, mismatchDescription); | |
| 74 } | |
| 75 } | |
| 66 } | 76 } |
| 67 | |
| 68 /** | 77 /** |
| 69 * Returns a matcher which matches [Iterable]s that have the same | 78 * Returns a matcher which matches [Iterable]s that have the same |
| 70 * length and the same elements as [expected], but not necessarily in | 79 * length and the same elements as [expected], but not necessarily in |
| 71 * the same order. Note that this is O(n^2) so should only be used on | 80 * the same order. Note that this is O(n^2) so should only be used on |
| 72 * small objects. | 81 * small objects. |
| 73 */ | 82 */ |
| 74 Matcher unorderedEquals(Iterable expected) => | 83 Matcher unorderedEquals(Iterable expected) => |
| 75 new _UnorderedEquals(expected); | 84 new _UnorderedEquals(expected); |
| 76 | 85 |
| 77 class _UnorderedEquals extends BaseMatcher { | 86 class _UnorderedEquals extends BaseMatcher { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 for (var actualElement in item) { | 126 for (var actualElement in item) { |
| 118 if (!matched[actualPosition]) { | 127 if (!matched[actualPosition]) { |
| 119 if (expectedElement == actualElement) { | 128 if (expectedElement == actualElement) { |
| 120 matched[actualPosition] = gotMatch = true; | 129 matched[actualPosition] = gotMatch = true; |
| 121 break; | 130 break; |
| 122 } | 131 } |
| 123 } | 132 } |
| 124 ++actualPosition; | 133 ++actualPosition; |
| 125 } | 134 } |
| 126 if (!gotMatch) { | 135 if (!gotMatch) { |
| 127 return 'has no match for element ${expectedElement} ' | 136 Description reason = new StringDescription(); |
| 128 'at position ${expectedPosition}'; | 137 reason.add('has no match for element '). |
| 138 addDescriptionOf(expectedElement). | |
| 139 add(' at position ${expectedPosition}'); | |
| 140 return reason.toString(); | |
| 129 } | 141 } |
| 130 ++expectedPosition; | 142 ++expectedPosition; |
| 131 } | 143 } |
| 132 return null; | 144 return null; |
| 133 } | 145 } |
| 134 | 146 |
| 135 bool matches(item) => (_test(item) == null); | 147 bool matches(item) => (_test(item) == null); |
| 136 | 148 |
| 137 Description describe(Description description) => | 149 Description describe(Description description) => |
| 138 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); | 150 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); |
| 139 | 151 |
| 140 Description describeMismatch(item, Description mismatchDescription) => | 152 Description describeMismatch(item, Description mismatchDescription) => |
| 141 mismatchDescription.add(_test(item)); | 153 mismatchDescription.add(_test(item)); |
| 142 } | 154 } |
| 143 | 155 |
| 144 /** | 156 /** |
| 145 * Collection matchers match against [Collection]s. We add this intermediate | 157 * Collection matchers match against [Collection]s. We add this intermediate |
| 146 * class to give better mismatch error messages than the base Matcher class. | 158 * class to give better mismatch error messages than the base Matcher class. |
| 147 */ | 159 */ |
| 148 | 160 /* abstract */ class _CollectionMatcher extends BaseMatcher { |
| 149 /*abstract*/ class _CollectionMatcher extends BaseMatcher { | |
| 150 const _CollectionMatcher(); | 161 const _CollectionMatcher(); |
| 151 Description describeMismatch(item, Description mismatchDescription) { | 162 Description describeMismatch(item, Description mismatchDescription) { |
| 152 if (item is !Collection) { | 163 if (item is !Collection) { |
| 153 return mismatchDescription. | 164 return mismatchDescription. |
| 154 addDescriptionOf(item). | 165 addDescriptionOf(item). |
| 155 add(' not a collection'); | 166 add(' not a collection'); |
| 156 } else { | 167 } else { |
| 157 return super.describeMismatch(item, mismatchDescription); | 168 return super.describeMismatch(item, mismatchDescription); |
| 158 } | 169 } |
| 159 } | 170 } |
| 160 } | 171 } |
| OLD | NEW |