| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 * framework expects lines that signal failed tests to start with | 36 * framework expects lines that signal failed tests to start with |
| 37 * the f-word and ignore all other lines. | 37 * the f-word and ignore all other lines. |
| 38 */ | 38 */ |
| 39 | 39 |
| 40 | 40 |
| 41 MjsUnitAssertionError.prototype.toString = function () { | 41 MjsUnitAssertionError.prototype.toString = function () { |
| 42 return this.message; | 42 return this.message; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 | 45 |
| 46 function classOf(object) { | 46 // Expected and found values the same objects, or the same primitive |
| 47 var string = Object.prototype.toString.call(object); | 47 // values. |
| 48 // String has format [object <ClassName>]. | 48 // For known primitive values, please use assertEquals. |
| 49 return string.substring(8, string.length - 1); | 49 var assertSame; |
| 50 } | 50 |
| 51 | 51 // Expected and found values are identical primitive values or functions |
| 52 | 52 // or similarly structured objects (checking internal properties |
| 53 function MjsUnitToString(value) { | 53 // of, e.g., Number and Date objects, the elements of arrays |
| 54 switch (typeof value) { | 54 // and the properties of non-Array objects). |
| 55 case "string": | 55 var assertEquals; |
| 56 return JSON.stringify(value); | 56 |
| 57 case "number": | 57 // The found object is an Array with the same length and elements |
| 58 if (value === 0 && (1 / value) < 0) return "-0"; | 58 // as the expected object. The expected object doesn't need to be an Array, |
| 59 // FALLTHROUGH. | 59 // as long as it's "array-ish". |
| 60 case "boolean": | 60 var assertArrayEquals; |
| 61 case "undefined": | 61 |
| 62 case "function": | 62 // The found object must have the same enumerable properties as the |
| 63 return String(value); | 63 // expected object. The type of object isn't checked. |
| 64 case "object": | 64 var assertPropertiesEqual; |
| 65 if (value === null) return "null"; | 65 |
| 66 var objectClass = classOf(value); | 66 // Assert that the string conversion of the found value is equal to |
| 67 switch (objectClass) { | 67 // the expected string. Only kept for backwards compatability, please |
| 68 // check the real structure of the found value. |
| 69 var assertToStringEquals; |
| 70 |
| 71 // Checks that the found value is true. Use with boolean expressions |
| 72 // for tests that doesn't have their own assertXXX function. |
| 73 var assertTrue; |
| 74 |
| 75 // Checks that the found value is false. |
| 76 var assertFalse; |
| 77 |
| 78 // Checks that the found value is null. Kept for historical compatability, |
| 79 // please just use assertEquals(null, expected). |
| 80 var assertNull; |
| 81 |
| 82 // Checks that the found value is *not* null. |
| 83 var assertNotNull; |
| 84 |
| 85 // Assert that the passed function or eval code throws an exception. |
| 86 // The optional second argument is an exception constructor that the |
| 87 // thrown exception is checked against with "instanceof". |
| 88 // The optional third argument is a message type string that is compared |
| 89 // to the type property on the thrown exception. |
| 90 var assertThrows; |
| 91 |
| 92 // Assert that the passed function or eval code does not throw an exception. |
| 93 var assertDoesNotThrow; |
| 94 |
| 95 // Asserts that the found value is an instance of the constructor passed |
| 96 // as the second argument. |
| 97 var assertInstanceof; |
| 98 |
| 99 // Assert that this code is never executed (i.e., always fails if executed). |
| 100 var assertUnreachable; |
| 101 |
| 102 (function () { // Scope for utility functions. |
| 103 |
| 104 function classOf(object) { |
| 105 // Argument must not be null or undefined. |
| 106 var string = Object.prototype.toString.call(object); |
| 107 // String has format [object <ClassName>]. |
| 108 return string.substring(8, string.length - 1); |
| 109 } |
| 110 |
| 111 |
| 112 function PrettyPrint(value) { |
| 113 switch (typeof value) { |
| 114 case "string": |
| 115 return JSON.stringify(value); |
| 116 case "number": |
| 117 if (value === 0 && (1 / value) < 0) return "-0"; |
| 118 // FALLTHROUGH. |
| 119 case "boolean": |
| 120 case "undefined": |
| 121 case "function": |
| 122 return String(value); |
| 123 case "object": |
| 124 if (value === null) return "null"; |
| 125 var objectClass = classOf(value); |
| 126 switch (objectClass) { |
| 68 case "Number": | 127 case "Number": |
| 69 case "String": | 128 case "String": |
| 70 case "Boolean": | 129 case "Boolean": |
| 71 case "Date": | 130 case "Date": |
| 72 return objectClass + "(" + MjsUnitToString(value.valueOf()) + ")"; | 131 return objectClass + "(" + PrettyPrint(value.valueOf()) + ")"; |
| 73 case "RegExp": | 132 case "RegExp": |
| 74 return value.toString(); | 133 return value.toString(); |
| 75 case "Array": | 134 case "Array": |
| 76 return "[" + value.map(MjsUnitArrayElementToString).join(",") + "]"; | 135 return "[" + value.map(PrettyPrintArrayElement).join(",") + "]"; |
| 77 case "Object": | 136 case "Object": |
| 78 break; | 137 break; |
| 79 default: | 138 default: |
| 80 return objectClass + "()"; | 139 return objectClass + "()"; |
| 81 } | 140 } |
| 82 // [[Class]] is "Object". | 141 // [[Class]] is "Object". |
| 83 var constructor = value.constructor.name; | 142 var name = value.constructor.name; |
| 84 if (name) return name + "()"; | 143 if (name) return name + "()"; |
| 85 return "Object()"; | 144 return "Object()"; |
| 86 default: | 145 default: |
| 87 return "-- unknown value --"; | 146 return "-- unknown value --"; |
| 88 } | 147 } |
| 89 } | 148 } |
| 90 | 149 |
| 91 | 150 |
| 92 function MjsUnitArrayElementToString(value, index, array) { | 151 function PrettyPrintArrayElement(value, index, array) { |
| 93 if (value === undefined && !(index in array)) return ""; | 152 if (value === undefined && !(index in array)) return ""; |
| 94 return MjsUnitToString(value); | 153 return PrettyPrint(value); |
| 95 } | 154 } |
| 96 | 155 |
| 97 | 156 |
| 98 function fail(expected, found, name_opt) { | 157 function fail(expectedText, found, name_opt) { |
| 99 var message = "Fail" + "ure"; | 158 var message = "Fail" + "ure"; |
| 100 if (name_opt) { | 159 if (name_opt) { |
| 160 // Fix this when we ditch the old test runner. |
| 161 message += " (" + name_opt + ")"; |
| 162 } |
| 163 |
| 164 message += ": expected <" + expectedText + |
| 165 "> found <" + PrettyPrint(found) + ">"; |
| 166 throw new MjsUnitAssertionError(message); |
| 167 } |
| 168 |
| 169 |
| 170 function deepObjectEquals(a, b) { |
| 171 var aProps = Object.keys(a); |
| 172 aProps.sort(); |
| 173 var bProps = Object.keys(b); |
| 174 bProps.sort(); |
| 175 if (!deepEquals(aProps, bProps)) { |
| 176 return false; |
| 177 } |
| 178 for (var i = 0; i < aProps.length; i++) { |
| 179 if (!deepEquals(a[aProps[i]], b[aProps[i]])) { |
| 180 return false; |
| 181 } |
| 182 } |
| 183 return true; |
| 184 } |
| 185 |
| 186 |
| 187 function deepEquals(a, b) { |
| 188 if (a === b) { |
| 189 // Check for -0. |
| 190 if (a === 0) return (1 / a) === (1 / b); |
| 191 return true; |
| 192 } |
| 193 if (typeof a != typeof b) return false; |
| 194 if (typeof a == "number") return isNaN(a) && isNaN(b); |
| 195 if (typeof a !== "object" && typeof a !== "function") return false; |
| 196 // Neither a nor b is primitive. |
| 197 var objectClass = classOf(a); |
| 198 if (objectClass !== classOf(b)) return false; |
| 199 if (objectClass === "RegExp") { |
| 200 // For RegExp, just compare pattern and flags using its toString. |
| 201 return (a.toString() === b.toString()); |
| 202 } |
| 203 // Functions are only identical to themselves. |
| 204 if (objectClass === "Function") return false; |
| 205 if (objectClass === "Array") { |
| 206 var elementCount = 0; |
| 207 if (a.length != b.length) { |
| 208 return false; |
| 209 } |
| 210 for (var i = 0; i < a.length; i++) { |
| 211 if (!deepEquals(a[i], b[i])) return false; |
| 212 } |
| 213 return true; |
| 214 } |
| 215 if (objectClass == "String" || objectClass == "Number" || |
| 216 objectClass == "Boolean" || objectClass == "Date") { |
| 217 if (a.valueOf() !== b.valueOf()) return false; |
| 218 } |
| 219 return deepObjectEquals(a, b); |
| 220 } |
| 221 |
| 222 |
| 223 assertSame = function assertSame(expected, found, name_opt) { |
| 224 if (found === expected) { |
| 225 if (expected !== 0 || (1 / expected) == (1 / found)) return; |
| 226 } else if (isNaN(expected) && isNaN(found)) { |
| 227 return; |
| 228 } |
| 229 fail(PrettyPrint(expected), found, name_opt); |
| 230 }; |
| 231 |
| 232 |
| 233 assertEquals = function assertEquals(expected, found, name_opt) { |
| 234 if (!deepEquals(found, expected)) { |
| 235 fail(PrettyPrint(expected), found, name_opt); |
| 236 } |
| 237 }; |
| 238 |
| 239 |
| 240 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { |
| 241 var start = ""; |
| 242 if (name_opt) { |
| 243 start = name_opt + " - "; |
| 244 } |
| 245 assertEquals(expected.length, found.length, start + "array length"); |
| 246 if (expected.length == found.length) { |
| 247 for (var i = 0; i < expected.length; ++i) { |
| 248 assertEquals(expected[i], found[i], |
| 249 start + "array element at index " + i); |
| 250 } |
| 251 } |
| 252 }; |
| 253 |
| 254 |
| 255 assertPropertiesEqual = function assertPropertiesEqual(expected, found, |
| 256 name_opt) { |
| 257 // Check properties only. |
| 258 if (!deepObjectEquals(expected, found)) { |
| 259 fail(expected, found, name_opt); |
| 260 } |
| 261 }; |
| 262 |
| 263 |
| 264 assertToStringEquals = function assertToStringEquals(expected, found, |
| 265 name_opt) { |
| 266 if (expected != String(found)) { |
| 267 fail(expected, found, name_opt); |
| 268 } |
| 269 }; |
| 270 |
| 271 |
| 272 assertTrue = function assertTrue(value, name_opt) { |
| 273 assertEquals(true, value, name_opt); |
| 274 }; |
| 275 |
| 276 |
| 277 assertFalse = function assertFalse(value, name_opt) { |
| 278 assertEquals(false, value, name_opt); |
| 279 }; |
| 280 |
| 281 |
| 282 assertNull = function assertNull(value, name_opt) { |
| 283 if (value !== null) { |
| 284 fail("null", value, name_opt); |
| 285 } |
| 286 }; |
| 287 |
| 288 |
| 289 assertNotNull = function assertNotNull(value, name_opt) { |
| 290 if (value === null) { |
| 291 fail("not null", value, name_opt); |
| 292 } |
| 293 }; |
| 294 |
| 295 |
| 296 assertThrows = function assertThrows(code, type_opt, cause_opt) { |
| 297 var threwException = true; |
| 298 try { |
| 299 if (typeof code == 'function') { |
| 300 code(); |
| 301 } else { |
| 302 eval(code); |
| 303 } |
| 304 threwException = false; |
| 305 } catch (e) { |
| 306 if (typeof type_opt == 'function') { |
| 307 assertInstanceof(e, type_opt); |
| 308 } |
| 309 if (arguments.length >= 3) { |
| 310 assertEquals(e.type, cause_opt); |
| 311 } |
| 312 // Success. |
| 313 return; |
| 314 } |
| 315 throw new MjsUnitAssertionError("Did not throw exception"); |
| 316 }; |
| 317 |
| 318 |
| 319 assertInstanceof = function assertInstanceof(obj, type) { |
| 320 if (!(obj instanceof type)) { |
| 321 var actualTypeName = null; |
| 322 var actualConstructor = Object.prototypeOf(obj).constructor; |
| 323 if (typeof actualConstructor == "function") { |
| 324 actualTypeName = actualConstructor.name || String(actualConstructor); |
| 325 } |
| 326 fail("Object <" + PrettyPrint(obj) + "> is not an instance of <" + |
| 327 (type.name || type) + ">" + |
| 328 (actualTypeName ? " but of < " + actualTypeName + ">" : "")); |
| 329 } |
| 330 }; |
| 331 |
| 332 |
| 333 assertDoesNotThrow = function assertDoesNotThrow(code, name_opt) { |
| 334 try { |
| 335 if (typeof code == 'function') { |
| 336 code(); |
| 337 } else { |
| 338 eval(code); |
| 339 } |
| 340 } catch (e) { |
| 341 fail("threw an exception: ", e.message || e, name_opt); |
| 342 } |
| 343 }; |
| 344 |
| 345 assertUnreachable = function assertUnreachable(name_opt) { |
| 101 // Fix this when we ditch the old test runner. | 346 // Fix this when we ditch the old test runner. |
| 102 message += " (" + name_opt + ")"; | 347 var message = "Fail" + "ure: unreachable"; |
| 103 } | 348 if (name_opt) { |
| 104 | 349 message += " - " + name_opt; |
| 105 message += ": expected <" + MjsUnitToString(expected) + | 350 } |
| 106 "> found <" + MjsUnitToString(found) + ">"; | 351 throw new MjsUnitAssertionError(message); |
| 107 throw new MjsUnitAssertionError(message); | 352 }; |
| 108 } | 353 |
| 109 | 354 })(); |
| 110 | 355 |
| 111 function deepObjectEquals(a, b) { | |
| 112 var aProps = []; | |
| 113 for (var key in a) { | |
| 114 aProps.push(key); | |
| 115 } | |
| 116 var bProps = []; | |
| 117 for (key in b) { | |
| 118 bProps.push(key); | |
| 119 } | |
| 120 aProps.sort(); | |
| 121 bProps.sort(); | |
| 122 if (!deepEquals(aProps, bProps)) | |
| 123 return false; | |
| 124 for (var i = 0; i < aProps.length; i++) { | |
| 125 if (!deepEquals(a[aProps[i]], b[aProps[i]])) | |
| 126 return false; | |
| 127 } | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 | |
| 132 function deepEquals(a, b) { | |
| 133 if (a == b) { | |
| 134 // Check for -0. | |
| 135 if (a === 0 && b === 0) return (1 / a) === (1 / b); | |
| 136 return true; | |
| 137 } | |
| 138 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { | |
| 139 return true; | |
| 140 } | |
| 141 if (a == null || b == null) return false; | |
| 142 var aClass = classOf(a); | |
| 143 var bClass = classOf(b); | |
| 144 if (aClass === "RegExp" || bClass === "RegExp") { | |
| 145 return (aClass === bClass) && (a.toString() === b.toString()); | |
| 146 } | |
| 147 if ((typeof a) !== 'object' || (typeof b) !== 'object' || | |
| 148 (a === null) || (b === null)) | |
| 149 return false; | |
| 150 if (aClass === "Array") { | |
| 151 if (bClass !== "Array") | |
| 152 return false; | |
| 153 if (a.length != b.length) | |
| 154 return false; | |
| 155 for (var i = 0; i < a.length; i++) { | |
| 156 if (i in a) { | |
| 157 if (!(i in b) || !(deepEquals(a[i], b[i]))) | |
| 158 return false; | |
| 159 } else if (i in b) { | |
| 160 return false; | |
| 161 } | |
| 162 } | |
| 163 return true; | |
| 164 } else if (bClass == "Array") { | |
| 165 return false; | |
| 166 } else { | |
| 167 return deepObjectEquals(a, b); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 | |
| 172 function assertSame(expected, found, name_opt) { | |
| 173 if (found !== expected) { | |
| 174 fail(expected, found, name_opt); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 | |
| 179 function assertEquals(expected, found, name_opt) { | |
| 180 if (!deepEquals(found, expected)) { | |
| 181 fail(expected, found, name_opt); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 | |
| 186 function assertArrayEquals(expected, found, name_opt) { | |
| 187 var start = ""; | |
| 188 if (name_opt) { | |
| 189 start = name_opt + " - "; | |
| 190 } | |
| 191 assertEquals(expected.length, found.length, start + "array length"); | |
| 192 if (expected.length == found.length) { | |
| 193 for (var i = 0; i < expected.length; ++i) { | |
| 194 assertEquals(expected[i], found[i], start + "array element at index " + i)
; | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 | |
| 200 function assertTrue(value, name_opt) { | |
| 201 assertEquals(true, value, name_opt); | |
| 202 } | |
| 203 | |
| 204 | |
| 205 function assertFalse(value, name_opt) { | |
| 206 assertEquals(false, value, name_opt); | |
| 207 } | |
| 208 | |
| 209 | |
| 210 function assertNaN(value, name_opt) { | |
| 211 if (!isNaN(value)) { | |
| 212 fail("NaN", value, name_opt); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 | |
| 217 function assertNull(value, name_opt) { | |
| 218 if (value !== null) { | |
| 219 fail("null", value, name_opt); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 | |
| 224 function assertNotNull(value, name_opt) { | |
| 225 if (value === null) { | |
| 226 fail("not null", value, name_opt); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 | |
| 231 function assertThrows(code, type_opt, cause_opt) { | |
| 232 var threwException = true; | |
| 233 try { | |
| 234 if (typeof code == 'function') { | |
| 235 code(); | |
| 236 } else { | |
| 237 eval(code); | |
| 238 } | |
| 239 threwException = false; | |
| 240 } catch (e) { | |
| 241 if (typeof type_opt == 'function') | |
| 242 assertInstanceof(e, type_opt); | |
| 243 if (arguments.length >= 3) | |
| 244 assertEquals(e.type, cause_opt); | |
| 245 // Do nothing. | |
| 246 } | |
| 247 if (!threwException) assertTrue(false, "did not throw exception"); | |
| 248 } | |
| 249 | |
| 250 | |
| 251 function assertInstanceof(obj, type) { | |
| 252 if (!(obj instanceof type)) { | |
| 253 assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + ">
"); | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 | |
| 258 function assertDoesNotThrow(code) { | |
| 259 try { | |
| 260 if (typeof code == 'function') { | |
| 261 code(); | |
| 262 } else { | |
| 263 eval(code); | |
| 264 } | |
| 265 } catch (e) { | |
| 266 assertTrue(false, "threw an exception: " + (e.message || e)); | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 | |
| 271 function assertUnreachable(name_opt) { | |
| 272 // Fix this when we ditch the old test runner. | |
| 273 var message = "Fail" + "ure: unreachable"; | |
| 274 if (name_opt) { | |
| 275 message += " - " + name_opt; | |
| 276 } | |
| 277 throw new MjsUnitAssertionError(message); | |
| 278 } | |
| OLD | NEW |