Skip to content

Commit 1925570

Browse files
committed
Add tests for HashMap Entry
1 parent 0f44513 commit 1925570

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/hashmap_test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,43 @@ Deno.test("HashMap.from() builds map from pairs", () => {
109109
expect(map.size()).toBe(3);
110110
expect(map.get("b")).toEqual(some(2));
111111
});
112+
113+
Deno.test("HashMap: entry().orInsert inserts if missing", () => {
114+
const map = new HashMap<string, number>();
115+
const entry = map.entry("x");
116+
entry.orInsert(42);
117+
118+
expect(map.get("x")).toEqual(some(42));
119+
});
120+
121+
Deno.test("HashMap: entry().orInsertWith inserts lazily", () => {
122+
const map = new HashMap<string, number>();
123+
let called = false;
124+
const entry = map.entry("x");
125+
entry.orInsertWith(() => {
126+
called = true;
127+
return 99;
128+
});
129+
130+
expect(called).toBe(true);
131+
expect(map.get("x")).toEqual(some(99));
132+
});
133+
134+
Deno.test("HashMap: entry().andModify modifies if present", () => {
135+
const map = new HashMap<string, number>();
136+
map.set("x", 10);
137+
map.entry("x").andModify((v) => v += 5);
138+
map.entry("y").andModify((v) => v += 5);
139+
140+
expect(map.get("x")).toEqual(some(15));
141+
expect(map.get("y")).toEqual(none());
142+
});
143+
144+
Deno.test("HashMap: Entry.remove() removes entry", () => {
145+
const map = new HashMap<string, string>();
146+
map.set("hello", "world");
147+
const entry = map.entry("hello");
148+
expect(entry.remove()).toEqual(some("world"));
149+
expect(map.has("hello")).toBe(false);
150+
expect(map.entry("bonjour").remove()).toEqual(none());
151+
});

0 commit comments

Comments
 (0)