Index: test/mjsunit/json.js |
diff --git a/test/mjsunit/json.js b/test/mjsunit/json.js |
index 1c55959d5fcd86a67898d3922ff1d4ee958dcfa3..d283d629ef5a7d439316e2b98c95d9841bea6885 100644 |
--- a/test/mjsunit/json.js |
+++ b/test/mjsunit/json.js |
@@ -44,26 +44,6 @@ function GenericToJSONChecks(Constructor, value, alternative) { |
assertEquals(null, n4.toJSON()); |
} |
-// Number toJSON |
-assertEquals(3, (3).toJSON()); |
-assertEquals(3, (3).toJSON(true)); |
-assertEquals(4, (new Number(4)).toJSON()); |
-GenericToJSONChecks(Number, 5, 6); |
- |
-// Boolean toJSON |
-assertEquals(true, (true).toJSON()); |
-assertEquals(true, (true).toJSON(false)); |
-assertEquals(false, (false).toJSON()); |
-assertEquals(true, (new Boolean(true)).toJSON()); |
-GenericToJSONChecks(Boolean, true, false); |
-GenericToJSONChecks(Boolean, false, true); |
- |
-// String toJSON |
-assertEquals("flot", "flot".toJSON()); |
-assertEquals("flot", "flot".toJSON(3)); |
-assertEquals("tolf", (new String("tolf")).toJSON()); |
-GenericToJSONChecks(String, "x", "y"); |
- |
// Date toJSON |
assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON()); |
assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON()); |
@@ -352,3 +332,69 @@ for (var i = 0; i < 65536; i++) { |
} |
assertEquals('"' + expected + '"', encoded, "Codepoint " + i); |
} |
+ |
+ |
+// Ensure that wrappers and callables are handled correctly. |
+var num37 = new Number(42); |
+num37.valueOf = function() { return 37; }; |
+ |
+var numFoo = new Number(42); |
+numFoo.valueOf = "not callable"; |
+numFoo.toString = function() { return "foo"; }; |
+ |
+var numTrue = new Number(42); |
+numTrue.valueOf = function() { return true; } |
+ |
+var strFoo = new String("bar"); |
+strFoo.toString = function() { return "foo"; }; |
+ |
+var str37 = new String("bar"); |
+str37.toString = "not callable"; |
+str37.valueOf = function() { return 37; }; |
+ |
+var strTrue = new String("bar"); |
+strTrue.toString = function() { return true; } |
+ |
+var func = function() { /* Is callable */ }; |
+ |
+var funcJSON = function() { /* Is callable */ }; |
+funcJSON.toJSON = function() { return "has toJSON"; }; |
+ |
+var re = /Is callable/; |
+ |
+var reJSON = /Is callable/; |
+reJSON.toJSON = function() { return "has toJSON"; }; |
+ |
+assertEquals('[37,null,1,"foo","37","true",null,"has toJSON",null,"has toJSON"]', |
Rico
2010/12/15 07:46:10
Long line + space after every comma
Lasse Reichstein
2010/12/15 08:59:54
It's a string literal, and the exact output of JSO
|
+ JSON.stringify([num37, numFoo, numTrue, |
+ strFoo, str37, strTrue, |
+ func, funcJSON, re, reJSON])); |
+ |
+ |
+var oddball = Object(42); |
+oddball.__proto__ = { __proto__: null, toString: function() { return true; } }; |
+assertEquals('1', JSON.stringify(oddball)); |
+ |
+var getCount = 0; |
+var callCount = 0; |
+var counter = { get toJSON() { getCount++; |
+ return function() { callCount++; |
+ return 42; }; } }; |
+assertEquals('42', JSON.stringify(counter)); |
+assertEquals(1, getCount); |
+assertEquals(1, callCount); |
+ |
+var oddball2 = Object(42); |
+var oddball3 = Object("foo"); |
+oddball3.__proto__ = { __proto__: null, |
+ toString: "not callable", |
+ valueOf: function() { return true; } }; |
+oddball2.__proto__ = { __proto__: null, |
+ toJSON: function () { return oddball3; } } |
+assertEquals('"true"', JSON.stringify(oddball2)); |
+ |
+ |
+var falseNum = Object("37"); |
+falseNum.__proto__ = Number.prototype; |
+falseNum.toString = function() { return 42; }; |
+assertEquals('"42"', JSON.stringify(falseNum)); |
Rico
2010/12/15 07:46:10
We should also test the case where toJSON is set a
Lasse Reichstein
2010/12/15 08:59:54
Adding tests of Date.prototype.toJSON when called
|