Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * This returns a matcher that inverts [matcher] to its logical negation. | |
| 7 */ | |
| 8 IMatcher isNot(matcher) => new _IsNot(wrapMatcher(matcher)); | |
| 9 | |
| 10 class _IsNot extends Matcher { | |
| 11 | |
| 12 IMatcher _matcher; | |
| 13 | |
| 14 _IsNot(IMatcher this._matcher); | |
| 15 | |
| 16 bool matches(item) => !_matcher.matches(item); | |
| 17 | |
| 18 IDescription describe(IDescription description) => | |
| 19 description.append('not ').appendDescriptionOf(_matcher); | |
| 20 } | |
| 21 | |
| 22 /** | |
| 23 * This returns a matcher that matches if all of the matchers passed as | |
| 24 * arguments (up to 7) match. Instead of passing the matchers separately | |
|
Bob Nystrom
2012/05/30 23:23:51
Why 7?
gram
2012/06/01 17:33:15
Because it is greater than 6 and less than 8 ;-)
Bob Nystrom
2012/06/01 18:22:22
Works for me.
| |
| 25 * they can be passed as a single List argument. | |
| 26 * Any argument that is not a matcher is implicitly wrapped in a | |
| 27 * Matcher to check for equality. | |
| 28 */ | |
| 29 IMatcher allOf(arg0, | |
| 30 [arg1 = null, | |
| 31 arg2 = null, | |
| 32 arg3 = null, | |
| 33 arg4 = null, | |
| 34 arg5 = null, | |
| 35 arg6 = null]) { | |
| 36 if (arg0 is List) { | |
| 37 expect(arg1, isNull()); | |
| 38 expect(arg2, isNull()); | |
| 39 expect(arg3, isNull()); | |
| 40 expect(arg4, isNull()); | |
| 41 expect(arg5, isNull()); | |
| 42 expect(arg6, isNull()); | |
| 43 for (int i = 0; i < arg0.length; i++) { | |
| 44 arg0[i] = wrapMatcher(arg0[i]); | |
| 45 } | |
| 46 return new _AllOf(arg0); | |
| 47 } else { | |
| 48 List matchers = new List(); | |
| 49 if (arg0 != null) { | |
| 50 matchers.add(arg0); | |
| 51 } | |
| 52 if (arg1 != null) { | |
| 53 matchers.add(arg1); | |
| 54 } | |
| 55 if (arg2 != null) { | |
| 56 matchers.add(arg2); | |
| 57 } | |
| 58 if (arg3 != null) { | |
| 59 matchers.add(arg3); | |
| 60 } | |
| 61 if (arg4 != null) { | |
| 62 matchers.add(arg4); | |
| 63 } | |
| 64 if (arg5 != null) { | |
| 65 matchers.add(arg5); | |
| 66 } | |
| 67 if (arg6 != null) { | |
| 68 matchers.add(arg6); | |
| 69 } | |
| 70 return new _AllOf(matchers); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 class _AllOf extends Matcher { | |
| 75 var _matchers; | |
| 76 | |
| 77 _AllOf(this._matchers); | |
| 78 | |
| 79 bool matches(item) { | |
| 80 for (var matcher in _matchers) { | |
| 81 if (!matcher.matches(item)) { | |
| 82 return false; | |
| 83 } | |
| 84 } | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 89 for (var matcher in _matchers) { | |
| 90 if (!matcher.matches(item)) { | |
| 91 mismatchDescription.appendDescriptionOf(matcher).append(' '); | |
| 92 matcher.describeMismatch(item, mismatchDescription); | |
| 93 break; | |
| 94 } | |
| 95 } | |
| 96 return mismatchDescription; | |
| 97 } | |
| 98 | |
| 99 IDescription describe(IDescription description) => | |
| 100 description.appendList('(', ' and ', ')', _matchers); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Matches if any of the given matchers evaluate to true. The | |
| 105 * arguments can be a set of matchers as separate parameters | |
| 106 * (up to 7), or a List of matchers. | |
| 107 * | |
| 108 * The matchers are evaluated from left to right using short-circuit | |
| 109 * evaluation, so evaluation stops as soon as a matcher returns true. | |
| 110 * | |
| 111 * Any argument that is not a matcher is implicitly wrapped in a | |
| 112 * Matcher to check for equality. | |
| 113 */ | |
| 114 | |
| 115 IMatcher anyOf(arg0, | |
| 116 [arg1 = null, | |
| 117 arg2 = null, | |
| 118 arg3 = null, | |
| 119 arg4 = null, | |
| 120 arg5 = null, | |
| 121 arg6 = null]) { | |
| 122 if (arg0 is List) { | |
| 123 expect(arg1, isNull()); | |
| 124 expect(arg2, isNull()); | |
| 125 expect(arg3, isNull()); | |
| 126 expect(arg4, isNull()); | |
| 127 expect(arg5, isNull()); | |
| 128 expect(arg6, isNull()); | |
| 129 for (int i = 0; i < arg0.length; i++) { | |
| 130 arg0[i] = wrapMatcher(arg0[i]); | |
| 131 } | |
| 132 return new _AnyOf(arg0); | |
| 133 } else { | |
| 134 List matchers = new List(); | |
| 135 if (arg0 != null) { | |
| 136 matchers.add(arg0); | |
| 137 } | |
| 138 if (arg1 != null) { | |
| 139 matchers.add(arg1); | |
| 140 } | |
| 141 if (arg2 != null) { | |
| 142 matchers.add(arg2); | |
| 143 } | |
| 144 if (arg3 != null) { | |
| 145 matchers.add(arg3); | |
| 146 } | |
| 147 if (arg4 != null) { | |
| 148 matchers.add(arg4); | |
| 149 } | |
| 150 if (arg5 != null) { | |
| 151 matchers.add(arg5); | |
| 152 } | |
| 153 if (arg6 != null) { | |
| 154 matchers.add(arg6); | |
| 155 } | |
| 156 return new _AnyOf(matchers); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 class _AnyOf extends Matcher { | |
| 161 var _matchers; | |
| 162 | |
| 163 _AnyOf(this._matchers); | |
| 164 | |
| 165 bool matches(item) { | |
| 166 for (var matcher in _matchers) { | |
| 167 if (matcher.matches(item)) { | |
| 168 return true; | |
| 169 } | |
| 170 } | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 IDescription describe(IDescription description) => | |
| 175 description.appendList('(', ' or ', ')', _matchers); | |
| 176 } | |
| 177 | |
| OLD | NEW |