js/src/tests/ecma_5/JSON/cyclic-stringify.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/cyclic-stringify.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +// Any copyright is dedicated to the Public Domain.
     1.5 +// http://creativecommons.org/licenses/publicdomain/
     1.6 +
     1.7 +//-----------------------------------------------------------------------------
     1.8 +var BUGNUMBER = 578273;
     1.9 +var summary =
    1.10 +  "ES5: Properly detect cycles in JSON.stringify (throw TypeError, check for " +
    1.11 +  "cycles rather than imprecisely rely on recursion limits)";
    1.12 +
    1.13 +print(BUGNUMBER + ": " + summary);
    1.14 +
    1.15 +/**************
    1.16 + * BEGIN TEST *
    1.17 + **************/
    1.18 +
    1.19 +// objects
    1.20 +
    1.21 +var count = 0;
    1.22 +var desc =
    1.23 +  {
    1.24 +    get: function() { count++; return obj; },
    1.25 +    enumerable: true,
    1.26 +    configurable: true
    1.27 +  };
    1.28 +var obj = Object.defineProperty({ p1: 0 }, "p2", desc);
    1.29 +
    1.30 +try
    1.31 +{
    1.32 +  var str = JSON.stringify(obj);
    1.33 +  assertEq(false, true, "should have thrown, got " + str);
    1.34 +}
    1.35 +catch (e)
    1.36 +{
    1.37 +  assertEq(e instanceof TypeError, true,
    1.38 +           "wrong error type: " + e.constructor.name);
    1.39 +  assertEq(count, 1,
    1.40 +           "cyclic data structures not detected immediately");
    1.41 +}
    1.42 +
    1.43 +count = 0;
    1.44 +var obj2 = Object.defineProperty({}, "obj", desc);
    1.45 +try
    1.46 +{
    1.47 +  var str = JSON.stringify(obj2);
    1.48 +  assertEq(false, true, "should have thrown, got " + str);
    1.49 +}
    1.50 +catch (e)
    1.51 +{
    1.52 +  assertEq(e instanceof TypeError, true,
    1.53 +           "wrong error type: " + e.constructor.name);
    1.54 +  assertEq(count, 2,
    1.55 +           "cyclic data structures not detected immediately");
    1.56 +}
    1.57 +
    1.58 +
    1.59 +// arrays
    1.60 +
    1.61 +var count = 0;
    1.62 +var desc =
    1.63 +  {
    1.64 +    get: function() { count++; return arr; },
    1.65 +    enumerable: true,
    1.66 +    configurable: true
    1.67 +  };
    1.68 +var arr = Object.defineProperty([], "0", desc);
    1.69 +
    1.70 +try
    1.71 +{
    1.72 +  var str = JSON.stringify(arr);
    1.73 +  assertEq(false, true, "should have thrown, got " + str);
    1.74 +}
    1.75 +catch (e)
    1.76 +{
    1.77 +  assertEq(e instanceof TypeError, true,
    1.78 +           "wrong error type: " + e.constructor.name);
    1.79 +  assertEq(count, 1,
    1.80 +           "cyclic data structures not detected immediately");
    1.81 +}
    1.82 +
    1.83 +count = 0;
    1.84 +var arr2 = Object.defineProperty([], "0", desc);
    1.85 +try
    1.86 +{
    1.87 +  var str = JSON.stringify(arr2);
    1.88 +  assertEq(false, true, "should have thrown, got " + str);
    1.89 +}
    1.90 +catch (e)
    1.91 +{
    1.92 +  assertEq(e instanceof TypeError, true,
    1.93 +           "wrong error type: " + e.constructor.name);
    1.94 +  assertEq(count, 2,
    1.95 +           "cyclic data structures not detected immediately");
    1.96 +}
    1.97 +
    1.98 +/******************************************************************************/
    1.99 +
   1.100 +if (typeof reportCompare === "function")
   1.101 +  reportCompare(true, true);
   1.102 +
   1.103 +print("Tests complete");

mercurial