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 |
| 11 /** | 11 /** |
| 12 * Expect is used for tests that do not want to make use of the | 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. | 13 * Dart unit test library - for example, the core language tests. |
| 14 * Third parties are discouraged from using this, and should use | 14 * Third parties are discouraged from using this, and should use |
| 15 * the expect() function in the unit test library instead for | 15 * the expect() function in the unit test library instead for |
| 16 * test assertions. | 16 * test assertions. |
| 17 */ | 17 */ |
| 18 class Expect { | 18 class Expect { |
| 19 static String _truncateString(String string, int start, int end, int length) { | |
|
floitsch
2013/05/30 12:13:48
make this a separate CL?
floitsch
2013/05/30 12:13:48
comment.
Lasse Reichstein Nielsen
2013/05/31 05:51:59
Done.
Lasse Reichstein Nielsen
2013/05/31 05:51:59
Rather not. It's just a tweak that happened as par
| |
| 20 if (end - start > length) { | |
| 21 end = start + length; | |
| 22 } else if (end - start < length) { | |
| 23 int overflow = length - (end - start); | |
| 24 if (overflow > 10) overflow = 10; | |
| 25 // Add context. | |
| 26 start = start - ((overflow + 1) >> 1); | |
|
floitsch
2013/05/30 12:13:48
~/ 2.
let the VM optimize this.
Lasse Reichstein Nielsen
2013/05/31 05:51:59
Done.
Lasse Reichstein Nielsen
2013/05/31 05:51:59
Done.
| |
| 27 end = end + (overflow >> 1); | |
| 28 if (start < 0) start = 0; | |
| 29 if (end > string.length) end = string.length; | |
| 30 } | |
| 31 if (start == 0 && end == string.length) return string; | |
| 32 StringBuffer buf = new StringBuffer(); | |
| 33 if (start > 0) buf.write("..."); | |
| 34 for (int i = start; i < end; i++) { | |
| 35 int code = string.codeUnitAt(i); | |
| 36 if (code < 0x20) { | |
| 37 buf.write(r"\x"); | |
| 38 buf.write("0123456789abcdef"[code >> 4]); | |
|
floitsch
2013/05/30 12:13:48
~/ 16
Lasse Reichstein Nielsen
2013/05/31 05:51:59
Done.
| |
| 39 buf.write("0123456789abcdef"[code & 0x0f]); | |
|
floitsch
2013/05/30 12:13:48
% 16
Lasse Reichstein Nielsen
2013/05/31 05:51:59
Done.
| |
| 40 } else { | |
| 41 buf.writeCharCode(string.codeUnitAt(i)); | |
| 42 } | |
| 43 } | |
| 44 if (end < string.length) buf.write("..."); | |
| 45 return buf.toString(); | |
| 46 } | |
| 47 | |
| 48 static String _stringDifference(String expected, String actual) { | |
| 49 if (expected.length < 20 && actual.length < 20) return null; | |
| 50 for (int i = 0; i < expected.length && i < actual.length; i++) { | |
| 51 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) { | |
| 52 int start = i; | |
| 53 i++; | |
| 54 while (i < expected.length && i < actual.length) { | |
| 55 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break; | |
| 56 } | |
| 57 int end = i; | |
| 58 var truncExpected = _truncateString(expected, start, end, 20); | |
| 59 var truncActual = _truncateString(actual, start, end, 20); | |
| 60 return "at index $start: Expected <$truncExpected>, " | |
| 61 "Found: <$truncActual>"; | |
| 62 } | |
| 63 } | |
| 64 return null; | |
| 65 } | |
| 66 | |
| 19 /** | 67 /** |
| 20 * Checks whether the expected and actual values are equal (using `==`). | 68 * Checks whether the expected and actual values are equal (using `==`). |
| 21 */ | 69 */ |
| 22 static void equals(var expected, var actual, [String reason = null]) { | 70 static void equals(var expected, var actual, [String reason = null]) { |
| 23 if (expected == actual) return; | 71 if (expected == actual) return; |
| 24 String msg = _getMessage(reason); | 72 String msg = _getMessage(reason); |
| 73 stringSpecialCase: | |
| 74 if (expected is String && actual is String) { | |
| 75 String stringDifference = _stringDifference(expected, actual); | |
| 76 if (stringDifference != null) { | |
| 77 _fail("Expect.equals($stringDifference$msg) fails."); | |
| 78 } | |
| 79 } | |
| 25 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); | 80 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); |
| 26 } | 81 } |
| 27 | 82 |
| 28 /** | 83 /** |
| 29 * Checks whether the actual value is a bool and its value is true. | 84 * Checks whether the actual value is a bool and its value is true. |
| 30 */ | 85 */ |
| 31 static void isTrue(var actual, [String reason = null]) { | 86 static void isTrue(var actual, [String reason = null]) { |
| 32 if (_identical(actual, true)) return; | 87 if (_identical(actual, true)) return; |
| 33 String msg = _getMessage(reason); | 88 String msg = _getMessage(reason); |
| 34 _fail("Expect.isTrue($actual$msg) fails."); | 89 _fail("Expect.isTrue($actual$msg) fails."); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 | 349 |
| 295 bool _identical(a, b) => identical(a, b); | 350 bool _identical(a, b) => identical(a, b); |
| 296 | 351 |
| 297 typedef bool _CheckExceptionFn(exception); | 352 typedef bool _CheckExceptionFn(exception); |
| 298 | 353 |
| 299 class ExpectException implements Exception { | 354 class ExpectException implements Exception { |
| 300 ExpectException(this.message); | 355 ExpectException(this.message); |
| 301 String toString() => message; | 356 String toString() => message; |
| 302 String message; | 357 String message; |
| 303 } | 358 } |
| OLD | NEW |