diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-12 23:58:03 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-13 00:33:18 +0100 |
commit | 322c8a3995e3fab7c77b78b60348337e24a2d9e2 (patch) | |
tree | b69890ed8b4054ed8c6a49ec200b02685e9ff216 /Userland/Libraries/LibJS/Tests | |
parent | 6c0d5163a1bdbfab120525f075830a023500d8dc (diff) | |
download | serenity-322c8a3995e3fab7c77b78b60348337e24a2d9e2.zip |
LibJS: Add the MapIterator built-in and the key/values/entries methods
While this implementation should be complete it is based on HashMap's
iterator, which currently follows bucket-order instead of the required
insertion order. This can be simply fixed by replacing the underlying
HashMap member in Map with an enhanced one that maintains a linked
list in insertion order.
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
3 files changed, 78 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.entries.js b/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.entries.js new file mode 100644 index 0000000000..b811fc1b9b --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.entries.js @@ -0,0 +1,26 @@ +test("length", () => { + expect(Map.prototype.entries.length).toBe(0); +}); + +test("basic functionality", () => { + const original = [ + ["a", 0], + ["b", 1], + ["c", 2], + ]; + const a = new Map(original); + const it = a.entries(); + // FIXME: This test should be rewritten once we have proper iteration order + const first = it.next(); + expect(first.done).toBeFalse(); + expect(a.has(first.value[0])).toBeTrue(); + const second = it.next(); + expect(second.done).toBeFalse(); + expect(a.has(second.value[0])).toBeTrue(); + const third = it.next(); + expect(third.done).toBeFalse(); + expect(a.has(third.value[0])).toBeTrue(); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.keys.js b/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.keys.js new file mode 100644 index 0000000000..2734be1308 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.keys.js @@ -0,0 +1,26 @@ +test("length", () => { + expect(Map.prototype.keys.length).toBe(0); +}); + +test("basic functionality", () => { + const original = [ + ["a", 0], + ["b", 1], + ["c", 2], + ]; + const a = new Map(original); + const it = a.keys(); + // FIXME: This test should be rewritten once we have proper iteration order + const first = it.next(); + expect(first.done).toBeFalse(); + expect(a.has(first.value)).toBeTrue(); + const second = it.next(); + expect(second.done).toBeFalse(); + expect(a.has(second.value)).toBeTrue(); + const third = it.next(); + expect(third.done).toBeFalse(); + expect(a.has(third.value)).toBeTrue(); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.values.js b/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.values.js new file mode 100644 index 0000000000..a6aec10da2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Map/Map.prototype.values.js @@ -0,0 +1,26 @@ +test("length", () => { + expect(Map.prototype.values.length).toBe(0); +}); + +test("basic functionality", () => { + const original = [ + ["a", 0], + ["b", 1], + ["c", 2], + ]; + const a = new Map(original); + const it = a.values(); + // FIXME: This test should be rewritten once we have proper iteration order + const first = it.next(); + expect(first.done).toBeFalse(); + expect([0, 1, 2].includes(first.value)).toBeTrue(); + const second = it.next(); + expect(second.done).toBeFalse(); + expect([0, 1, 2].includes(second.value)).toBeTrue(); + const third = it.next(); + expect(third.done).toBeFalse(); + expect([0, 1, 2].includes(third.value)).toBeTrue(); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); + expect(it.next()).toEqual({ value: undefined, done: true }); +}); |