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 = null, arg2 = null, arg3 = null, arg4 = null, |
Siggi Cherem (dart-lang)
2015/01/14 20:30:09
request for a lint tool: remove `= null`, it's unn
kevmoo
2015/01/14 22:28:45
Acknowledged.
| |
31 [arg1 = null, | 31 arg5 = null, arg6 = 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)); | 32 return new _AllOf(_wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6)); |
38 } | 33 } |
39 | 34 |
40 class _AllOf extends Matcher { | 35 class _AllOf extends Matcher { |
41 final List<Matcher> _matchers; | 36 final List<Matcher> _matchers; |
42 | 37 |
43 const _AllOf(this._matchers); | 38 const _AllOf(this._matchers); |
44 | 39 |
45 bool matches(item, Map matchState) { | 40 bool matches(item, Map matchState) { |
46 for (var matcher in _matchers) { | 41 for (var matcher in _matchers) { |
47 if (!matcher.matches(item, matchState)) { | 42 if (!matcher.matches(item, matchState)) { |
48 addStateInfo(matchState, {'matcher': matcher}); | 43 addStateInfo(matchState, {'matcher': matcher}); |
49 return false; | 44 return false; |
50 } | 45 } |
51 } | 46 } |
52 return true; | 47 return true; |
53 } | 48 } |
54 | 49 |
55 Description describeMismatch(item, Description mismatchDescription, | 50 Description describeMismatch( |
56 Map matchState, bool verbose) { | 51 item, Description mismatchDescription, Map matchState, bool verbose) { |
57 var matcher = matchState['matcher']; | 52 var matcher = matchState['matcher']; |
58 matcher.describeMismatch(item, mismatchDescription, | 53 matcher.describeMismatch( |
59 matchState['state'], verbose); | 54 item, mismatchDescription, matchState['state'], verbose); |
60 return mismatchDescription; | 55 return mismatchDescription; |
61 } | 56 } |
62 | 57 |
63 Description describe(Description description) => | 58 Description describe(Description description) => |
64 description.addAll('(', ' and ', ')', _matchers); | 59 description.addAll('(', ' and ', ')', _matchers); |
65 } | 60 } |
66 | 61 |
67 /// Matches if any of the given matchers evaluate to true. | 62 /// Matches if any of the given matchers evaluate to true. |
68 /// | 63 /// |
69 /// The arguments can be a set of matchers as separate parameters | 64 /// The arguments can be a set of matchers as separate parameters |
70 /// (up to 7), or a List of matchers. | 65 /// (up to 7), or a List of matchers. |
71 /// | 66 /// |
72 /// The matchers are evaluated from left to right using short-circuit | 67 /// The matchers are evaluated from left to right using short-circuit |
73 /// evaluation, so evaluation stops as soon as a matcher returns true. | 68 /// evaluation, so evaluation stops as soon as a matcher returns true. |
74 /// | 69 /// |
75 /// Any argument that is not a matcher is implicitly wrapped in a | 70 /// Any argument that is not a matcher is implicitly wrapped in a |
76 /// Matcher to check for equality. | 71 /// Matcher to check for equality. |
77 Matcher anyOf(arg0, | 72 Matcher anyOf(arg0, [arg1 = null, arg2 = null, arg3 = null, arg4 = null, |
78 [arg1 = null, | 73 arg5 = null, arg6 = 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)); | 74 return new _AnyOf(_wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6)); |
85 } | 75 } |
86 | 76 |
87 class _AnyOf extends Matcher { | 77 class _AnyOf extends Matcher { |
88 final List<Matcher> _matchers; | 78 final List<Matcher> _matchers; |
89 | 79 |
90 const _AnyOf(this._matchers); | 80 const _AnyOf(this._matchers); |
91 | 81 |
92 bool matches(item, Map matchState) { | 82 bool matches(item, Map matchState) { |
93 for (var matcher in _matchers) { | 83 for (var matcher in _matchers) { |
94 if (matcher.matches(item, matchState)) { | 84 if (matcher.matches(item, matchState)) { |
95 return true; | 85 return true; |
96 } | 86 } |
97 } | 87 } |
98 return false; | 88 return false; |
99 } | 89 } |
100 | 90 |
101 Description describe(Description description) => | 91 Description describe(Description description) => |
102 description.addAll('(', ' or ', ')', _matchers); | 92 description.addAll('(', ' or ', ')', _matchers); |
103 } | 93 } |
104 | 94 |
105 List<Matcher> _wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6) { | 95 List<Matcher> _wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6) { |
106 Iterable<Matcher> matchers; | 96 Iterable<Matcher> matchers; |
107 if (arg0 is List) { | 97 if (arg0 is List) { |
108 if (arg1 != null || arg2 != null || arg3 != null || arg4 != null || | 98 if (arg1 != null || |
109 arg5 != null || arg6 != null) { | 99 arg2 != null || |
100 arg3 != null || | |
101 arg4 != null || | |
102 arg5 != null || | |
103 arg6 != null) { | |
110 throw new ArgumentError('If arg0 is a List, all other arguments must be' | 104 throw new ArgumentError('If arg0 is a List, all other arguments must be' |
111 ' null.'); | 105 ' null.'); |
112 } | 106 } |
113 | 107 |
114 matchers = arg0; | 108 matchers = arg0; |
115 } else { | 109 } else { |
116 matchers = [arg0, arg1, arg2, arg3, arg4, arg5, arg6] | 110 matchers = |
117 .where((e) => e != null); | 111 [arg0, arg1, arg2, arg3, arg4, arg5, arg6].where((e) => e != null); |
118 } | 112 } |
119 | 113 |
120 return matchers | 114 return matchers.map((e) => wrapMatcher(e)).toList(); |
121 .map((e) => wrapMatcher(e)) | |
122 .toList(); | |
123 } | 115 } |
OLD | NEW |