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

Side by Side Diff: sdk/lib/core/expect.dart

Issue 11361190: a === b -> identical(a, b) (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 1 month 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 | « sdk/lib/core/errors.dart ('k') | sdk/lib/core/map.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 * Expect is used for tests that do not want to make use of the 6 * Expect is used for tests that do not want to make use of the
7 * Dart unit test library - for example, the core language tests. 7 * Dart unit test library - for example, the core language tests.
8 * Third parties are discouraged from using this, and should use 8 * Third parties are discouraged from using this, and should use
9 * the expect() function in the unit test library instead for 9 * the expect() function in the unit test library instead for
10 * test assertions. 10 * test assertions.
11 */ 11 */
12 class Expect { 12 class Expect {
13 /** 13 /**
14 * Checks whether the expected and actual values are equal (using `==`). 14 * Checks whether the expected and actual values are equal (using `==`).
15 */ 15 */
16 static void equals(var expected, var actual, [String reason = null]) { 16 static void equals(var expected, var actual, [String reason = null]) {
17 if (expected == actual) return; 17 if (expected == actual) return;
18 String msg = _getMessage(reason); 18 String msg = _getMessage(reason);
19 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); 19 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails.");
20 } 20 }
21 21
22 /** 22 /**
23 * Checks whether the actual value is a bool and its value is true. 23 * Checks whether the actual value is a bool and its value is true.
24 */ 24 */
25 static void isTrue(var actual, [String reason = null]) { 25 static void isTrue(var actual, [String reason = null]) {
26 if (actual === true) return; 26 if (_identical(actual, true)) return;
27 String msg = _getMessage(reason); 27 String msg = _getMessage(reason);
28 _fail("Expect.isTrue($actual$msg) fails."); 28 _fail("Expect.isTrue($actual$msg) fails.");
29 } 29 }
30 30
31 /** 31 /**
32 * Checks whether the actual value is a bool and its value is false. 32 * Checks whether the actual value is a bool and its value is false.
33 */ 33 */
34 static void isFalse(var actual, [String reason = null]) { 34 static void isFalse(var actual, [String reason = null]) {
35 if (actual === false) return; 35 if (_identical(actual, false)) return;
36 String msg = _getMessage(reason); 36 String msg = _getMessage(reason);
37 _fail("Expect.isFalse($actual$msg) fails."); 37 _fail("Expect.isFalse($actual$msg) fails.");
38 } 38 }
39 39
40 /** 40 /**
41 * Checks whether [actual] is null. 41 * Checks whether [actual] is null.
42 */ 42 */
43 static void isNull(actual, [String reason = null]) { 43 static void isNull(actual, [String reason = null]) {
44 if (null === actual) return; 44 if (null == actual) return;
45 String msg = _getMessage(reason); 45 String msg = _getMessage(reason);
46 _fail("Expect.isNull(actual: <$actual>$msg) fails."); 46 _fail("Expect.isNull(actual: <$actual>$msg) fails.");
47 } 47 }
48 48
49 /** 49 /**
50 * Checks whether [actual] is not null. 50 * Checks whether [actual] is not null.
51 */ 51 */
52 static void isNotNull(actual, [String reason = null]) { 52 static void isNotNull(actual, [String reason = null]) {
53 if (null !== actual) return; 53 if (null != actual) return;
54 String msg = _getMessage(reason); 54 String msg = _getMessage(reason);
55 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); 55 _fail("Expect.isNotNull(actual: <$actual>$msg) fails.");
56 } 56 }
57 57
58 /** 58 /**
59 * Checks whether the expected and actual values are identical 59 * Checks whether the expected and actual values are identical
60 * (using `===`). 60 * (using `===`).
61 */ 61 */
62 static void identical(var expected, var actual, [String reason = null]) { 62 static void identical(var expected, var actual, [String reason = null]) {
63 if (expected === actual) return; 63 if (_identical(expected, actual)) return;
64 String msg = _getMessage(reason); 64 String msg = _getMessage(reason);
65 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " 65 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) "
66 "fails."); 66 "fails.");
67 } 67 }
68 68
69 // Unconditional failure. 69 // Unconditional failure.
70 static void fail(String msg) { 70 static void fail(String msg) {
71 _fail("Expect.fail('$msg')"); 71 _fail("Expect.fail('$msg')");
72 } 72 }
73 73
74 /** 74 /**
75 * Failure if the difference between expected and actual is greater than the 75 * Failure if the difference between expected and actual is greater than the
76 * given tolerance. If no tolerance is given, tolerance is assumed to be the 76 * given tolerance. If no tolerance is given, tolerance is assumed to be the
77 * value 4 significant digits smaller than the value given for expected. 77 * value 4 significant digits smaller than the value given for expected.
78 */ 78 */
79 static void approxEquals(num expected, 79 static void approxEquals(num expected,
80 num actual, 80 num actual,
81 [num tolerance = null, 81 [num tolerance = null,
82 String reason = null]) { 82 String reason = null]) {
83 if (tolerance === null) { 83 if (tolerance == null) {
84 tolerance = (expected / 1e4).abs(); 84 tolerance = (expected / 1e4).abs();
85 } 85 }
86 // Note: use !( <= ) rather than > so we fail on NaNs 86 // Note: use !( <= ) rather than > so we fail on NaNs
87 if ((expected - actual).abs() <= tolerance) return; 87 if ((expected - actual).abs() <= tolerance) return;
88 88
89 String msg = _getMessage(reason); 89 String msg = _getMessage(reason);
90 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' 90 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, '
91 'tolerance:<$tolerance>$msg) fails'); 91 'tolerance:<$tolerance>$msg) fails');
92 } 92 }
93 93
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 * this method shows where the mismatch starts and ends. 152 * this method shows where the mismatch starts and ends.
153 */ 153 */
154 static void stringEquals(String expected, 154 static void stringEquals(String expected,
155 String actual, 155 String actual,
156 [String reason = null]) { 156 [String reason = null]) {
157 String msg = _getMessage(reason); 157 String msg = _getMessage(reason);
158 String defaultMessage = 158 String defaultMessage =
159 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; 159 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails';
160 160
161 if (expected == actual) return; 161 if (expected == actual) return;
162 if ((expected === null) || (actual === null)) { 162 if ((expected == null) || (actual == null)) {
163 _fail('$defaultMessage'); 163 _fail('$defaultMessage');
164 } 164 }
165 // scan from the left until we find a mismatch 165 // scan from the left until we find a mismatch
166 int left = 0; 166 int left = 0;
167 int eLen = expected.length; 167 int eLen = expected.length;
168 int aLen = actual.length; 168 int aLen = actual.length;
169 while (true) { 169 while (true) {
170 if (left == eLen) { 170 if (left == eLen) {
171 assert (left < aLen); 171 assert (left < aLen);
172 String snippet = actual.substring(left, aLen); 172 String snippet = actual.substring(left, aLen);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 * the type of the exception you could write this: 258 * the type of the exception you could write this:
259 * 259 *
260 * Expect.throws(myThrowingFunction, (e) => e is MyException); 260 * Expect.throws(myThrowingFunction, (e) => e is MyException);
261 */ 261 */
262 static void throws(void f(), 262 static void throws(void f(),
263 [_CheckExceptionFn check = null, 263 [_CheckExceptionFn check = null,
264 String reason = null]) { 264 String reason = null]) {
265 try { 265 try {
266 f(); 266 f();
267 } catch (e, s) { 267 } catch (e, s) {
268 if (check !== null) { 268 if (check != null) {
269 if (!check(e)) { 269 if (!check(e)) {
270 String msg = reason == null ? "" : reason; 270 String msg = reason == null ? "" : reason;
271 _fail("Expect.throws($msg): Unexpected '$e'\n$s"); 271 _fail("Expect.throws($msg): Unexpected '$e'\n$s");
272 } 272 }
273 } 273 }
274 return; 274 return;
275 } 275 }
276 String msg = reason == null ? "" : reason; 276 String msg = reason == null ? "" : reason;
277 _fail('Expect.throws($msg) fails'); 277 _fail('Expect.throws($msg) fails');
278 } 278 }
279 279
280 static String _getMessage(String reason) 280 static String _getMessage(String reason)
281 => (reason === null) ? "" : ", '$reason'"; 281 => (reason == null) ? "" : ", '$reason'";
282 282
283 static void _fail(String message) { 283 static void _fail(String message) {
284 throw new ExpectException(message); 284 throw new ExpectException(message);
285 } 285 }
286 } 286 }
287 287
288 bool _identical(a, b) => identical(a, b);
289
288 typedef bool _CheckExceptionFn(exception); 290 typedef bool _CheckExceptionFn(exception);
289 291
290 class ExpectException implements Exception { 292 class ExpectException implements Exception {
291 ExpectException(this.message); 293 ExpectException(this.message);
292 String toString() => message; 294 String toString() => message;
293 String message; 295 String message;
294 } 296 }
OLDNEW
« no previous file with comments | « sdk/lib/core/errors.dart ('k') | sdk/lib/core/map.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698