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

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

Issue 2413073002: Start cleaning up the HTML tests. (Closed)
Patch Set: Unfork expect.dart. Created 4 years, 2 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
« no previous file with comments | « pkg/dev_compiler/tool/build_test_pkgs.sh ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * This library contains an Expect class with static methods that can be used 6 * This library contains an Expect class with static methods that can be used
7 * for simple unit-tests. 7 * for simple unit-tests.
8 */ 8 */
9 library expect; 9 library expect;
10 10
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 int start = i; 74 int start = i;
75 i++; 75 i++;
76 while (i < expected.length && i < actual.length) { 76 while (i < expected.length && i < actual.length) {
77 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break; 77 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break;
78 i++; 78 i++;
79 } 79 }
80 int end = i; 80 int end = i;
81 var truncExpected = _truncateString(expected, start, end, 20); 81 var truncExpected = _truncateString(expected, start, end, 20);
82 var truncActual = _truncateString(actual, start, end, 20); 82 var truncActual = _truncateString(actual, start, end, 20);
83 return "at index $start: Expected <$truncExpected>, " 83 return "at index $start: Expected <$truncExpected>, "
84 "Found: <$truncActual>"; 84 "Found: <$truncActual>";
85 } 85 }
86 } 86 }
87 return null; 87 return null;
88 } 88 }
89 89
90 /** 90 /**
91 * Checks whether the expected and actual values are equal (using `==`). 91 * Checks whether the expected and actual values are equal (using `==`).
92 */ 92 */
93 static void equals(var expected, var actual, [String reason = null]) { 93 static void equals(var expected, var actual, [String reason = null]) {
94 if (expected == actual) return; 94 if (expected == actual) return;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 } 139 }
140 140
141 /** 141 /**
142 * Checks whether the expected and actual values are identical 142 * Checks whether the expected and actual values are identical
143 * (using `identical`). 143 * (using `identical`).
144 */ 144 */
145 static void identical(var expected, var actual, [String reason = null]) { 145 static void identical(var expected, var actual, [String reason = null]) {
146 if (_identical(expected, actual)) return; 146 if (_identical(expected, actual)) return;
147 String msg = _getMessage(reason); 147 String msg = _getMessage(reason);
148 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " 148 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) "
149 "fails."); 149 "fails.");
150 } 150 }
151 151
152 // Unconditional failure. 152 // Unconditional failure.
153 static void fail(String msg) { 153 static void fail(String msg) {
154 _fail("Expect.fail('$msg')"); 154 _fail("Expect.fail('$msg')");
155 } 155 }
156 156
157 /** 157 /**
158 * Failure if the difference between expected and actual is greater than the 158 * Failure if the difference between expected and actual is greater than the
159 * given tolerance. If no tolerance is given, tolerance is assumed to be the 159 * given tolerance. If no tolerance is given, tolerance is assumed to be the
160 * value 4 significant digits smaller than the value given for expected. 160 * value 4 significant digits smaller than the value given for expected.
161 */ 161 */
162 static void approxEquals(num expected, 162 static void approxEquals(num expected, num actual,
163 num actual, 163 [num tolerance = null, String reason = null]) {
164 [num tolerance = null,
165 String reason = null]) {
166 if (tolerance == null) { 164 if (tolerance == null) {
167 tolerance = (expected / 1e4).abs(); 165 tolerance = (expected / 1e4).abs();
168 } 166 }
169 // Note: use !( <= ) rather than > so we fail on NaNs 167 // Note: use !( <= ) rather than > so we fail on NaNs
170 if ((expected - actual).abs() <= tolerance) return; 168 if ((expected - actual).abs() <= tolerance) return;
171 169
172 String msg = _getMessage(reason); 170 String msg = _getMessage(reason);
173 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' 171 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, '
174 'tolerance:<$tolerance>$msg) fails'); 172 'tolerance:<$tolerance>$msg) fails');
175 } 173 }
176 174
177 static void notEquals(unexpected, actual, [String reason = null]) { 175 static void notEquals(unexpected, actual, [String reason = null]) {
178 if (unexpected != actual) return; 176 if (unexpected != actual) return;
179 String msg = _getMessage(reason); 177 String msg = _getMessage(reason);
180 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " 178 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) "
181 "fails."); 179 "fails.");
182 } 180 }
183 181
184 /** 182 /**
185 * Checks that all elements in [expected] and [actual] are equal `==`. 183 * Checks that all elements in [expected] and [actual] are equal `==`.
186 * This is different than the typical check for identity equality `identical` 184 * This is different than the typical check for identity equality `identical`
187 * used by the standard list implementation. It should also produce nicer 185 * used by the standard list implementation. It should also produce nicer
188 * error messages than just calling `Expect.equals(expected, actual)`. 186 * error messages than just calling `Expect.equals(expected, actual)`.
189 */ 187 */
190 static void listEquals(List expected, List actual, [String reason = null]) { 188 static void listEquals(List expected, List actual, [String reason = null]) {
191 String msg = _getMessage(reason); 189 String msg = _getMessage(reason);
192 int n = (expected.length < actual.length) ? expected.length : actual.length; 190 int n = (expected.length < actual.length) ? expected.length : actual.length;
193 for (int i = 0; i < n; i++) { 191 for (int i = 0; i < n; i++) {
194 if (expected[i] != actual[i]) { 192 if (expected[i] != actual[i]) {
195 _fail('Expect.listEquals(at index $i, ' 193 _fail('Expect.listEquals(at index $i, '
196 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); 194 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails');
197 } 195 }
198 } 196 }
199 // We check on length at the end in order to provide better error 197 // We check on length at the end in order to provide better error
200 // messages when an unexpected item is inserted in a list. 198 // messages when an unexpected item is inserted in a list.
201 if (expected.length != actual.length) { 199 if (expected.length != actual.length) {
202 _fail('Expect.listEquals(list length, ' 200 _fail('Expect.listEquals(list length, '
203 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' 201 'expected: <${expected.length}>, actual: <${actual.length}>$msg) '
204 'fails: Next element <' 202 'fails: Next element <'
205 '${expected.length > n ? expected[n] : actual[n]}>'); 203 '${expected.length > n ? expected[n] : actual[n]}>');
206 } 204 }
207 } 205 }
208 206
209 /** 207 /**
210 * Checks that all [expected] and [actual] have the same set of keys (using 208 * Checks that all [expected] and [actual] have the same set of keys (using
211 * the semantics of [Map.containsKey] to determine what "same" means. For 209 * the semantics of [Map.containsKey] to determine what "same" means. For
212 * each key, checks that the values in both maps are equal using `==`. 210 * each key, checks that the values in both maps are equal using `==`.
213 */ 211 */
214 static void mapEquals(Map expected, Map actual, [String reason = null]) { 212 static void mapEquals(Map expected, Map actual, [String reason = null]) {
215 String msg = _getMessage(reason); 213 String msg = _getMessage(reason);
(...skipping 12 matching lines...) Expand all
228 if (!expected.containsKey(key)) { 226 if (!expected.containsKey(key)) {
229 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails'); 227 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails');
230 } 228 }
231 } 229 }
232 } 230 }
233 231
234 /** 232 /**
235 * Specialized equality test for strings. When the strings don't match, 233 * Specialized equality test for strings. When the strings don't match,
236 * this method shows where the mismatch starts and ends. 234 * this method shows where the mismatch starts and ends.
237 */ 235 */
238 static void stringEquals(String expected, 236 static void stringEquals(String expected, String actual,
239 String actual, 237 [String reason = null]) {
240 [String reason = null]) {
241 if (expected == actual) return; 238 if (expected == actual) return;
242 239
243 String msg = _getMessage(reason); 240 String msg = _getMessage(reason);
244 String defaultMessage = 241 String defaultMessage =
245 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; 242 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails';
246 243
247 if ((expected == null) || (actual == null)) { 244 if ((expected == null) || (actual == null)) {
248 _fail('$defaultMessage'); 245 _fail('$defaultMessage');
249 } 246 }
250 247
251 // Scan from the left until we find the mismatch. 248 // Scan from the left until we find the mismatch.
252 int left = 0; 249 int left = 0;
253 int right = 0; 250 int right = 0;
254 int eLen = expected.length; 251 int eLen = expected.length;
255 int aLen = actual.length; 252 int aLen = actual.length;
256 253
257 while (true) { 254 while (true) {
258 if (left == eLen || left == aLen || expected[left] != actual[left]) { 255 if (left == eLen || left == aLen || expected[left] != actual[left]) {
259 break; 256 break;
260 } 257 }
261 left++; 258 left++;
262 } 259 }
263 260
264 // Scan from the right until we find the mismatch. 261 // Scan from the right until we find the mismatch.
265 int eRem = eLen - left; // Remaining length ignoring left match. 262 int eRem = eLen - left; // Remaining length ignoring left match.
266 int aRem = aLen - left; 263 int aRem = aLen - left;
267 while (true) { 264 while (true) {
268 if (right == eRem || right == aRem || 265 if (right == eRem ||
266 right == aRem ||
269 expected[eLen - right - 1] != actual[aLen - right - 1]) { 267 expected[eLen - right - 1] != actual[aLen - right - 1]) {
270 break; 268 break;
271 } 269 }
272 right++; 270 right++;
273 } 271 }
274 272
275 // First difference is at index `left`, last at `length - right - 1` 273 // First difference is at index `left`, last at `length - right - 1`
276 // Make useful difference message. 274 // Make useful difference message.
277 // Example: 275 // Example:
278 // Diff (1209..1209/1246): 276 // Diff (1209..1209/1246):
279 // ...,{"name":"[ ]FallThroug... 277 // ...,{"name":"[ ]FallThroug...
280 // ...,{"name":"[ IndexError","kind":"class"},{"name":" ]FallThroug... 278 // ...,{"name":"[ IndexError","kind":"class"},{"name":" ]FallThroug...
281 // (colors would be great!) 279 // (colors would be great!)
282 280
283 // Make snippets of up to ten characters before and after differences. 281 // Make snippets of up to ten characters before and after differences.
284 282
285 String leftSnippet = expected.substring(left < 10 ? 0 : left - 10, left); 283 String leftSnippet = expected.substring(left < 10 ? 0 : left - 10, left);
286 int rightSnippetLength = right < 10 ? right : 10; 284 int rightSnippetLength = right < 10 ? right : 10;
287 String rightSnippet = 285 String rightSnippet =
288 expected.substring(eLen - right, eLen - right + rightSnippetLength); 286 expected.substring(eLen - right, eLen - right + rightSnippetLength);
289 287
290 // Make snippets of the differences. 288 // Make snippets of the differences.
291 String eSnippet = expected.substring(left, eLen - right); 289 String eSnippet = expected.substring(left, eLen - right);
292 String aSnippet = actual.substring(left, aLen - right); 290 String aSnippet = actual.substring(left, aLen - right);
293 291
294 // If snippets are long, elide the middle. 292 // If snippets are long, elide the middle.
295 if (eSnippet.length > 43) { 293 if (eSnippet.length > 43) {
296 eSnippet = eSnippet.substring(0, 20) + "..." + 294 eSnippet = eSnippet.substring(0, 20) +
297 eSnippet.substring(eSnippet.length - 20); 295 "..." +
296 eSnippet.substring(eSnippet.length - 20);
298 } 297 }
299 if (aSnippet.length > 43) { 298 if (aSnippet.length > 43) {
300 aSnippet = aSnippet.substring(0, 20) + "..." + 299 aSnippet = aSnippet.substring(0, 20) +
301 aSnippet.substring(aSnippet.length - 20); 300 "..." +
301 aSnippet.substring(aSnippet.length - 20);
302 } 302 }
303 // Add "..." before and after, unless the snippets reach the end. 303 // Add "..." before and after, unless the snippets reach the end.
304 String leftLead = "..."; 304 String leftLead = "...";
305 String rightTail = "..."; 305 String rightTail = "...";
306 if (left <= 10) leftLead = ""; 306 if (left <= 10) leftLead = "";
307 if (right <= 10) rightTail = ""; 307 if (right <= 10) rightTail = "";
308 308
309 String diff = '\nDiff ($left..${eLen - right}/${aLen - right}):\n' 309 String diff = '\nDiff ($left..${eLen - right}/${aLen - right}):\n'
310 '$leftLead$leftSnippet[ $eSnippet ]$rightSnippet$rightTail\n' 310 '$leftLead$leftSnippet[ $eSnippet ]$rightSnippet$rightTail\n'
311 '$leftLead$leftSnippet[ $aSnippet ]$rightSnippet$rightTail'; 311 '$leftLead$leftSnippet[ $aSnippet ]$rightSnippet$rightTail';
312 _fail("$defaultMessage$diff"); 312 _fail("$defaultMessage$diff");
313 } 313 }
314 314
315 /** 315 /**
316 * Checks that every element of [expected] is also in [actual], and that 316 * Checks that every element of [expected] is also in [actual], and that
317 * every element of [actual] is also in [expected]. 317 * every element of [actual] is also in [expected].
318 */ 318 */
319 static void setEquals(Iterable expected, 319 static void setEquals(Iterable expected, Iterable actual,
320 Iterable actual, 320 [String reason = null]) {
321 [String reason = null]) {
322 final missingSet = new Set.from(expected); 321 final missingSet = new Set.from(expected);
323 missingSet.removeAll(actual); 322 missingSet.removeAll(actual);
324 final extraSet = new Set.from(actual); 323 final extraSet = new Set.from(actual);
325 extraSet.removeAll(expected); 324 extraSet.removeAll(expected);
326 325
327 if (extraSet.isEmpty && missingSet.isEmpty) return; 326 if (extraSet.isEmpty && missingSet.isEmpty) return;
328 String msg = _getMessage(reason); 327 String msg = _getMessage(reason);
329 328
330 StringBuffer sb = new StringBuffer("Expect.setEquals($msg) fails"); 329 StringBuffer sb = new StringBuffer("Expect.setEquals($msg) fails");
331 // Report any missing items. 330 // Report any missing items.
(...skipping 10 matching lines...) Expand all
342 sb.write('\nExpected collection should not contain: '); 341 sb.write('\nExpected collection should not contain: ');
343 } 342 }
344 343
345 for (final val in extraSet) { 344 for (final val in extraSet) {
346 sb.write('$val '); 345 sb.write('$val ');
347 } 346 }
348 _fail(sb.toString()); 347 _fail(sb.toString());
349 } 348 }
350 349
351 /** 350 /**
351 * Checks that [expected] is equivalent to [actual].
352 *
353 * If the objects are iterables or maps, recurses into them.
354 */
355 static void deepEquals(Object expected, Object actual) {
356 // Early exit check for equality.
357 if (expected == actual) return;
358
359 if (expected is String && actual is String) {
360 stringEquals(expected, actual);
361 } else if (expected is Iterable && actual is Iterable) {
362 var expectedLength = expected.length;
363 var actualLength = actual.length;
364
365 var length =
366 expectedLength < actualLength ? expectedLength : actualLength;
367 for (var i = 0; i < length; i++) {
368 deepEquals(expected.elementAt(i), actual.elementAt(i));
369 }
370
371 // We check on length at the end in order to provide better error
372 // messages when an unexpected item is inserted in a list.
373 if (expectedLength != actualLength) {
374 var nextElement =
375 (expectedLength > length ? expected : actual).elementAt(length);
376 _fail('Expect.deepEquals(list length, '
377 'expected: <$expectedLength>, actual: <$actualLength>) '
378 'fails: Next element <$nextElement>');
379 }
380 } else if (expected is Map && actual is Map) {
381 // Make sure all of the values are present in both and match.
382 for (final key in expected.keys) {
383 if (!actual.containsKey(key)) {
384 _fail('Expect.deepEquals(missing expected key: <$key>) fails');
385 }
386
387 Expect.deepEquals(expected[key], actual[key]);
388 }
389
390 // Make sure the actual map doesn't have any extra keys.
391 for (final key in actual.keys) {
392 if (!expected.containsKey(key)) {
393 _fail('Expect.deepEquals(unexpected key: <$key>) fails');
394 }
395 }
396 } else {
397 _fail("Expect.deepEquals(expected: <$expected>, actual: <$actual>) "
398 "fails.");
399 }
400 }
401
402 /**
352 * Calls the function [f] and verifies that it throws an exception. 403 * Calls the function [f] and verifies that it throws an exception.
353 * The optional [check] function can provide additional validation 404 * The optional [check] function can provide additional validation
354 * that the correct exception is being thrown. For example, to check 405 * that the correct exception is being thrown. For example, to check
355 * the type of the exception you could write this: 406 * the type of the exception you could write this:
356 * 407 *
357 * Expect.throws(myThrowingFunction, (e) => e is MyException); 408 * Expect.throws(myThrowingFunction, (e) => e is MyException);
358 */ 409 */
359 static void throws(void f(), 410 static void throws(void f(),
360 [bool check(exception) = null, 411 [_CheckExceptionFn check = null, String reason = null]) {
361 String reason = null]) {
362 String msg = reason == null ? "" : "($reason)"; 412 String msg = reason == null ? "" : "($reason)";
363 if (f is! _Nullary) { 413 if (f is! _Nullary) {
364 // Only throws from executing the funtion body should count as throwing. 414 // Only throws from executing the funtion body should count as throwing.
365 // The failure to even call `f` should throw outside the try/catch. 415 // The failure to even call `f` should throw outside the try/catch.
366 _fail("Expect.throws$msg: Function f not callable with zero arguments"); 416 _fail("Expect.throws$msg: Function f not callable with zero arguments");
367 } 417 }
368 try { 418 try {
369 f(); 419 f();
370 } catch (e, s) { 420 } catch (e, s) {
371 if (check != null) { 421 if (check != null) {
372 if (!check(e)) { 422 if (!check(e)) {
373 _fail("Expect.throws$msg: Unexpected '$e'\n$s"); 423 _fail("Expect.throws$msg: Unexpected '$e'\n$s");
374 } 424 }
375 } 425 }
376 return; 426 return;
377 } 427 }
378 _fail('Expect.throws$msg fails: Did not throw'); 428 _fail('Expect.throws$msg fails: Did not throw');
379 } 429 }
380 430
381 static String _getMessage(String reason) 431 static String _getMessage(String reason) =>
382 => (reason == null) ? "" : ", '$reason'"; 432 (reason == null) ? "" : ", '$reason'";
383 433
384 static void _fail(String message) { 434 static void _fail(String message) {
385 throw new ExpectException(message); 435 throw new ExpectException(message);
386 } 436 }
387 } 437 }
388 438
389 bool _identical(a, b) => identical(a, b); 439 bool _identical(a, b) => identical(a, b);
390 440
391 typedef _Nullary(); // Expect.throws argument must be this type. 441 typedef bool _CheckExceptionFn(exception);
442 typedef _Nullary(); // Expect.throws argument must be this type.
392 443
393 class ExpectException implements Exception { 444 class ExpectException implements Exception {
394 ExpectException(this.message); 445 ExpectException(this.message);
395 String toString() => message; 446 String toString() => message;
396 String message; 447 String message;
397 } 448 }
398 449
399 /// Annotation class for testing of dart2js. Use this as metadata on method 450 /// Annotation class for testing of dart2js. Use this as metadata on method
400 /// declarations to disable inlining of the annotated method. 451 /// declarations to disable inlining of the annotated method.
401 class NoInline { 452 class NoInline {
(...skipping 14 matching lines...) Expand all
416 const TrustTypeAnnotations(); 467 const TrustTypeAnnotations();
417 } 468 }
418 469
419 /// Annotation class for testing of dart2js. Use this as metadata on method 470 /// Annotation class for testing of dart2js. Use this as metadata on method
420 /// declarations to disable closed world assumptions on parameters, effectively 471 /// declarations to disable closed world assumptions on parameters, effectively
421 /// assuming that the runtime arguments could be any value. Note that the 472 /// assuming that the runtime arguments could be any value. Note that the
422 /// constraints due to [TrustTypeAnnotations] still apply. 473 /// constraints due to [TrustTypeAnnotations] still apply.
423 class AssumeDynamic { 474 class AssumeDynamic {
424 const AssumeDynamic(); 475 const AssumeDynamic();
425 } 476 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/tool/build_test_pkgs.sh ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698