1
0
forked from rosa/hakurei

cmd/pkgserver/ui_test: implement DSL and runner

This commit is contained in:
Kat
2026-03-29 03:07:05 +11:00
parent f2fded0620
commit 0ddae23eda
6 changed files with 285 additions and 12 deletions

View File

@@ -1,8 +1,133 @@
// =============================================================================
// DSL
type TestTree = TestGroup | Test;
type TestGroup = { name: string; children: TestTree[] };
type Test = { name: string; test: (t: TestController) => void };
export class TestRegistrar {
#suites: TestGroup[];
constructor() {
this.#suites = [];
}
suite(name: string, children: TestTree[]) {
checkDuplicates(name, children);
this.#suites.push({ name, children });
}
run(reporter: Reporter) {
for (const suite of this.#suites) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
reporter.finalize();
}
}
export let GLOBAL_REGISTRAR = new TestRegistrar();
// Register a suite in the global registrar.
export function suite(name: string, children: TestTree[]) {
GLOBAL_REGISTRAR.suite(name, children);
}
export function group(name: string, children: TestTree[]): TestTree {
checkDuplicates(name, children);
return { name, children };
}
export const context = group;
export const describe = group;
export function test(name: string, test: (t: 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 {}
export class TestController {
logs: string[];
#failed: boolean;
constructor() {
this.logs = [];
this.#failed = false;
}
fail() {
this.#failed = true;
}
failed(): boolean {
return this.#failed;
}
failNow(): never {
this.fail();
throw new FailNowSentinel();
}
log(message: string) {
this.logs.push(message);
}
error(message: string) {
this.log(message);
this.fail();
}
fatal(message: string): never {
this.log(message);
this.failNow();
}
}
// =============================================================================
// Execution
export interface TestResult {
success: boolean;
logs: string[];
}
function runTests(reporter: Reporter, parents: string[], node: TestTree) {
const path = [...parents, node.name];
if ("children" in node) {
for (const c of node.children) runTests(reporter, path, c);
return;
}
let controller = new TestController();
try {
node.test(controller);
} catch (e) {
if (!(e instanceof FailNowSentinel)) {
controller.error(extractExceptionString(e));
}
}
reporter.update(path, { success: !controller.failed(), logs: controller.logs });
}
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 && e.stack)) return s;
// v8 (Chromium, NodeJS) includes the error message, while Firefox and
// WebKit do not.
if (e.stack.startsWith(s)) return e.stack;
return `${s}\n${e.stack}`;
}
// =============================================================================
// Reporting
@@ -11,6 +136,24 @@ export interface Reporter {
finalize(): void;
}
export class NoOpReporter implements Reporter {
results: ({ path: string[] } & TestResult)[];
finalized: boolean;
constructor() {
this.results = [];
this.finalized = false;
}
update(path: string[], result: TestResult) {
this.results.push({ path, ...result });
}
finalize() {
this.finalized = true;
}
}
export interface Stream {
writeln(s: string): void;
}
@@ -30,6 +173,10 @@ export class StreamReporter implements Reporter {
this.counts = { successes: 0, failures: 0 };
}
succeeded(): boolean {
return this.counts.successes > 0 && this.counts.failures === 0;
}
update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty");
const pathStr = path.join(SEP);
@@ -145,14 +292,3 @@ export class DOMReporter implements Reporter {
finalize() {}
}
let r = globalThis.document ? new DOMReporter() : new StreamReporter({ writeln: console.log });
r.update(["alien", "can walk"], { success: false, logs: ["assertion failed"] });
r.update(["alien", "can speak"], { success: false, logs: ["Uncaught ReferenceError: larynx is not defined"] });
r.update(["alien", "sleep"], { success: true, logs: [] });
r.update(["Tetromino", "generate", "tessellates"], { success: false, logs: ["assertion failed: 1 != 2"] });
r.update(["Tetromino", "solve", "works"], { success: true, logs: [] });
r.update(["discombobulate", "english"], { success: false, logs: ["hippopotomonstrosesquipedaliophobia\npneumonoultramicroscopicsilicovolcanoconiosis", "supercalifragilisticexpialidocious"] });
r.update(["discombobulate", "geography"], { success: false, logs: ["Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu"] });
r.update(["recombobulate"], { success: true, logs: [] });
r.finalize();