| 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 /** | |
| 6 * This library contains an Expect class with static methods that can be used | |
| 7 * for simple unit-tests. | |
| 8 */ | |
| 9 library expect; | |
| 10 | |
| 11 /** | |
| 12 * Expect is used for tests that do not want to make use of the | |
| 13 * Dart unit test library - for example, the core language tests. | |
| 14 * Third parties are discouraged from using this, and should use | |
| 15 * the expect() function in the unit test library instead for | |
| 16 * test assertions. | |
| 17 */ | |
| 18 class Expect { | |
| 19 /** | |
| 20 * Return a slice of a string. | |
| 21 * | |
| 22 * The slice will contain at least the substring from [start] to the lower of | |
| 23 * [end] and `start + length`. | |
| 24 * If the result is no more than `length - 10` characters long, | |
| 25 * context may be added by extending the range of the slice, by decreasing | |
| 26 * [start] and increasing [end], up to at most length characters. | |
| 27 * If the start or end of the slice are not matching the start or end of | |
| 28 * the string, ellipses are added before or after the slice. | |
| 29 * Control characters may be encoded as "\xhh" codes. | |
| 30 */ | |
| 31 static String _truncateString(String string, int start, int end, int length) { | |
| 32 if (end - start > length) { | |
| 33 end = start + length; | |
| 34 } else if (end - start < length) { | |
| 35 int overflow = length - (end - start); | |
| 36 if (overflow > 10) overflow = 10; | |
| 37 // Add context. | |
| 38 start = start - ((overflow + 1) ~/ 2); | |
| 39 end = end + (overflow ~/ 2); | |
| 40 if (start < 0) start = 0; | |
| 41 if (end > string.length) end = string.length; | |
| 42 } | |
| 43 if (start == 0 && end == string.length) return string; | |
| 44 StringBuffer buf = new StringBuffer(); | |
| 45 if (start > 0) buf.write("..."); | |
| 46 for (int i = start; i < end; i++) { | |
| 47 int code = string.codeUnitAt(i); | |
| 48 if (code < 0x20) { | |
| 49 buf.write(r"\x"); | |
| 50 buf.write("0123456789abcdef"[code ~/ 16]); | |
| 51 buf.write("0123456789abcdef"[code % 16]); | |
| 52 } else { | |
| 53 buf.writeCharCode(string.codeUnitAt(i)); | |
| 54 } | |
| 55 } | |
| 56 if (end < string.length) buf.write("..."); | |
| 57 return buf.toString(); | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Find the difference between two strings. | |
| 62 * | |
| 63 * This finds the first point where two strings differ, and returns | |
| 64 * a text describing the difference. | |
| 65 * | |
| 66 * For small strings (length less than 20) nothing is done, and null is | |
| 67 * returned. Small strings can be compared visually, but for longer strings | |
| 68 * only a slice containing the first difference will be shown. | |
| 69 */ | |
| 70 static String _stringDifference(String expected, String actual) { | |
| 71 if (expected.length < 20 && actual.length < 20) return null; | |
| 72 for (int i = 0; i < expected.length && i < actual.length; i++) { | |
| 73 if (expected.codeUnitAt(i) != actual.codeUnitAt(i)) { | |
| 74 int start = i; | |
| 75 i++; | |
| 76 while (i < expected.length && i < actual.length) { | |
| 77 if (expected.codeUnitAt(i) == actual.codeUnitAt(i)) break; | |
| 78 i++; | |
| 79 } | |
| 80 int end = i; | |
| 81 var truncExpected = _truncateString(expected, start, end, 20); | |
| 82 var truncActual = _truncateString(actual, start, end, 20); | |
| 83 return "at index $start: Expected <$truncExpected>, " | |
| 84 "Found: <$truncActual>"; | |
| 85 } | |
| 86 } | |
| 87 return null; | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * Checks whether the expected and actual values are equal (using `==`). | |
| 92 */ | |
| 93 static void equals(var expected, var actual, [String reason = null]) { | |
| 94 if (expected == actual) return; | |
| 95 String msg = _getMessage(reason); | |
| 96 if (expected is String && actual is String) { | |
| 97 String stringDifference = _stringDifference(expected, actual); | |
| 98 if (stringDifference != null) { | |
| 99 _fail("Expect.equals($stringDifference$msg) fails."); | |
| 100 } | |
| 101 } | |
| 102 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Checks whether the actual value is a bool and its value is true. | |
| 107 */ | |
| 108 static void isTrue(var actual, [String reason = null]) { | |
| 109 if (_identical(actual, true)) return; | |
| 110 String msg = _getMessage(reason); | |
| 111 _fail("Expect.isTrue($actual$msg) fails."); | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Checks whether the actual value is a bool and its value is false. | |
| 116 */ | |
| 117 static void isFalse(var actual, [String reason = null]) { | |
| 118 if (_identical(actual, false)) return; | |
| 119 String msg = _getMessage(reason); | |
| 120 _fail("Expect.isFalse($actual$msg) fails."); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Checks whether [actual] is null. | |
| 125 */ | |
| 126 static void isNull(actual, [String reason = null]) { | |
| 127 if (null == actual) return; | |
| 128 String msg = _getMessage(reason); | |
| 129 _fail("Expect.isNull(actual: <$actual>$msg) fails."); | |
| 130 } | |
| 131 | |
| 132 /** | |
| 133 * Checks whether [actual] is not null. | |
| 134 */ | |
| 135 static void isNotNull(actual, [String reason = null]) { | |
| 136 if (null != actual) return; | |
| 137 String msg = _getMessage(reason); | |
| 138 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); | |
| 139 } | |
| 140 | |
| 141 /** | |
| 142 * Checks whether the expected and actual values are identical | |
| 143 * (using `identical`). | |
| 144 */ | |
| 145 static void identical(var expected, var actual, [String reason = null]) { | |
| 146 if (_identical(expected, actual)) return; | |
| 147 String msg = _getMessage(reason); | |
| 148 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " | |
| 149 "fails."); | |
| 150 } | |
| 151 | |
| 152 // Unconditional failure. | |
| 153 static void fail(String msg) { | |
| 154 _fail("Expect.fail('$msg')"); | |
| 155 } | |
| 156 | |
| 157 /** | |
| 158 * Failure if the difference between expected and actual is greater than the | |
| 159 * given tolerance. If no tolerance is given, tolerance is assumed to be the | |
| 160 * value 4 significant digits smaller than the value given for expected. | |
| 161 */ | |
| 162 static void approxEquals(num expected, num actual, | |
| 163 [num tolerance = null, String reason = null]) { | |
| 164 if (tolerance == null) { | |
| 165 tolerance = (expected / 1e4).abs(); | |
| 166 } | |
| 167 // Note: use !( <= ) rather than > so we fail on NaNs | |
| 168 if ((expected - actual).abs() <= tolerance) return; | |
| 169 | |
| 170 String msg = _getMessage(reason); | |
| 171 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' | |
| 172 'tolerance:<$tolerance>$msg) fails'); | |
| 173 } | |
| 174 | |
| 175 static void notEquals(unexpected, actual, [String reason = null]) { | |
| 176 if (unexpected != actual) return; | |
| 177 String msg = _getMessage(reason); | |
| 178 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " | |
| 179 "fails."); | |
| 180 } | |
| 181 | |
| 182 /** | |
| 183 * Checks that all elements in [expected] and [actual] are equal `==`. | |
| 184 * This is different than the typical check for identity equality `identical` | |
| 185 * used by the standard list implementation. It should also produce nicer | |
| 186 * error messages than just calling `Expect.equals(expected, actual)`. | |
| 187 */ | |
| 188 static void listEquals(List expected, List actual, [String reason = null]) { | |
| 189 String msg = _getMessage(reason); | |
| 190 int n = (expected.length < actual.length) ? expected.length : actual.length; | |
| 191 for (int i = 0; i < n; i++) { | |
| 192 if (expected[i] != actual[i]) { | |
| 193 _fail('Expect.listEquals(at index $i, ' | |
| 194 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); | |
| 195 } | |
| 196 } | |
| 197 // We check on length at the end in order to provide better error | |
| 198 // messages when an unexpected item is inserted in a list. | |
| 199 if (expected.length != actual.length) { | |
| 200 _fail('Expect.listEquals(list length, ' | |
| 201 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' | |
| 202 'fails: Next element <' | |
| 203 '${expected.length > n ? expected[n] : actual[n]}>'); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 /** | |
| 208 * Checks that all [expected] and [actual] have the same set of keys (using | |
| 209 * the semantics of [Map.containsKey] to determine what "same" means. For | |
| 210 * each key, checks that the values in both maps are equal using `==`. | |
| 211 */ | |
| 212 static void mapEquals(Map expected, Map actual, [String reason = null]) { | |
| 213 String msg = _getMessage(reason); | |
| 214 | |
| 215 // Make sure all of the values are present in both and match. | |
| 216 for (final key in expected.keys) { | |
| 217 if (!actual.containsKey(key)) { | |
| 218 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails'); | |
| 219 } | |
| 220 | |
| 221 Expect.equals(expected[key], actual[key]); | |
| 222 } | |
| 223 | |
| 224 // Make sure the actual map doesn't have any extra keys. | |
| 225 for (final key in actual.keys) { | |
| 226 if (!expected.containsKey(key)) { | |
| 227 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails'); | |
| 228 } | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 /** | |
| 233 * Specialized equality test for strings. When the strings don't match, | |
| 234 * this method shows where the mismatch starts and ends. | |
| 235 */ | |
| 236 static void stringEquals(String expected, String actual, | |
| 237 [String reason = null]) { | |
| 238 if (expected == actual) return; | |
| 239 | |
| 240 String msg = _getMessage(reason); | |
| 241 String defaultMessage = | |
| 242 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; | |
| 243 | |
| 244 if ((expected == null) || (actual == null)) { | |
| 245 _fail('$defaultMessage'); | |
| 246 } | |
| 247 | |
| 248 // Scan from the left until we find the mismatch. | |
| 249 int left = 0; | |
| 250 int right = 0; | |
| 251 int eLen = expected.length; | |
| 252 int aLen = actual.length; | |
| 253 | |
| 254 while (true) { | |
| 255 if (left == eLen || left == aLen || expected[left] != actual[left]) { | |
| 256 break; | |
| 257 } | |
| 258 left++; | |
| 259 } | |
| 260 | |
| 261 // Scan from the right until we find the mismatch. | |
| 262 int eRem = eLen - left; // Remaining length ignoring left match. | |
| 263 int aRem = aLen - left; | |
| 264 while (true) { | |
| 265 if (right == eRem || | |
| 266 right == aRem || | |
| 267 expected[eLen - right - 1] != actual[aLen - right - 1]) { | |
| 268 break; | |
| 269 } | |
| 270 right++; | |
| 271 } | |
| 272 | |
| 273 // First difference is at index `left`, last at `length - right - 1` | |
| 274 // Make useful difference message. | |
| 275 // Example: | |
| 276 // Diff (1209..1209/1246): | |
| 277 // ...,{"name":"[ ]FallThroug... | |
| 278 // ...,{"name":"[ IndexError","kind":"class"},{"name":" ]FallThroug... | |
| 279 // (colors would be great!) | |
| 280 | |
| 281 // Make snippets of up to ten characters before and after differences. | |
| 282 | |
| 283 String leftSnippet = expected.substring(left < 10 ? 0 : left - 10, left); | |
| 284 int rightSnippetLength = right < 10 ? right : 10; | |
| 285 String rightSnippet = | |
| 286 expected.substring(eLen - right, eLen - right + rightSnippetLength); | |
| 287 | |
| 288 // Make snippets of the differences. | |
| 289 String eSnippet = expected.substring(left, eLen - right); | |
| 290 String aSnippet = actual.substring(left, aLen - right); | |
| 291 | |
| 292 // If snippets are long, elide the middle. | |
| 293 if (eSnippet.length > 43) { | |
| 294 eSnippet = eSnippet.substring(0, 20) + | |
| 295 "..." + | |
| 296 eSnippet.substring(eSnippet.length - 20); | |
| 297 } | |
| 298 if (aSnippet.length > 43) { | |
| 299 aSnippet = aSnippet.substring(0, 20) + | |
| 300 "..." + | |
| 301 aSnippet.substring(aSnippet.length - 20); | |
| 302 } | |
| 303 // Add "..." before and after, unless the snippets reach the end. | |
| 304 String leftLead = "..."; | |
| 305 String rightTail = "..."; | |
| 306 if (left <= 10) leftLead = ""; | |
| 307 if (right <= 10) rightTail = ""; | |
| 308 | |
| 309 String diff = '\nDiff ($left..${eLen - right}/${aLen - right}):\n' | |
| 310 '$leftLead$leftSnippet[ $eSnippet ]$rightSnippet$rightTail\n' | |
| 311 '$leftLead$leftSnippet[ $aSnippet ]$rightSnippet$rightTail'; | |
| 312 _fail("$defaultMessage$diff"); | |
| 313 } | |
| 314 | |
| 315 /** | |
| 316 * Checks that every element of [expected] is also in [actual], and that | |
| 317 * every element of [actual] is also in [expected]. | |
| 318 */ | |
| 319 static void setEquals(Iterable expected, Iterable actual, | |
| 320 [String reason = null]) { | |
| 321 final missingSet = new Set.from(expected); | |
| 322 missingSet.removeAll(actual); | |
| 323 final extraSet = new Set.from(actual); | |
| 324 extraSet.removeAll(expected); | |
| 325 | |
| 326 if (extraSet.isEmpty && missingSet.isEmpty) return; | |
| 327 String msg = _getMessage(reason); | |
| 328 | |
| 329 StringBuffer sb = new StringBuffer("Expect.setEquals($msg) fails"); | |
| 330 // Report any missing items. | |
| 331 if (!missingSet.isEmpty) { | |
| 332 sb.write('\nExpected collection does not contain: '); | |
| 333 } | |
| 334 | |
| 335 for (final val in missingSet) { | |
| 336 sb.write('$val '); | |
| 337 } | |
| 338 | |
| 339 // Report any extra items. | |
| 340 if (!extraSet.isEmpty) { | |
| 341 sb.write('\nExpected collection should not contain: '); | |
| 342 } | |
| 343 | |
| 344 for (final val in extraSet) { | |
| 345 sb.write('$val '); | |
| 346 } | |
| 347 _fail(sb.toString()); | |
| 348 } | |
| 349 | |
| 350 /** | |
| 351 * Checks that [expected] is equivalent to [actual]. | |
| 352 * | |
| 353 * If the objects are lists or maps, recurses into them. | |
| 354 */ | |
| 355 static void deepEquals(Object expected, Object actual) { | |
| 356 // Early exit check for equality. | |
| 357 if (expected == actual) return; | |
| 358 | |
| 359 if (expected is List && actual is List) { | |
| 360 int n = | |
| 361 (expected.length < actual.length) ? expected.length : actual.length; | |
| 362 for (int i = 0; i < n; i++) { | |
| 363 deepEquals(expected[i], actual[i]); | |
| 364 } | |
| 365 | |
| 366 // We check on length at the end in order to provide better error | |
| 367 // messages when an unexpected item is inserted in a list. | |
| 368 if (expected.length != actual.length) { | |
| 369 _fail('Expect.deepEquals(list length, ' | |
| 370 'expected: <${expected.length}>, actual: <${actual.length}>) ' | |
| 371 'fails: Next element <' | |
| 372 '${expected.length > n ? expected[n] : actual[n]}>'); | |
| 373 } | |
| 374 } else if (expected is Map && actual is Map) { | |
| 375 // Make sure all of the values are present in both and match. | |
| 376 for (final key in expected.keys) { | |
| 377 if (!actual.containsKey(key)) { | |
| 378 _fail('Expect.deepEquals(missing expected key: <$key>) fails'); | |
| 379 } | |
| 380 | |
| 381 Expect.deepEquals(expected[key], actual[key]); | |
| 382 } | |
| 383 | |
| 384 // Make sure the actual map doesn't have any extra keys. | |
| 385 for (final key in actual.keys) { | |
| 386 if (!expected.containsKey(key)) { | |
| 387 _fail('Expect.deepEquals(unexpected key: <$key>) fails'); | |
| 388 } | |
| 389 } | |
| 390 } else if (expected is String && actual is String) { | |
| 391 String stringDifference = _stringDifference(expected, actual); | |
| 392 // If we get here, they should not be equal. | |
| 393 assert(stringDifference != null); | |
| 394 _fail("Expect.deepEquals($stringDifference) fails."); | |
| 395 } else { | |
| 396 _fail("Expect.deepEquals(expected: <$expected>, actual: <$actual>) " | |
| 397 "fails."); | |
| 398 } | |
| 399 } | |
| 400 | |
| 401 /** | |
| 402 * Calls the function [f] and verifies that it throws an exception. | |
| 403 * The optional [check] function can provide additional validation | |
| 404 * that the correct exception is being thrown. For example, to check | |
| 405 * the type of the exception you could write this: | |
| 406 * | |
| 407 * Expect.throws(myThrowingFunction, (e) => e is MyException); | |
| 408 */ | |
| 409 static void throws(void f(), | |
| 410 [_CheckExceptionFn check = null, String reason = null]) { | |
| 411 String msg = reason == null ? "" : "($reason)"; | |
| 412 if (f is! _Nullary) { | |
| 413 // Only throws from executing the funtion body should count as throwing. | |
| 414 // The failure to even call `f` should throw outside the try/catch. | |
| 415 _fail("Expect.throws$msg: Function f not callable with zero arguments"); | |
| 416 } | |
| 417 try { | |
| 418 f(); | |
| 419 } catch (e, s) { | |
| 420 if (check != null) { | |
| 421 if (!check(e)) { | |
| 422 _fail("Expect.throws$msg: Unexpected '$e'\n$s"); | |
| 423 } | |
| 424 } | |
| 425 return; | |
| 426 } | |
| 427 _fail('Expect.throws$msg fails: Did not throw'); | |
| 428 } | |
| 429 | |
| 430 static String _getMessage(String reason) => | |
| 431 (reason == null) ? "" : ", '$reason'"; | |
| 432 | |
| 433 static void _fail(String message) { | |
| 434 throw new ExpectException(message); | |
| 435 } | |
| 436 } | |
| 437 | |
| 438 bool _identical(a, b) => identical(a, b); | |
| 439 | |
| 440 typedef bool _CheckExceptionFn(exception); | |
| 441 typedef _Nullary(); // Expect.throws argument must be this type. | |
| 442 | |
| 443 class ExpectException implements Exception { | |
| 444 ExpectException(this.message); | |
| 445 String toString() => message; | |
| 446 String message; | |
| 447 } | |
| 448 | |
| 449 /// Annotation class for testing of dart2js. Use this as metadata on method | |
| 450 /// declarations to disable inlining of the annotated method. | |
| 451 class NoInline { | |
| 452 const NoInline(); | |
| 453 } | |
| 454 | |
| 455 /// Annotation class for testing of dart2js. Use this as metadata on method | |
| 456 /// declarations to make the type inferrer trust the parameter and return types, | |
| 457 /// effectively asserting the runtime values will (at least) be subtypes of the | |
| 458 /// annotated types. | |
| 459 /// | |
| 460 /// While the actually inferred type is guaranteed to be a subtype of the | |
| 461 /// annotation, it often is more precise. In particular, if a method is only | |
| 462 /// called with `null`, the inferrer will still infer null. To ensure that | |
| 463 /// the annotated type is also the inferred type, additionally use | |
| 464 /// [AssumeDynamic]. | |
| 465 class TrustTypeAnnotations { | |
| 466 const TrustTypeAnnotations(); | |
| 467 } | |
| 468 | |
| 469 /// Annotation class for testing of dart2js. Use this as metadata on method | |
| 470 /// declarations to disable closed world assumptions on parameters, effectively | |
| 471 /// assuming that the runtime arguments could be any value. Note that the | |
| 472 /// constraints due to [TrustTypeAnnotations] still apply. | |
| 473 class AssumeDynamic { | |
| 474 const AssumeDynamic(); | |
| 475 } | |
| OLD | NEW |