1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:27:45 +00:00

LibJS: Add tests for symbol object integration

This commit is contained in:
Matthew Olsson 2020-07-07 21:39:36 -07:00 committed by Andreas Kling
parent 7a1d485b19
commit 119386ffb0
13 changed files with 198 additions and 13 deletions

View file

@ -1,5 +1,7 @@
describe("normal functionality", () => {
test("non-configurable property", () => {
let s = Symbol("foo");
test("non-configurable string property", () => {
let o = {};
Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });
@ -13,6 +15,20 @@ describe("normal functionality", () => {
expect(o).toHaveValueProperty("foo", 1);
});
test("non-configurable symbol property", () => {
let o = {};
Object.defineProperty(o, s, { value: 1, writable: false, enumerable: false });
expect(o[s]).toBe(1);
o[s] = 2;
expect(o[s]).toBe(1);
expect(o).not.toHaveConfigurableProperty(s);
expect(o).not.toHaveEnumerableProperty(s);
expect(o).not.toHaveWritableProperty(s);
expect(o).toHaveValueProperty(s, 1);
});
test("array index getter", () => {
let o = {};
Object.defineProperty(o, 2, {
@ -23,7 +39,17 @@ describe("normal functionality", () => {
expect(o[2]).toBe(10);
});
test("configurable property", () => {
test("symbol property getter", () => {
let o = {};
Object.defineProperty(o, s, {
get() {
return 10;
},
});
expect(o[s]).toBe(10);
});
test("configurable string property", () => {
let o = {};
Object.defineProperty(o, "foo", { value: "hi", writable: true, enumerable: true });
@ -37,7 +63,21 @@ describe("normal functionality", () => {
expect(o).toHaveValueProperty("foo", "ho");
});
test("reconfigure configurable property", () => {
test("configurable symbol property", () => {
let o = {};
Object.defineProperty(o, s, { value: "hi", writable: true, enumerable: true });
expect(o[s]).toBe("hi");
o[s] = "ho";
expect(o[s]).toBe("ho");
expect(o).not.toHaveConfigurableProperty(s);
expect(o).toHaveEnumerableProperty(s);
expect(o).toHaveWritableProperty(s);
expect(o).toHaveValueProperty(s, "ho");
});
test("reconfigure configurable string property", () => {
let o = {};
Object.defineProperty(o, "foo", { value: 9, configurable: true, writable: false });
Object.defineProperty(o, "foo", { configurable: true, writable: true });
@ -48,7 +88,18 @@ describe("normal functionality", () => {
expect(o).toHaveValueProperty("foo", 9);
});
test("define accessor", () => {
test("reconfigure configurable symbol property", () => {
let o = {};
Object.defineProperty(o, s, { value: 9, configurable: true, writable: false });
Object.defineProperty(o, s, { configurable: true, writable: true });
expect(o).toHaveConfigurableProperty(s);
expect(o).toHaveWritableProperty(s);
expect(o).not.toHaveEnumerableProperty(s);
expect(o).toHaveValueProperty(s, 9);
});
test("define string accessor", () => {
let o = {};
Object.defineProperty(o, "foo", {
@ -72,6 +123,31 @@ describe("normal functionality", () => {
expect((o.foo = 5)).toBe(5);
expect((o.foo = 4)).toBe(4);
});
test("define symbol accessor", () => {
let o = {};
Object.defineProperty(o, s, {
configurable: true,
get() {
return o.secret_foo + 1;
},
set(value) {
this.secret_foo = value + 1;
},
});
o[s] = 10;
expect(o[s]).toBe(12);
o[s] = 20;
expect(o[s]).toBe(22);
Object.defineProperty(o, s, { configurable: true, value: 4 });
expect(o[s]).toBe(4);
expect((o[s] = 5)).toBe(5);
expect((o[s] = 4)).toBe(4);
});
});
describe("errors", () => {
@ -87,6 +163,19 @@ describe("errors", () => {
);
});
test("redefine non-configurable symbol property", () => {
let o = {};
let s = Symbol("foo");
Object.defineProperty(o, s, { value: 1, writable: true, enumerable: true });
expect(() => {
Object.defineProperty(o, s, { value: 2, writable: false, enumerable: true });
}).toThrowWithMessage(
TypeError,
"Cannot change attributes of non-configurable property 'Symbol(foo)'"
);
});
test("cannot define 'value' and 'get' in the same descriptor", () => {
let o = {};

View file

@ -19,6 +19,15 @@ describe("basic functionality", () => {
]);
});
test("entries with objects with symbol keys", () => {
let entries = Object.entries({ foo: 1, [Symbol("bar")]: 2, baz: 3 });
expect(entries).toEqual([
["foo", 1],
["baz", 3],
]);
});
test("entries with array", () => {
entries = Object.entries(["a", "b", "c"]);
expect(entries).toEqual([

View file

@ -9,6 +9,18 @@ test("plain property", () => {
expect(o).not.toHaveSetterProperty("foo");
});
test("symbol property", () => {
let s = Symbol("foo");
let o = { [s]: "bar" };
expect(o).toHaveConfigurableProperty(s);
expect(o).toHaveEnumerableProperty(s);
expect(o).toHaveWritableProperty(s);
expect(o).toHaveValueProperty(s, "bar");
expect(o).not.toHaveGetterProperty(s);
expect(o).not.toHaveSetterProperty(s);
});
test("getter property", () => {
let o = { get foo() {} };

View file

@ -7,3 +7,8 @@ test("use with object", () => {
let names = Object.getOwnPropertyNames({ foo: 1, bar: 2, baz: 3 });
expect(names).toEqual(["foo", "bar", "baz"]);
});
test("use with object with symbol keys", () => {
let names = Object.getOwnPropertyNames({ foo: 1, [Symbol("bar")]: 2, baz: 3 });
expect(names).toEqual(["foo", "baz"]);
});

View file

@ -14,6 +14,11 @@ describe("correct behavior", () => {
expect(keys).toEqual(["foo", "bar", "baz"]);
});
test("object argument with symbol keys", () => {
let keys = Object.keys({ foo: 1, [Symbol("bar")]: 2, baz: 3 });
expect(keys).toEqual(["foo", "baz"]);
});
test("array argument", () => {
let keys = Object.keys(["a", "b", "c"]);
expect(keys).toEqual(["0", "1", "2"]);

View file

@ -14,6 +14,11 @@ describe("correct behavior", () => {
expect(values).toEqual([1, 2, 3]);
});
test("object argument with symbol keys", () => {
let values = Object.values({ foo: 1, [Symbol("bar")]: 2, baz: 3 });
expect(values).toEqual([1, 3]);
});
test("array argument", () => {
let values = Object.values(["a", "b", "c"]);
expect(values).toEqual(["a", "b", "c"]);