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