OLD | NEW |
---|---|
(Empty) | |
1 <script> | |
2 if (window.testRunner) | |
3 testRunner.dumpAsText(); | |
4 </script> | |
5 | |
6 <div id="test"></div> | |
7 <pre id="output"></pre> | |
8 | |
9 <script> | |
10 var div = document.querySelector("#test"); | |
11 var output = document.querySelector("#output"); | |
12 var forEachValues = []; | |
13 | |
14 function checkForEachValues(expectedValues) { | |
15 if (forEachValues.length !== expectedValues.length) | |
16 return false; | |
17 for (var i = 0; i < forEachValues.length; i++) { | |
18 if (forEachValues[i][0] !== expectedValues[i][0] || forEachValues[i][1] !== expectedValues[i][1]) | |
19 return false; | |
20 } | |
21 return true; | |
22 } | |
23 | |
24 function checkForEachValuesAndOutput(expectedValues) { | |
25 if (checkForEachValues(expectedValues)) { | |
26 output.innerText += "pass\n"; | |
27 } else { | |
28 output.innerText += "fail\n"; | |
29 function outputItem(item) { output.innerText += " " + item[0] + ": " + item[1] + "\n"; } | |
arv (Not doing code reviews)
2013/07/31 16:45:24
new line after {
alancutter (OOO until 2018)
2013/08/05 09:33:49
Done.
| |
30 output.innerText += " expected:\n"; | |
31 expectedValues.forEach(outputItem); | |
32 output.innerText += " actual:\n"; | |
33 forEachValues.forEach(outputItem); | |
34 } | |
35 } | |
36 | |
37 output.innerText += "Test calling forEach with no parameters: "; | |
38 try { | |
39 div.style.var.forEach(); | |
40 output.innerText += "fail"; | |
41 } | |
42 catch (error) { | |
43 output.innerText += error; | |
44 } | |
45 output.innerText += "\n"; | |
46 | |
47 output.innerText += "Test calling forEach with non-function as first argument: " ; | |
48 try { | |
49 div.style.var.forEach({}); | |
50 output.innerText += "fail"; | |
51 } | |
52 catch (error) { | |
53 output.innerText += error; | |
54 } | |
55 output.innerText += "\n"; | |
56 | |
57 output.innerText += "Test calling forEach with thisArg specified: "; | |
58 div.style.var.clear(); | |
59 div.style.var.set("existing", "pass"); | |
60 window.x = "fail"; | |
61 div.style.var.forEach(function() { | |
62 output.innerText += this.x; | |
63 }, {x: "pass"}); | |
64 output.innerText += "\n"; | |
65 | |
66 output.innerText += "Test adding variable in forEach: "; | |
67 forEachValues = []; | |
68 div.style.var.forEach(function(value, name, varMap) { | |
69 forEachValues.push([name, value]); | |
70 if (name === "existing") { | |
71 varMap.set("added", "pass"); | |
72 } | |
73 }); | |
74 checkForEachValuesAndOutput([ | |
75 ["existing", "pass"], | |
76 ["added", "pass"], | |
77 ]); | |
78 | |
79 output.innerText += "Test deleting in forEach: "; | |
80 div.style.var.clear(); | |
81 div.style.var.set("existing", "pass"); | |
82 div.style.var.set("toDelete", "fail"); | |
83 forEachValues = []; | |
84 div.style.var.forEach(function(value, name, varMap) { | |
85 forEachValues.push([name, value]); | |
86 if (name === "existing") { | |
87 varMap.delete("toDelete"); | |
88 } | |
89 }); | |
90 checkForEachValuesAndOutput([ | |
91 ["existing", "pass"], | |
92 ]); | |
93 | |
94 output.innerText += "Test clearing in forEach: "; | |
95 div.style.var.clear(); | |
96 div.style.var.set("existing", "pass"); | |
97 div.style.var.set("toClear", "fail"); | |
98 forEachValues = []; | |
99 div.style.var.forEach(function(value, name, varMap) { | |
100 forEachValues.push([name, value]); | |
101 if (name === "existing") { | |
102 varMap.clear(); | |
103 } | |
104 }); | |
105 checkForEachValuesAndOutput([ | |
106 ["existing", "pass"], | |
107 ]); | |
108 | |
109 output.innerText += "Test adding then deleting in forEach: "; | |
110 div.style.var.clear(); | |
111 div.style.var.set("existing", "pass"); | |
112 forEachValues = []; | |
113 div.style.var.forEach(function(value, name, varMap) { | |
114 forEachValues.push([name, value]); | |
115 if (name === "existing") { | |
116 varMap.set("toDelete", "fail"); | |
117 varMap.delete("toDelete"); | |
118 } | |
119 }); | |
120 checkForEachValuesAndOutput([ | |
121 ["existing", "pass"], | |
122 ]); | |
123 | |
124 output.innerText += "Test adding then clearing in forEach: "; | |
125 div.style.var.clear(); | |
126 div.style.var.set("existing", "pass"); | |
127 forEachValues = []; | |
128 div.style.var.forEach(function(value, name, varMap) { | |
129 forEachValues.push([name, value]); | |
130 if (name === "existing") { | |
131 varMap.set("toClear", "fail"); | |
132 varMap.clear("toClear"); | |
133 } | |
134 }); | |
135 checkForEachValuesAndOutput([ | |
136 ["existing", "pass"], | |
137 ]); | |
138 | |
139 output.innerText += "Test deleting then adding in forEach: "; | |
140 div.style.var.clear(); | |
141 div.style.var.set("existing", "pass"); | |
142 div.style.var.set("toDelete", "fail"); | |
143 forEachValues = []; | |
144 div.style.var.forEach(function(value, name, varMap) { | |
145 forEachValues.push([name, value]); | |
146 if (name === "existing") { | |
147 varMap.delete("toDelete"); | |
148 varMap.set("added", "pass"); | |
149 } | |
150 }); | |
151 checkForEachValuesAndOutput([ | |
152 ["existing", "pass"], | |
153 ["added", "pass"], | |
154 ]); | |
155 | |
156 output.innerText += "Test clearing then adding in forEach: "; | |
157 div.style.var.clear(); | |
158 div.style.var.set("existing", "pass"); | |
159 forEachValues = []; | |
160 div.style.var.forEach(function(value, name, varMap) { | |
161 forEachValues.push([name, value]); | |
162 if (name === "existing") { | |
163 varMap.clear(); | |
164 varMap.set("added", "pass"); | |
165 } | |
166 }); | |
167 checkForEachValuesAndOutput([ | |
168 ["existing", "pass"], | |
169 ["added", "pass"], | |
170 ]); | |
171 | |
172 output.innerText += "Test updating visited variable in forEach: "; | |
173 div.style.var.clear(); | |
174 div.style.var.set("existingA", "pass"); | |
175 div.style.var.set("existingB", "pass"); | |
176 forEachValues = []; | |
177 div.style.var.forEach(function(value, name, varMap) { | |
178 forEachValues.push([name, value]); | |
179 if (name === "existingB") { | |
180 varMap.set("existingA", "fail"); | |
181 } | |
182 }); | |
183 checkForEachValuesAndOutput([ | |
184 ["existingA", "pass"], | |
185 ["existingB", "pass"], | |
186 ]); | |
187 | |
188 output.innerText += "Test nested forEach calls with addition and deletion: "; | |
189 div.style.var.clear(); | |
190 div.style.var.set("existingA", "pass"); | |
191 div.style.var.set("existingB", "pass"); | |
192 forEachValues = []; | |
193 div.style.var.forEach(function(value, name, varMap) { | |
194 forEachValues.push([name, value]); | |
195 varMap.forEach(function(innerValue, innerName) { | |
196 forEachValues.push([" " + innerName, innerValue]); | |
197 if (name === "existingA" && innerName === "existingB") { | |
198 varMap.delete("existingB"); | |
199 varMap.set("innerAdded", "pass"); | |
200 } | |
201 }); | |
202 }); | |
203 checkForEachValuesAndOutput([ | |
204 ["existingA", "pass"], | |
205 [" existingA", "pass"], | |
206 [" existingB", "pass"], | |
207 [" innerAdded", "pass"], | |
208 ["innerAdded", "pass"], | |
209 [" existingA", "pass"], | |
210 [" innerAdded", "pass"], | |
211 ]); | |
212 | |
213 </script> | |
OLD | NEW |