michael@0: // Any copyright is dedicated to the Public Domain. michael@0: // http://creativecommons.org/licenses/publicdomain/ michael@0: michael@0: var gTestfile = 'parse-reviver-array-delete.js'; michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 999999; michael@0: var summary = "JSON.parse with a reviver which elides array elements"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: /* michael@0: * The reviver deletes all properties from the to-be-returned array. Thus michael@0: * stringification reveals properties on the prototype chain -- but there are michael@0: * none, so this result is unsurprising. michael@0: */ michael@0: assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]', michael@0: function revive(k, v) michael@0: { michael@0: if (k === "") michael@0: return v; michael@0: return undefined; michael@0: }) + "", michael@0: ",,,,,,,,,,,,,,,,,,,"); michael@0: michael@0: /* michael@0: * Now let's try a reviver that deletes every property but a mega-huge one. michael@0: */ michael@0: var str = "["; michael@0: var expected = ""; michael@0: var expected2 = ""; michael@0: for (var i = 0; i < 2048; i++) michael@0: { michael@0: str += "1,"; michael@0: if (i === 2047) michael@0: { michael@0: expected += "1"; michael@0: expected2 += "1"; michael@0: } michael@0: if (i === 3) michael@0: expected2 += "17"; michael@0: expected += ","; michael@0: expected2 += ","; michael@0: } michael@0: str += "1]"; michael@0: michael@0: assertEq(JSON.parse(str, michael@0: function reviver(k, v) michael@0: { michael@0: if (k === "" || k === "2047") michael@0: return v; michael@0: return undefined; michael@0: }) + "", michael@0: expected); michael@0: michael@0: michael@0: Array.prototype[3] = 17; michael@0: michael@0: /* Now, with a property on the prototype chain, it'll show through. */ michael@0: assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]', michael@0: function revive(k, v) michael@0: { michael@0: if (k === "") michael@0: return v; michael@0: return undefined; michael@0: }) + "", michael@0: ",,,17,,,,,,,,,,,,,,,,"); michael@0: michael@0: michael@0: /* And here too. */ michael@0: assertEq(JSON.parse(str, michael@0: function reviver(k, v) michael@0: { michael@0: if (k === "" || k === "2047") michael@0: return v; michael@0: return undefined; michael@0: }) + "", michael@0: expected2); michael@0: michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("Tests complete");