| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class Expect { | 5 class Expect { |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Checks whether the expected and actual values are equal (using [:==:]). | 8 * Checks whether the expected and actual values are equal (using [:==:]). |
| 9 */ | 9 */ |
| 10 static void equals(var expected, var actual, [String reason = null]) { | 10 static void equals(var expected, var actual, [String reason = null]) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * Failure if the difference between expected and actual is greater than the | 69 * Failure if the difference between expected and actual is greater than the |
| 70 * given tolerance. If no tolerance is given, tolerance is assumed to be the | 70 * given tolerance. If no tolerance is given, tolerance is assumed to be the |
| 71 * value 4 significant digits smaller than the value given for expected. | 71 * value 4 significant digits smaller than the value given for expected. |
| 72 */ | 72 */ |
| 73 static void approxEquals(num expected, | 73 static void approxEquals(num expected, |
| 74 num actual, | 74 num actual, |
| 75 [num tolerance = null, | 75 [num tolerance = null, |
| 76 String reason = null]) { | 76 String reason = null]) { |
| 77 if (tolerance == null) { | 77 if (tolerance === null) { |
| 78 tolerance = (expected / Math.pow(10.0, 4.0)).abs(); | 78 tolerance = (expected / Math.pow(10.0, 4.0)).abs(); |
| 79 } | 79 } |
| 80 // Note: use !( <= ) rather than > so we fail on NaNs | 80 // Note: use !( <= ) rather than > so we fail on NaNs |
| 81 if ((expected - actual).abs() <= tolerance) return; | 81 if ((expected - actual).abs() <= tolerance) return; |
| 82 | 82 |
| 83 String msg = _getMessage(reason); | 83 String msg = _getMessage(reason); |
| 84 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' + | 84 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' + |
| 85 'tolerance:<$tolerance>$msg) fails'); | 85 'tolerance:<$tolerance>$msg) fails'); |
| 86 } | 86 } |
| 87 | 87 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 if (check != null) { | 235 if (check != null) { |
| 236 Expect.isTrue(check(e)); | 236 Expect.isTrue(check(e)); |
| 237 } | 237 } |
| 238 return; | 238 return; |
| 239 } | 239 } |
| 240 String msg = _getMessage(reason); | 240 String msg = _getMessage(reason); |
| 241 _fail('Expect.throws($msg) fails'); | 241 _fail('Expect.throws($msg) fails'); |
| 242 } | 242 } |
| 243 | 243 |
| 244 static String _getMessage(String reason) | 244 static String _getMessage(String reason) |
| 245 => (reason == null) ? "" : ", '$reason'"; | 245 => (reason === null) ? "" : ", '$reason'"; |
| 246 | 246 |
| 247 static void _fail(String message) { | 247 static void _fail(String message) { |
| 248 throw new ExpectException(message); | 248 throw new ExpectException(message); |
| 249 } | 249 } |
| 250 } | 250 } |
| 251 | 251 |
| 252 typedef bool _CheckExceptionFn(exception); | 252 typedef bool _CheckExceptionFn(exception); |
| 253 | 253 |
| 254 class ExpectException implements Exception { | 254 class ExpectException implements Exception { |
| 255 ExpectException(this.message); | 255 ExpectException(this.message); |
| 256 String toString() => message; | 256 String toString() => message; |
| 257 String message; | 257 String message; |
| 258 } | 258 } |
| OLD | NEW |