mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:37:34 +00:00
Libraries: Move to Userland/Libraries/
This commit is contained in:
parent
dc28c07fa5
commit
13d7c09125
1857 changed files with 266 additions and 274 deletions
|
@ -0,0 +1,12 @@
|
|||
const getIteratorPrototype = () =>
|
||||
Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
|
||||
|
||||
test("prototype of %IteratorPrototype% is %ObjectPrototype%", () => {
|
||||
let itProto = getIteratorPrototype();
|
||||
expect(Object.getPrototypeOf(itProto)).toBe(Object.getPrototypeOf({}));
|
||||
});
|
||||
|
||||
test("@@iterator of %IteratorPrototype% is itself", () => {
|
||||
let itProto = getIteratorPrototype();
|
||||
expect(itProto[Symbol.iterator]()).toBe(itProto);
|
||||
});
|
53
Userland/Libraries/LibJS/Tests/iterators/array-iterator.js
Normal file
53
Userland/Libraries/LibJS/Tests/iterators/array-iterator.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
test("length", () => {
|
||||
expect(Array.prototype[Symbol.iterator]).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("@@toStringTag", () => {
|
||||
expect([].values()[Symbol.toStringTag]).toBe("Array Iterator");
|
||||
expect([].values().toString()).toBe("[object Array Iterator]");
|
||||
});
|
||||
|
||||
test("same function as Array.prototype.values", () => {
|
||||
expect(Array.prototype[Symbol.iterator]).toBe(Array.prototype.values);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const a = [1, 2, 3];
|
||||
const it = a[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
expect(it.next()).toEqual({ value: 3, 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 = [][Symbol.iterator].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 = [1, 2];
|
||||
const it = a[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
a.push(3);
|
||||
expect(it.next()).toEqual({ value: 3, 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 = [1, 2];
|
||||
const it = a[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: 1, done: false });
|
||||
expect(it.next()).toEqual({ value: 2, done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
a.push(3);
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
48
Userland/Libraries/LibJS/Tests/iterators/string-iterator.js
Normal file
48
Userland/Libraries/LibJS/Tests/iterators/string-iterator.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
test("length", () => {
|
||||
expect(String.prototype[Symbol.iterator]).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const s = "abcd";
|
||||
const it = s[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: "a", done: false });
|
||||
expect(it.next()).toEqual({ value: "b", done: false });
|
||||
expect(it.next()).toEqual({ value: "c", done: false });
|
||||
expect(it.next()).toEqual({ value: "d", 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("casts |this| to string", () => {
|
||||
const it = String.prototype[Symbol.iterator].call(45);
|
||||
expect(it.next()).toEqual({ value: "4", done: false });
|
||||
expect(it.next()).toEqual({ value: "5", done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
|
||||
const it = String.prototype[Symbol.iterator].call(false);
|
||||
expect(it.next()).toEqual({ value: "f", done: false });
|
||||
expect(it.next()).toEqual({ value: "a", done: false });
|
||||
expect(it.next()).toEqual({ value: "l", done: false });
|
||||
expect(it.next()).toEqual({ value: "s", done: false });
|
||||
expect(it.next()).toEqual({ value: "e", done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
|
||||
expect(() => {
|
||||
String.prototype[Symbol.iterator].call(null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
expect(() => {
|
||||
String.prototype[Symbol.iterator].call(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
|
||||
test("utf8 compatible", () => {
|
||||
const it = "ab\u{1f41e}cde"[Symbol.iterator]();
|
||||
expect(it.next()).toEqual({ value: "a", done: false });
|
||||
expect(it.next()).toEqual({ value: "b", done: false });
|
||||
expect(it.next()).toEqual({ value: "🐞", done: false });
|
||||
expect(it.next()).toEqual({ value: "c", done: false });
|
||||
expect(it.next()).toEqual({ value: "d", done: false });
|
||||
expect(it.next()).toEqual({ value: "e", done: false });
|
||||
expect(it.next()).toEqual({ value: undefined, done: true });
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue