Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: pkg/unittest/operator_matchers.dart

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

Powered by Google App Engine
This is Rietveld 408576698