michael@0: // Any copyright is dedicated to the Public Domain. michael@0: // http://creativecommons.org/licenses/publicdomain/ michael@0: michael@0: var gTestfile = 'stringify-boxed-primitives.js'; michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 584909; michael@0: var summary = "Stringification of Boolean/String/Number objects"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: function redefine(obj, prop, fun) michael@0: { michael@0: var desc = michael@0: { value: fun, writable: true, configurable: true, enumerable: false }; michael@0: Object.defineProperty(obj, prop, desc); michael@0: } michael@0: michael@0: assertEq(JSON.stringify(new Boolean(false)), "false"); michael@0: michael@0: assertEq(JSON.stringify(new Number(5)), "5"); michael@0: michael@0: assertEq(JSON.stringify(new String("foopy")), '"foopy"'); michael@0: michael@0: michael@0: var numToString = Number.prototype.toString; michael@0: var numValueOf = Number.prototype.valueOf; michael@0: var objToString = Object.prototype.toString; michael@0: var objValueOf = Object.prototype.valueOf; michael@0: var boolToString = Boolean.prototype.toString; michael@0: var boolValueOf = Boolean.prototype.valueOf; michael@0: michael@0: redefine(Boolean.prototype, "toString", function() { return 17; }); michael@0: assertEq(JSON.stringify(new Boolean(false)), "false") michael@0: delete Boolean.prototype.toString; michael@0: assertEq(JSON.stringify(new Boolean(false)), "false"); michael@0: delete Object.prototype.toString; michael@0: assertEq(JSON.stringify(new Boolean(false)), "false"); michael@0: delete Boolean.prototype.valueOf; michael@0: assertEq(JSON.stringify(new Boolean(false)), "false"); michael@0: delete Object.prototype.valueOf; michael@0: assertEq(JSON.stringify(new Boolean(false)), "false"); michael@0: michael@0: michael@0: redefine(Boolean.prototype, "toString", boolToString); michael@0: redefine(Boolean.prototype, "valueOf", boolValueOf); michael@0: redefine(Object.prototype, "toString", objToString); michael@0: redefine(Object.prototype, "valueOf", objValueOf); michael@0: michael@0: redefine(Number.prototype, "toString", function() { return 42; }); michael@0: assertEq(JSON.stringify(new Number(5)), "5"); michael@0: redefine(Number.prototype, "valueOf", function() { return 17; }); michael@0: assertEq(JSON.stringify(new Number(5)), "17"); michael@0: delete Number.prototype.toString; michael@0: assertEq(JSON.stringify(new Number(5)), "17"); michael@0: delete Number.prototype.valueOf; michael@0: assertEq(JSON.stringify(new Number(5)), "null"); // isNaN(Number("[object Number]")) michael@0: delete Object.prototype.toString; michael@0: try michael@0: { michael@0: JSON.stringify(new Number(5)); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "ToNumber failure, should throw TypeError"); michael@0: } michael@0: delete Object.prototype.valueOf; michael@0: try michael@0: { michael@0: JSON.stringify(new Number(5)); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "ToNumber failure, should throw TypeError"); michael@0: } michael@0: michael@0: michael@0: redefine(Number.prototype, "toString", numToString); michael@0: redefine(Number.prototype, "valueOf", numValueOf); michael@0: redefine(Object.prototype, "toString", objToString); michael@0: redefine(Object.prototype, "valueOf", objValueOf); michael@0: michael@0: michael@0: redefine(String.prototype, "valueOf", function() { return 17; }); michael@0: assertEq(JSON.stringify(new String(5)), '"5"'); michael@0: redefine(String.prototype, "toString", function() { return 42; }); michael@0: assertEq(JSON.stringify(new String(5)), '"42"'); michael@0: delete String.prototype.toString; michael@0: assertEq(JSON.stringify(new String(5)), '"[object String]"'); michael@0: delete Object.prototype.toString; michael@0: assertEq(JSON.stringify(new String(5)), '"17"'); michael@0: delete String.prototype.valueOf; michael@0: try michael@0: { michael@0: JSON.stringify(new String(5)); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "ToString failure, should throw TypeError"); michael@0: } michael@0: delete Object.prototype.valueOf; michael@0: try michael@0: { michael@0: JSON.stringify(new String(5)); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "ToString failure, should throw TypeError"); michael@0: } michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");