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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/JSON/stringify-boxed-primitives.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +// Any copyright is dedicated to the Public Domain.
     1.5 +// http://creativecommons.org/licenses/publicdomain/
     1.6 +
     1.7 +var gTestfile = 'stringify-boxed-primitives.js';
     1.8 +//-----------------------------------------------------------------------------
     1.9 +var BUGNUMBER = 584909;
    1.10 +var summary = "Stringification of Boolean/String/Number objects";
    1.11 +
    1.12 +print(BUGNUMBER + ": " + summary);
    1.13 +
    1.14 +/**************
    1.15 + * BEGIN TEST *
    1.16 + **************/
    1.17 +
    1.18 +function redefine(obj, prop, fun)
    1.19 +{
    1.20 +  var desc =
    1.21 +    { value: fun, writable: true, configurable: true, enumerable: false };
    1.22 +  Object.defineProperty(obj, prop, desc);
    1.23 +}
    1.24 +
    1.25 +assertEq(JSON.stringify(new Boolean(false)), "false");
    1.26 +
    1.27 +assertEq(JSON.stringify(new Number(5)), "5");
    1.28 +
    1.29 +assertEq(JSON.stringify(new String("foopy")), '"foopy"');
    1.30 +
    1.31 +
    1.32 +var numToString = Number.prototype.toString;
    1.33 +var numValueOf = Number.prototype.valueOf;
    1.34 +var objToString = Object.prototype.toString;
    1.35 +var objValueOf = Object.prototype.valueOf;
    1.36 +var boolToString = Boolean.prototype.toString;
    1.37 +var boolValueOf = Boolean.prototype.valueOf;
    1.38 +
    1.39 +redefine(Boolean.prototype, "toString", function() { return 17; });
    1.40 +assertEq(JSON.stringify(new Boolean(false)), "false")
    1.41 +delete Boolean.prototype.toString;
    1.42 +assertEq(JSON.stringify(new Boolean(false)), "false");
    1.43 +delete Object.prototype.toString;
    1.44 +assertEq(JSON.stringify(new Boolean(false)), "false");
    1.45 +delete Boolean.prototype.valueOf;
    1.46 +assertEq(JSON.stringify(new Boolean(false)), "false");
    1.47 +delete Object.prototype.valueOf;
    1.48 +assertEq(JSON.stringify(new Boolean(false)), "false");
    1.49 +
    1.50 +
    1.51 +redefine(Boolean.prototype, "toString", boolToString);
    1.52 +redefine(Boolean.prototype, "valueOf", boolValueOf);
    1.53 +redefine(Object.prototype, "toString", objToString);
    1.54 +redefine(Object.prototype, "valueOf", objValueOf);
    1.55 +
    1.56 +redefine(Number.prototype, "toString", function() { return 42; });
    1.57 +assertEq(JSON.stringify(new Number(5)), "5");
    1.58 +redefine(Number.prototype, "valueOf", function() { return 17; });
    1.59 +assertEq(JSON.stringify(new Number(5)), "17");
    1.60 +delete Number.prototype.toString;
    1.61 +assertEq(JSON.stringify(new Number(5)), "17");
    1.62 +delete Number.prototype.valueOf;
    1.63 +assertEq(JSON.stringify(new Number(5)), "null"); // isNaN(Number("[object Number]"))
    1.64 +delete Object.prototype.toString;
    1.65 +try
    1.66 +{
    1.67 +  JSON.stringify(new Number(5));
    1.68 +  throw new Error("didn't throw");
    1.69 +}
    1.70 +catch (e)
    1.71 +{
    1.72 +  assertEq(e instanceof TypeError, true,
    1.73 +           "ToNumber failure, should throw TypeError");
    1.74 +}
    1.75 +delete Object.prototype.valueOf;
    1.76 +try
    1.77 +{
    1.78 +  JSON.stringify(new Number(5));
    1.79 +  throw new Error("didn't throw");
    1.80 +}
    1.81 +catch (e)
    1.82 +{
    1.83 +  assertEq(e instanceof TypeError, true,
    1.84 +           "ToNumber failure, should throw TypeError");
    1.85 +}
    1.86 +
    1.87 +
    1.88 +redefine(Number.prototype, "toString", numToString);
    1.89 +redefine(Number.prototype, "valueOf", numValueOf);
    1.90 +redefine(Object.prototype, "toString", objToString);
    1.91 +redefine(Object.prototype, "valueOf", objValueOf);
    1.92 +
    1.93 +
    1.94 +redefine(String.prototype, "valueOf", function() { return 17; });
    1.95 +assertEq(JSON.stringify(new String(5)), '"5"');
    1.96 +redefine(String.prototype, "toString", function() { return 42; });
    1.97 +assertEq(JSON.stringify(new String(5)), '"42"');
    1.98 +delete String.prototype.toString;
    1.99 +assertEq(JSON.stringify(new String(5)), '"[object String]"');
   1.100 +delete Object.prototype.toString;
   1.101 +assertEq(JSON.stringify(new String(5)), '"17"');
   1.102 +delete String.prototype.valueOf;
   1.103 +try
   1.104 +{
   1.105 +  JSON.stringify(new String(5));
   1.106 +  throw new Error("didn't throw");
   1.107 +}
   1.108 +catch (e)
   1.109 +{
   1.110 +  assertEq(e instanceof TypeError, true,
   1.111 +           "ToString failure, should throw TypeError");
   1.112 +}
   1.113 +delete Object.prototype.valueOf;
   1.114 +try
   1.115 +{
   1.116 +  JSON.stringify(new String(5));
   1.117 +  throw new Error("didn't throw");
   1.118 +}
   1.119 +catch (e)
   1.120 +{
   1.121 +  assertEq(e instanceof TypeError, true,
   1.122 +           "ToString failure, should throw TypeError");
   1.123 +}
   1.124 +
   1.125 +/******************************************************************************/
   1.126 +
   1.127 +if (typeof reportCompare === "function")
   1.128 +  reportCompare(true, true);
   1.129 +
   1.130 +print("All tests passed!");

mercurial