1
0
forked from rosa/hakurei

cmd/pkgserver: implement JS test DSL and runner

This commit is contained in:
Kat
2026-03-14 20:52:28 +11:00
parent ef8663461b
commit 2a3f6f5384
5 changed files with 170 additions and 11 deletions

View File

@@ -0,0 +1,4 @@
#!/usr/bin/env node
import "./test_tests.js";
import { run, StreamReporter } from "./test.js";
run(new StreamReporter({ writeln: console.log }));

View File

@@ -1,8 +1,127 @@
// =============================================================================
// DSL
type TestTree = { name: string } & (TestGroup | Test);
type TestGroup = { children: TestTree[] };
type Test = { test: (TestController) => void };
let TESTS: ({ name: string } & TestGroup)[] = [];
export function suite(name: string, children: TestTree[]) {
checkDuplicates(name, children)
TESTS.push({ name, children });
}
export function context(name: string, children: TestTree[]): TestTree {
checkDuplicates(name, children)
return { name, children };
}
export const group = context;
export function test(name: string, test: (TestController) => void): TestTree {
return { name, test };
}
function checkDuplicates(parent: string, names: { name: string }[]) {
let seen = new Set<string>();
for (const { name } of names) {
if (seen.has(name)) {
throw new RangeError(`duplicate name '${name}' in '${parent}'`);
}
seen.add(name);
}
}
class FailNowSentinel {}
class TestController {
#logBuf: string[];
#failed: boolean;
constructor() {
this.#logBuf = [];
this.#failed = false;
}
fail() {
this.#failed = true;
}
failed(): boolean {
return this.#failed;
}
failNow(): never {
this.fail();
throw new FailNowSentinel();
}
log(message: string) {
this.#logBuf.push(message);
}
error(message: string) {
this.log(message);
this.fail();
}
fatal(message: string): never {
this.log(message);
this.failNow();
}
getLog(): string {
return this.#logBuf.join("\n");
}
}
// =============================================================================
// Execution
export interface TestResult {
success: boolean;
output: string;
}
function runTests(reporter: Reporter, parents: string[], tree: TestTree) {
const path = [...parents, tree.name];
if ("children" in tree) {
for (const c of tree.children) runTests(reporter, path, c);
return;
}
let controller = new TestController();
let excStr: string;
try {
tree.test(controller);
} catch (e) {
if (!(e instanceof FailNowSentinel)) {
controller.fail();
excStr = extractExceptionString(e);
}
}
const log = controller.getLog();
const output = (log && excStr) ? `${log}\n${excStr}` : `${log}${excStr ?? ''}`;
reporter.update(path, { success: !controller.failed(), output });
}
export function run(reporter: Reporter) {
for (const suite of TESTS) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
reporter.finalize();
}
function extractExceptionString(e: any): string {
// String() instead of .toString() as null and undefined don't have
// properties.
const s = String(e);
if (!(e instanceof Error && "stack" in e)) return s;
// v8 (Chromium, NodeJS) include the error message, while
// Firefox and WebKit do not.
if (e.stack.includes(s)) return e.stack;
return `${s}\n${e.stack}`;
}
// =============================================================================
// Reporting
@@ -121,13 +240,3 @@ export class DOMReporter implements Reporter {
finalize() {}
}
let r = typeof document !== "undefined" ? new DOMReporter() : new StreamReporter({ writeln: console.log });
r.update(["alien", "can walk"], { success: false, output: "assertion failed" });
r.update(["alien", "can speak"], { success: false, output: "Uncaught ReferenceError: larynx is not defined" });
r.update(["alien", "sleep"], { success: true, output: "" });
r.update(["Tetromino", "generate", "tessellates"], { success: false, output: "assertion failed: 1 != 2" });
r.update(["Tetromino", "solve", "works"], { success: true, output: "" });
r.update(["discombobulate"], { success: false, output: "hippopotamonstrosesquippedaliophobia" });
r.update(["recombobulate"], { success: true, output: "" });
r.finalize();

View File

@@ -0,0 +1,40 @@
import { context, group, suite, test } from "./test.js";
suite("dog", [
group("tail", [
test("wags when happy", (t) => {
if (0 / 0 !== Infinity / Infinity) {
t.fatal("undefined must not be defined");
}
}),
test("idle when down", (t) => {
t.log("test test");
t.error("dog whining noises go here");
}),
]),
test("likes headpats", (t) => {
if (2 !== 2) {
t.error("IEEE 754 violated: 2 is NaN");
}
}),
context("near cat", [
test("is ecstatic", (t) => {
if (("b" + "a" + + "a" + "a").toLowerCase() == "banana") {
t.error("🍌🍌🍌");
t.error("🍌🍌🍌");
t.error("🍌🍌🍌");
t.failNow();
}
}),
test("playfully bites cats' tails", (t) => {
t.log("arf!");
throw new Error("nom");
}),
]),
]);
suite("cat", [
test("likes headpats", (t) => {
t.log("meow");
}),
]);