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, |
| 163 num actual, |
| 164 [num tolerance = null, |
| 165 String reason = null]) { |
| 166 if (tolerance == null) { |
| 167 tolerance = (expected / 1e4).abs(); |
| 168 } |
| 169 // Note: use !( <= ) rather than > so we fail on NaNs |
| 170 if ((expected - actual).abs() <= tolerance) return; |
| 171 |
| 172 String msg = _getMessage(reason); |
| 173 _fail('Expect.approxEquals(expected:<$expected>, actual:<$actual>, ' |
| 174 'tolerance:<$tolerance>$msg) fails'); |
| 175 } |
| 176 |
| 177 static void notEquals(unexpected, actual, [String reason = null]) { |
| 178 if (unexpected != actual) return; |
| 179 String msg = _getMessage(reason); |
| 180 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " |
| 181 "fails."); |
| 182 } |
| 183 |
| 184 /** |
| 185 * Checks that all elements in [expected] and [actual] are equal `==`. |
| 186 * This is different than the typical check for identity equality `identical` |
| 187 * used by the standard list implementation. It should also produce nicer |
| 188 * error messages than just calling `Expect.equals(expected, actual)`. |
| 189 */ |
| 190 static void listEquals(List expected, List actual, [String reason = null]) { |
| 191 String msg = _getMessage(reason); |
| 192 int n = (expected.length < actual.length) ? expected.length : actual.length; |
| 193 for (int i = 0; i < n; i++) { |
| 194 if (expected[i] != actual[i]) { |
| 195 _fail('Expect.listEquals(at index $i, ' |
| 196 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); |
| 197 } |
| 198 } |
| 199 // We check on length at the end in order to provide better error |
| 200 // messages when an unexpected item is inserted in a list. |
| 201 if (expected.length != actual.length) { |
| 202 _fail('Expect.listEquals(list length, ' |
| 203 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' |
| 204 'fails: Next element <' |
| 205 '${expected.length > n ? expected[n] : actual[n]}>'); |
| 206 } |
| 207 } |
| 208 |
| 209 /** |
| 210 * Checks that all [expected] and [actual] have the same set of keys (using |
| 211 * the semantics of [Map.containsKey] to determine what "same" means. For |
| 212 * each key, checks that the values in both maps are equal using `==`. |
| 213 */ |
| 214 static void mapEquals(Map expected, Map actual, [String reason = null]) { |
| 215 String msg = _getMessage(reason); |
| 216 |
| 217 // Make sure all of the values are present in both and match. |
| 218 for (final key in expected.keys) { |
| 219 if (!actual.containsKey(key)) { |
| 220 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails'); |
| 221 } |
| 222 |
| 223 Expect.equals(expected[key], actual[key]); |
| 224 } |
| 225 |
| 226 // Make sure the actual map doesn't have any extra keys. |
| 227 for (final key in actual.keys) { |
| 228 if (!expected.containsKey(key)) { |
| 229 _fail('Expect.mapEquals(unexpected key: <$key>$msg) fails'); |
| 230 } |
| 231 } |
| 232 } |
| 233 |
| 234 /** |
| 235 * Specialized equality test for strings. When the strings don't match, |
| 236 * this method shows where the mismatch starts and ends. |
| 237 */ |
| 238 static void stringEquals(String expected, |
| 239 String actual, |
| 240 [String reason = null]) { |
| 241 String msg = _getMessage(reason); |
| 242 String defaultMessage = |
| 243 'Expect.stringEquals(expected: <$expected>", <$actual>$msg) fails'; |
| 244 |
| 245 if (expected == actual) return; |
| 246 if ((expected == null) || (actual == null)) { |
| 247 _fail('$defaultMessage'); |
| 248 } |
| 249 // scan from the left until we find a mismatch |
| 250 int left = 0; |
| 251 int eLen = expected.length; |
| 252 int aLen = actual.length; |
| 253 while (true) { |
| 254 if (left == eLen) { |
| 255 assert (left < aLen); |
| 256 String snippet = actual.substring(left, aLen); |
| 257 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); |
| 258 return; |
| 259 } |
| 260 if (left == aLen) { |
| 261 assert (left < eLen); |
| 262 String snippet = expected.substring(left, eLen); |
| 263 _fail('$defaultMessage\nDiff:\n...[ ]\n...[ $snippet ]'); |
| 264 return; |
| 265 } |
| 266 if (expected[left] != actual[left]) { |
| 267 break; |
| 268 } |
| 269 left++; |
| 270 } |
| 271 |
| 272 // scan from the right until we find a mismatch |
| 273 int right = 0; |
| 274 while (true) { |
| 275 if (right == eLen) { |
| 276 assert (right < aLen); |
| 277 String snippet = actual.substring(0, aLen - right); |
| 278 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); |
| 279 return; |
| 280 } |
| 281 if (right == aLen) { |
| 282 assert (right < eLen); |
| 283 String snippet = expected.substring(0, eLen - right); |
| 284 _fail('$defaultMessage\nDiff:\n[ ]...\n[ $snippet ]...'); |
| 285 return; |
| 286 } |
| 287 // stop scanning if we've reached the end of the left-to-right match |
| 288 if (eLen - right <= left || aLen - right <= left) { |
| 289 break; |
| 290 } |
| 291 if (expected[eLen - right - 1] != actual[aLen - right - 1]) { |
| 292 break; |
| 293 } |
| 294 right++; |
| 295 } |
| 296 String eSnippet = expected.substring(left, eLen - right); |
| 297 String aSnippet = actual.substring(left, aLen - right); |
| 298 String diff = '\nDiff:\n...[ $eSnippet ]...\n...[ $aSnippet ]...'; |
| 299 _fail('$defaultMessage$diff'); |
| 300 } |
| 301 |
| 302 /** |
| 303 * Checks that every element of [expected] is also in [actual], and that |
| 304 * every element of [actual] is also in [expected]. |
| 305 */ |
| 306 static void setEquals(Iterable expected, |
| 307 Iterable actual, |
| 308 [String reason = null]) { |
| 309 final missingSet = new Set.from(expected); |
| 310 missingSet.removeAll(actual); |
| 311 final extraSet = new Set.from(actual); |
| 312 extraSet.removeAll(expected); |
| 313 |
| 314 if (extraSet.isEmpty && missingSet.isEmpty) return; |
| 315 String msg = _getMessage(reason); |
| 316 |
| 317 StringBuffer sb = new StringBuffer("Expect.setEquals($msg) fails"); |
| 318 // Report any missing items. |
| 319 if (!missingSet.isEmpty) { |
| 320 sb.write('\nExpected collection does not contain: '); |
| 321 } |
| 322 |
| 323 for (final val in missingSet) { |
| 324 sb.write('$val '); |
| 325 } |
| 326 |
| 327 // Report any extra items. |
| 328 if (!extraSet.isEmpty) { |
| 329 sb.write('\nExpected collection should not contain: '); |
| 330 } |
| 331 |
| 332 for (final val in extraSet) { |
| 333 sb.write('$val '); |
| 334 } |
| 335 _fail(sb.toString()); |
| 336 } |
| 337 |
| 338 /** |
| 339 * Calls the function [f] and verifies that it throws an exception. |
| 340 * The optional [check] function can provide additional validation |
| 341 * that the correct exception is being thrown. For example, to check |
| 342 * the type of the exception you could write this: |
| 343 * |
| 344 * Expect.throws(myThrowingFunction, (e) => e is MyException); |
| 345 */ |
| 346 static void throws(void f(), |
| 347 [_CheckExceptionFn check = null, |
| 348 String reason = null]) { |
| 349 try { |
| 350 f(); |
| 351 } catch (e, s) { |
| 352 if (check != null) { |
| 353 if (!check(e)) { |
| 354 String msg = reason == null ? "" : reason; |
| 355 _fail("Expect.throws($msg): Unexpected '$e'\n$s"); |
| 356 } |
| 357 } |
| 358 return; |
| 359 } |
| 360 String msg = reason == null ? "" : reason; |
| 361 _fail('Expect.throws($msg) fails'); |
| 362 } |
| 363 |
| 364 static String _getMessage(String reason) |
| 365 => (reason == null) ? "" : ", '$reason'"; |
| 366 |
| 367 static void _fail(String message) { |
| 368 throw new ExpectException(message); |
| 369 } |
| 370 } |
| 371 |
| 372 bool _identical(a, b) => identical(a, b); |
| 373 |
| 374 typedef bool _CheckExceptionFn(exception); |
| 375 |
| 376 class ExpectException implements Exception { |
| 377 ExpectException(this.message); |
| 378 String toString() => message; |
| 379 String message; |
| 380 } |
OLD | NEW |