js/src/tests/ecma_5/JSON/shell.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/shell.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +gTestsubsuite='JSON';
     1.5 +
     1.6 +function testJSON(str, expectSyntaxError)
     1.7 +{
     1.8 +  // Leading and trailing whitespace never affect parsing, so test the string
     1.9 +  // multiple times with and without whitespace around it as it's easy and can
    1.10 +  // potentially detect bugs.
    1.11 +
    1.12 +  // Try the provided string
    1.13 +  try
    1.14 +  {
    1.15 +    JSON.parse(str);
    1.16 +    reportCompare(false, expectSyntaxError,
    1.17 +                  "string <" + str + "> " +
    1.18 +                  "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.19 +                  "have parsed as JSON");
    1.20 +  }
    1.21 +  catch (e)
    1.22 +  {
    1.23 +    if (!(e instanceof SyntaxError))
    1.24 +    {
    1.25 +      reportCompare(true, false,
    1.26 +                    "parsing string <" + str + "> threw a non-SyntaxError " +
    1.27 +                    "exception: " + e);
    1.28 +    }
    1.29 +    else
    1.30 +    {
    1.31 +      reportCompare(true, expectSyntaxError,
    1.32 +                    "string <" + str + "> " +
    1.33 +                    "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.34 +                    "have parsed as JSON, exception: " + e);
    1.35 +    }
    1.36 +  }
    1.37 +
    1.38 +  // Now try the provided string with trailing whitespace
    1.39 +  try
    1.40 +  {
    1.41 +    JSON.parse(str + " ");
    1.42 +    reportCompare(false, expectSyntaxError,
    1.43 +                  "string <" + str + " > " +
    1.44 +                  "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.45 +                  "have parsed as JSON");
    1.46 +  }
    1.47 +  catch (e)
    1.48 +  {
    1.49 +    if (!(e instanceof SyntaxError))
    1.50 +    {
    1.51 +      reportCompare(true, false,
    1.52 +                    "parsing string <" + str + " > threw a non-SyntaxError " +
    1.53 +                    "exception: " + e);
    1.54 +    }
    1.55 +    else
    1.56 +    {
    1.57 +      reportCompare(true, expectSyntaxError,
    1.58 +                    "string <" + str + " > " +
    1.59 +                    "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.60 +                    "have parsed as JSON, exception: " + e);
    1.61 +    }
    1.62 +  }
    1.63 +
    1.64 +  // Now try the provided string with leading whitespace
    1.65 +  try
    1.66 +  {
    1.67 +    JSON.parse(" " + str);
    1.68 +    reportCompare(false, expectSyntaxError,
    1.69 +                  "string < " + str + "> " +
    1.70 +                  "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.71 +                  "have parsed as JSON");
    1.72 +  }
    1.73 +  catch (e)
    1.74 +  {
    1.75 +    if (!(e instanceof SyntaxError))
    1.76 +    {
    1.77 +      reportCompare(true, false,
    1.78 +                    "parsing string < " + str + "> threw a non-SyntaxError " +
    1.79 +                    "exception: " + e);
    1.80 +    }
    1.81 +    else
    1.82 +    {
    1.83 +      reportCompare(true, expectSyntaxError,
    1.84 +                    "string < " + str + "> " +
    1.85 +                    "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.86 +                    "have parsed as JSON, exception: " + e);
    1.87 +    }
    1.88 +  }
    1.89 +
    1.90 +  // Now try the provided string with whitespace surrounding it
    1.91 +  try
    1.92 +  {
    1.93 +    JSON.parse(" " + str + " ");
    1.94 +    reportCompare(false, expectSyntaxError,
    1.95 +                  "string < " + str + " > " +
    1.96 +                  "should" + (expectSyntaxError ? "n't" : "") + " " +
    1.97 +                  "have parsed as JSON");
    1.98 +  }
    1.99 +  catch (e)
   1.100 +  {
   1.101 +    if (!(e instanceof SyntaxError))
   1.102 +    {
   1.103 +      reportCompare(true, false,
   1.104 +                    "parsing string < " + str + " > threw a non-SyntaxError " +
   1.105 +                    "exception: " + e);
   1.106 +    }
   1.107 +    else
   1.108 +    {
   1.109 +      reportCompare(true, expectSyntaxError,
   1.110 +                    "string < " + str + " > " +
   1.111 +                    "should" + (expectSyntaxError ? "n't" : "") + " " +
   1.112 +                    "have parsed as JSON, exception: " + e);
   1.113 +    }
   1.114 +  }
   1.115 +}

mercurial