|
1 // Any copyright is dedicated to the Public Domain. |
|
2 // http://creativecommons.org/licenses/publicdomain/ |
|
3 |
|
4 var gTestfile = 'stringify-boxed-primitives.js'; |
|
5 //----------------------------------------------------------------------------- |
|
6 var BUGNUMBER = 584909; |
|
7 var summary = "Stringification of Boolean/String/Number objects"; |
|
8 |
|
9 print(BUGNUMBER + ": " + summary); |
|
10 |
|
11 /************** |
|
12 * BEGIN TEST * |
|
13 **************/ |
|
14 |
|
15 function redefine(obj, prop, fun) |
|
16 { |
|
17 var desc = |
|
18 { value: fun, writable: true, configurable: true, enumerable: false }; |
|
19 Object.defineProperty(obj, prop, desc); |
|
20 } |
|
21 |
|
22 assertEq(JSON.stringify(new Boolean(false)), "false"); |
|
23 |
|
24 assertEq(JSON.stringify(new Number(5)), "5"); |
|
25 |
|
26 assertEq(JSON.stringify(new String("foopy")), '"foopy"'); |
|
27 |
|
28 |
|
29 var numToString = Number.prototype.toString; |
|
30 var numValueOf = Number.prototype.valueOf; |
|
31 var objToString = Object.prototype.toString; |
|
32 var objValueOf = Object.prototype.valueOf; |
|
33 var boolToString = Boolean.prototype.toString; |
|
34 var boolValueOf = Boolean.prototype.valueOf; |
|
35 |
|
36 redefine(Boolean.prototype, "toString", function() { return 17; }); |
|
37 assertEq(JSON.stringify(new Boolean(false)), "false") |
|
38 delete Boolean.prototype.toString; |
|
39 assertEq(JSON.stringify(new Boolean(false)), "false"); |
|
40 delete Object.prototype.toString; |
|
41 assertEq(JSON.stringify(new Boolean(false)), "false"); |
|
42 delete Boolean.prototype.valueOf; |
|
43 assertEq(JSON.stringify(new Boolean(false)), "false"); |
|
44 delete Object.prototype.valueOf; |
|
45 assertEq(JSON.stringify(new Boolean(false)), "false"); |
|
46 |
|
47 |
|
48 redefine(Boolean.prototype, "toString", boolToString); |
|
49 redefine(Boolean.prototype, "valueOf", boolValueOf); |
|
50 redefine(Object.prototype, "toString", objToString); |
|
51 redefine(Object.prototype, "valueOf", objValueOf); |
|
52 |
|
53 redefine(Number.prototype, "toString", function() { return 42; }); |
|
54 assertEq(JSON.stringify(new Number(5)), "5"); |
|
55 redefine(Number.prototype, "valueOf", function() { return 17; }); |
|
56 assertEq(JSON.stringify(new Number(5)), "17"); |
|
57 delete Number.prototype.toString; |
|
58 assertEq(JSON.stringify(new Number(5)), "17"); |
|
59 delete Number.prototype.valueOf; |
|
60 assertEq(JSON.stringify(new Number(5)), "null"); // isNaN(Number("[object Number]")) |
|
61 delete Object.prototype.toString; |
|
62 try |
|
63 { |
|
64 JSON.stringify(new Number(5)); |
|
65 throw new Error("didn't throw"); |
|
66 } |
|
67 catch (e) |
|
68 { |
|
69 assertEq(e instanceof TypeError, true, |
|
70 "ToNumber failure, should throw TypeError"); |
|
71 } |
|
72 delete Object.prototype.valueOf; |
|
73 try |
|
74 { |
|
75 JSON.stringify(new Number(5)); |
|
76 throw new Error("didn't throw"); |
|
77 } |
|
78 catch (e) |
|
79 { |
|
80 assertEq(e instanceof TypeError, true, |
|
81 "ToNumber failure, should throw TypeError"); |
|
82 } |
|
83 |
|
84 |
|
85 redefine(Number.prototype, "toString", numToString); |
|
86 redefine(Number.prototype, "valueOf", numValueOf); |
|
87 redefine(Object.prototype, "toString", objToString); |
|
88 redefine(Object.prototype, "valueOf", objValueOf); |
|
89 |
|
90 |
|
91 redefine(String.prototype, "valueOf", function() { return 17; }); |
|
92 assertEq(JSON.stringify(new String(5)), '"5"'); |
|
93 redefine(String.prototype, "toString", function() { return 42; }); |
|
94 assertEq(JSON.stringify(new String(5)), '"42"'); |
|
95 delete String.prototype.toString; |
|
96 assertEq(JSON.stringify(new String(5)), '"[object String]"'); |
|
97 delete Object.prototype.toString; |
|
98 assertEq(JSON.stringify(new String(5)), '"17"'); |
|
99 delete String.prototype.valueOf; |
|
100 try |
|
101 { |
|
102 JSON.stringify(new String(5)); |
|
103 throw new Error("didn't throw"); |
|
104 } |
|
105 catch (e) |
|
106 { |
|
107 assertEq(e instanceof TypeError, true, |
|
108 "ToString failure, should throw TypeError"); |
|
109 } |
|
110 delete Object.prototype.valueOf; |
|
111 try |
|
112 { |
|
113 JSON.stringify(new String(5)); |
|
114 throw new Error("didn't throw"); |
|
115 } |
|
116 catch (e) |
|
117 { |
|
118 assertEq(e instanceof TypeError, true, |
|
119 "ToString failure, should throw TypeError"); |
|
120 } |
|
121 |
|
122 /******************************************************************************/ |
|
123 |
|
124 if (typeof reportCompare === "function") |
|
125 reportCompare(true, true); |
|
126 |
|
127 print("All tests passed!"); |