1
// Any copyright is dedicated to the Public Domain.
2
// http://creativecommons.org/licenses/publicdomain/
4
var gTestfile = 'toJSON-01.js';
5
//-----------------------------------------------------------------------------
6
var BUGNUMBER = 584811;
7
var summary = "Date.prototype.toJSON isn't to spec";
9
print(BUGNUMBER + ": " + summary);
17
var dateToJSON = Date.prototype.toJSON;
18
assertEq(Date.prototype.hasOwnProperty("toJSON"), true);
19
assertEq(typeof dateToJSON, "function");
21
// brief test to exercise this outside of isolation, just for sanity
22
var invalidDate = new Date();
23
invalidDate.setTime(NaN);
24
assertEq(JSON.stringify({ p: invalidDate }), '{"p":null}');
27
/* 15.9.5.44 Date.prototype.toJSON ( key ) */
28
assertEq(dateToJSON.length, 1);
31
* 1. Let O be the result of calling ToObject, giving it the this value as its
36
dateToJSON.call(null);
37
throw new Error("should have thrown a TypeError");
41
assertEq(e instanceof TypeError, true,
42
"ToObject throws TypeError for null/undefined");
47
dateToJSON.call(undefined);
48
throw new Error("should have thrown a TypeError");
52
assertEq(e instanceof TypeError, true,
53
"ToObject throws TypeError for null/undefined");
58
* 2. Let tv be ToPrimitive(O, hint Number).
60
* 1. Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf".
61
* 2. If IsCallable(valueOf) is true then,
62
* a. Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and
63
* an empty argument list.
64
* b. If val is a primitive value, return val.
65
* 3. Let toString be the result of calling the [[Get]] internal method of object O with argument "toString".
66
* 4. If IsCallable(toString) is true then,
67
* a. Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and
68
* an empty argument list.
69
* b. If str is a primitive value, return str.
70
* 5. Throw a TypeError exception.
74
var r = dateToJSON.call({ get valueOf() { throw 17; } });
75
throw new Error("didn't throw, returned: " + r);
79
assertEq(e, 17, "bad exception: " + e);
83
assertEq(dateToJSON.call({ valueOf: null,
84
toString: function() { called = true; return 12; },
85
toISOString: function() { return "ohai"; } }),
87
assertEq(called, true);
90
assertEq(dateToJSON.call({ valueOf: function() { called = true; return 42; },
91
toISOString: function() { return null; } }),
93
assertEq(called, true);
98
dateToJSON.call({ valueOf: function() { called = true; return {}; },
99
get toString() { throw 42; } });
103
assertEq(called, true);
104
assertEq(e, 42, "bad exception: " + e);
108
assertEq(dateToJSON.call({ valueOf: function() { called = true; return {}; },
109
get toString() { return function() { return 8675309; }; },
110
toISOString: function() { return true; } }),
112
assertEq(called, true);
114
var asserted = false;
116
assertEq(dateToJSON.call({ valueOf: function() { called = true; return {}; },
119
assertEq(called, true);
121
return function() { return 8675309; };
123
toISOString: function() { return NaN; } }),
125
assertEq(asserted, true);
129
var r = dateToJSON.call({ valueOf: null, toString: null,
132
throw new Error("shouldn't have been gotten");
134
throw new Error("didn't throw, returned: " + r);
138
assertEq(e instanceof TypeError, true, "bad exception: " + e);
142
/* 3. If tv is a Number and is not finite, return null. */
143
assertEq(dateToJSON.call({ valueOf: function() { return Infinity; } }), null);
144
assertEq(dateToJSON.call({ valueOf: function() { return -Infinity; } }), null);
145
assertEq(dateToJSON.call({ valueOf: function() { return NaN; } }), null);
147
assertEq(dateToJSON.call({ valueOf: function() { return Infinity; },
148
toISOString: function() { return {}; } }), null);
149
assertEq(dateToJSON.call({ valueOf: function() { return -Infinity; },
150
toISOString: function() { return []; } }), null);
151
assertEq(dateToJSON.call({ valueOf: function() { return NaN; },
152
toISOString: function() { return undefined; } }), null);
156
* 4. Let toISO be the result of calling the [[Get]] internal method of O with
157
* argument "toISOString".
161
var r = dateToJSON.call({ get toISOString() { throw 42; } });
162
throw new Error("didn't throw, returned: " + r);
166
assertEq(e, 42, "bad exception: " + e);
170
/* 5. If IsCallable(toISO) is false, throw a TypeError exception. */
173
var r = dateToJSON.call({ toISOString: null });
174
throw new Error("didn't throw, returned: " + r);
178
assertEq(e instanceof TypeError, true, "bad exception: " + e);
183
var r = dateToJSON.call({ toISOString: undefined });
184
throw new Error("didn't throw, returned: " + r);
188
assertEq(e instanceof TypeError, true, "bad exception: " + e);
193
var r = dateToJSON.call({ toISOString: "oogabooga" });
194
throw new Error("didn't throw, returned: " + r);
198
assertEq(e instanceof TypeError, true, "bad exception: " + e);
203
var r = dateToJSON.call({ toISOString: Math.PI });
204
throw new Error("didn't throw, returned: " + r);
208
assertEq(e instanceof TypeError, true, "bad exception: " + e);
213
* 6. Return the result of calling the [[Call]] internal method of toISO with O
214
* as the this value and an empty argument list.
218
toISOString: function(a)
222
assertEq(a, undefined);
223
assertEq(arguments.length, 0);
229
assertEq(dateToJSON.call(o), obj, "should have gotten obj back");
230
assertEq(called, true);
233
/******************************************************************************/
235
if (typeof reportCompare === "function")
236
reportCompare(true, true);
238
print("All tests passed!");