forked from security/hakurei
cmd/pkgserver: implement JS test DSL and runner
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user