blob: 9e21996eb15d177fa043ed48d0d21cd65ac5fe9e [file] [log] [blame]
<html>
<head>
<script type="text/javascript">
function print(str) {
document.writeln(str+"<br/>");
}
if (window.testRunner)
testRunner.dumpAsText();
</script>
</head>
<body>
1.0 Single Argument Testing<br/>
The following tests forEach with one argument, the callback. It should print the contents of the array [2, 5, 9] alongside each index.<br/><br/>
<script>
function printElt(element, index, array) {
print("[" + index + "] is " + element);
}
[2, 5, 9].forEach(printElt);
</script>
<br/>
2.0 Two Argument Testing<br/>
The following tests forEach with two arguments, the callback and the applied "this" object. It should print the contents of the array.<br/><br/>
<script>
var writer = {
sb: [],
write: function (s) {
this.sb.push(s);
},
writeln: function (s) {
this.write(s+"<br/>");
},
toString: function () {
return this.sb.join("");
}
};
[2, 5, 9].forEach(writer.writeln, writer);
print(writer.toString());
</script>
3.0 Array Mutation Tests<br/>
These tests the affects of array mutation during execution of forEach.<br/><br/>
3.1 Array Element Removal<br/>
This test removes elements from the array, these elements should thus not appear since forEach doesn't iterate over non-existing properties.<br/><br/>
<script>
function printEltAndPop(element, index, array) {
print("[" + index + "] is " + element);
array.pop();
}
[2, 5, 9].forEach(printEltAndPop);
</script>
<br/>
3.2 Array Element Addition<br/>
This test adds elements to the array, these elements should not appear since forEach uses the original length to create the range it iterates over. It should be identical to 1.0.<br/><br/>
<script>
function printEltAndPush(element, index, array) {
print("[" + index + "] is " + element);
array.push(1);
}
[2, 5, 9].forEach(printEltAndPush);
</script>
<br/>
3.3 Array Element Changing<br/>
This test changes elements in the array, these elements should appear in their mutated form when reached by forEach.<br/><br/>
<script>
function printEltAndChange(element, index, array) {
print("[" + index + "] is " + element);
array[array.length - 1 - index] = "changed";
}
[2, 5, 9].forEach(printEltAndChange);
</script>
<br/>
4.0 Exception Test<br/>
This test uses a function that throws an exception, and thus halts the execution of forEach.<br/><br/>
<script>
function printEltAndException(element, index, array) {
print("[" + index + "] is " + element);
if (index == 1)
throw "Exception thrown, execution halted!";
}
try {
[2, 5, 9].forEach(printEltAndException);
} catch (e) {
print(e);
}
</script>
<br/>
5.0 Wrong Type for Callback Test
This test sends in incorrect types for the callback parameter of forEach. An exception should be thrown in each case. There should be 6 type errors (and no crashes!):<br/><br/>
<script>
try {
[2, 5, 9].forEach(5);
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach("wrong");
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach(new Object());
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach(null);
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach(undefined);
} catch (e) {
print(e)
}
try {
[2, 5, 9].forEach();
} catch (e) {
print(e);
}
</script>
<br/>
6.0 Behavior for Holes in Arrays<br/>
This test checks that holes in arrays (indexes which have been deleted or are not present) are not included in enumeration:<br/><br/>
<script>
function throwIfUndefined(element, index, array) {
if (typeof element === "undefined")
throw "undefined element enumerated!";
}
var arr;
try {
arr = [5, 5, 5, 5];
delete arr[1];
arr.forEach(throwIfUndefined);
print("Manually deleted index not enumerated");
} catch (e) {
print(e);
}
try {
arr = new Array(20);
arr.forEach(throwIfUndefined);
print("Array created using constructor has no properties, so no indexes enumerated");
} catch (e) {
print(e);
}
</script>
<br/>
7.0 Return Value<br/>
This test checks that the return value of Array.prototype.forEach is undefined:<br/><br/>
<script>
var wasUndefined = typeof ([1, 2, 3].forEach(function(){})) === "undefined";
print("Return value is " + (wasUndefined ? "" : "NOT ") + "undefined!");
</script>
</body>
</html>