js/src/tests/ecma_5/Date/toJSON-01.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Any copyright is dedicated to the Public Domain.
michael@0 2 // http://creativecommons.org/licenses/publicdomain/
michael@0 3
michael@0 4 var gTestfile = 'toJSON-01.js';
michael@0 5 //-----------------------------------------------------------------------------
michael@0 6 var BUGNUMBER = 584811;
michael@0 7 var summary = "Date.prototype.toJSON isn't to spec";
michael@0 8
michael@0 9 print(BUGNUMBER + ": " + summary);
michael@0 10
michael@0 11 /**************
michael@0 12 * BEGIN TEST *
michael@0 13 **************/
michael@0 14
michael@0 15 var called;
michael@0 16
michael@0 17 var dateToJSON = Date.prototype.toJSON;
michael@0 18 assertEq(Date.prototype.hasOwnProperty("toJSON"), true);
michael@0 19 assertEq(typeof dateToJSON, "function");
michael@0 20
michael@0 21 // brief test to exercise this outside of isolation, just for sanity
michael@0 22 var invalidDate = new Date();
michael@0 23 invalidDate.setTime(NaN);
michael@0 24 assertEq(JSON.stringify({ p: invalidDate }), '{"p":null}');
michael@0 25
michael@0 26
michael@0 27 /* 15.9.5.44 Date.prototype.toJSON ( key ) */
michael@0 28 assertEq(dateToJSON.length, 1);
michael@0 29
michael@0 30 /*
michael@0 31 * 1. Let O be the result of calling ToObject, giving it the this value as its
michael@0 32 * argument.
michael@0 33 */
michael@0 34 try
michael@0 35 {
michael@0 36 dateToJSON.call(null);
michael@0 37 throw new Error("should have thrown a TypeError");
michael@0 38 }
michael@0 39 catch (e)
michael@0 40 {
michael@0 41 assertEq(e instanceof TypeError, true,
michael@0 42 "ToObject throws TypeError for null/undefined");
michael@0 43 }
michael@0 44
michael@0 45 try
michael@0 46 {
michael@0 47 dateToJSON.call(undefined);
michael@0 48 throw new Error("should have thrown a TypeError");
michael@0 49 }
michael@0 50 catch (e)
michael@0 51 {
michael@0 52 assertEq(e instanceof TypeError, true,
michael@0 53 "ToObject throws TypeError for null/undefined");
michael@0 54 }
michael@0 55
michael@0 56
michael@0 57 /*
michael@0 58 * 2. Let tv be ToPrimitive(O, hint Number).
michael@0 59 * ...expands to:
michael@0 60 * 1. Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf".
michael@0 61 * 2. If IsCallable(valueOf) is true then,
michael@0 62 * a. Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and
michael@0 63 * an empty argument list.
michael@0 64 * b. If val is a primitive value, return val.
michael@0 65 * 3. Let toString be the result of calling the [[Get]] internal method of object O with argument "toString".
michael@0 66 * 4. If IsCallable(toString) is true then,
michael@0 67 * a. Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and
michael@0 68 * an empty argument list.
michael@0 69 * b. If str is a primitive value, return str.
michael@0 70 * 5. Throw a TypeError exception.
michael@0 71 */
michael@0 72 try
michael@0 73 {
michael@0 74 var r = dateToJSON.call({ get valueOf() { throw 17; } });
michael@0 75 throw new Error("didn't throw, returned: " + r);
michael@0 76 }
michael@0 77 catch (e)
michael@0 78 {
michael@0 79 assertEq(e, 17, "bad exception: " + e);
michael@0 80 }
michael@0 81
michael@0 82 called = false;
michael@0 83 assertEq(dateToJSON.call({ valueOf: null,
michael@0 84 toString: function() { called = true; return 12; },
michael@0 85 toISOString: function() { return "ohai"; } }),
michael@0 86 "ohai");
michael@0 87 assertEq(called, true);
michael@0 88
michael@0 89 called = false;
michael@0 90 assertEq(dateToJSON.call({ valueOf: function() { called = true; return 42; },
michael@0 91 toISOString: function() { return null; } }),
michael@0 92 null);
michael@0 93 assertEq(called, true);
michael@0 94
michael@0 95 try
michael@0 96 {
michael@0 97 called = false;
michael@0 98 dateToJSON.call({ valueOf: function() { called = true; return {}; },
michael@0 99 get toString() { throw 42; } });
michael@0 100 }
michael@0 101 catch (e)
michael@0 102 {
michael@0 103 assertEq(called, true);
michael@0 104 assertEq(e, 42, "bad exception: " + e);
michael@0 105 }
michael@0 106
michael@0 107 called = false;
michael@0 108 assertEq(dateToJSON.call({ valueOf: function() { called = true; return {}; },
michael@0 109 get toString() { return function() { return 8675309; }; },
michael@0 110 toISOString: function() { return true; } }),
michael@0 111 true);
michael@0 112 assertEq(called, true);
michael@0 113
michael@0 114 var asserted = false;
michael@0 115 called = false;
michael@0 116 assertEq(dateToJSON.call({ valueOf: function() { called = true; return {}; },
michael@0 117 get toString()
michael@0 118 {
michael@0 119 assertEq(called, true);
michael@0 120 asserted = true;
michael@0 121 return function() { return 8675309; };
michael@0 122 },
michael@0 123 toISOString: function() { return NaN; } }),
michael@0 124 NaN);
michael@0 125 assertEq(asserted, true);
michael@0 126
michael@0 127 try
michael@0 128 {
michael@0 129 var r = dateToJSON.call({ valueOf: null, toString: null,
michael@0 130 get toISOString()
michael@0 131 {
michael@0 132 throw new Error("shouldn't have been gotten");
michael@0 133 } });
michael@0 134 throw new Error("didn't throw, returned: " + r);
michael@0 135 }
michael@0 136 catch (e)
michael@0 137 {
michael@0 138 assertEq(e instanceof TypeError, true, "bad exception: " + e);
michael@0 139 }
michael@0 140
michael@0 141
michael@0 142 /* 3. If tv is a Number and is not finite, return null. */
michael@0 143 assertEq(dateToJSON.call({ valueOf: function() { return Infinity; } }), null);
michael@0 144 assertEq(dateToJSON.call({ valueOf: function() { return -Infinity; } }), null);
michael@0 145 assertEq(dateToJSON.call({ valueOf: function() { return NaN; } }), null);
michael@0 146
michael@0 147 assertEq(dateToJSON.call({ valueOf: function() { return Infinity; },
michael@0 148 toISOString: function() { return {}; } }), null);
michael@0 149 assertEq(dateToJSON.call({ valueOf: function() { return -Infinity; },
michael@0 150 toISOString: function() { return []; } }), null);
michael@0 151 assertEq(dateToJSON.call({ valueOf: function() { return NaN; },
michael@0 152 toISOString: function() { return undefined; } }), null);
michael@0 153
michael@0 154
michael@0 155 /*
michael@0 156 * 4. Let toISO be the result of calling the [[Get]] internal method of O with
michael@0 157 * argument "toISOString".
michael@0 158 */
michael@0 159 try
michael@0 160 {
michael@0 161 var r = dateToJSON.call({ get toISOString() { throw 42; } });
michael@0 162 throw new Error("didn't throw, returned: " + r);
michael@0 163 }
michael@0 164 catch (e)
michael@0 165 {
michael@0 166 assertEq(e, 42, "bad exception: " + e);
michael@0 167 }
michael@0 168
michael@0 169
michael@0 170 /* 5. If IsCallable(toISO) is false, throw a TypeError exception. */
michael@0 171 try
michael@0 172 {
michael@0 173 var r = dateToJSON.call({ toISOString: null });
michael@0 174 throw new Error("didn't throw, returned: " + r);
michael@0 175 }
michael@0 176 catch (e)
michael@0 177 {
michael@0 178 assertEq(e instanceof TypeError, true, "bad exception: " + e);
michael@0 179 }
michael@0 180
michael@0 181 try
michael@0 182 {
michael@0 183 var r = dateToJSON.call({ toISOString: undefined });
michael@0 184 throw new Error("didn't throw, returned: " + r);
michael@0 185 }
michael@0 186 catch (e)
michael@0 187 {
michael@0 188 assertEq(e instanceof TypeError, true, "bad exception: " + e);
michael@0 189 }
michael@0 190
michael@0 191 try
michael@0 192 {
michael@0 193 var r = dateToJSON.call({ toISOString: "oogabooga" });
michael@0 194 throw new Error("didn't throw, returned: " + r);
michael@0 195 }
michael@0 196 catch (e)
michael@0 197 {
michael@0 198 assertEq(e instanceof TypeError, true, "bad exception: " + e);
michael@0 199 }
michael@0 200
michael@0 201 try
michael@0 202 {
michael@0 203 var r = dateToJSON.call({ toISOString: Math.PI });
michael@0 204 throw new Error("didn't throw, returned: " + r);
michael@0 205 }
michael@0 206 catch (e)
michael@0 207 {
michael@0 208 assertEq(e instanceof TypeError, true, "bad exception: " + e);
michael@0 209 }
michael@0 210
michael@0 211
michael@0 212 /*
michael@0 213 * 6. Return the result of calling the [[Call]] internal method of toISO with O
michael@0 214 * as the this value and an empty argument list.
michael@0 215 */
michael@0 216 var o =
michael@0 217 {
michael@0 218 toISOString: function(a)
michael@0 219 {
michael@0 220 called = true;
michael@0 221 assertEq(this, o);
michael@0 222 assertEq(a, undefined);
michael@0 223 assertEq(arguments.length, 0);
michael@0 224 return obj;
michael@0 225 }
michael@0 226 };
michael@0 227 var obj = {};
michael@0 228 called = false;
michael@0 229 assertEq(dateToJSON.call(o), obj, "should have gotten obj back");
michael@0 230 assertEq(called, true);
michael@0 231
michael@0 232
michael@0 233 /******************************************************************************/
michael@0 234
michael@0 235 if (typeof reportCompare === "function")
michael@0 236 reportCompare(true, true);
michael@0 237
michael@0 238 print("All tests passed!");

mercurial