mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-16 14:32:18 +00:00
LibJS: Add Array.prototype.keys()
This commit is contained in:
parent
dc65f54c06
commit
e044a3e428
Notes:
sideshowbarker
2024-07-18 12:22:16 +09:00
Author: https://github.com/davidot
Commit: e044a3e428
Pull-request: https://github.com/SerenityOS/serenity/pull/8020
Reviewed-by: https://github.com/linusg
3 changed files with 56 additions and 0 deletions
|
@ -0,0 +1,44 @@
|
|||
test("length", () => {
|
||||
expect(Array.prototype.keys.length).toBe(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const a = ["a", "b", "c"];
|
||||
const it = a.keys();
|
||||
expect(it.next()).toEqual({ value: 0, done: false });
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
||||
|
||||
test("works when applied to non-object", () => {
|
||||
[true, false, 9, 2n, Symbol()].forEach(primitive => {
|
||||
const it = [].keys.call(primitive);
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
||||
});
|
||||
|
||||
test("item added to array before exhaustion is accessible", () => {
|
||||
const a = ["a", "b"];
|
||||
const it = a.keys();
|
||||
expect(it.next()).toEqual({ value: 0, done: false });
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
a.push("c");
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
||||
|
||||
test("item added to array after exhaustion is inaccessible", () => {
|
||||
const a = ["a", "b"];
|
||||
const it = a.keys();
|
||||
expect(it.next()).toEqual({ value: 0, done: false });
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
a.push("c");
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue