"use strict";
const assert = require("assert");
const { otherSide, nodePath, nextMatchIndex, pickRendering } = require("./viewer.js");
function el(className, kind, children) {
const node = {
classList: { contains: (c) => className.split(" ").includes(c) },
dataset: { kind },
children: children || [],
parentElement: null,
};
for (const child of node.children) child.parentElement = node;
return node;
}
const node = (kind, children) => el("node", kind, children);
const leaf = (kind) => el("node leaf", kind);
const summary = () => el("summary", undefined);
{
const literalB = leaf("integer_literal");
const letB = node("let_declaration", [
summary(),
leaf("let"),
leaf("identifier"),
leaf("="),
literalB,
leaf(";"),
]);
const block = node("block", [
summary(),
leaf("{"),
node("let_declaration", [
summary(),
leaf("let"),
leaf("identifier"),
leaf("="),
leaf("integer_literal"),
leaf(";"),
]),
letB,
leaf("}"),
]);
const root = node("source_file", [
summary(),
node("function_item", [summary(), leaf("fn"), leaf("identifier"), node("parameters", []), block]),
]);
(function link(parent) {
for (const child of parent.children) {
child.parentElement = parent;
link(child);
}
})(root);
assert.strictEqual(
nodePath(literalB),
"function_item:1/block:1/let_declaration:2/integer_literal:1",
"if this string changes, change it in generate_mapping_site.rs too - they pin each other"
);
assert.strictEqual(nodePath(root), "");
assert.strictEqual(nodePath(block), "function_item:1/block:1");
}
{
const texts = ["alpha", "beta", "gamma", "beta again"];
assert.strictEqual(nextMatchIndex(texts, -1, "beta"), 1);
assert.strictEqual(nextMatchIndex(texts, 1, "beta"), 3);
assert.strictEqual(nextMatchIndex(texts, 3, "beta"), 1);
assert.strictEqual(nextMatchIndex(texts, 2, "gamma"), 2);
assert.strictEqual(nextMatchIndex(texts, 0, "ALPHA"), 0, "search is case-insensitive");
assert.strictEqual(nextMatchIndex(texts, 0, "nothing here"), -1);
assert.strictEqual(nextMatchIndex([], 0, "beta"), -1);
assert.strictEqual(nextMatchIndex(texts, 0, ""), -1, "an empty query matches nothing, not everything");
}
{
const painted = ["From the node mapping", "Minimal", "Full"];
assert.strictEqual(pickRendering(painted, "Full"), 2);
assert.strictEqual(
pickRendering(painted, "Only one solution"),
0,
"a name this page doesn't have falls back to the node mapping, which every page has"
);
assert.strictEqual(pickRendering(["From the node mapping"], "Minimal"), 0);
assert.strictEqual(pickRendering([], "Minimal"), -1, "a page with no renderings selects nothing");
}
{
assert.strictEqual(otherSide("before"), "after");
assert.strictEqual(otherSide("after"), "before");
}
console.log("viewer.test.js: all assertions passed");