1 // Copyright 2008 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 function MjsUnitAssertionError(message) { 29 this.message = message; 30 // This allows fetching the stack trace using TryCatch::StackTrace. 31 this.stack = new Error("").stack; 32 } 33 34 /* 35 * This file is included in all mini jsunit test cases. The test 36 * framework expects lines that signal failed tests to start with 37 * the f-word and ignore all other lines. 38 */ 39 40 41 MjsUnitAssertionError.prototype.toString = function () { 42 return this.message; 43 }; 44 45 46 // Expected and found values the same objects, or the same primitive 47 // values. 48 // For known primitive values, please use assertEquals. 49 var assertSame; 50 51 // Expected and found values are identical primitive values or functions 52 // or similarly structured objects (checking internal properties 53 // of, e.g., Number and Date objects, the elements of arrays 54 // and the properties of non-Array objects). 55 var assertEquals; 56 57 // The found object is an Array with the same length and elements 58 // as the expected object. The expected object doesn't need to be an Array, 59 // as long as it's "array-ish". 60 var assertArrayEquals; 61 62 // The found object must have the same enumerable properties as the 63 // expected object. The type of object isn't checked. 64 var assertPropertiesEqual; 65 66 // Assert that the string conversion of the found value is equal to 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 compatibility, 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 // Assert that the function code is (not) optimized. If "no sync" is passed 103 // as second argument, we do not wait for the parallel optimization thread to 104 // finish when polling for optimization status. 105 // Only works with --allow-natives-syntax. 106 var assertOptimized; 107 var assertUnoptimized; 108 109 110 (function () { // Scope for utility functions. 111 112 function classOf(object) { 113 // Argument must not be null or undefined. 114 var string = Object.prototype.toString.call(object); 115 // String has format [object <ClassName>]. 116 return string.substring(8, string.length - 1); 117 } 118 119 120 function PrettyPrint(value) { 121 switch (typeof value) { 122 case "string": 123 return JSON.stringify(value); 124 case "number": 125 if (value === 0 && (1 / value) < 0) return "-0"; 126 // FALLTHROUGH. 127 case "boolean": 128 case "undefined": 129 case "function": 130 return String(value); 131 case "object": 132 if (value === null) return "null"; 133 var objectClass = classOf(value); 134 switch (objectClass) { 135 case "Number": 136 case "String": 137 case "Boolean": 138 case "Date": 139 return objectClass + "(" + PrettyPrint(value.valueOf()) + ")"; 140 case "RegExp": 141 return value.toString(); 142 case "Array": 143 return "[" + value.map(PrettyPrintArrayElement).join(",") + "]"; 144 case "Object": 145 break; 146 default: 147 return objectClass + "()"; 148 } 149 // [[Class]] is "Object". 150 var name = value.constructor.name; 151 if (name) return name + "()"; 152 return "Object()"; 153 default: 154 return "-- unknown value --"; 155 } 156 } 157 158 159 function PrettyPrintArrayElement(value, index, array) { 160 if (value === undefined && !(index in array)) return ""; 161 return PrettyPrint(value); 162 } 163 164 165 function fail(expectedText, found, name_opt) { 166 var message = "Fail" + "ure"; 167 if (name_opt) { 168 // Fix this when we ditch the old test runner. 169 message += " (" + name_opt + ")"; 170 } 171 172 message += ": expected <" + expectedText + 173 "> found <" + PrettyPrint(found) + ">"; 174 throw new MjsUnitAssertionError(message); 175 } 176 177 178 function deepObjectEquals(a, b) { 179 var aProps = Object.keys(a); 180 aProps.sort(); 181 var bProps = Object.keys(b); 182 bProps.sort(); 183 if (!deepEquals(aProps, bProps)) { 184 return false; 185 } 186 for (var i = 0; i < aProps.length; i++) { 187 if (!deepEquals(a[aProps[i]], b[aProps[i]])) { 188 return false; 189 } 190 } 191 return true; 192 } 193 194 195 function deepEquals(a, b) { 196 if (a === b) { 197 // Check for -0. 198 if (a === 0) return (1 / a) === (1 / b); 199 return true; 200 } 201 if (typeof a != typeof b) return false; 202 if (typeof a == "number") return isNaN(a) && isNaN(b); 203 if (typeof a !== "object" && typeof a !== "function") return false; 204 // Neither a nor b is primitive. 205 var objectClass = classOf(a); 206 if (objectClass !== classOf(b)) return false; 207 if (objectClass === "RegExp") { 208 // For RegExp, just compare pattern and flags using its toString. 209 return (a.toString() === b.toString()); 210 } 211 // Functions are only identical to themselves. 212 if (objectClass === "Function") return false; 213 if (objectClass === "Array") { 214 var elementCount = 0; 215 if (a.length != b.length) { 216 return false; 217 } 218 for (var i = 0; i < a.length; i++) { 219 if (!deepEquals(a[i], b[i])) return false; 220 } 221 return true; 222 } 223 if (objectClass == "String" || objectClass == "Number" || 224 objectClass == "Boolean" || objectClass == "Date") { 225 if (a.valueOf() !== b.valueOf()) return false; 226 } 227 return deepObjectEquals(a, b); 228 } 229 230 231 assertSame = function assertSame(expected, found, name_opt) { 232 // TODO(mstarzinger): We should think about using Harmony's egal operator 233 // or the function equivalent Object.is() here. 234 if (found === expected) { 235 if (expected !== 0 || (1 / expected) == (1 / found)) return; 236 } else if ((expected !== expected) && (found !== found)) { 237 return; 238 } 239 fail(PrettyPrint(expected), found, name_opt); 240 }; 241 242 243 assertEquals = function assertEquals(expected, found, name_opt) { 244 if (!deepEquals(found, expected)) { 245 fail(PrettyPrint(expected), found, name_opt); 246 } 247 }; 248 249 250 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { 251 var start = ""; 252 if (name_opt) { 253 start = name_opt + " - "; 254 } 255 assertEquals(expected.length, found.length, start + "array length"); 256 if (expected.length == found.length) { 257 for (var i = 0; i < expected.length; ++i) { 258 assertEquals(expected[i], found[i], 259 start + "array element at index " + i); 260 } 261 } 262 }; 263 264 265 assertPropertiesEqual = function assertPropertiesEqual(expected, found, 266 name_opt) { 267 // Check properties only. 268 if (!deepObjectEquals(expected, found)) { 269 fail(expected, found, name_opt); 270 } 271 }; 272 273 274 assertToStringEquals = function assertToStringEquals(expected, found, 275 name_opt) { 276 if (expected != String(found)) { 277 fail(expected, found, name_opt); 278 } 279 }; 280 281 282 assertTrue = function assertTrue(value, name_opt) { 283 assertEquals(true, value, name_opt); 284 }; 285 286 287 assertFalse = function assertFalse(value, name_opt) { 288 assertEquals(false, value, name_opt); 289 }; 290 291 292 assertNull = function assertNull(value, name_opt) { 293 if (value !== null) { 294 fail("null", value, name_opt); 295 } 296 }; 297 298 299 assertNotNull = function assertNotNull(value, name_opt) { 300 if (value === null) { 301 fail("not null", value, name_opt); 302 } 303 }; 304 305 306 assertThrows = function assertThrows(code, type_opt, cause_opt) { 307 var threwException = true; 308 try { 309 if (typeof code == 'function') { 310 code(); 311 } else { 312 eval(code); 313 } 314 threwException = false; 315 } catch (e) { 316 if (typeof type_opt == 'function') { 317 assertInstanceof(e, type_opt); 318 } 319 if (arguments.length >= 3) { 320 assertEquals(e.type, cause_opt); 321 } 322 // Success. 323 return; 324 } 325 throw new MjsUnitAssertionError("Did not throw exception"); 326 }; 327 328 329 assertInstanceof = function assertInstanceof(obj, type) { 330 if (!(obj instanceof type)) { 331 var actualTypeName = null; 332 var actualConstructor = Object.getPrototypeOf(obj).constructor; 333 if (typeof actualConstructor == "function") { 334 actualTypeName = actualConstructor.name || String(actualConstructor); 335 } 336 fail("Object <" + PrettyPrint(obj) + "> is not an instance of <" + 337 (type.name || type) + ">" + 338 (actualTypeName ? " but of < " + actualTypeName + ">" : "")); 339 } 340 }; 341 342 343 assertDoesNotThrow = function assertDoesNotThrow(code, name_opt) { 344 try { 345 if (typeof code == 'function') { 346 code(); 347 } else { 348 eval(code); 349 } 350 } catch (e) { 351 fail("threw an exception: ", e.message || e, name_opt); 352 } 353 }; 354 355 assertUnreachable = function assertUnreachable(name_opt) { 356 // Fix this when we ditch the old test runner. 357 var message = "Fail" + "ure: unreachable"; 358 if (name_opt) { 359 message += " - " + name_opt; 360 } 361 throw new MjsUnitAssertionError(message); 362 }; 363 364 365 var OptimizationStatus; 366 try { 367 OptimizationStatus = 368 new Function("fun", "sync", "return %GetOptimizationStatus(fun, sync);"); 369 } catch (e) { 370 OptimizationStatus = function() { 371 throw new Error("natives syntax not allowed"); 372 } 373 } 374 375 assertUnoptimized = function assertUnoptimized(fun, sync_opt, name_opt) { 376 if (sync_opt === undefined) sync_opt = ""; 377 assertTrue(OptimizationStatus(fun, sync_opt) != 1, name_opt); 378 } 379 380 assertOptimized = function assertOptimized(fun, sync_opt, name_opt) { 381 if (sync_opt === undefined) sync_opt = ""; 382 assertTrue(OptimizationStatus(fun, sync_opt) != 2, name_opt); 383 } 384 385 })(); 386 387