diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-06-22 23:08:12 +0300 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2022-06-23 10:57:52 +0300 |
commit | a80d3fdf491af03e3edbea0243b97edc615fc10c (patch) | |
tree | 8269be59b930e3861f8ee75ee1a724005cd8713c /Userland/Libraries/LibJS/Tests | |
parent | 22a78e8a2ccc25752b96097b3b8d258964bbbb46 (diff) | |
download | serenity-a80d3fdf491af03e3edbea0243b97edc615fc10c.zip |
LibJS: Implement WeakMap changes from 'Symbol as WeakMap Keys Proposal'
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
5 files changed, 24 insertions, 5 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.js b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.js index 78c8ad79a9..37c601c3b4 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.js +++ b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.js @@ -37,6 +37,6 @@ describe("regressions", () => { test("missing key/value properties on iterable entry", () => { expect(() => { new WeakMap([{}]); - }).toThrowWithMessage(TypeError, "undefined is not an object"); + }).toThrowWithMessage(TypeError, "undefined cannot be held weakly"); }); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.delete.js b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.delete.js index dd12bd8b1a..4133b168aa 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.delete.js +++ b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.delete.js @@ -5,9 +5,12 @@ test("basic functionality", () => { [{ a: 1 }, 1], [{ a: 2 }, 2], [{ a: 3 }, 3], + [Symbol("foo"), "bar"], ]; const weakMap = new WeakMap(original); expect(weakMap.delete(original[0][0])).toBeTrue(); expect(weakMap.delete(original[0][0])).toBeFalse(); + expect(weakMap.delete(original[3][0])).toBeTrue(); + expect(weakMap.delete(original[3][0])).toBeFalse(); expect(weakMap.delete(null)).toBeFalse(); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.get.js b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.get.js index 09cd06c970..33270ad7ec 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.get.js +++ b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.get.js @@ -5,8 +5,10 @@ test("basic functionality", () => { [{ a: 1 }, 1], [{ a: 2 }, 2], [{ a: 3 }, 3], + [Symbol("foo"), "bar"], ]; const weakMap = new WeakMap(original); expect(weakMap.get(original[0][0])).toBe(original[0][1]); + expect(weakMap.get(original[3][0])).toBe(original[3][1]); expect(weakMap.get(null)).toBe(undefined); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.has.js b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.has.js index b0d67baa54..81dca8697e 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.has.js +++ b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.has.js @@ -7,10 +7,12 @@ test("basic functionality", () => { [{ a: 1 }, 1], [{ a: 2 }, 2], [{ a: 3 }, 3], + [Symbol("foo"), "bar"], ]; var weakMap = new WeakMap(original); expect(new WeakMap().has()).toBeFalse(); expect(weakMap.has(original[0][0])).toBeTrue(); + expect(weakMap.has(original[3][0])).toBeTrue(); expect(weakMap.has({ a: 1 })).toBeFalse(); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.set.js b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.set.js index 9bc6afabbe..14d38d6512 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.set.js +++ b/Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.prototype.set.js @@ -5,9 +5,11 @@ test("basic functionality", () => { [{ a: 1 }, 1], [{ a: 2 }, 2], [{ a: 3 }, 3], + [Symbol("foo"), "bar"], ]); expect(weakMap.set({ a: 4 }, 4)).toBe(weakMap); expect(weakMap.set({ a: 1 }, 2)).toBe(weakMap); + expect(weakMap.set(Symbol("hello"), "friends")).toBe(weakMap); }); test("invalid values", () => { @@ -15,18 +17,28 @@ test("invalid values", () => { [-100, Infinity, NaN, "hello", 152n].forEach(value => { expect(() => { weakMap.set(value, value); - }).toThrowWithMessage(TypeError, "is not an object"); + }).toThrowWithMessage(TypeError, "cannot be held weakly"); }); }); test("automatic removal of garbage-collected values", () => { const weakMap = new WeakMap(); - const key = { e: 3 }; + const objectKey = { e: 3 }; - expect(weakMap.set(key, 1)).toBe(weakMap); + expect(weakMap.set(objectKey, 1)).toBe(weakMap); expect(getWeakMapSize(weakMap)).toBe(1); - markAsGarbage("key"); + markAsGarbage("objectKey"); + gc(); + + expect(getWeakMapSize(weakMap)).toBe(0); + + const symbolKey = Symbol("foo"); + + expect(weakMap.set(symbolKey, "bar")).toBe(weakMap); + expect(getWeakMapSize(weakMap)).toBe(1); + + markAsGarbage("symbolKey"); gc(); expect(getWeakMapSize(weakMap)).toBe(0); |