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