1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 21:08:12 +00:00

LibJS: Implement Symbol.toStringTag

This commit is contained in:
Matthew Olsson 2020-07-11 10:27:00 -07:00 committed by Andreas Kling
parent 5ecd504f4e
commit 43d955014d
11 changed files with 55 additions and 0 deletions

View file

@ -0,0 +1,3 @@
test("basic functionality", () => {
expect(BigInt.prototype[Symbol.toStringTag]).toBe("BigInt");
});

View file

@ -0,0 +1,4 @@
test("basic functionality", () => {
expect(JSON[Symbol.toStringTag]).toBe("JSON");
expect(JSON.toString()).toBe("[object JSON]");
});

View file

@ -0,0 +1,4 @@
test("basic functionality", () => {
expect(Math[Symbol.toStringTag]).toBe("Math");
expect(Math.toString()).toBe("[object Math]");
});

View file

@ -0,0 +1,3 @@
test("basic functionality", () => {
expect(Symbol.prototype[Symbol.toStringTag]).toBe("Symbol");
});

View file

@ -0,0 +1,26 @@
test("inside objects", () => {
const o = {
[Symbol.toStringTag]: "hello friends",
};
expect(o.toString()).toBe("[object hello friends]");
});
test("inside classes", () => {
class A {
constructor() {
this[Symbol.toStringTag] = "hello friends";
}
}
const a = new A();
expect(a.toString()).toBe("[object hello friends]");
});
test("non-string values are ignored", () => {
const o = {
[Symbol.toStringTag]: [1, 2, 3],
};
expect(o.toString()).toBe("[object Object]");
});

View file

@ -2,6 +2,11 @@ test("length", () => {
expect(Array.prototype[Symbol.iterator].length).toBe(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);
});