Chromium Code Reviews| 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 220 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 long, elide the middle. |
|
ricow1
2014/11/11 09:10:39
if snippets long -> If snippets are long?
| |
| 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 String leftLead = "..."; | |
| 304 String rightTail = "..."; | |
| 305 if (left <= 10) leftLead = ""; | |
| 306 if (right <= 10) rightTail = ""; | |
| 307 String diff = '\nDiff ($left..${eLen - right}/${aLen - right}):\n' | |
| 308 '$leftLead$leftSnippet[ $eSnippet ]$rightSnippet$rightTail\n' | |
| 309 '$leftLead$leftSnippet[ $aSnippet ]$rightSnippet$rightTail'; | |
| 310 _fail("$defaultMessage$diff"); | |
| 300 } | 311 } |
| 301 | 312 |
| 302 /** | 313 /** |
| 303 * Checks that every element of [expected] is also in [actual], and that | 314 * Checks that every element of [expected] is also in [actual], and that |
| 304 * every element of [actual] is also in [expected]. | 315 * every element of [actual] is also in [expected]. |
| 305 */ | 316 */ |
| 306 static void setEquals(Iterable expected, | 317 static void setEquals(Iterable expected, |
| 307 Iterable actual, | 318 Iterable actual, |
| 308 [String reason = null]) { | 319 [String reason = null]) { |
| 309 final missingSet = new Set.from(expected); | 320 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); | 387 bool _identical(a, b) => identical(a, b); |
| 377 | 388 |
| 378 typedef bool _CheckExceptionFn(exception); | 389 typedef bool _CheckExceptionFn(exception); |
| 379 typedef _Nullary(); // Expect.throws argument must be this type. | 390 typedef _Nullary(); // Expect.throws argument must be this type. |
| 380 | 391 |
| 381 class ExpectException implements Exception { | 392 class ExpectException implements Exception { |
| 382 ExpectException(this.message); | 393 ExpectException(this.message); |
| 383 String toString() => message; | 394 String toString() => message; |
| 384 String message; | 395 String message; |
| 385 } | 396 } |
| OLD | NEW |