1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/JSON/stringify-replacer.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,157 @@ 1.4 +// Ported from dom/src/json/test/unit/test_replacer.js 1.5 + 1.6 +/** 1.7 + * These return* functions are used by the 1.8 + * replacer tests taken from bug 512447 1.9 + */ 1.10 +function returnObjectFor1(k, v) 1.11 +{ 1.12 + if (k == "1") 1.13 + return {}; 1.14 + return v; 1.15 +} 1.16 +function returnArrayFor1(k, v) 1.17 +{ 1.18 + if (k == "1") 1.19 + return []; 1.20 + return v; 1.21 +} 1.22 +function returnNullFor1(k, v) 1.23 +{ 1.24 + if (k == "1") 1.25 + return null; 1.26 + return v; 1.27 +} 1.28 +function returnStringForUndefined(k, v) 1.29 +{ 1.30 + if (v === undefined) 1.31 + return "undefined value"; 1.32 + return v; 1.33 +} 1.34 +var cycleObject = {}; cycleObject.cycle = cycleObject; 1.35 +function returnCycleObjectFor1(k, v) 1.36 +{ 1.37 + if (k == "1") 1.38 + return cycleObject; 1.39 + return v; 1.40 +} 1.41 +var array = [0, 1, 2]; array[3] = array; 1.42 +function returnCycleArrayFor1(k, v) 1.43 +{ 1.44 + if (k == "1") 1.45 + return array; 1.46 + return v; 1.47 +} 1.48 + 1.49 +// BEGIN TEST 1.50 +var x; 1.51 + 1.52 +x = JSON.stringify({ key: 2 }, 1.53 + function(k,v) { return k ? undefined : v; }); 1.54 +assertEq(x, "{}"); 1.55 + 1.56 +x = JSON.stringify(["hmm", "hmm"], 1.57 + function(k,v) { return k !== "" ? undefined : v; }); 1.58 +assertEq(x, "[null,null]"); 1.59 + 1.60 +var foo = ["hmm"]; 1.61 +function censor(k, v) 1.62 +{ 1.63 + if (v !== foo) 1.64 + return "XXX"; 1.65 + return v; 1.66 +} 1.67 +x = JSON.stringify(foo, censor); 1.68 +assertEq(x, '["XXX"]'); 1.69 + 1.70 +foo = ["bar", ["baz"], "qux"]; 1.71 +x = JSON.stringify(foo, censor); 1.72 +assertEq(x, '["XXX","XXX","XXX"]'); 1.73 + 1.74 +function censor2(k, v) 1.75 +{ 1.76 + if (typeof(v) == "string") 1.77 + return "XXX"; 1.78 + return v; 1.79 +} 1.80 + 1.81 +foo = ["bar", ["baz"], "qux"]; 1.82 +x = JSON.stringify(foo, censor2); 1.83 +assertEq(x, '["XXX",["XXX"],"XXX"]'); 1.84 + 1.85 +foo = { bar: 42, qux: 42, quux: 42 }; 1.86 +x = JSON.stringify(foo, ["bar"]); 1.87 +assertEq(x, '{"bar":42}'); 1.88 + 1.89 +foo = {bar: {bar: 42, schmoo:[]}, qux: 42, quux: 42}; 1.90 +x = JSON.stringify(foo, ["bar", "schmoo"]); 1.91 +assertEq(x, '{"bar":{"bar":42,"schmoo":[]}}'); 1.92 + 1.93 +x = JSON.stringify(foo, null, ""); 1.94 +assertEq(x, '{"bar":{"bar":42,"schmoo":[]},"qux":42,"quux":42}'); 1.95 + 1.96 +x = JSON.stringify(foo, null, " "); 1.97 +assertEq(x, '{\n "bar": {\n "bar": 42,\n "schmoo": []\n },\n "qux": 42,\n "quux": 42\n}'); 1.98 + 1.99 +foo = {bar:{bar:{}}} 1.100 +x = JSON.stringify(foo, null, " "); 1.101 +assertEq(x, '{\n "bar": {\n "bar": {}\n }\n}'); 1.102 + 1.103 +x = JSON.stringify({ x: 1, arr: [1] }, 1.104 + function (k,v) { return typeof v === 'number' ? 3 : v; }); 1.105 +assertEq(x, '{"x":3,"arr":[3]}'); 1.106 + 1.107 +foo = ['e']; 1.108 +x = JSON.stringify(foo, null, '\t'); 1.109 +assertEq(x, '[\n\t"e"\n]'); 1.110 + 1.111 +foo = {0:0, 1:1, 2:2, 3:undefined}; 1.112 +x = JSON.stringify(foo, returnObjectFor1); 1.113 +assertEq(x, '{"0":0,"1":{},"2":2}'); 1.114 + 1.115 +x = JSON.stringify(foo, returnArrayFor1); 1.116 +assertEq(x, '{"0":0,"1":[],"2":2}'); 1.117 + 1.118 +x = JSON.stringify(foo, returnNullFor1); 1.119 +assertEq(x, '{"0":0,"1":null,"2":2}'); 1.120 + 1.121 +x = JSON.stringify(foo, returnStringForUndefined); 1.122 +assertEq(x, '{"0":0,"1":1,"2":2,"3":"undefined value"}'); 1.123 + 1.124 +try 1.125 +{ 1.126 + JSON.stringify(foo, returnCycleObjectFor1); 1.127 + throw new Error("no error thrown"); 1.128 +} 1.129 +catch (e) 1.130 +{ 1.131 + assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); 1.132 +} 1.133 + 1.134 +try 1.135 +{ 1.136 + JSON.stringify(foo, returnCycleArrayFor1); 1.137 + throw new Error("no error thrown"); 1.138 +} 1.139 +catch (e) 1.140 +{ 1.141 + assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); 1.142 +} 1.143 + 1.144 +foo = [0, 1, 2, undefined]; 1.145 +try 1.146 +{ 1.147 + JSON.stringify(foo, returnCycleObjectFor1); 1.148 + throw new Error("no error thrown"); 1.149 +} 1.150 +catch (e) 1.151 +{ 1.152 + assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); 1.153 +} 1.154 + 1.155 +/******************************************************************************/ 1.156 + 1.157 +if (typeof reportCompare === "function") 1.158 + reportCompare(true, true); 1.159 + 1.160 +print("Tests complete");