1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/Date/toJSON-01.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,238 @@ 1.4 +// Any copyright is dedicated to the Public Domain. 1.5 +// http://creativecommons.org/licenses/publicdomain/ 1.6 + 1.7 +var gTestfile = 'toJSON-01.js'; 1.8 +//----------------------------------------------------------------------------- 1.9 +var BUGNUMBER = 584811; 1.10 +var summary = "Date.prototype.toJSON isn't to spec"; 1.11 + 1.12 +print(BUGNUMBER + ": " + summary); 1.13 + 1.14 +/************** 1.15 + * BEGIN TEST * 1.16 + **************/ 1.17 + 1.18 +var called; 1.19 + 1.20 +var dateToJSON = Date.prototype.toJSON; 1.21 +assertEq(Date.prototype.hasOwnProperty("toJSON"), true); 1.22 +assertEq(typeof dateToJSON, "function"); 1.23 + 1.24 +// brief test to exercise this outside of isolation, just for sanity 1.25 +var invalidDate = new Date(); 1.26 +invalidDate.setTime(NaN); 1.27 +assertEq(JSON.stringify({ p: invalidDate }), '{"p":null}'); 1.28 + 1.29 + 1.30 +/* 15.9.5.44 Date.prototype.toJSON ( key ) */ 1.31 +assertEq(dateToJSON.length, 1); 1.32 + 1.33 +/* 1.34 + * 1. Let O be the result of calling ToObject, giving it the this value as its 1.35 + * argument. 1.36 + */ 1.37 +try 1.38 +{ 1.39 + dateToJSON.call(null); 1.40 + throw new Error("should have thrown a TypeError"); 1.41 +} 1.42 +catch (e) 1.43 +{ 1.44 + assertEq(e instanceof TypeError, true, 1.45 + "ToObject throws TypeError for null/undefined"); 1.46 +} 1.47 + 1.48 +try 1.49 +{ 1.50 + dateToJSON.call(undefined); 1.51 + throw new Error("should have thrown a TypeError"); 1.52 +} 1.53 +catch (e) 1.54 +{ 1.55 + assertEq(e instanceof TypeError, true, 1.56 + "ToObject throws TypeError for null/undefined"); 1.57 +} 1.58 + 1.59 + 1.60 +/* 1.61 + * 2. Let tv be ToPrimitive(O, hint Number). 1.62 + * ...expands to: 1.63 + * 1. Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf". 1.64 + * 2. If IsCallable(valueOf) is true then, 1.65 + * a. Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and 1.66 + * an empty argument list. 1.67 + * b. If val is a primitive value, return val. 1.68 + * 3. Let toString be the result of calling the [[Get]] internal method of object O with argument "toString". 1.69 + * 4. If IsCallable(toString) is true then, 1.70 + * a. Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and 1.71 + * an empty argument list. 1.72 + * b. If str is a primitive value, return str. 1.73 + * 5. Throw a TypeError exception. 1.74 + */ 1.75 +try 1.76 +{ 1.77 + var r = dateToJSON.call({ get valueOf() { throw 17; } }); 1.78 + throw new Error("didn't throw, returned: " + r); 1.79 +} 1.80 +catch (e) 1.81 +{ 1.82 + assertEq(e, 17, "bad exception: " + e); 1.83 +} 1.84 + 1.85 +called = false; 1.86 +assertEq(dateToJSON.call({ valueOf: null, 1.87 + toString: function() { called = true; return 12; }, 1.88 + toISOString: function() { return "ohai"; } }), 1.89 + "ohai"); 1.90 +assertEq(called, true); 1.91 + 1.92 +called = false; 1.93 +assertEq(dateToJSON.call({ valueOf: function() { called = true; return 42; }, 1.94 + toISOString: function() { return null; } }), 1.95 + null); 1.96 +assertEq(called, true); 1.97 + 1.98 +try 1.99 +{ 1.100 + called = false; 1.101 + dateToJSON.call({ valueOf: function() { called = true; return {}; }, 1.102 + get toString() { throw 42; } }); 1.103 +} 1.104 +catch (e) 1.105 +{ 1.106 + assertEq(called, true); 1.107 + assertEq(e, 42, "bad exception: " + e); 1.108 +} 1.109 + 1.110 +called = false; 1.111 +assertEq(dateToJSON.call({ valueOf: function() { called = true; return {}; }, 1.112 + get toString() { return function() { return 8675309; }; }, 1.113 + toISOString: function() { return true; } }), 1.114 + true); 1.115 +assertEq(called, true); 1.116 + 1.117 +var asserted = false; 1.118 +called = false; 1.119 +assertEq(dateToJSON.call({ valueOf: function() { called = true; return {}; }, 1.120 + get toString() 1.121 + { 1.122 + assertEq(called, true); 1.123 + asserted = true; 1.124 + return function() { return 8675309; }; 1.125 + }, 1.126 + toISOString: function() { return NaN; } }), 1.127 + NaN); 1.128 +assertEq(asserted, true); 1.129 + 1.130 +try 1.131 +{ 1.132 + var r = dateToJSON.call({ valueOf: null, toString: null, 1.133 + get toISOString() 1.134 + { 1.135 + throw new Error("shouldn't have been gotten"); 1.136 + } }); 1.137 + throw new Error("didn't throw, returned: " + r); 1.138 +} 1.139 +catch (e) 1.140 +{ 1.141 + assertEq(e instanceof TypeError, true, "bad exception: " + e); 1.142 +} 1.143 + 1.144 + 1.145 +/* 3. If tv is a Number and is not finite, return null. */ 1.146 +assertEq(dateToJSON.call({ valueOf: function() { return Infinity; } }), null); 1.147 +assertEq(dateToJSON.call({ valueOf: function() { return -Infinity; } }), null); 1.148 +assertEq(dateToJSON.call({ valueOf: function() { return NaN; } }), null); 1.149 + 1.150 +assertEq(dateToJSON.call({ valueOf: function() { return Infinity; }, 1.151 + toISOString: function() { return {}; } }), null); 1.152 +assertEq(dateToJSON.call({ valueOf: function() { return -Infinity; }, 1.153 + toISOString: function() { return []; } }), null); 1.154 +assertEq(dateToJSON.call({ valueOf: function() { return NaN; }, 1.155 + toISOString: function() { return undefined; } }), null); 1.156 + 1.157 + 1.158 +/* 1.159 + * 4. Let toISO be the result of calling the [[Get]] internal method of O with 1.160 + * argument "toISOString". 1.161 + */ 1.162 +try 1.163 +{ 1.164 + var r = dateToJSON.call({ get toISOString() { throw 42; } }); 1.165 + throw new Error("didn't throw, returned: " + r); 1.166 +} 1.167 +catch (e) 1.168 +{ 1.169 + assertEq(e, 42, "bad exception: " + e); 1.170 +} 1.171 + 1.172 + 1.173 +/* 5. If IsCallable(toISO) is false, throw a TypeError exception. */ 1.174 +try 1.175 +{ 1.176 + var r = dateToJSON.call({ toISOString: null }); 1.177 + throw new Error("didn't throw, returned: " + r); 1.178 +} 1.179 +catch (e) 1.180 +{ 1.181 + assertEq(e instanceof TypeError, true, "bad exception: " + e); 1.182 +} 1.183 + 1.184 +try 1.185 +{ 1.186 + var r = dateToJSON.call({ toISOString: undefined }); 1.187 + throw new Error("didn't throw, returned: " + r); 1.188 +} 1.189 +catch (e) 1.190 +{ 1.191 + assertEq(e instanceof TypeError, true, "bad exception: " + e); 1.192 +} 1.193 + 1.194 +try 1.195 +{ 1.196 + var r = dateToJSON.call({ toISOString: "oogabooga" }); 1.197 + throw new Error("didn't throw, returned: " + r); 1.198 +} 1.199 +catch (e) 1.200 +{ 1.201 + assertEq(e instanceof TypeError, true, "bad exception: " + e); 1.202 +} 1.203 + 1.204 +try 1.205 +{ 1.206 + var r = dateToJSON.call({ toISOString: Math.PI }); 1.207 + throw new Error("didn't throw, returned: " + r); 1.208 +} 1.209 +catch (e) 1.210 +{ 1.211 + assertEq(e instanceof TypeError, true, "bad exception: " + e); 1.212 +} 1.213 + 1.214 + 1.215 +/* 1.216 + * 6. Return the result of calling the [[Call]] internal method of toISO with O 1.217 + * as the this value and an empty argument list. 1.218 + */ 1.219 +var o = 1.220 + { 1.221 + toISOString: function(a) 1.222 + { 1.223 + called = true; 1.224 + assertEq(this, o); 1.225 + assertEq(a, undefined); 1.226 + assertEq(arguments.length, 0); 1.227 + return obj; 1.228 + } 1.229 + }; 1.230 +var obj = {}; 1.231 +called = false; 1.232 +assertEq(dateToJSON.call(o), obj, "should have gotten obj back"); 1.233 +assertEq(called, true); 1.234 + 1.235 + 1.236 +/******************************************************************************/ 1.237 + 1.238 +if (typeof reportCompare === "function") 1.239 + reportCompare(true, true); 1.240 + 1.241 +print("All tests passed!");