Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 30 | // This allows fetching the stack trace using TryCatch::StackTrace. |
| 31 | this.stack = new Error("").stack; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 40 | |
| 41 | MjsUnitAssertionError.prototype.toString = function () { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 42 | return this.message + "\n\nStack: " + this.stack; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | |
| 58 | // The difference between expected and found value is within certain tolerance. |
| 59 | var assertEqualsDelta; |
| 60 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 61 | // The found object is an Array with the same length and elements |
| 62 | // as the expected object. The expected object doesn't need to be an Array, |
| 63 | // as long as it's "array-ish". |
| 64 | var assertArrayEquals; |
| 65 | |
| 66 | // The found object must have the same enumerable properties as the |
| 67 | // expected object. The type of object isn't checked. |
| 68 | var assertPropertiesEqual; |
| 69 | |
| 70 | // Assert that the string conversion of the found value is equal to |
| 71 | // the expected string. Only kept for backwards compatability, please |
| 72 | // check the real structure of the found value. |
| 73 | var assertToStringEquals; |
| 74 | |
| 75 | // Checks that the found value is true. Use with boolean expressions |
| 76 | // for tests that doesn't have their own assertXXX function. |
| 77 | var assertTrue; |
| 78 | |
| 79 | // Checks that the found value is false. |
| 80 | var assertFalse; |
| 81 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | // Checks that the found value is null. Kept for historical compatibility, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 83 | // please just use assertEquals(null, expected). |
| 84 | var assertNull; |
| 85 | |
| 86 | // Checks that the found value is *not* null. |
| 87 | var assertNotNull; |
| 88 | |
| 89 | // Assert that the passed function or eval code throws an exception. |
| 90 | // The optional second argument is an exception constructor that the |
| 91 | // thrown exception is checked against with "instanceof". |
| 92 | // The optional third argument is a message type string that is compared |
| 93 | // to the type property on the thrown exception. |
| 94 | var assertThrows; |
| 95 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 96 | // Assert that the passed function throws an exception. |
| 97 | // The exception is checked against the second argument using assertEquals. |
| 98 | var assertThrowsEquals; |
| 99 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 100 | // Assert that the passed function or eval code does not throw an exception. |
| 101 | var assertDoesNotThrow; |
| 102 | |
| 103 | // Asserts that the found value is an instance of the constructor passed |
| 104 | // as the second argument. |
| 105 | var assertInstanceof; |
| 106 | |
| 107 | // Assert that this code is never executed (i.e., always fails if executed). |
| 108 | var assertUnreachable; |
| 109 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 110 | // Assert that the function code is (not) optimized. If "no sync" is passed |
| 111 | // as second argument, we do not wait for the concurrent optimization thread to |
| 112 | // finish when polling for optimization status. |
| 113 | // Only works with --allow-natives-syntax. |
| 114 | var assertOptimized; |
| 115 | var assertUnoptimized; |
| 116 | |
| 117 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 118 | (function () { // Scope for utility functions. |
| 119 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 120 | var ObjectPrototypeToString = Object.prototype.toString; |
| 121 | var NumberPrototypeValueOf = Number.prototype.valueOf; |
| 122 | var BooleanPrototypeValueOf = Boolean.prototype.valueOf; |
| 123 | var StringPrototypeValueOf = String.prototype.valueOf; |
| 124 | var DatePrototypeValueOf = Date.prototype.valueOf; |
| 125 | var RegExpPrototypeToString = RegExp.prototype.toString; |
| 126 | var ArrayPrototypeMap = Array.prototype.map; |
| 127 | var ArrayPrototypeJoin = Array.prototype.join; |
| 128 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 129 | function classOf(object) { |
| 130 | // Argument must not be null or undefined. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 131 | var string = ObjectPrototypeToString.call(object); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 132 | // String has format [object <ClassName>]. |
| 133 | return string.substring(8, string.length - 1); |
| 134 | } |
| 135 | |
| 136 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 137 | function ValueOf(value) { |
| 138 | switch (classOf(value)) { |
| 139 | case "Number": |
| 140 | return NumberPrototypeValueOf.call(value); |
| 141 | case "String": |
| 142 | return StringPrototypeValueOf.call(value); |
| 143 | case "Boolean": |
| 144 | return BooleanPrototypeValueOf.call(value); |
| 145 | case "Date": |
| 146 | return DatePrototypeValueOf.call(value); |
| 147 | default: |
| 148 | return value; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 153 | function PrettyPrint(value) { |
| 154 | switch (typeof value) { |
| 155 | case "string": |
| 156 | return JSON.stringify(value); |
| 157 | case "number": |
| 158 | if (value === 0 && (1 / value) < 0) return "-0"; |
| 159 | // FALLTHROUGH. |
| 160 | case "boolean": |
| 161 | case "undefined": |
| 162 | case "function": |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 163 | case "symbol": |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 164 | return String(value); |
| 165 | case "object": |
| 166 | if (value === null) return "null"; |
| 167 | var objectClass = classOf(value); |
| 168 | switch (objectClass) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 169 | case "Number": |
| 170 | case "String": |
| 171 | case "Boolean": |
| 172 | case "Date": |
| 173 | return objectClass + "(" + PrettyPrint(ValueOf(value)) + ")"; |
| 174 | case "RegExp": |
| 175 | return RegExpPrototypeToString.call(value); |
| 176 | case "Array": |
| 177 | var mapped = ArrayPrototypeMap.call(value, PrettyPrintArrayElement); |
| 178 | var joined = ArrayPrototypeJoin.call(mapped, ","); |
| 179 | return "[" + joined + "]"; |
| 180 | case "Object": |
| 181 | break; |
| 182 | default: |
| 183 | return objectClass + "()"; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 184 | } |
| 185 | // [[Class]] is "Object". |
| 186 | var name = value.constructor.name; |
| 187 | if (name) return name + "()"; |
| 188 | return "Object()"; |
| 189 | default: |
| 190 | return "-- unknown value --"; |
| 191 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 193 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 194 | |
| 195 | function PrettyPrintArrayElement(value, index, array) { |
| 196 | if (value === undefined && !(index in array)) return ""; |
| 197 | return PrettyPrint(value); |
| 198 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 199 | |
| 200 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 201 | function fail(expectedText, found, name_opt) { |
| 202 | var message = "Fail" + "ure"; |
| 203 | if (name_opt) { |
| 204 | // Fix this when we ditch the old test runner. |
| 205 | message += " (" + name_opt + ")"; |
| 206 | } |
| 207 | |
| 208 | message += ": expected <" + expectedText + |
| 209 | "> found <" + PrettyPrint(found) + ">"; |
| 210 | throw new MjsUnitAssertionError(message); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | function deepObjectEquals(a, b) { |
| 215 | var aProps = Object.keys(a); |
| 216 | aProps.sort(); |
| 217 | var bProps = Object.keys(b); |
| 218 | bProps.sort(); |
| 219 | if (!deepEquals(aProps, bProps)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | return false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 221 | } |
| 222 | for (var i = 0; i < aProps.length; i++) { |
| 223 | if (!deepEquals(a[aProps[i]], b[aProps[i]])) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | return true; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | function deepEquals(a, b) { |
| 232 | if (a === b) { |
| 233 | // Check for -0. |
| 234 | if (a === 0) return (1 / a) === (1 / b); |
| 235 | return true; |
| 236 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 237 | if (typeof a !== typeof b) return false; |
| 238 | if (typeof a === "number") return isNaN(a) && isNaN(b); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 239 | if (typeof a !== "object" && typeof a !== "function") return false; |
| 240 | // Neither a nor b is primitive. |
| 241 | var objectClass = classOf(a); |
| 242 | if (objectClass !== classOf(b)) return false; |
| 243 | if (objectClass === "RegExp") { |
| 244 | // For RegExp, just compare pattern and flags using its toString. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 245 | return RegExpPrototypeToString.call(a) === |
| 246 | RegExpPrototypeToString.call(b); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 247 | } |
| 248 | // Functions are only identical to themselves. |
| 249 | if (objectClass === "Function") return false; |
| 250 | if (objectClass === "Array") { |
| 251 | var elementCount = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 252 | if (a.length !== b.length) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | for (var i = 0; i < a.length; i++) { |
| 256 | if (!deepEquals(a[i], b[i])) return false; |
| 257 | } |
| 258 | return true; |
| 259 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 260 | if (objectClass === "String" || objectClass === "Number" || |
| 261 | objectClass === "Boolean" || objectClass === "Date") { |
| 262 | if (ValueOf(a) !== ValueOf(b)) return false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 263 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | return deepObjectEquals(a, b); |
| 265 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 266 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 267 | assertSame = function assertSame(expected, found, name_opt) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 268 | // TODO(mstarzinger): We should think about using Harmony's egal operator |
| 269 | // or the function equivalent Object.is() here. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 270 | if (found === expected) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 271 | if (expected !== 0 || (1 / expected) === (1 / found)) return; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 272 | } else if ((expected !== expected) && (found !== found)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 273 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 274 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 275 | fail(PrettyPrint(expected), found, name_opt); |
| 276 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 277 | |
| 278 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 279 | assertEquals = function assertEquals(expected, found, name_opt) { |
| 280 | if (!deepEquals(found, expected)) { |
| 281 | fail(PrettyPrint(expected), found, name_opt); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 283 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | |
| 285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 286 | assertEqualsDelta = |
| 287 | function assertEqualsDelta(expected, found, delta, name_opt) { |
| 288 | assertTrue(Math.abs(expected - found) <= delta, name_opt); |
| 289 | }; |
| 290 | |
| 291 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 292 | assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { |
| 293 | var start = ""; |
| 294 | if (name_opt) { |
| 295 | start = name_opt + " - "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 297 | assertEquals(expected.length, found.length, start + "array length"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 298 | if (expected.length === found.length) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 299 | for (var i = 0; i < expected.length; ++i) { |
| 300 | assertEquals(expected[i], found[i], |
| 301 | start + "array element at index " + i); |
| 302 | } |
| 303 | } |
| 304 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | |
| 306 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 307 | assertPropertiesEqual = function assertPropertiesEqual(expected, found, |
| 308 | name_opt) { |
| 309 | // Check properties only. |
| 310 | if (!deepObjectEquals(expected, found)) { |
| 311 | fail(expected, found, name_opt); |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | |
| 316 | assertToStringEquals = function assertToStringEquals(expected, found, |
| 317 | name_opt) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 318 | if (expected !== String(found)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 319 | fail(expected, found, name_opt); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | assertTrue = function assertTrue(value, name_opt) { |
| 325 | assertEquals(true, value, name_opt); |
| 326 | }; |
| 327 | |
| 328 | |
| 329 | assertFalse = function assertFalse(value, name_opt) { |
| 330 | assertEquals(false, value, name_opt); |
| 331 | }; |
| 332 | |
| 333 | |
| 334 | assertNull = function assertNull(value, name_opt) { |
| 335 | if (value !== null) { |
| 336 | fail("null", value, name_opt); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | |
| 341 | assertNotNull = function assertNotNull(value, name_opt) { |
| 342 | if (value === null) { |
| 343 | fail("not null", value, name_opt); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | |
| 348 | assertThrows = function assertThrows(code, type_opt, cause_opt) { |
| 349 | var threwException = true; |
| 350 | try { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 351 | if (typeof code === 'function') { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 352 | code(); |
| 353 | } else { |
| 354 | eval(code); |
| 355 | } |
| 356 | threwException = false; |
| 357 | } catch (e) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 358 | if (typeof type_opt === 'function') { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 359 | assertInstanceof(e, type_opt); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 360 | } else if (type_opt !== void 0) { |
| 361 | fail("invalid use of assertThrows, maybe you want assertThrowsEquals"); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 362 | } |
| 363 | if (arguments.length >= 3) { |
| 364 | assertEquals(e.type, cause_opt); |
| 365 | } |
| 366 | // Success. |
| 367 | return; |
| 368 | } |
| 369 | throw new MjsUnitAssertionError("Did not throw exception"); |
| 370 | }; |
| 371 | |
| 372 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 373 | assertThrowsEquals = function assertThrowsEquals(fun, val) { |
| 374 | try { |
| 375 | fun(); |
| 376 | } catch(e) { |
| 377 | assertEquals(val, e); |
| 378 | return; |
| 379 | } |
| 380 | throw new MjsUnitAssertionError("Did not throw exception"); |
| 381 | }; |
| 382 | |
| 383 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 384 | assertInstanceof = function assertInstanceof(obj, type) { |
| 385 | if (!(obj instanceof type)) { |
| 386 | var actualTypeName = null; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | var actualConstructor = Object.getPrototypeOf(obj).constructor; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 388 | if (typeof actualConstructor === "function") { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 389 | actualTypeName = actualConstructor.name || String(actualConstructor); |
| 390 | } |
| 391 | fail("Object <" + PrettyPrint(obj) + "> is not an instance of <" + |
| 392 | (type.name || type) + ">" + |
| 393 | (actualTypeName ? " but of < " + actualTypeName + ">" : "")); |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | |
| 398 | assertDoesNotThrow = function assertDoesNotThrow(code, name_opt) { |
| 399 | try { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 400 | if (typeof code === 'function') { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 401 | code(); |
| 402 | } else { |
| 403 | eval(code); |
| 404 | } |
| 405 | } catch (e) { |
| 406 | fail("threw an exception: ", e.message || e, name_opt); |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | assertUnreachable = function assertUnreachable(name_opt) { |
| 411 | // Fix this when we ditch the old test runner. |
| 412 | var message = "Fail" + "ure: unreachable"; |
| 413 | if (name_opt) { |
| 414 | message += " - " + name_opt; |
| 415 | } |
| 416 | throw new MjsUnitAssertionError(message); |
| 417 | }; |
| 418 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 419 | var OptimizationStatusImpl = undefined; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 420 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | var OptimizationStatus = function(fun, sync_opt) { |
| 422 | if (OptimizationStatusImpl === undefined) { |
| 423 | try { |
| 424 | OptimizationStatusImpl = new Function( |
| 425 | "fun", "sync", "return %GetOptimizationStatus(fun, sync);"); |
| 426 | } catch (e) { |
| 427 | throw new Error("natives syntax not allowed"); |
| 428 | } |
| 429 | } |
| 430 | return OptimizationStatusImpl(fun, sync_opt); |
| 431 | } |
| 432 | |
| 433 | assertUnoptimized = function assertUnoptimized(fun, sync_opt, name_opt) { |
| 434 | if (sync_opt === undefined) sync_opt = ""; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 435 | assertTrue(OptimizationStatus(fun, sync_opt) !== 1, name_opt); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | assertOptimized = function assertOptimized(fun, sync_opt, name_opt) { |
| 439 | if (sync_opt === undefined) sync_opt = ""; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 440 | assertTrue(OptimizationStatus(fun, sync_opt) !== 2, name_opt); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | })(); |