1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 09:34:57 +09:00

LibJS: Add Array.prototype.forEach()

This commit is contained in:
Linus Groh 2020-04-13 18:49:08 +01:00 committed by Andreas Kling
parent 29253bf932
commit 866172a721
Notes: sideshowbarker 2024-07-19 07:36:25 +09:00
3 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,64 @@
load("test-common.js");
try {
assert(Array.prototype.forEach.length === 1);
try {
[].forEach();
assertNotReached();
} catch (e) {
assert(e.name === "TypeError");
assert(e.message === "Array.prototype.forEach() requires at least one argument");
}
try {
[].forEach(undefined);
assertNotReached();
} catch (e) {
assert(e.name === "TypeError");
assert(e.message === "undefined is not a function");
}
var a = [1, 2, 3];
var o = {};
var callbackCalled = 0;
var callback = () => { callbackCalled++; };
assert([].forEach(callback) === undefined);
assert(callbackCalled === 0);
assert(a.forEach(callback) === undefined);
assert(callbackCalled === 3);
callbackCalled = 0;
a.forEach(function(value, index) {
assert(value === a[index]);
assert(index === a[index] - 1);
});
callbackCalled = 0;
a.forEach(function(_, _, array) {
callbackCalled++;
assert(a.length === array.length);
a.push("test");
});
assert(callbackCalled === 3);
assert(a.length === 6);
callbackCalled = 0;
a.forEach(function(value, index) {
callbackCalled++;
this[index] = value;
}, o);
assert(callbackCalled === 6);
assert(o[0] === 1);
assert(o[1] === 2);
assert(o[2] === 3);
assert(o[3] === "test");
assert(o[4] === "test");
assert(o[5] === "test");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}