| 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 part of dart.core; | 5 /** |
| 6 * This library contains an Expect class with static methods that can be used |
| 7 * for simple unit-tests. |
| 8 */ |
| 9 library expect; |
| 6 | 10 |
| 7 /** | 11 /** |
| 8 * This class is *deprecated*. | |
| 9 * | |
| 10 * 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 |
| 11 * Dart unit test library - for example, the core language tests. | 13 * Dart unit test library - for example, the core language tests. |
| 12 * Third parties are discouraged from using this, and should use | 14 * Third parties are discouraged from using this, and should use |
| 13 * the expect() function in the unit test library instead for | 15 * the expect() function in the unit test library instead for |
| 14 * test assertions. | 16 * test assertions. |
| 15 */ | 17 */ |
| 16 @deprecated | |
| 17 class Expect { | 18 class Expect { |
| 18 /** | 19 /** |
| 19 * Checks whether the expected and actual values are equal (using `==`). | 20 * Checks whether the expected and actual values are equal (using `==`). |
| 20 */ | 21 */ |
| 21 @deprecated | |
| 22 static void equals(var expected, var actual, [String reason = null]) { | 22 static void equals(var expected, var actual, [String reason = null]) { |
| 23 if (expected == actual) return; | 23 if (expected == actual) return; |
| 24 String msg = _getMessage(reason); | 24 String msg = _getMessage(reason); |
| 25 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); | 25 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); |
| 26 } | 26 } |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Checks whether the actual value is a bool and its value is true. | 29 * Checks whether the actual value is a bool and its value is true. |
| 30 */ | 30 */ |
| 31 @deprecated | |
| 32 static void isTrue(var actual, [String reason = null]) { | 31 static void isTrue(var actual, [String reason = null]) { |
| 33 if (_identical(actual, true)) return; | 32 if (_identical(actual, true)) return; |
| 34 String msg = _getMessage(reason); | 33 String msg = _getMessage(reason); |
| 35 _fail("Expect.isTrue($actual$msg) fails."); | 34 _fail("Expect.isTrue($actual$msg) fails."); |
| 36 } | 35 } |
| 37 | 36 |
| 38 /** | 37 /** |
| 39 * Checks whether the actual value is a bool and its value is false. | 38 * Checks whether the actual value is a bool and its value is false. |
| 40 */ | 39 */ |
| 41 @deprecated | |
| 42 static void isFalse(var actual, [String reason = null]) { | 40 static void isFalse(var actual, [String reason = null]) { |
| 43 if (_identical(actual, false)) return; | 41 if (_identical(actual, false)) return; |
| 44 String msg = _getMessage(reason); | 42 String msg = _getMessage(reason); |
| 45 _fail("Expect.isFalse($actual$msg) fails."); | 43 _fail("Expect.isFalse($actual$msg) fails."); |
| 46 } | 44 } |
| 47 | 45 |
| 48 /** | 46 /** |
| 49 * Checks whether [actual] is null. | 47 * Checks whether [actual] is null. |
| 50 */ | 48 */ |
| 51 @deprecated | |
| 52 static void isNull(actual, [String reason = null]) { | 49 static void isNull(actual, [String reason = null]) { |
| 53 if (null == actual) return; | 50 if (null == actual) return; |
| 54 String msg = _getMessage(reason); | 51 String msg = _getMessage(reason); |
| 55 _fail("Expect.isNull(actual: <$actual>$msg) fails."); | 52 _fail("Expect.isNull(actual: <$actual>$msg) fails."); |
| 56 } | 53 } |
| 57 | 54 |
| 58 /** | 55 /** |
| 59 * Checks whether [actual] is not null. | 56 * Checks whether [actual] is not null. |
| 60 */ | 57 */ |
| 61 @deprecated | |
| 62 static void isNotNull(actual, [String reason = null]) { | 58 static void isNotNull(actual, [String reason = null]) { |
| 63 if (null != actual) return; | 59 if (null != actual) return; |
| 64 String msg = _getMessage(reason); | 60 String msg = _getMessage(reason); |
| 65 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); | 61 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); |
| 66 } | 62 } |
| 67 | 63 |
| 68 /** | 64 /** |
| 69 * Checks whether the expected and actual values are identical | 65 * Checks whether the expected and actual values are identical |
| 70 * (using `identical`). | 66 * (using `identical`). |
| 71 */ | 67 */ |
| 72 @deprecated | |
| 73 static void identical(var expected, var actual, [String reason = null]) { | 68 static void identical(var expected, var actual, [String reason = null]) { |
| 74 if (_identical(expected, actual)) return; | 69 if (_identical(expected, actual)) return; |
| 75 String msg = _getMessage(reason); | 70 String msg = _getMessage(reason); |
| 76 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " | 71 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " |
| 77 "fails."); | 72 "fails."); |
| 78 } | 73 } |
| 79 | 74 |
| 80 // Unconditional failure. | 75 // Unconditional failure. |
| 81 @deprecated | |
| 82 static void fail(String msg) { | 76 static void fail(String msg) { |
| 83 _fail("Expect.fail('$msg')"); | 77 _fail("Expect.fail('$msg')"); |
| 84 } | 78 } |
| 85 | 79 |
| 86 /** | 80 /** |
| 87 * Failure if the difference between expected and actual is greater than the | 81 * Failure if the difference between expected and actual is greater than the |
| 88 * given tolerance. If no tolerance is given, tolerance is assumed to be the | 82 * given tolerance. If no tolerance is given, tolerance is assumed to be the |
| 89 * value 4 significant digits smaller than the value given for expected. | 83 * value 4 significant digits smaller than the value given for expected. |
| 90 */ | 84 */ |
| 91 @deprecated | |
| 92 static void approxEquals(num expected, | 85 static void approxEquals(num expected, |
| 93 num actual, | 86 num actual, |
| 94 [num tolerance = null, | 87 [num tolerance = null, |
| 95 String reason = null]) { | 88 String reason = null]) { |
| 96 if (tolerance == null) { | 89 if (tolerance == null) { |
| 97 tolerance = (expected / 1e4).abs(); | 90 tolerance = (expected / 1e4).abs(); |
| 98 } | 91 } |
| 99 // Note: use !( <= ) rather than > so we fail on NaNs | 92 // Note: use !( <= ) rather than > so we fail on NaNs |
| 100 if ((expected - actual).abs() <= tolerance) return; | 93 if ((expected - actual).abs() <= tolerance) return; |
| 101 | 94 |
| 102 String msg = _getMessage(reason); | 95 String msg = _getMessage(reason); |
| 103 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' | 96 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' |
| 104 'tolerance:<$tolerance>$msg) fails'); | 97 'tolerance:<$tolerance>$msg) fails'); |
| 105 } | 98 } |
| 106 | 99 |
| 107 @deprecated | |
| 108 static void notEquals(unexpected, actual, [String reason = null]) { | 100 static void notEquals(unexpected, actual, [String reason = null]) { |
| 109 if (unexpected != actual) return; | 101 if (unexpected != actual) return; |
| 110 String msg = _getMessage(reason); | 102 String msg = _getMessage(reason); |
| 111 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " | 103 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " |
| 112 "fails."); | 104 "fails."); |
| 113 } | 105 } |
| 114 | 106 |
| 115 /** | 107 /** |
| 116 * Checks that all elements in [expected] and [actual] are equal `==`. | 108 * Checks that all elements in [expected] and [actual] are equal `==`. |
| 117 * This is different than the typical check for identity equality `identical` | 109 * This is different than the typical check for identity equality `identical` |
| 118 * used by the standard list implementation. It should also produce nicer | 110 * used by the standard list implementation. It should also produce nicer |
| 119 * error messages than just calling `Expect.equals(expected, actual)`. | 111 * error messages than just calling `Expect.equals(expected, actual)`. |
| 120 */ | 112 */ |
| 121 @deprecated | |
| 122 static void listEquals(List expected, List actual, [String reason = null]) { | 113 static void listEquals(List expected, List actual, [String reason = null]) { |
| 123 String msg = _getMessage(reason); | 114 String msg = _getMessage(reason); |
| 124 int n = (expected.length < actual.length) ? expected.length : actual.length; | 115 int n = (expected.length < actual.length) ? expected.length : actual.length; |
| 125 for (int i = 0; i < n; i++) { | 116 for (int i = 0; i < n; i++) { |
| 126 if (expected[i] != actual[i]) { | 117 if (expected[i] != actual[i]) { |
| 127 _fail('Expect.listEquals(at index $i, ' | 118 _fail('Expect.listEquals(at index $i, ' |
| 128 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); | 119 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); |
| 129 } | 120 } |
| 130 } | 121 } |
| 131 // We check on length at the end in order to provide better error | 122 // We check on length at the end in order to provide better error |
| 132 // messages when an unexpected item is inserted in a list. | 123 // messages when an unexpected item is inserted in a list. |
| 133 if (expected.length != actual.length) { | 124 if (expected.length != actual.length) { |
| 134 _fail('Expect.listEquals(list length, ' | 125 _fail('Expect.listEquals(list length, ' |
| 135 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' | 126 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' |
| 136 'fails: Next element <' | 127 'fails: Next element <' |
| 137 '${expected.length > n ? expected[n] : actual[n]}>'); | 128 '${expected.length > n ? expected[n] : actual[n]}>'); |
| 138 } | 129 } |
| 139 } | 130 } |
| 140 | 131 |
| 141 /** | 132 /** |
| 142 * Checks that all [expected] and [actual] have the same set of keys (using | 133 * Checks that all [expected] and [actual] have the same set of keys (using |
| 143 * the semantics of [Map.containsKey] to determine what "same" means. For | 134 * the semantics of [Map.containsKey] to determine what "same" means. For |
| 144 * each key, checks that the values in both maps are equal using `==`. | 135 * each key, checks that the values in both maps are equal using `==`. |
| 145 */ | 136 */ |
| 146 @deprecated | |
| 147 static void mapEquals(Map expected, Map actual, [String reason = null]) { | 137 static void mapEquals(Map expected, Map actual, [String reason = null]) { |
| 148 String msg = _getMessage(reason); | 138 String msg = _getMessage(reason); |
| 149 | 139 |
| 150 // Make sure all of the values are present in both and match. | 140 // Make sure all of the values are present in both and match. |
| 151 for (final key in expected.keys) { | 141 for (final key in expected.keys) { |
| 152 if (!actual.containsKey(key)) { | 142 if (!actual.containsKey(key)) { |
| 153 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails'); | 143 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails'); |
| 154 } | 144 } |
| 155 | 145 |
| 156 Expect.equals(expected[key], actual[key]); | 146 Expect.equals(expected[key], actual[key]); |
| 157 } | 147 } |
| 158 | 148 |
| 159 // Make sure the actual map doesn't have any extra keys. | 149 // Make sure the actual map doesn't have any extra keys. |
| 160 for (final key in actual.keys) { | 150 for (final key in actual.keys) { |
| 161 if (!expected.containsKey(key)) { | 151 if (!expected.containsKey(key)) { |
| 162 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails'); | 152 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails'); |
| 163 } | 153 } |
| 164 } | 154 } |
| 165 } | 155 } |
| 166 | 156 |
| 167 /** | 157 /** |
| 168 * Specialized equality test for strings. When the strings don't match, | 158 * Specialized equality test for strings. When the strings don't match, |
| 169 * this method shows where the mismatch starts and ends. | 159 * this method shows where the mismatch starts and ends. |
| 170 */ | 160 */ |
| 171 @deprecated | |
| 172 static void stringEquals(String expected, | 161 static void stringEquals(String expected, |
| 173 String actual, | 162 String actual, |
| 174 [String reason = null]) { | 163 [String reason = null]) { |
| 175 String msg = _getMessage(reason); | 164 String msg = _getMessage(reason); |
| 176 String defaultMessage = | 165 String defaultMessage = |
| 177 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; | 166 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; |
| 178 | 167 |
| 179 if (expected == actual) return; | 168 if (expected == actual) return; |
| 180 if ((expected == null) || (actual == null)) { | 169 if ((expected == null) || (actual == null)) { |
| 181 _fail('$defaultMessage'); | 170 _fail('$defaultMessage'); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 String eSnippet = expected.substring(left, eLen - right); | 219 String eSnippet = expected.substring(left, eLen - right); |
| 231 String aSnippet = actual.substring(left, aLen - right); | 220 String aSnippet = actual.substring(left, aLen - right); |
| 232 String diff = '\nDiff:\n...[ $eSnippet ]...\n...[ $aSnippet ]...'; | 221 String diff = '\nDiff:\n...[ $eSnippet ]...\n...[ $aSnippet ]...'; |
| 233 _fail('$defaultMessage$diff'); | 222 _fail('$defaultMessage$diff'); |
| 234 } | 223 } |
| 235 | 224 |
| 236 /** | 225 /** |
| 237 * Checks that every element of [expected] is also in [actual], and that | 226 * Checks that every element of [expected] is also in [actual], and that |
| 238 * every element of [actual] is also in [expected]. | 227 * every element of [actual] is also in [expected]. |
| 239 */ | 228 */ |
| 240 @deprecated | |
| 241 static void setEquals(Iterable expected, | 229 static void setEquals(Iterable expected, |
| 242 Iterable actual, | 230 Iterable actual, |
| 243 [String reason = null]) { | 231 [String reason = null]) { |
| 244 final missingSet = new Set.from(expected); | 232 final missingSet = new Set.from(expected); |
| 245 missingSet.removeAll(actual); | 233 missingSet.removeAll(actual); |
| 246 final extraSet = new Set.from(actual); | 234 final extraSet = new Set.from(actual); |
| 247 extraSet.removeAll(expected); | 235 extraSet.removeAll(expected); |
| 248 | 236 |
| 249 if (extraSet.isEmpty && missingSet.isEmpty) return; | 237 if (extraSet.isEmpty && missingSet.isEmpty) return; |
| 250 String msg = _getMessage(reason); | 238 String msg = _getMessage(reason); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 271 } | 259 } |
| 272 | 260 |
| 273 /** | 261 /** |
| 274 * Calls the function [f] and verifies that it throws an exception. | 262 * Calls the function [f] and verifies that it throws an exception. |
| 275 * The optional [check] function can provide additional validation | 263 * The optional [check] function can provide additional validation |
| 276 * that the correct exception is being thrown. For example, to check | 264 * that the correct exception is being thrown. For example, to check |
| 277 * the type of the exception you could write this: | 265 * the type of the exception you could write this: |
| 278 * | 266 * |
| 279 * Expect.throws(myThrowingFunction, (e) => e is MyException); | 267 * Expect.throws(myThrowingFunction, (e) => e is MyException); |
| 280 */ | 268 */ |
| 281 @deprecated | |
| 282 static void throws(void f(), | 269 static void throws(void f(), |
| 283 [_CheckExceptionFn check = null, | 270 [_CheckExceptionFn check = null, |
| 284 String reason = null]) { | 271 String reason = null]) { |
| 285 try { | 272 try { |
| 286 f(); | 273 f(); |
| 287 } catch (e, s) { | 274 } catch (e, s) { |
| 288 if (check != null) { | 275 if (check != null) { |
| 289 if (!check(e)) { | 276 if (!check(e)) { |
| 290 String msg = reason == null ? "" : reason; | 277 String msg = reason == null ? "" : reason; |
| 291 _fail("Expect.throws($msg): Unexpected '$e'\n$s"); | 278 _fail("Expect.throws($msg): Unexpected '$e'\n$s"); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 302 | 289 |
| 303 static void _fail(String message) { | 290 static void _fail(String message) { |
| 304 throw new ExpectException(message); | 291 throw new ExpectException(message); |
| 305 } | 292 } |
| 306 } | 293 } |
| 307 | 294 |
| 308 bool _identical(a, b) => identical(a, b); | 295 bool _identical(a, b) => identical(a, b); |
| 309 | 296 |
| 310 typedef bool _CheckExceptionFn(exception); | 297 typedef bool _CheckExceptionFn(exception); |
| 311 | 298 |
| 312 @deprecated | |
| 313 class ExpectException implements Exception { | 299 class ExpectException implements Exception { |
| 314 ExpectException(this.message); | 300 ExpectException(this.message); |
| 315 String toString() => message; | 301 String toString() => message; |
| 316 String message; | 302 String message; |
| 317 } | 303 } |
| OLD | NEW |