1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/shell.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,176 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 + 1.9 +if (typeof assertThrowsInstanceOf === 'undefined') { 1.10 + var assertThrowsInstanceOf = function assertThrowsInstanceOf(f, ctor, msg) { 1.11 + var fullmsg; 1.12 + try { 1.13 + f(); 1.14 + } catch (exc) { 1.15 + if (exc instanceof ctor) 1.16 + return; 1.17 + fullmsg = "Assertion failed: expected exception " + ctor.name + ", got " + exc; 1.18 + } 1.19 + if (fullmsg === undefined) 1.20 + fullmsg = "Assertion failed: expected exception " + ctor.name + ", no exception thrown"; 1.21 + if (msg !== undefined) 1.22 + fullmsg += " - " + msg; 1.23 + throw new Error(fullmsg); 1.24 + }; 1.25 +} 1.26 + 1.27 +if (typeof assertThrowsValue === 'undefined') { 1.28 + var assertThrowsValue = function assertThrowsValue(f, val, msg) { 1.29 + var fullmsg; 1.30 + try { 1.31 + f(); 1.32 + } catch (exc) { 1.33 + if ((exc === val) === (val === val) && (val !== 0 || 1 / exc === 1 / val)) 1.34 + return; 1.35 + fullmsg = "Assertion failed: expected exception " + val + ", got " + exc; 1.36 + } 1.37 + if (fullmsg === undefined) 1.38 + fullmsg = "Assertion failed: expected exception " + val + ", no exception thrown"; 1.39 + if (msg !== undefined) 1.40 + fullmsg += " - " + msg; 1.41 + throw new Error(fullmsg); 1.42 + }; 1.43 +} 1.44 + 1.45 +if (typeof assertDeepEq === 'undefined') { 1.46 + var assertDeepEq = (function(){ 1.47 + var call = Function.prototype.call, 1.48 + Map_ = Map, 1.49 + Error_ = Error, 1.50 + Map_has = call.bind(Map.prototype.has), 1.51 + Map_get = call.bind(Map.prototype.get), 1.52 + Map_set = call.bind(Map.prototype.set), 1.53 + Object_toString = call.bind(Object.prototype.toString), 1.54 + Function_toString = call.bind(Function.prototype.toString), 1.55 + Object_getPrototypeOf = Object.getPrototypeOf, 1.56 + Object_hasOwnProperty = call.bind(Object.prototype.hasOwnProperty), 1.57 + Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor, 1.58 + Object_isExtensible = Object.isExtensible, 1.59 + Object_getOwnPropertyNames = Object.getOwnPropertyNames, 1.60 + uneval_ = uneval; 1.61 + 1.62 + // Return true iff ES6 Type(v) isn't Object. 1.63 + // Note that `typeof document.all === "undefined"`. 1.64 + function isPrimitive(v) { 1.65 + return (v === null || 1.66 + v === undefined || 1.67 + typeof v === "boolean" || 1.68 + typeof v === "number" || 1.69 + typeof v === "string" || 1.70 + typeof v === "symbol"); 1.71 + } 1.72 + 1.73 + function assertSameValue(a, b, msg) { 1.74 + try { 1.75 + assertEq(a, b); 1.76 + } catch (exc) { 1.77 + throw new Error(exc.message + (msg ? " " + msg : "")); 1.78 + } 1.79 + } 1.80 + 1.81 + function assertSameClass(a, b, msg) { 1.82 + var ac = Object_toString(a), bc = Object_toString(b); 1.83 + assertSameValue(ac, bc, msg); 1.84 + switch (ac) { 1.85 + case "[object Function]": 1.86 + assertSameValue(Function_toString(a), Function_toString(b), msg); 1.87 + } 1.88 + } 1.89 + 1.90 + function at(prevmsg, segment) { 1.91 + return prevmsg ? prevmsg + segment : "at _" + segment; 1.92 + } 1.93 + 1.94 + // Assert that the arguments a and b are thoroughly structurally equivalent. 1.95 + // 1.96 + // For the sake of speed, we cut a corner: 1.97 + // var x = {}, y = {}, ax = [x]; 1.98 + // assertDeepEq([ax, x], [ax, y]); // passes (?!) 1.99 + // 1.100 + // Technically this should fail, since the two object graphs are different. 1.101 + // (The graph of [ax, y] contains one more object than the graph of [ax, x].) 1.102 + // 1.103 + // To get technically correct behavior, pass {strictEquivalence: true}. 1.104 + // This is slower because we have to walk the entire graph, and Object.prototype 1.105 + // is big. 1.106 + // 1.107 + return function assertDeepEq(a, b, options) { 1.108 + var strictEquivalence = options ? options.strictEquivalence : false; 1.109 + 1.110 + function assertSameProto(a, b, msg) { 1.111 + check(Object_getPrototypeOf(a), Object_getPrototypeOf(b), at(msg, ".__proto__")); 1.112 + } 1.113 + 1.114 + function failPropList(na, nb, msg) { 1.115 + throw Error_("got own properties " + uneval_(na) + ", expected " + uneval_(nb) + 1.116 + (msg ? " " + msg : "")); 1.117 + } 1.118 + 1.119 + function assertSameProps(a, b, msg) { 1.120 + var na = Object_getOwnPropertyNames(a), 1.121 + nb = Object_getOwnPropertyNames(b); 1.122 + if (na.length !== nb.length) 1.123 + failPropList(na, nb, msg); 1.124 + for (var i = 0; i < na.length; i++) { 1.125 + var name = na[i]; 1.126 + if (name !== nb[i]) 1.127 + failPropList(na, nb, msg); 1.128 + var da = Object_getOwnPropertyDescriptor(a, name), 1.129 + db = Object_getOwnPropertyDescriptor(b, name); 1.130 + var pmsg = at(msg, /^[_$A-Za-z0-9]+$/.test(name) 1.131 + ? /0|[1-9][0-9]*/.test(name) ? "[" + name + "]" : "." + name 1.132 + : "[" + uneval_(name) + "]"); 1.133 + assertSameValue(da.configurable, db.configurable, at(pmsg, ".[[Configurable]]")); 1.134 + assertSameValue(da.enumerable, db.enumerable, at(pmsg, ".[[Enumerable]]")); 1.135 + if (Object_hasOwnProperty(da, "value")) { 1.136 + if (!Object_hasOwnProperty(db, "value")) 1.137 + throw Error_("got data property, expected accessor property" + pmsg); 1.138 + check(da.value, db.value, pmsg); 1.139 + } else { 1.140 + if (Object_hasOwnProperty(db, "value")) 1.141 + throw Error_("got accessor property, expected data property" + pmsg); 1.142 + check(da.get, db.get, at(pmsg, ".[[Get]]")); 1.143 + check(da.set, db.set, at(pmsg, ".[[Set]]")); 1.144 + } 1.145 + } 1.146 + }; 1.147 + 1.148 + var ab = Map_(); 1.149 + var bpath = Map_(); 1.150 + 1.151 + function check(a, b, path) { 1.152 + if (isPrimitive(a)) { 1.153 + assertSameValue(a, b, path); 1.154 + } else if (isPrimitive(b)) { 1.155 + throw Error_("got " + Object_toString(a) + ", expected " + uneval_(b) + " " + path); 1.156 + } else if (Map_has(ab, a)) { 1.157 + assertSameValue(Map_get(ab, a), b, path); 1.158 + } else if (Map_has(bpath, b)) { 1.159 + var bPrevPath = Map_get(bpath, b) || "_"; 1.160 + throw Error_("got distinct objects " + at(path, "") + " and " + at(bPrevPath, "") + 1.161 + ", expected the same object both places"); 1.162 + } else { 1.163 + Map_set(ab, a, b); 1.164 + Map_set(bpath, b, path); 1.165 + if (a !== b || strictEquivalence) { 1.166 + assertSameClass(a, b, path); 1.167 + assertSameProto(a, b, path); 1.168 + assertSameProps(a, b, path); 1.169 + assertSameValue(Object_isExtensible(a), 1.170 + Object_isExtensible(b), 1.171 + at(path, ".[[Extensible]]")); 1.172 + } 1.173 + } 1.174 + } 1.175 + 1.176 + check(a, b, ""); 1.177 + }; 1.178 + })(); 1.179 +}