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