js/src/tests/ecma_5/JSON/parse-crockford-01.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/parse-crockford-01.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +// Ported from dom/src/json/test/unit/test_decode.js
     1.5 +
     1.6 +var str =
     1.7 +  '[\n' +
     1.8 +  '    "JSON Test Pattern pass1",\n' +
     1.9 +  '    {"object with 1 member":["array with 1 element"]},\n' +
    1.10 +  '    {},\n' +
    1.11 +  '    [],\n' +
    1.12 +  '    -42,\n' +
    1.13 +  '    true,\n' +
    1.14 +  '    false,\n' +
    1.15 +  '    null,\n' +
    1.16 +  '    {\n' +
    1.17 +  '        "integer": 1234567890,\n' +
    1.18 +  '        "real": -9876.543210,\n' +
    1.19 +  '        "e": 0.123456789e-12,\n' +
    1.20 +  '        "E": 1.234567890E+34,\n' +
    1.21 +  '        "":  23456789012E66,\n' +
    1.22 +  '        "zero": 0,\n' +
    1.23 +  '        "one": 1,\n' +
    1.24 +  '        "space": " ",\n' +
    1.25 +  '        "quote": "\\"",\n' +
    1.26 +  '        "backslash": "\\\\",\n' +
    1.27 +  '        "controls": "\\b\\f\\n\\r\\t",\n' +
    1.28 +  '        "slash": "/ & \\/",\n' +
    1.29 +  '        "alpha": "abcdefghijklmnopqrstuvwyz",\n' +
    1.30 +  '        "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",\n' +
    1.31 +  '        "digit": "0123456789",\n' +
    1.32 +  '        "0123456789": "digit",\n' +
    1.33 +  '        "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",\n' +
    1.34 +  '        "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",\n' +
    1.35 +  '        "true": true,\n' +
    1.36 +  '        "false": false,\n' +
    1.37 +  '        "null": null,\n' +
    1.38 +  '        "array":[  ],\n' +
    1.39 +  '        "object":{  },\n' +
    1.40 +  '        "address": "50 St. James Street",\n' +
    1.41 +  '        "url": "http://www.JSON.org/",\n' +
    1.42 +  '        "comment": "// /* <!-- --",\n' +
    1.43 +  '        "# -- --> */": " ",\n' +
    1.44 +  '        " s p a c e d " :[1,2 , 3\n' +
    1.45 +  '\n' +
    1.46 +  ',\n' +
    1.47 +  '\n' +
    1.48 +  '4 , 5        ,          6           ,7        ],"compact":[1,2,3,4,5,6,7],\n' +
    1.49 +  '        "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",\n' +
    1.50 +  '        "quotes": "&#34; \\u0022 %22 0x22 034 &#x22;",\n' +
    1.51 +  '        "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"\n' +
    1.52 +  ': "A key can be any string"\n' +
    1.53 +  '    },\n' +
    1.54 +  '    0.5 ,98.6\n' +
    1.55 +  ',\n' +
    1.56 +  '99.44\n' +
    1.57 +  ',\n' +
    1.58 +  '\n' +
    1.59 +  '1066,\n' +
    1.60 +  '1e1,\n' +
    1.61 +  '0.1e1,\n' +
    1.62 +  '1e-1,\n' +
    1.63 +  '1e00,2e+00,2e-00\n' +
    1.64 +  ',"rosebud"]\n';
    1.65 +
    1.66 +var x = JSON.parse(str);
    1.67 +
    1.68 +assertEq(x[0], "JSON Test Pattern pass1");
    1.69 +assertEq(x[1]["object with 1 member"][0], "array with 1 element");
    1.70 +assertEq(x[2].constructor, Object);
    1.71 +assertEq(x[3].constructor, Array);
    1.72 +assertEq(x[4], -42);
    1.73 +assertEq(x[5], true);
    1.74 +assertEq(x[6], false);
    1.75 +assertEq(x[7], null);
    1.76 +assertEq(x[8].constructor, Object);
    1.77 +assertEq(x[8]["integer"], 1234567890);
    1.78 +assertEq(x[8]["real"], -9876.543210);
    1.79 +assertEq(x[8]["e"], 0.123456789e-12);
    1.80 +assertEq(x[8]["E"], 1.234567890E+34);
    1.81 +assertEq(x[8][""], 23456789012E66);
    1.82 +assertEq(x[8]["zero"], 0);
    1.83 +assertEq(x[8]["one"], 1);
    1.84 +assertEq(x[8]["space"], " ");
    1.85 +assertEq(x[8]["quote"], "\"");
    1.86 +assertEq(x[8]["backslash"], "\\");
    1.87 +assertEq(x[8]["controls"], "\b\f\n\r\t");
    1.88 +assertEq(x[8]["slash"], "/ & /");
    1.89 +assertEq(x[8]["alpha"], "abcdefghijklmnopqrstuvwyz");
    1.90 +assertEq(x[8]["ALPHA"], "ABCDEFGHIJKLMNOPQRSTUVWYZ");
    1.91 +assertEq(x[8]["digit"], "0123456789");
    1.92 +assertEq(x[8]["0123456789"], "digit");
    1.93 +assertEq(x[8]["special"], "`1~!@#$%^&*()_+-={':[,]}|;.</>?");
    1.94 +assertEq(x[8]["hex"], "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A");
    1.95 +assertEq(x[8]["true"], true);
    1.96 +assertEq(x[8]["false"], false);
    1.97 +assertEq(x[8]["null"], null);
    1.98 +assertEq(x[8]["array"].length, 0);
    1.99 +assertEq(x[8]["object"].constructor, Object);
   1.100 +assertEq(x[8]["address"], "50 St. James Street");
   1.101 +assertEq(x[8]["url"], "http://www.JSON.org/");
   1.102 +assertEq(x[8]["comment"], "// /* <!-- --");
   1.103 +assertEq(x[8]["# -- --> */"], " ");
   1.104 +assertEq(x[8][" s p a c e d "].length, 7);
   1.105 +assertEq(x[8]["compact"].length, 7);
   1.106 +assertEq(x[8]["jsontext"], "{\"object with 1 member\":[\"array with 1 element\"]}");
   1.107 +assertEq(x[8]["quotes"], "&#34; \u0022 %22 0x22 034 &#x22;");
   1.108 +assertEq(x[8]["\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"], "A key can be any string");
   1.109 +assertEq(x[9], 0.5);
   1.110 +assertEq(x[10], 98.6);
   1.111 +assertEq(x[11], 99.44);
   1.112 +assertEq(x[12], 1066);
   1.113 +assertEq(x[13], 1e1);
   1.114 +assertEq(x[14], 0.1e1);
   1.115 +assertEq(x[15], 1e-1);
   1.116 +assertEq(x[16], 1e00);
   1.117 +assertEq(x[17], 2e+00);
   1.118 +assertEq(x[18], 2e-00);
   1.119 +assertEq(x[19], "rosebud");
   1.120 +
   1.121 +/******************************************************************************/
   1.122 +
   1.123 +if (typeof reportCompare === "function")
   1.124 +  reportCompare(true, true);
   1.125 +
   1.126 +print("Tests complete");

mercurial