OLD | NEW |
1 // Copyright (c) 2011, 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 */ |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 /** | 75 /** |
76 * Failure if the difference between expected and actual is greater than the | 76 * Failure if the difference between expected and actual is greater than the |
77 * given tolerance. If no tolerance is given, tolerance is assumed to be the | 77 * given tolerance. If no tolerance is given, tolerance is assumed to be the |
78 * value 4 significant digits smaller than the value given for expected. | 78 * value 4 significant digits smaller than the value given for expected. |
79 */ | 79 */ |
80 static void approxEquals(num expected, | 80 static void approxEquals(num expected, |
81 num actual, | 81 num actual, |
82 [num tolerance = null, | 82 [num tolerance = null, |
83 String reason = null]) { | 83 String reason = null]) { |
84 if (tolerance === null) { | 84 if (tolerance === null) { |
85 tolerance = (expected / Math.pow(10.0, 4.0)).abs(); | 85 tolerance = (expected / pow(10.0, 4.0)).abs(); |
86 } | 86 } |
87 // Note: use !( <= ) rather than > so we fail on NaNs | 87 // Note: use !( <= ) rather than > so we fail on NaNs |
88 if ((expected - actual).abs() <= tolerance) return; | 88 if ((expected - actual).abs() <= tolerance) return; |
89 | 89 |
90 String msg = _getMessage(reason); | 90 String msg = _getMessage(reason); |
91 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' | 91 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' |
92 'tolerance:<$tolerance>$msg) fails'); | 92 'tolerance:<$tolerance>$msg) fails'); |
93 } | 93 } |
94 | 94 |
95 static void notEquals(unexpected, actual, [String reason = null]) { | 95 static void notEquals(unexpected, actual, [String reason = null]) { |
96 if (unexpected != actual) return; | 96 if (unexpected != actual) return; |
97 String msg = _getMessage(reason); | 97 String msg = _getMessage(reason); |
98 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " | 98 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " |
99 "fails."); | 99 "fails."); |
100 } | 100 } |
101 | 101 |
102 /** | 102 /** |
103 * Checks that all elements in [expected] and [actual] are equal `==`. | 103 * Checks that all elements in [expected] and [actual] are equal `==`. |
104 * This is different than the typical check for identity equality `===` | 104 * This is different than the typical check for identity equality `===` |
105 * used by the standard list implementation. It should also produce nicer | 105 * used by the standard list implementation. It should also produce nicer |
106 * error messages than just calling `Expect.equals(expected, actual)`. | 106 * error messages than just calling `Expect.equals(expected, actual)`. |
107 */ | 107 */ |
108 static void listEquals(List expected, List actual, [String reason = null]) { | 108 static void listEquals(List expected, List actual, [String reason = null]) { |
109 String msg = _getMessage(reason); | 109 String msg = _getMessage(reason); |
110 int n = Math.min(expected.length, actual.length); | 110 int n = min(expected.length, actual.length); |
111 for (int i = 0; i < n; i++) { | 111 for (int i = 0; i < n; i++) { |
112 if (expected[i] != actual[i]) { | 112 if (expected[i] != actual[i]) { |
113 _fail('Expect.listEquals(at index $i, ' | 113 _fail('Expect.listEquals(at index $i, ' |
114 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); | 114 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); |
115 } | 115 } |
116 } | 116 } |
117 // We check on length at the end in order to provide better error | 117 // We check on length at the end in order to provide better error |
118 // messages when an unexpected item is inserted in a list. | 118 // messages when an unexpected item is inserted in a list. |
119 if (expected.length != actual.length) { | 119 if (expected.length != actual.length) { |
120 _fail('Expect.listEquals(list length, ' | 120 _fail('Expect.listEquals(list length, ' |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 typedef bool _CheckExceptionFn(exception); | 286 typedef bool _CheckExceptionFn(exception); |
287 | 287 |
288 class ExpectException implements Exception { | 288 class ExpectException implements Exception { |
289 ExpectException(this.message); | 289 ExpectException(this.message); |
290 String toString() => message; | 290 String toString() => message; |
291 String message; | 291 String message; |
292 } | 292 } |
OLD | NEW |