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