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

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

Issue 16125005: Make new StreamController be async by default. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/http/lib/src/multipart_request.dart » ('j') | 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
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 /** 19 /**
20 * Return a slice of a string.
21 *
22 * The slice will contain at least the substring from [start] to the lower of
23 * [end] and `start + length`.
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
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
28 * the string, ellipses are added before or after the slice.
29 * Control characters may be encoded as "\xhh" codes.
30 */
31 static String _truncateString(String string, int start, int end, int length) {
32 if (end - start > length) {
33 end = start + length;
34 } else if (end - start < length) {
35 int overflow = length - (end - start);
36 if (overflow > 10) overflow = 10;
37 // Add context.
38 start = start - ((overflow + 1) ~/ 2);
39 end = end + (overflow ~/ 2);
40 if (start < 0) start = 0;
41 if (end > string.length) end = string.length;
42 }
43 if (start == 0 && end == string.length) return string;
44 StringBuffer buf = new StringBuffer();
45 if (start > 0) buf.write("...");
46 for (int i = start; i < end; i++) {
47 int code = string.codeUnitAt(i);
48 if (code < 0x20) {
49 buf.write(r"\x");
50 buf.write("0123456789abcdef"[code ~/ 16]);
51 buf.write("0123456789abcdef"[code % 16]);
52 } else {
53 buf.writeCharCode(string.codeUnitAt(i));
54 }
55 }
56 if (end < string.length) buf.write("...");
57 return buf.toString();
58 }
59
60 /**
61 * Find the difference between two strings.
62 *
63 * This finds the first point where two strings differ, and returns
64 * a text describing the difference.
65 *
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
68 * only a slice containing the first difference will be shown.
69 */
70 static String _stringDifference(String expected, String actual) {
71 if (expected.length < 20 && actual.length < 20) return null;
72 for (int i = 0; i < expected.length && i < actual.length; i++) {
73 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) {
74 int start = i;
75 i++;
76 while (i < expected.length && i < actual.length) {
77 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break;
78 }
79 int end = i;
80 var truncExpected = _truncateString(expected, start, end, 20);
81 var truncActual = _truncateString(actual, start, end, 20);
82 return "at index $start: Expected <$truncExpected>, "
83 "Found: <$truncActual>";
84 }
85 }
86 return null;
87 }
88
89 /**
20 * Checks whether the expected and actual values are equal (using `==`). 90 * Checks whether the expected and actual values are equal (using `==`).
21 */ 91 */
22 static void equals(var expected, var actual, [String reason = null]) { 92 static void equals(var expected, var actual, [String reason = null]) {
23 if (expected == actual) return; 93 if (expected == actual) return;
24 String msg = _getMessage(reason); 94 String msg = _getMessage(reason);
95 stringSpecialCase:
96 if (expected is String && actual is String) {
97 String stringDifference = _stringDifference(expected, actual);
98 if (stringDifference != null) {
99 _fail("Expect.equals($stringDifference$msg) fails.");
100 }
101 }
25 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); 102 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails.");
26 } 103 }
27 104
28 /** 105 /**
29 * Checks whether the actual value is a bool and its value is true. 106 * Checks whether the actual value is a bool and its value is true.
30 */ 107 */
31 static void isTrue(var actual, [String reason = null]) { 108 static void isTrue(var actual, [String reason = null]) {
32 if (_identical(actual, true)) return; 109 if (_identical(actual, true)) return;
33 String msg = _getMessage(reason); 110 String msg = _getMessage(reason);
34 _fail("Expect.isTrue($actual$msg) fails."); 111 _fail("Expect.isTrue($actual$msg) fails.");
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 371
295 bool _identical(a, b) => identical(a, b); 372 bool _identical(a, b) => identical(a, b);
296 373
297 typedef bool _CheckExceptionFn(exception); 374 typedef bool _CheckExceptionFn(exception);
298 375
299 class ExpectException implements Exception { 376 class ExpectException implements Exception {
300 ExpectException(this.message); 377 ExpectException(this.message);
301 String toString() => message; 378 String toString() => message;
302 String message; 379 String message;
303 } 380 }
OLDNEW
« no previous file with comments | « no previous file | pkg/http/lib/src/multipart_request.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698