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