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

LibJS: Add most of the Map.prototype methods

Specifically all aside from "keys", "values" and "entries" which
require an implementation of the MapIterator object.
This commit is contained in:
Idan Horowitz 2021-06-12 23:57:01 +03:00 committed by Linus Groh
parent a96ac8bd56
commit 6c0d5163a1
8 changed files with 212 additions and 0 deletions

View file

@ -0,0 +1,12 @@
test("basic functionality", () => {
expect(Map.prototype.clear).toHaveLength(0);
const map = new Map([
["a", 0],
["b", 1],
["c", 2],
]);
expect(map).toHaveSize(3);
map.clear();
expect(map).toHaveSize(0);
});

View file

@ -0,0 +1,14 @@
test("basic functionality", () => {
expect(Map.prototype.delete).toHaveLength(1);
const map = new Map([
["a", 0],
["b", 1],
["c", 2],
]);
expect(map).toHaveSize(3);
expect(map.delete("b")).toBeTrue();
expect(map).toHaveSize(2);
expect(map.delete("b")).toBeFalse();
expect(map).toHaveSize(2);
});

View file

@ -0,0 +1,56 @@
test("length is 1", () => {
expect(Map.prototype.forEach).toHaveLength(1);
});
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
new Map().forEach();
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
test("callback must be a function", () => {
expect(() => {
new Map().forEach(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("never calls callback with empty set", () => {
var callbackCalled = 0;
expect(
new Map().forEach(() => {
callbackCalled++;
})
).toBeUndefined();
expect(callbackCalled).toBe(0);
});
test("calls callback once for every item", () => {
var callbackCalled = 0;
expect(
new Map([
["a", 0],
["b", 1],
["c", 2],
]).forEach(() => {
callbackCalled++;
})
).toBeUndefined();
expect(callbackCalled).toBe(3);
});
test("callback receives value, key and map", () => {
var a = new Map([
["a", 0],
["b", 1],
["c", 2],
]);
a.forEach((value, key, map) => {
expect(a.has(key)).toBeTrue();
expect(a.get(key)).toBe(value);
expect(map).toBe(a);
});
});
});

View file

@ -0,0 +1,11 @@
test("basic functionality", () => {
expect(Map.prototype.get).toHaveLength(1);
const map = new Map([
["a", 0],
["b", 1],
["c", 2],
]);
expect(map.get("a")).toBe(0);
expect(map.get("d")).toBe(undefined);
});

View file

@ -0,0 +1,17 @@
test("length is 1", () => {
expect(Map.prototype.has).toHaveLength(1);
});
test("basic functionality", () => {
var map = new Map([
["a", 0],
[1, "b"],
["c", 2],
]);
expect(new Map().has()).toBeFalse();
expect(new Map([{}]).has()).toBeTrue();
expect(map.has("a")).toBeTrue();
expect(map.has(1)).toBeTrue();
expect(map.has("serenity")).toBeFalse();
});

View file

@ -0,0 +1,14 @@
test("basic functionality", () => {
expect(Map.prototype.set).toHaveLength(2);
const map = new Map([
["a", 0],
["b", 1],
["c", 2],
]);
expect(map).toHaveSize(3);
expect(map.set("d", 3)).toBe(map);
expect(map).toHaveSize(4);
expect(map.set("a", -1)).toBe(map);
expect(map).toHaveSize(4);
});