diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/JSON/stringify-boxed-primitives.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | js/src/tests/non262/JSON/stringify-boxed-primitives.js | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/js/src/tests/non262/JSON/stringify-boxed-primitives.js b/js/src/tests/non262/JSON/stringify-boxed-primitives.js new file mode 100644 index 0000000000..0769c2ec6f --- /dev/null +++ b/js/src/tests/non262/JSON/stringify-boxed-primitives.js @@ -0,0 +1,127 @@ +// Any copyright is dedicated to the Public Domain. +// http://creativecommons.org/licenses/publicdomain/ + +var gTestfile = 'stringify-boxed-primitives.js'; +//----------------------------------------------------------------------------- +var BUGNUMBER = 584909; +var summary = "Stringification of Boolean/String/Number objects"; + +print(BUGNUMBER + ": " + summary); + +/************** + * BEGIN TEST * + **************/ + +function redefine(obj, prop, fun) +{ + var desc = + { value: fun, writable: true, configurable: true, enumerable: false }; + Object.defineProperty(obj, prop, desc); +} + +assertEq(JSON.stringify(new Boolean(false)), "false"); + +assertEq(JSON.stringify(new Number(5)), "5"); + +assertEq(JSON.stringify(new String("foopy")), '"foopy"'); + + +var numToString = Number.prototype.toString; +var numValueOf = Number.prototype.valueOf; +var objToString = Object.prototype.toString; +var objValueOf = Object.prototype.valueOf; +var boolToString = Boolean.prototype.toString; +var boolValueOf = Boolean.prototype.valueOf; + +redefine(Boolean.prototype, "toString", function() { return 17; }); +assertEq(JSON.stringify(new Boolean(false)), "false") +delete Boolean.prototype.toString; +assertEq(JSON.stringify(new Boolean(false)), "false"); +delete Object.prototype.toString; +assertEq(JSON.stringify(new Boolean(false)), "false"); +delete Boolean.prototype.valueOf; +assertEq(JSON.stringify(new Boolean(false)), "false"); +delete Object.prototype.valueOf; +assertEq(JSON.stringify(new Boolean(false)), "false"); + + +redefine(Boolean.prototype, "toString", boolToString); +redefine(Boolean.prototype, "valueOf", boolValueOf); +redefine(Object.prototype, "toString", objToString); +redefine(Object.prototype, "valueOf", objValueOf); + +redefine(Number.prototype, "toString", function() { return 42; }); +assertEq(JSON.stringify(new Number(5)), "5"); +redefine(Number.prototype, "valueOf", function() { return 17; }); +assertEq(JSON.stringify(new Number(5)), "17"); +delete Number.prototype.toString; +assertEq(JSON.stringify(new Number(5)), "17"); +delete Number.prototype.valueOf; +assertEq(JSON.stringify(new Number(5)), "null"); // isNaN(Number("[object Number]")) +delete Object.prototype.toString; +try +{ + JSON.stringify(new Number(5)); + throw new Error("didn't throw"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, + "ToNumber failure, should throw TypeError"); +} +delete Object.prototype.valueOf; +try +{ + JSON.stringify(new Number(5)); + throw new Error("didn't throw"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, + "ToNumber failure, should throw TypeError"); +} + + +redefine(Number.prototype, "toString", numToString); +redefine(Number.prototype, "valueOf", numValueOf); +redefine(Object.prototype, "toString", objToString); +redefine(Object.prototype, "valueOf", objValueOf); + + +redefine(String.prototype, "valueOf", function() { return 17; }); +assertEq(JSON.stringify(new String(5)), '"5"'); +redefine(String.prototype, "toString", function() { return 42; }); +assertEq(JSON.stringify(new String(5)), '"42"'); +delete String.prototype.toString; +assertEq(JSON.stringify(new String(5)), '"[object String]"'); +delete Object.prototype.toString; +assertEq(JSON.stringify(new String(5)), '"17"'); +delete String.prototype.valueOf; +try +{ + JSON.stringify(new String(5)); + throw new Error("didn't throw"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, + "ToString failure, should throw TypeError"); +} +delete Object.prototype.valueOf; +try +{ + JSON.stringify(new String(5)); + throw new Error("didn't throw"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, + "ToString failure, should throw TypeError"); +} + +/******************************************************************************/ + +if (typeof reportCompare === "function") + reportCompare(true, true); + +print("All tests passed!"); |