2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const {zrequire} = require("./lib/namespace.js");
|
|
|
|
const {run_test} = require("./lib/test.js");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-08-03 23:57:48 +02:00
|
|
|
const {FoldDict} = zrequire("fold_dict");
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basic", () => {
|
2019-12-26 15:34:17 +01:00
|
|
|
const d = new FoldDict();
|
|
|
|
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
assert.deepEqual([...d.keys()], []);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
d.set("foo", "bar");
|
|
|
|
assert.equal(d.get("foo"), "bar");
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.notEqual(d.size, 0);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
d.set("foo", "baz");
|
|
|
|
assert.equal(d.get("foo"), "baz");
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
d.set("bar", "qux");
|
|
|
|
assert.equal(d.get("foo"), "baz");
|
|
|
|
assert.equal(d.get("bar"), "qux");
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(d.has("bar"), true);
|
|
|
|
assert.equal(d.has("baz"), false);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
assert.deepEqual([...d.keys()], ["foo", "bar"]);
|
|
|
|
assert.deepEqual([...d.values()], ["baz", "qux"]);
|
|
|
|
assert.deepEqual(
|
|
|
|
[...d],
|
|
|
|
[
|
|
|
|
["foo", "baz"],
|
|
|
|
["bar", "qux"],
|
|
|
|
],
|
|
|
|
);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
d.delete("bar");
|
|
|
|
assert.equal(d.has("bar"), false);
|
|
|
|
assert.strictEqual(d.get("bar"), undefined);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
assert.deepEqual([...d.keys()], ["foo"]);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const val = ["foo"];
|
|
|
|
const res = d.set("abc", val);
|
2020-02-03 07:18:13 +01:00
|
|
|
assert.strictEqual(res, d);
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("case insensitivity", () => {
|
2019-12-26 15:34:17 +01:00
|
|
|
const d = new FoldDict();
|
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
assert.deepEqual([...d.keys()], []);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!d.has("foo"));
|
2021-05-10 07:02:14 +02:00
|
|
|
d.set("fOO", "Hello world");
|
|
|
|
assert.equal(d.get("foo"), "Hello world");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(d.has("foo"));
|
|
|
|
assert.ok(d.has("FOO"));
|
|
|
|
assert.ok(!d.has("not_a_key"));
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
assert.deepEqual([...d.keys()], ["fOO"]);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
d.delete("Foo");
|
|
|
|
assert.equal(d.has("foo"), false);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
assert.deepEqual([...d.keys()], []);
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("clear", () => {
|
2019-12-26 15:34:17 +01:00
|
|
|
const d = new FoldDict();
|
|
|
|
|
|
|
|
function populate() {
|
2020-07-15 01:29:15 +02:00
|
|
|
d.set("fOO", 1);
|
|
|
|
assert.equal(d.get("foo"), 1);
|
|
|
|
d.set("bAR", 2);
|
|
|
|
assert.equal(d.get("bar"), 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
d.clear();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(d.get("fOO"), undefined);
|
|
|
|
assert.equal(d.get("bAR"), undefined);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
// make sure it still works after clearing
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("undefined_keys", () => {
|
2019-12-26 15:34:17 +01:00
|
|
|
const d = new FoldDict();
|
|
|
|
|
2021-08-10 22:18:53 +02:00
|
|
|
assert.throws(() => d.has(undefined), {
|
|
|
|
name: "TypeError",
|
|
|
|
message: "Tried to call a FoldDict method with an undefined key.",
|
|
|
|
});
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|