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

Side by Side Diff: pkg/expect/lib/expect.dart

Issue 12743005: Revert "Remove Expect from core library." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Reupload (first upload failed). Created 7 years, 9 months 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
« no previous file with comments | « no previous file | pkg/expect/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /**
6 * This library contains an Expect class with static methods that can be used
7 * for simple unit-tests.
8 */
9 library expect;
10
11 /**
12 * Expect is used for tests that do not want to make use of the
13 * Dart unit test library - for example, the core language tests.
14 * Third parties are discouraged from using this, and should use
15 * the expect() function in the unit test library instead for
16 * test assertions.
17 */
18 class Expect {
19 /**
20 * Checks whether the expected and actual values are equal (using `==`).
21 */
22 static void equals(var expected, var actual, [String reason = null]) {
23 if (expected == actual) return;
24 String msg = _getMessage(reason);
25 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails.");
26 }
27
28 /**
29 * Checks whether the actual value is a bool and its value is true.
30 */
31 static void isTrue(var actual, [String reason = null]) {
32 if (_identical(actual, true)) return;
33 String msg = _getMessage(reason);
34 _fail("Expect.isTrue($actual$msg) fails.");
35 }
36
37 /**
38 * Checks whether the actual value is a bool and its value is false.
39 */
40 static void isFalse(var actual, [String reason = null]) {
41 if (_identical(actual, false)) return;
42 String msg = _getMessage(reason);
43 _fail("Expect.isFalse($actual$msg) fails.");
44 }
45
46 /**
47 * Checks whether [actual] is null.
48 */
49 static void isNull(actual, [String reason = null]) {
50 if (null == actual) return;
51 String msg = _getMessage(reason);
52 _fail("Expect.isNull(actual: <$actual>$msg) fails.");
53 }
54
55 /**
56 * Checks whether [actual] is not null.
57 */
58 static void isNotNull(actual, [String reason = null]) {
59 if (null != actual) return;
60 String msg = _getMessage(reason);
61 _fail("Expect.isNotNull(actual: <$actual>$msg) fails.");
62 }
63
64 /**
65 * Checks whether the expected and actual values are identical
66 * (using `identical`).
67 */
68 static void identical(var expected, var actual, [String reason = null]) {
69 if (_identical(expected, actual)) return;
70 String msg = _getMessage(reason);
71 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) "
72 "fails.");
73 }
74
75 // Unconditional failure.
76 static void fail(String msg) {
77 _fail("Expect.fail('$msg')");
78 }
79
80 /**
81 * Failure if the difference between expected and actual is greater than the
82 * given tolerance. If no tolerance is given, tolerance is assumed to be the
83 * value 4 significant digits smaller than the value given for expected.
84 */
85 static void approxEquals(num expected,
86 num actual,
87 [num tolerance = null,
88 String reason = null]) {
89 if (tolerance == null) {
90 tolerance = (expected / 1e4).abs();
91 }
92 // Note: use !( <= ) rather than > so we fail on NaNs
93 if ((expected - actual).abs() <= tolerance) return;
94
95 String msg = _getMessage(reason);
96 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, '
97 'tolerance:<$tolerance>$msg) fails');
98 }
99
100 static void notEquals(unexpected, actual, [String reason = null]) {
101 if (unexpected != actual) return;
102 String msg = _getMessage(reason);
103 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) "
104 "fails.");
105 }
106
107 /**
108 * Checks that all elements in [expected] and [actual] are equal `==`.
109 * This is different than the typical check for identity equality `identical`
110 * used by the standard list implementation. It should also produce nicer
111 * error messages than just calling `Expect.equals(expected, actual)`.
112 */
113 static void listEquals(List expected, List actual, [String reason = null]) {
114 String msg = _getMessage(reason);
115 int n = (expected.length < actual.length) ? expected.length : actual.length;
116 for (int i = 0; i < n; i++) {
117 if (expected[i] != actual[i]) {
118 _fail('Expect.listEquals(at index $i, '
119 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails');
120 }
121 }
122 // We check on length at the end in order to provide better error
123 // messages when an unexpected item is inserted in a list.
124 if (expected.length != actual.length) {
125 _fail('Expect.listEquals(list length, '
126 'expected: <${expected.length}>, actual: <${actual.length}>$msg) '
127 'fails: Next element <'
128 '${expected.length > n ? expected[n] : actual[n]}>');
129 }
130 }
131
132 /**
133 * Checks that all [expected] and [actual] have the same set of keys (using
134 * the semantics of [Map.containsKey] to determine what "same" means. For
135 * each key, checks that the values in both maps are equal using `==`.
136 */
137 static void mapEquals(Map expected, Map actual, [String reason = null]) {
138 String msg = _getMessage(reason);
139
140 // Make sure all of the values are present in both and match.
141 for (final key in expected.keys) {
142 if (!actual.containsKey(key)) {
143 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails');
144 }
145
146 Expect.equals(expected[key], actual[key]);
147 }
148
149 // Make sure the actual map doesn't have any extra keys.
150 for (final key in actual.keys) {
151 if (!expected.containsKey(key)) {
152 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails');
153 }
154 }
155 }
156
157 /**
158 * Specialized equality test for strings. When the strings don't match,
159 * this method shows where the mismatch starts and ends.
160 */
161 static void stringEquals(String expected,
162 String actual,
163 [String reason = null]) {
164 String msg = _getMessage(reason);
165 String defaultMessage =
166 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails';
167
168 if (expected == actual) return;
169 if ((expected == null) || (actual == null)) {
170 _fail('$defaultMessage');
171 }
172 // scan from the left until we find a mismatch
173 int left = 0;
174 int eLen = expected.length;
175 int aLen = actual.length;
176 while (true) {
177 if (left == eLen) {
178 assert (left < aLen);
179 String snippet = actual.substring(left, aLen);
180 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]');
181 return;
182 }
183 if (left == aLen) {
184 assert (left < eLen);
185 String snippet = expected.substring(left, eLen);
186 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]');
187 return;
188 }
189 if (expected[left] != actual[left]) {
190 break;
191 }
192 left++;
193 }
194
195 // scan from the right until we find a mismatch
196 int right = 0;
197 while (true) {
198 if (right == eLen) {
199 assert (right < aLen);
200 String snippet = actual.substring(0, aLen - right);
201 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...');
202 return;
203 }
204 if (right == aLen) {
205 assert (right < eLen);
206 String snippet = expected.substring(0, eLen - right);
207 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...');
208 return;
209 }
210 // stop scanning if we've reached the end of the left-to-right match
211 if (eLen - right <= left || aLen - right <= left) {
212 break;
213 }
214 if (expected[eLen - right - 1] != actual[aLen - right - 1]) {
215 break;
216 }
217 right++;
218 }
219 String eSnippet = expected.substring(left, eLen - right);
220 String aSnippet = actual.substring(left, aLen - right);
221 String diff = '\nDiff:\n...[ $eSnippet ]...\n...[ $aSnippet ]...';
222 _fail('$defaultMessage$diff');
223 }
224
225 /**
226 * Checks that every element of [expected] is also in [actual], and that
227 * every element of [actual] is also in [expected].
228 */
229 static void setEquals(Iterable expected,
230 Iterable actual,
231 [String reason = null]) {
232 final missingSet = new Set.from(expected);
233 missingSet.removeAll(actual);
234 final extraSet = new Set.from(actual);
235 extraSet.removeAll(expected);
236
237 if (extraSet.isEmpty && missingSet.isEmpty) return;
238 String msg = _getMessage(reason);
239
240 StringBuffer sb = new StringBuffer("Expect.setEquals($msg) fails");
241 // Report any missing items.
242 if (!missingSet.isEmpty) {
243 sb.write('\nExpected collection does not contain: ');
244 }
245
246 for (final val in missingSet) {
247 sb.write('$val ');
248 }
249
250 // Report any extra items.
251 if (!extraSet.isEmpty) {
252 sb.write('\nExpected collection should not contain: ');
253 }
254
255 for (final val in extraSet) {
256 sb.write('$val ');
257 }
258 _fail(sb.toString());
259 }
260
261 /**
262 * Calls the function [f] and verifies that it throws an exception.
263 * The optional [check] function can provide additional validation
264 * that the correct exception is being thrown. For example, to check
265 * the type of the exception you could write this:
266 *
267 * Expect.throws(myThrowingFunction, (e) => e is MyException);
268 */
269 static void throws(void f(),
270 [_CheckExceptionFn check = null,
271 String reason = null]) {
272 try {
273 f();
274 } catch (e, s) {
275 if (check != null) {
276 if (!check(e)) {
277 String msg = reason == null ? "" : reason;
278 _fail("Expect.throws($msg): Unexpected '$e'\n$s");
279 }
280 }
281 return;
282 }
283 String msg = reason == null ? "" : reason;
284 _fail('Expect.throws($msg) fails');
285 }
286
287 static String _getMessage(String reason)
288 => (reason == null) ? "" : ", '$reason'";
289
290 static void _fail(String message) {
291 throw new ExpectException(message);
292 }
293 }
294
295 bool _identical(a, b) => identical(a, b);
296
297 typedef bool _CheckExceptionFn(exception);
298
299 class ExpectException implements Exception {
300 ExpectException(this.message);
301 String toString() => message;
302 String message;
303 }
OLDNEW
« no previous file with comments | « no previous file | pkg/expect/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698