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