1 description("Test behaviour of JSON reviver function.")
3 Array.isArray = function(o) { return o.constructor === Array; }
5 function arrayReviver(i,v) {
9 debug("Ensure the holder for our array is indeed an array");
10 shouldBeTrue("Array.isArray(currentHolder)");
11 shouldBe("currentHolder.length", "" + expectedLength);
14 debug("Ensure that we always get the same holder");
15 shouldBe("currentHolder", "lastHolder");
21 debug("Ensure that the holder already has all the properties present at the start of filtering");
22 shouldBe("currentHolder[0]", '"a value"');
23 shouldBe("currentHolder[1]", '"another value"');
24 shouldBe("currentHolder[2]", '"and another value"');
25 shouldBe("currentHolder[3]", '"to delete"');
26 shouldBe("currentHolder[4]", '"extra value"');
31 debug("Ensure that returning undefined has removed the property 0 from the holder during filtering.");
32 shouldBeFalse("currentHolder.hasOwnProperty(0)");
33 currentHolder[2] = "a replaced value";
38 debug("Ensure that changing the value of a property is reflected while filtering.")
39 shouldBe("currentHolder[2]", '"a replaced value"');
42 debug("Ensure that the changed value is reflected in the arguments passed to the reviver");
43 shouldBe("value", "currentHolder[2]");
49 debug("Ensure that we visited a value that we have deleted, and that deletion is reflected while filtering.");
50 shouldBeFalse("currentHolder.hasOwnProperty(3)");
53 debug("Ensure that when visiting a deleted property value is undefined");
54 shouldBeUndefined("value");
55 v = "undelete the property";
56 expectedLength = this.length = 3;
60 if (this.length != 3) {
61 testFailed("Did not call reviver for deleted property");
62 expectedLength = this.length = 3;
67 testPassed("Ensured that property was visited despite Array length being reduced.");
69 shouldBeUndefined("value");
74 testFailed("Visited unexpected property " + i + " with value " + v);
81 var result = JSON.parse('["a value", "another value", "and another value", "to delete", "extra value"]', arrayReviver);
83 debug("Ensure that we created the root holder as specified in ES5");
84 shouldBeTrue("'' in lastHolder");
85 shouldBe("result", "lastHolder['']");
87 debug("Ensure that a deleted value is revived if the reviver function returns a value");
88 shouldBeTrue("result.hasOwnProperty(3)");
90 function objectReviver(i,v) {
93 shouldBeTrue("currentHolder != globalObject");
96 debug("Ensure that we get the same holder object for each property");
97 shouldBe("currentHolder", "lastHolder");
104 debug("Ensure that the holder already has all the properties present at the start of filtering");
105 shouldBe("currentHolder['a property']", '"a value"');
106 shouldBe("currentHolder['another property']", '"another value"');
107 shouldBe("currentHolder['and another property']", '"and another value"');
108 shouldBe("currentHolder['to delete']", '"will be deleted"');
111 case "another property":
113 debug("Ensure that returning undefined has correctly removed the property 'a property' from the holder object");
114 shouldBeFalse("currentHolder.hasOwnProperty('a property')");
115 currentHolder['and another property'] = "a replaced value";
118 case "and another property":
119 debug("Ensure that changing the value of a property is reflected while filtering.");
120 shouldBe("currentHolder['and another property']", '"a replaced value"');
123 debug("Ensure that the changed value is reflected in the arguments passed to the reviver");
124 shouldBe("value", '"a replaced value"');
125 delete this["to delete"];
130 debug("Ensure that we visited a value that we have deleted, and that deletion is reflected while filtering.");
131 shouldBeFalse("currentHolder.hasOwnProperty('to delete')");
134 debug("Ensure that when visiting a deleted property value is undefined");
135 shouldBeUndefined("value");
136 v = "undelete the property";
137 this["new property"] = "fail";
140 testFailed("Visited unexpected property " + i + " with value " + v);
148 debug("Test behaviour of revivor used in conjunction with an object");
150 var globalObject = this;
151 var result = JSON.parse('{"a property" : "a value", "another property" : "another value", "and another property" : "and another value", "to delete" : "will be deleted"}', objectReviver);
153 debug("Ensure that we created the root holder as specified in ES5");
154 shouldBeTrue("lastHolder.hasOwnProperty('')");
155 shouldBeFalse("result.hasOwnProperty('a property')");
156 shouldBeTrue("result.hasOwnProperty('to delete')");
157 shouldBe("result", "lastHolder['']");
160 debug("Test behaviour of revivor that introduces a cycle");
161 function reviveAddsCycle(i, v) {
167 shouldThrow('JSON.parse("[0,1]", reviveAddsCycle)');
170 debug("Test behaviour of revivor that introduces a new array classed object (the result of a regex)");
171 var createdBadness = false;
172 function reviveIntroducesNewArrayLikeObject(i, v) {
173 if (i == 0 && !createdBadness) {
174 this[1] = /(a)+/.exec("a");
175 createdBadness = true;
180 shouldBe('JSON.stringify(JSON.parse("[0,1]", reviveIntroducesNewArrayLikeObject))', '\'[0,["a","a"]]\'');