js/src/tests/ecma_5/JSON/parse-reviver-array-delete.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:42ef4f7aa0e4
1 // Any copyright is dedicated to the Public Domain.
2 // http://creativecommons.org/licenses/publicdomain/
3
4 var gTestfile = 'parse-reviver-array-delete.js';
5 //-----------------------------------------------------------------------------
6 var BUGNUMBER = 999999;
7 var summary = "JSON.parse with a reviver which elides array elements";
8
9 print(BUGNUMBER + ": " + summary);
10
11 /**************
12 * BEGIN TEST *
13 **************/
14
15 /*
16 * The reviver deletes all properties from the to-be-returned array. Thus
17 * stringification reveals properties on the prototype chain -- but there are
18 * none, so this result is unsurprising.
19 */
20 assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]',
21 function revive(k, v)
22 {
23 if (k === "")
24 return v;
25 return undefined;
26 }) + "",
27 ",,,,,,,,,,,,,,,,,,,");
28
29 /*
30 * Now let's try a reviver that deletes every property but a mega-huge one.
31 */
32 var str = "[";
33 var expected = "";
34 var expected2 = "";
35 for (var i = 0; i < 2048; i++)
36 {
37 str += "1,";
38 if (i === 2047)
39 {
40 expected += "1";
41 expected2 += "1";
42 }
43 if (i === 3)
44 expected2 += "17";
45 expected += ",";
46 expected2 += ",";
47 }
48 str += "1]";
49
50 assertEq(JSON.parse(str,
51 function reviver(k, v)
52 {
53 if (k === "" || k === "2047")
54 return v;
55 return undefined;
56 }) + "",
57 expected);
58
59
60 Array.prototype[3] = 17;
61
62 /* Now, with a property on the prototype chain, it'll show through. */
63 assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]',
64 function revive(k, v)
65 {
66 if (k === "")
67 return v;
68 return undefined;
69 }) + "",
70 ",,,17,,,,,,,,,,,,,,,,");
71
72
73 /* And here too. */
74 assertEq(JSON.parse(str,
75 function reviver(k, v)
76 {
77 if (k === "" || k === "2047")
78 return v;
79 return undefined;
80 }) + "",
81 expected2);
82
83
84 /******************************************************************************/
85
86 if (typeof reportCompare === "function")
87 reportCompare(true, true);
88
89 print("Tests complete");

mercurial