-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.test.ts
69 lines (55 loc) · 1.84 KB
/
mod.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { assertEquals } from "https://deno.land/std@0.84.0/testing/asserts.ts";
import LRU from "./mod.ts";
const lru = new LRU(10);
Deno.test("Add single entry.", () => {
lru.set("hello", "world");
assertEquals(lru.get("hello"), "world", "Value is different.");
});
Deno.test("Add another entry.", () => {
lru.set("foo", "bar");
assertEquals(lru.get("foo"), "bar", "Value is different.");
});
Deno.test("Get nonexistent key", () => {
assertEquals(lru.get("test"), undefined, "Entry found is not null");
});
Deno.test("Get size", () => {
assertEquals(lru.size, 2, "Size is bigger than it should");
});
Deno.test("Has key", () => {
assertEquals(lru.has("hello"), true, "Couldn't find key");
assertEquals(lru.has("foo"), true, "Couldn't find key");
assertEquals(lru.has("bar"), false, "Found key");
});
Deno.test("Get object", () => {
assertEquals(lru.object, { hello: "world", foo: "bar" });
});
Deno.test("Get values", () => {
assertEquals(lru.values, ["world", "bar"]);
});
Deno.test("Get keys", () => {
assertEquals(lru.keys, ["hello", "foo"]);
});
Deno.test("Loop through", () => {
const temp: { [key: string]: any } = [];
lru.forEach((value, key) => temp.push({ [key]: value }));
assertEquals(temp, [{ hello: "world" }, { foo: "bar" }]);
});
Deno.test("Map to array", () => {
const temp = lru.map(([key, value]) => ({ [key]: value }));
assertEquals(temp, [{ hello: "world" }, { foo: "bar" }]);
});
Deno.test("Filter cache", () => {
const temp = lru.filter(([key, value]) => value === "bar");
assertEquals(temp, [["foo", "bar"]]);
});
Deno.test("Reduce cache", () => {
const temp = lru.reduce((prev, current) => {
if (current[1] === "bar") prev.push(current[1]);
return prev;
}, [] as string[]);
assertEquals(temp, ["bar"]);
});
Deno.test("Clear everything", () => {
lru.clear();
assertEquals(lru.size, 0);
});