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:
parent
29253bf932
commit
866172a721
Notes:
sideshowbarker
2024-07-19 07:36:25 +09:00
Author: https://github.com/linusg
Commit: 866172a721
Pull-request: https://github.com/SerenityOS/serenity/pull/1793
3 changed files with 106 additions and 0 deletions
64
Libraries/LibJS/Tests/Array.prototype.forEach.js
Normal file
64
Libraries/LibJS/Tests/Array.prototype.forEach.js
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue