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 /** | 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 /** | 60 /** |
61 * Find the difference between two strings. | 61 * Find the difference between two strings. |
62 * | 62 * |
63 * This finds the first point where two strings differ, and returns | 63 * This finds the first point where two strings differ, and returns |
64 * a text describing the difference. | 64 * a text describing the difference. |
65 * | 65 * |
66 * For small strings (length less than 20) nothing is done, and null is | 66 * For small strings (length less than 20) nothing is done, and null is |
67 * returned. Small strings can be compared visually, but for longer strings | 67 * returned. Small strings can be compared visually, but for longer strings |
68 * only a slice containing the first difference will be shown. | 68 * only a slice containing the first difference will be shown. |
69 */ | 69 */ |
70 static String _stringDifference(String expected, String actual) { | 70 static String _stringDifference(String expected, String actual) { |
71 if (expected.length < 20 && actual.length < 20) return null; | 71 if (expected.length < 20 && actual.length < 20) return null; |
72 for (int i = 0; i < expected.length && i < actual.length; i++) { | 72 for (int i = 0; i < expected.length && i < actual.length; i++) { |
73 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) { | 73 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) { |
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++; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 } | 231 } |
232 } | 232 } |
233 | 233 |
234 /** | 234 /** |
235 * Specialized equality test for strings. When the strings don't match, | 235 * Specialized equality test for strings. When the strings don't match, |
236 * this method shows where the mismatch starts and ends. | 236 * this method shows where the mismatch starts and ends. |
237 */ | 237 */ |
238 static void stringEquals(String expected, | 238 static void stringEquals(String expected, |
239 String actual, | 239 String actual, |
240 [String reason = null]) { | 240 [String reason = null]) { |
| 241 if (expected == actual) return; |
| 242 |
241 String msg = _getMessage(reason); | 243 String msg = _getMessage(reason); |
242 String defaultMessage = | 244 String defaultMessage = |
243 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; | 245 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; |
244 | 246 |
245 if (expected == actual) return; | |
246 if ((expected == null) || (actual == null)) { | 247 if ((expected == null) || (actual == null)) { |
247 _fail('$defaultMessage'); | 248 _fail('$defaultMessage'); |
248 } | 249 } |
249 // scan from the left until we find a mismatch | 250 |
| 251 // Scan from the left until we find the mismatch. |
250 int left = 0; | 252 int left = 0; |
| 253 int right = 0; |
251 int eLen = expected.length; | 254 int eLen = expected.length; |
252 int aLen = actual.length; | 255 int aLen = actual.length; |
| 256 |
253 while (true) { | 257 while (true) { |
254 if (left == eLen) { | 258 if (left == eLen || left == aLen || expected[left] != actual[left]) { |
255 assert (left < aLen); | |
256 String snippet = actual.substring(left, aLen); | |
257 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); | |
258 return; | |
259 } | |
260 if (left == aLen) { | |
261 assert (left < eLen); | |
262 String snippet = expected.substring(left, eLen); | |
263 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); | |
264 return; | |
265 } | |
266 if (expected[left] != actual[left]) { | |
267 break; | 259 break; |
268 } | 260 } |
269 left++; | 261 left++; |
270 } | 262 } |
271 | 263 |
272 // scan from the right until we find a mismatch | 264 // Scan from the right until we find the mismatch. |
273 int right = 0; | 265 int eRem = eLen - left; // Remaining length ignoring left match. |
| 266 int aRem = aLen - left; |
274 while (true) { | 267 while (true) { |
275 if (right == eLen) { | 268 if (right == eRem || right == aRem || |
276 assert (right < aLen); | 269 expected[eLen - right - 1] != actual[aLen - right - 1]) { |
277 String snippet = actual.substring(0, aLen - right); | |
278 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); | |
279 return; | |
280 } | |
281 if (right == aLen) { | |
282 assert (right < eLen); | |
283 String snippet = expected.substring(0, eLen - right); | |
284 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); | |
285 return; | |
286 } | |
287 // stop scanning if we've reached the end of the left-to-right match | |
288 if (eLen - right <= left || aLen - right <= left) { | |
289 break; | |
290 } | |
291 if (expected[eLen - right - 1] != actual[aLen - right - 1]) { | |
292 break; | 270 break; |
293 } | 271 } |
294 right++; | 272 right++; |
295 } | 273 } |
| 274 |
| 275 // First difference is at index `left`, last at `length - right - 1` |
| 276 // Make useful difference message. |
| 277 // Example: |
| 278 // Diff (1209..1209/1246): |
| 279 // ...,{"name":"[ ]FallThroug... |
| 280 // ...,{"name":"[ IndexError","kind":"class"},{"name":" ]FallThroug... |
| 281 // (colors would be great!) |
| 282 |
| 283 // Make snippets of up to ten characters before and after differences. |
| 284 |
| 285 String leftSnippet = expected.substring(left < 10 ? 0 : left - 10, left); |
| 286 int rightSnippetLength = right < 10 ? right : 10; |
| 287 String rightSnippet = |
| 288 expected.substring(eLen - right, eLen - right + rightSnippetLength); |
| 289 |
| 290 // Make snippets of the differences. |
296 String eSnippet = expected.substring(left, eLen - right); | 291 String eSnippet = expected.substring(left, eLen - right); |
297 String aSnippet = actual.substring(left, aLen - right); | 292 String aSnippet = actual.substring(left, aLen - right); |
298 String diff = '\nDiff:\n...[ $eSnippet ]...\n...[ $aSnippet ]...'; | 293 |
299 _fail('$defaultMessage$diff'); | 294 // If snippets are long, elide the middle. |
| 295 if (eSnippet.length > 43) { |
| 296 eSnippet = eSnippet.substring(0, 20) + "..." + |
| 297 eSnippet.substring(eSnippet.length - 20); |
| 298 } |
| 299 if (aSnippet.length > 43) { |
| 300 aSnippet = aSnippet.substring(0, 20) + "..." + |
| 301 aSnippet.substring(aSnippet.length - 20); |
| 302 } |
| 303 // Add "..." before and after, unless the snippets reach the end. |
| 304 String leftLead = "..."; |
| 305 String rightTail = "..."; |
| 306 if (left <= 10) leftLead = ""; |
| 307 if (right <= 10) rightTail = ""; |
| 308 |
| 309 String diff = '\nDiff ($left..${eLen - right}/${aLen - right}):\n' |
| 310 '$leftLead$leftSnippet[ $eSnippet ]$rightSnippet$rightTail\n' |
| 311 '$leftLead$leftSnippet[ $aSnippet ]$rightSnippet$rightTail'; |
| 312 _fail("$defaultMessage$diff"); |
300 } | 313 } |
301 | 314 |
302 /** | 315 /** |
303 * 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 |
304 * every element of [actual] is also in [expected]. | 317 * every element of [actual] is also in [expected]. |
305 */ | 318 */ |
306 static void setEquals(Iterable expected, | 319 static void setEquals(Iterable expected, |
307 Iterable actual, | 320 Iterable actual, |
308 [String reason = null]) { | 321 [String reason = null]) { |
309 final missingSet = new Set.from(expected); | 322 final missingSet = new Set.from(expected); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 bool _identical(a, b) => identical(a, b); | 389 bool _identical(a, b) => identical(a, b); |
377 | 390 |
378 typedef bool _CheckExceptionFn(exception); | 391 typedef bool _CheckExceptionFn(exception); |
379 typedef _Nullary(); // Expect.throws argument must be this type. | 392 typedef _Nullary(); // Expect.throws argument must be this type. |
380 | 393 |
381 class ExpectException implements Exception { | 394 class ExpectException implements Exception { |
382 ExpectException(this.message); | 395 ExpectException(this.message); |
383 String toString() => message; | 396 String toString() => message; |
384 String message; | 397 String message; |
385 } | 398 } |
OLD | NEW |