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