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 library matcher.operator_matchers; |
| 6 |
| 7 import 'interfaces.dart'; |
| 8 import 'util.dart'; |
| 9 |
| 10 /// This returns a matcher that inverts [matcher] to its logical negation. |
| 11 Matcher isNot(matcher) => new _IsNot(wrapMatcher(matcher)); |
| 12 |
| 13 class _IsNot extends Matcher { |
| 14 final Matcher _matcher; |
| 15 |
| 16 const _IsNot(this._matcher); |
| 17 |
| 18 bool matches(item, Map matchState) => !_matcher.matches(item, matchState); |
| 19 |
| 20 Description describe(Description description) => |
| 21 description.add('not ').addDescriptionOf(_matcher); |
| 22 } |
| 23 |
| 24 /// This returns a matcher that matches if all of the matchers passed as |
| 25 /// arguments (up to 7) match. |
| 26 /// |
| 27 /// Instead of passing the matchers separately they can be passed as a single |
| 28 /// List argument. Any argument that is not a matcher is implicitly wrapped in a |
| 29 /// Matcher to check for equality. |
| 30 Matcher allOf(arg0, |
| 31 [arg1 = null, |
| 32 arg2 = null, |
| 33 arg3 = null, |
| 34 arg4 = null, |
| 35 arg5 = null, |
| 36 arg6 = null]) { |
| 37 return new _AllOf(_wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6)); |
| 38 } |
| 39 |
| 40 class _AllOf extends Matcher { |
| 41 final List<Matcher> _matchers; |
| 42 |
| 43 const _AllOf(this._matchers); |
| 44 |
| 45 bool matches(item, Map matchState) { |
| 46 for (var matcher in _matchers) { |
| 47 if (!matcher.matches(item, matchState)) { |
| 48 addStateInfo(matchState, {'matcher': matcher}); |
| 49 return false; |
| 50 } |
| 51 } |
| 52 return true; |
| 53 } |
| 54 |
| 55 Description describeMismatch(item, Description mismatchDescription, |
| 56 Map matchState, bool verbose) { |
| 57 var matcher = matchState['matcher']; |
| 58 matcher.describeMismatch(item, mismatchDescription, |
| 59 matchState['state'], verbose); |
| 60 return mismatchDescription; |
| 61 } |
| 62 |
| 63 Description describe(Description description) => |
| 64 description.addAll('(', ' and ', ')', _matchers); |
| 65 } |
| 66 |
| 67 /// Matches if any of the given matchers evaluate to true. |
| 68 /// |
| 69 /// The arguments can be a set of matchers as separate parameters |
| 70 /// (up to 7), or a List of matchers. |
| 71 /// |
| 72 /// The matchers are evaluated from left to right using short-circuit |
| 73 /// evaluation, so evaluation stops as soon as a matcher returns true. |
| 74 /// |
| 75 /// Any argument that is not a matcher is implicitly wrapped in a |
| 76 /// Matcher to check for equality. |
| 77 Matcher anyOf(arg0, |
| 78 [arg1 = null, |
| 79 arg2 = null, |
| 80 arg3 = null, |
| 81 arg4 = null, |
| 82 arg5 = 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) { |
| 106 Iterable<Matcher> matchers; |
| 107 if (arg0 is List) { |
| 108 if (arg1 != null || arg2 != null || arg3 != null || arg4 != null || |
| 109 arg5 != null || arg6 != null) { |
| 110 throw new ArgumentError('If arg0 is a List, all other arguments must be' |
| 111 ' null.'); |
| 112 } |
| 113 |
| 114 matchers = arg0; |
| 115 } else { |
| 116 matchers = [arg0, arg1, arg2, arg3, arg4, arg5, arg6] |
| 117 .where((e) => e != null); |
| 118 } |
| 119 |
| 120 return matchers |
| 121 .map((e) => wrapMatcher(e)) |
| 122 .toList(); |
| 123 } |
OLD | NEW |