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 11 matching lines...) Expand all Loading... | |
22 * The slice will contain at least the substring from [start] to the lower of | 22 * The slice will contain at least the substring from [start] to the lower of |
23 * [end] and `start + length`. | 23 * [end] and `start + length`. |
24 * If the result is no more than `length - 10` characters long, | 24 * If the result is no more than `length - 10` characters long, |
25 * context may be added by extending the range of the slice, by decreasing | 25 * context may be added by extending the range of the slice, by decreasing |
26 * [start] and increasing [end], up to at most length characters. | 26 * [start] and increasing [end], up to at most length characters. |
27 * If the start or end of the slice are not matching the start or end of | 27 * If the start or end of the slice are not matching the start or end of |
28 * the string, ellipses are added before or after the slice. | 28 * the string, ellipses are added before or after the slice. |
29 * Control characters may be encoded as "\xhh" codes. | 29 * Control characters may be encoded as "\xhh" codes. |
30 */ | 30 */ |
31 static String _truncateString(String string, int start, int end, int length) { | 31 static String _truncateString(String string, int start, int end, int length) { |
32 print("$string: $start: $end: $length"); | |
kasperl
2013/05/31 08:14:22
Did you really mean to submit this with the printi
kasperl
2013/05/31 08:28:30
Remove printing.
| |
32 if (end - start > length) { | 33 if (end - start > length) { |
33 end = start + length; | 34 end = start + length; |
34 } else if (end - start < length) { | 35 } else if (end - start < length) { |
35 int overflow = length - (end - start); | 36 int overflow = length - (end - start); |
36 if (overflow > 10) overflow = 10; | 37 if (overflow > 10) overflow = 10; |
37 // Add context. | 38 // Add context. |
38 start = start - ((overflow + 1) ~/ 2); | 39 start = start - ((overflow + 1) ~/ 2); |
39 end = end + (overflow ~/ 2); | 40 end = end + (overflow ~/ 2); |
40 if (start < 0) start = 0; | 41 if (start < 0) start = 0; |
41 if (end > string.length) end = string.length; | 42 if (end > string.length) end = string.length; |
(...skipping 16 matching lines...) Expand all Loading... | |
58 } | 59 } |
59 | 60 |
60 /** | 61 /** |
61 * Find the difference between two strings. | 62 * Find the difference between two strings. |
62 * | 63 * |
63 * This finds the first point where two strings differ, and returns | 64 * This finds the first point where two strings differ, and returns |
64 * a text describing the difference. | 65 * a text describing the difference. |
65 * | 66 * |
66 * For small strings (length less than 20) nothing is done, and null is | 67 * 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 | 68 * returned. Small strings can be compared visually, but for longer strings |
68 * only a slice containing the first difference will be shown. | 69 * only a slice containing the first difference will be shown. |
kasperl
2013/05/31 08:28:30
Remove one space after slice.
| |
69 */ | 70 */ |
70 static String _stringDifference(String expected, String actual) { | 71 static String _stringDifference(String expected, String actual) { |
72 print("digg: $expected, $actual"); | |
kasperl
2013/05/31 08:28:30
Remove printing.
| |
71 if (expected.length < 20 && actual.length < 20) return null; | 73 if (expected.length < 20 && actual.length < 20) return null; |
72 for (int i = 0; i < expected.length && i < actual.length; i++) { | 74 for (int i = 0; i < expected.length && i < actual.length; i++) { |
73 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) { | 75 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) { |
74 int start = i; | 76 int start = i; |
75 i++; | 77 i++; |
76 while (i < expected.length && i < actual.length) { | 78 while (i < expected.length && i < actual.length) { |
77 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break; | 79 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break; |
80 i++; | |
78 } | 81 } |
79 int end = i; | 82 int end = i; |
80 var truncExpected = _truncateString(expected, start, end, 20); | 83 var truncExpected = _truncateString(expected, start, end, 20); |
81 var truncActual = _truncateString(actual, start, end, 20); | 84 var truncActual = _truncateString(actual, start, end, 20); |
82 return "at index $start: Expected <$truncExpected>, " | 85 return "at index $start: Expected <$truncExpected>, " |
83 "Found: <$truncActual>"; | 86 "Found: <$truncActual>"; |
84 } | 87 } |
85 } | 88 } |
86 return null; | 89 return null; |
87 } | 90 } |
88 | 91 |
89 /** | 92 /** |
90 * Checks whether the expected and actual values are equal (using `==`). | 93 * Checks whether the expected and actual values are equal (using `==`). |
91 */ | 94 */ |
92 static void equals(var expected, var actual, [String reason = null]) { | 95 static void equals(var expected, var actual, [String reason = null]) { |
93 if (expected == actual) return; | 96 if (expected == actual) return; |
94 String msg = _getMessage(reason); | 97 String msg = _getMessage(reason); |
95 stringSpecialCase: | |
96 if (expected is String && actual is String) { | 98 if (expected is String && actual is String) { |
97 String stringDifference = _stringDifference(expected, actual); | 99 String stringDifference = _stringDifference(expected, actual); |
98 if (stringDifference != null) { | 100 if (stringDifference != null) { |
99 _fail("Expect.equals($stringDifference$msg) fails."); | 101 _fail("Expect.equals($stringDifference$msg) fails."); |
100 } | 102 } |
101 } | 103 } |
102 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); | 104 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); |
103 } | 105 } |
104 | 106 |
105 /** | 107 /** |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 | 373 |
372 bool _identical(a, b) => identical(a, b); | 374 bool _identical(a, b) => identical(a, b); |
373 | 375 |
374 typedef bool _CheckExceptionFn(exception); | 376 typedef bool _CheckExceptionFn(exception); |
375 | 377 |
376 class ExpectException implements Exception { | 378 class ExpectException implements Exception { |
377 ExpectException(this.message); | 379 ExpectException(this.message); |
378 String toString() => message; | 380 String toString() => message; |
379 String message; | 381 String message; |
380 } | 382 } |
OLD | NEW |