js/src/tests/ecma_5/JSON/parse-crockford-01.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Ported from dom/src/json/test/unit/test_decode.js
michael@0 2
michael@0 3 var str =
michael@0 4 '[\n' +
michael@0 5 ' "JSON Test Pattern pass1",\n' +
michael@0 6 ' {"object with 1 member":["array with 1 element"]},\n' +
michael@0 7 ' {},\n' +
michael@0 8 ' [],\n' +
michael@0 9 ' -42,\n' +
michael@0 10 ' true,\n' +
michael@0 11 ' false,\n' +
michael@0 12 ' null,\n' +
michael@0 13 ' {\n' +
michael@0 14 ' "integer": 1234567890,\n' +
michael@0 15 ' "real": -9876.543210,\n' +
michael@0 16 ' "e": 0.123456789e-12,\n' +
michael@0 17 ' "E": 1.234567890E+34,\n' +
michael@0 18 ' "": 23456789012E66,\n' +
michael@0 19 ' "zero": 0,\n' +
michael@0 20 ' "one": 1,\n' +
michael@0 21 ' "space": " ",\n' +
michael@0 22 ' "quote": "\\"",\n' +
michael@0 23 ' "backslash": "\\\\",\n' +
michael@0 24 ' "controls": "\\b\\f\\n\\r\\t",\n' +
michael@0 25 ' "slash": "/ & \\/",\n' +
michael@0 26 ' "alpha": "abcdefghijklmnopqrstuvwyz",\n' +
michael@0 27 ' "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",\n' +
michael@0 28 ' "digit": "0123456789",\n' +
michael@0 29 ' "0123456789": "digit",\n' +
michael@0 30 ' "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",\n' +
michael@0 31 ' "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",\n' +
michael@0 32 ' "true": true,\n' +
michael@0 33 ' "false": false,\n' +
michael@0 34 ' "null": null,\n' +
michael@0 35 ' "array":[ ],\n' +
michael@0 36 ' "object":{ },\n' +
michael@0 37 ' "address": "50 St. James Street",\n' +
michael@0 38 ' "url": "http://www.JSON.org/",\n' +
michael@0 39 ' "comment": "// /* <!-- --",\n' +
michael@0 40 ' "# -- --> */": " ",\n' +
michael@0 41 ' " s p a c e d " :[1,2 , 3\n' +
michael@0 42 '\n' +
michael@0 43 ',\n' +
michael@0 44 '\n' +
michael@0 45 '4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],\n' +
michael@0 46 ' "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",\n' +
michael@0 47 ' "quotes": "&#34; \\u0022 %22 0x22 034 &#x22;",\n' +
michael@0 48 ' "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"\n' +
michael@0 49 ': "A key can be any string"\n' +
michael@0 50 ' },\n' +
michael@0 51 ' 0.5 ,98.6\n' +
michael@0 52 ',\n' +
michael@0 53 '99.44\n' +
michael@0 54 ',\n' +
michael@0 55 '\n' +
michael@0 56 '1066,\n' +
michael@0 57 '1e1,\n' +
michael@0 58 '0.1e1,\n' +
michael@0 59 '1e-1,\n' +
michael@0 60 '1e00,2e+00,2e-00\n' +
michael@0 61 ',"rosebud"]\n';
michael@0 62
michael@0 63 var x = JSON.parse(str);
michael@0 64
michael@0 65 assertEq(x[0], "JSON Test Pattern pass1");
michael@0 66 assertEq(x[1]["object with 1 member"][0], "array with 1 element");
michael@0 67 assertEq(x[2].constructor, Object);
michael@0 68 assertEq(x[3].constructor, Array);
michael@0 69 assertEq(x[4], -42);
michael@0 70 assertEq(x[5], true);
michael@0 71 assertEq(x[6], false);
michael@0 72 assertEq(x[7], null);
michael@0 73 assertEq(x[8].constructor, Object);
michael@0 74 assertEq(x[8]["integer"], 1234567890);
michael@0 75 assertEq(x[8]["real"], -9876.543210);
michael@0 76 assertEq(x[8]["e"], 0.123456789e-12);
michael@0 77 assertEq(x[8]["E"], 1.234567890E+34);
michael@0 78 assertEq(x[8][""], 23456789012E66);
michael@0 79 assertEq(x[8]["zero"], 0);
michael@0 80 assertEq(x[8]["one"], 1);
michael@0 81 assertEq(x[8]["space"], " ");
michael@0 82 assertEq(x[8]["quote"], "\"");
michael@0 83 assertEq(x[8]["backslash"], "\\");
michael@0 84 assertEq(x[8]["controls"], "\b\f\n\r\t");
michael@0 85 assertEq(x[8]["slash"], "/ & /");
michael@0 86 assertEq(x[8]["alpha"], "abcdefghijklmnopqrstuvwyz");
michael@0 87 assertEq(x[8]["ALPHA"], "ABCDEFGHIJKLMNOPQRSTUVWYZ");
michael@0 88 assertEq(x[8]["digit"], "0123456789");
michael@0 89 assertEq(x[8]["0123456789"], "digit");
michael@0 90 assertEq(x[8]["special"], "`1~!@#$%^&*()_+-={':[,]}|;.</>?");
michael@0 91 assertEq(x[8]["hex"], "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A");
michael@0 92 assertEq(x[8]["true"], true);
michael@0 93 assertEq(x[8]["false"], false);
michael@0 94 assertEq(x[8]["null"], null);
michael@0 95 assertEq(x[8]["array"].length, 0);
michael@0 96 assertEq(x[8]["object"].constructor, Object);
michael@0 97 assertEq(x[8]["address"], "50 St. James Street");
michael@0 98 assertEq(x[8]["url"], "http://www.JSON.org/");
michael@0 99 assertEq(x[8]["comment"], "// /* <!-- --");
michael@0 100 assertEq(x[8]["# -- --> */"], " ");
michael@0 101 assertEq(x[8][" s p a c e d "].length, 7);
michael@0 102 assertEq(x[8]["compact"].length, 7);
michael@0 103 assertEq(x[8]["jsontext"], "{\"object with 1 member\":[\"array with 1 element\"]}");
michael@0 104 assertEq(x[8]["quotes"], "&#34; \u0022 %22 0x22 034 &#x22;");
michael@0 105 assertEq(x[8]["\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"], "A key can be any string");
michael@0 106 assertEq(x[9], 0.5);
michael@0 107 assertEq(x[10], 98.6);
michael@0 108 assertEq(x[11], 99.44);
michael@0 109 assertEq(x[12], 1066);
michael@0 110 assertEq(x[13], 1e1);
michael@0 111 assertEq(x[14], 0.1e1);
michael@0 112 assertEq(x[15], 1e-1);
michael@0 113 assertEq(x[16], 1e00);
michael@0 114 assertEq(x[17], 2e+00);
michael@0 115 assertEq(x[18], 2e-00);
michael@0 116 assertEq(x[19], "rosebud");
michael@0 117
michael@0 118 /******************************************************************************/
michael@0 119
michael@0 120 if (typeof reportCompare === "function")
michael@0 121 reportCompare(true, true);
michael@0 122
michael@0 123 print("Tests complete");

mercurial