js/src/tests/ecma_5/JSON/stringify-boxed-primitives.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial