diff options
Diffstat (limited to 'Libraries/LibJS/Tests')
13 files changed, 198 insertions, 13 deletions
diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.defineProperty.js b/Libraries/LibJS/Tests/builtins/Object/Object.defineProperty.js index 7b44e0810e..ef72515569 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.defineProperty.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.defineProperty.js @@ -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 = {}; diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.entries.js b/Libraries/LibJS/Tests/builtins/Object/Object.entries.js index 0296074072..c8ed8ffc4c 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.entries.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.entries.js @@ -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([ diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js b/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js index a1783add2c..3b69241d9d 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js @@ -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() {} }; diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyNames.js b/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyNames.js index 69ab0685df..da7bccbeba 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyNames.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyNames.js @@ -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"]); +}); diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.keys.js b/Libraries/LibJS/Tests/builtins/Object/Object.keys.js index ae7847a424..64e822abd7 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.keys.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.keys.js @@ -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"]); diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.values.js b/Libraries/LibJS/Tests/builtins/Object/Object.values.js index ba6867a34a..5917d94cc8 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.values.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.values.js @@ -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"]); diff --git a/Libraries/LibJS/Tests/classes/class-getters.js b/Libraries/LibJS/Tests/classes/class-getters.js index f3ee040994..5b51910b3e 100644 --- a/Libraries/LibJS/Tests/classes/class-getters.js +++ b/Libraries/LibJS/Tests/classes/class-getters.js @@ -18,6 +18,8 @@ test("name", () => { }); test("extended name syntax", () => { + const s = Symbol("foo"); + class A { get "method with space"() { return 1; @@ -30,12 +32,17 @@ test("extended name syntax", () => { get [`he${"llo"}`]() { return 3; } + + get [s]() { + return 4; + } } const a = new A(); expect(a["method with space"]).toBe(1); expect(a[12]).toBe(2); expect(a.hello).toBe(3); + expect(a[s]).toBe(4); }); test("inherited getter", () => { diff --git a/Libraries/LibJS/Tests/classes/class-setters.js b/Libraries/LibJS/Tests/classes/class-setters.js index 024ad42ac9..99c307994b 100644 --- a/Libraries/LibJS/Tests/classes/class-setters.js +++ b/Libraries/LibJS/Tests/classes/class-setters.js @@ -29,6 +29,8 @@ test("name", () => { }); test("extended name syntax", () => { + const s = Symbol("foo"); + class A { set "method with space"(value) { this.a = value; @@ -41,15 +43,21 @@ test("extended name syntax", () => { set [`he${"llo"}`](value) { this.c = value; } + + set [s](value) { + this.d = value; + } } const a = new A(); a["method with space"] = 1; a[12] = 2; a.hello = 3; + a[s] = 4; expect(a.a).toBe(1); expect(a.b).toBe(2); expect(a.c).toBe(3); + expect(a.d).toBe(4); }); test("inherited setter", () => { diff --git a/Libraries/LibJS/Tests/classes/class-static-getters.js b/Libraries/LibJS/Tests/classes/class-static-getters.js index 2fb54b501c..c158a00595 100644 --- a/Libraries/LibJS/Tests/classes/class-static-getters.js +++ b/Libraries/LibJS/Tests/classes/class-static-getters.js @@ -20,6 +20,8 @@ describe("correct behavior", () => { }); test("extended name syntax", () => { + const s = Symbol("foo"); + class A { static get "method with space"() { return 1; @@ -32,11 +34,16 @@ describe("correct behavior", () => { static get [`he${"llo"}`]() { return 3; } + + static get [s]() { + return 4; + } } expect(A["method with space"]).toBe(1); expect(A[12]).toBe(2); expect(A.hello).toBe(3); + expect(A[s]).toBe(4); }); test("inherited static getter", () => { diff --git a/Libraries/LibJS/Tests/classes/class-static-setters.js b/Libraries/LibJS/Tests/classes/class-static-setters.js index 0989a6ae03..38cbbe2394 100644 --- a/Libraries/LibJS/Tests/classes/class-static-setters.js +++ b/Libraries/LibJS/Tests/classes/class-static-setters.js @@ -27,6 +27,8 @@ describe("correct behavior", () => { }); test("extended name syntax", () => { + const s = Symbol("foo"); + class A { static set "method with space"(value) { this.a = value; @@ -39,14 +41,20 @@ describe("correct behavior", () => { static set [`he${"llo"}`](value) { this.c = value; } + + static set [s](value) { + this.d = value; + } } A["method with space"] = 1; A[12] = 2; A.hello = 3; + A[s] = 4; expect(A.a).toBe(1); expect(A.b).toBe(2); expect(A.c).toBe(3); + expect(A.d).toBe(4); }); test("inherited static setter", () => { diff --git a/Libraries/LibJS/Tests/object-basic.js b/Libraries/LibJS/Tests/object-basic.js index 754d281dc7..e00382fdc2 100644 --- a/Libraries/LibJS/Tests/object-basic.js +++ b/Libraries/LibJS/Tests/object-basic.js @@ -31,6 +31,14 @@ describe("correct behavior", () => { expect(o["hello"]).toBe("friends"); }); + test("symbol keys", () => { + let object = {}; + let symbol = Symbol("foo"); + + object[symbol] = 2; + expect(object[symbol]).toBe(2); + }); + test("computed properties", () => { const foo = "bar"; const computed = "computed"; diff --git a/Libraries/LibJS/Tests/object-method-shorthand.js b/Libraries/LibJS/Tests/object-method-shorthand.js index aedff10bf9..8d8dfd512f 100644 --- a/Libraries/LibJS/Tests/object-method-shorthand.js +++ b/Libraries/LibJS/Tests/object-method-shorthand.js @@ -37,3 +37,14 @@ test("computed property method shorthand", () => { }; expect(o[14]()).toBe("bar"); }); + +test("symbol computed property shorthand", () => { + const s = Symbol("foo"); + const o = { + foo: "bar", + [s]() { + return this.foo; + }, + }; + expect(o[s]()).toBe("bar"); +}); diff --git a/Libraries/LibJS/Tests/object-spread.js b/Libraries/LibJS/Tests/object-spread.js index c9da1a1ac7..1da002cc05 100644 --- a/Libraries/LibJS/Tests/object-spread.js +++ b/Libraries/LibJS/Tests/object-spread.js @@ -12,7 +12,7 @@ const testObjStrSpread = obj => { }; test("spread object literal inside object literal", () => { - let obj = { + const obj = { foo: 0, ...{ bar: 1, baz: 2 }, qux: 3, @@ -21,19 +21,19 @@ test("spread object literal inside object literal", () => { }); test("spread object with assigned property inside object literal", () => { - obj = { foo: 0, bar: 1, baz: 2 }; + const obj = { foo: 0, bar: 1, baz: 2 }; obj.qux = 3; testObjSpread({ ...obj }); }); test("spread object inside object literal", () => { let a = { bar: 1, baz: 2 }; - obj = { foo: 0, ...a, qux: 3 }; + const obj = { foo: 0, ...a, qux: 3 }; testObjSpread(obj); }); test("complex nested object spreading", () => { - obj = { + const obj = { ...{}, ...{ ...{ foo: 0, bar: 1, baz: 2 }, @@ -44,31 +44,42 @@ test("complex nested object spreading", () => { }); test("spread string in object literal", () => { - obj = { ..."abcd" }; + const obj = { ..."abcd" }; testObjStrSpread(obj); }); test("spread array in object literal", () => { - obj = { ...["a", "b", "c", "d"] }; + const obj = { ...["a", "b", "c", "d"] }; testObjStrSpread(obj); }); test("spread string object in object literal", () => { - obj = { ...String("abcd") }; + const obj = { ...String("abcd") }; testObjStrSpread(obj); }); test("spread object with non-enumerable property", () => { - a = { foo: 0 }; + const a = { foo: 0 }; Object.defineProperty(a, "bar", { value: 1, enumerable: false, }); - obj = { ...a }; + const obj = { ...a }; expect(obj.foo).toBe(0); expect(obj).not.toHaveProperty("bar"); }); +test("spread object with symbol keys", () => { + const s = Symbol("baz"); + const a = { + foo: "bar", + [s]: "qux", + }; + const obj = { ...a }; + expect(obj.foo).toBe("bar"); + expect(obj[s]).toBe("qux"); +}); + test("spreading non-spreadable values", () => { let empty = { ...undefined, |