This commit is contained in:
proteriax
2021-07-18 16:12:04 -04:00
parent 99fa963fc0
commit 98cc916432
24 changed files with 726 additions and 269 deletions

43
test/weakMap.test.ts Normal file
View File

@ -0,0 +1,43 @@
import { describe, it } from "mocha";
import { expect } from "chai";
import { WeakValueMap } from "../src/weakMap";
declare const gc: () => void;
describe("WeakValueMap", () => {
interface Value {
value: number;
}
it("covers base use cases", () => {
const map = new WeakValueMap<string, Value>();
const object = { value: 1 };
map.set("key", object);
expect(map.get("key")!.value).to.equal(1);
expect(map.delete("key")).to.be.true;
expect(!map.delete("key")).to.be.true;
});
it("overrides previous value", () => {
const map = new WeakValueMap<string, Value>();
map.set("key", { value: 2 });
map.set("key", { value: 3 });
expect(map.get("key")!.value).to.equal(3);
});
it("deletes garbage collected values", (done) => {
const map = new WeakValueMap<string, Value>();
map.set("key", { value: 1 });
setTimeout(() => {
gc();
expect(map.has("key")).to.be.false;
map.set("key", { value: 2 });
setTimeout(() => {
gc();
done();
});
});
});
});