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