1
0
forked from rosa/hakurei

2 Commits

5 changed files with 35 additions and 78 deletions

View File

@@ -29,8 +29,6 @@ func serveStaticContent(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, content, "ui/static/test.js") http.ServeFileFS(w, r, content, "ui/static/test.js")
case "/static/test.css": case "/static/test.css":
http.ServeFileFS(w, r, content, "ui/static/test.css") http.ServeFileFS(w, r, content, "ui/static/test.css")
case "/static/all_tests.js":
http.ServeFileFS(w, r, content, "ui/static/all_tests.js")
case "/static/test_tests.js": case "/static/test_tests.js":
http.ServeFileFS(w, r, content, "ui/static/test_tests.js") http.ServeFileFS(w, r, content, "ui/static/test_tests.js")
default: default:

View File

@@ -1 +0,0 @@
import "./test_tests.js";

View File

@@ -2,6 +2,6 @@
// Many editors have terminal emulators built in, so running tests with NodeJS // Many editors have terminal emulators built in, so running tests with NodeJS
// provides faster iteration, especially for those acclimated to test-driven // provides faster iteration, especially for those acclimated to test-driven
// development. // development.
import "./all_tests.js"; import "./test_tests.js";
import { run, StreamReporter } from "./test.js"; import { run, StreamReporter } from "./test.js";
run(new StreamReporter({ writeln: console.log })); run(new StreamReporter({ writeln: console.log }));

View File

@@ -1,9 +1,9 @@
// ============================================================================= // =============================================================================
// DSL // DSL
type TestTree = TestGroup | Test; type TestTree = { name: string } & (TestGroup | Test);
type TestGroup = { name: string, children: TestTree[] }; type TestGroup = { children: TestTree[] };
type Test = { name: string, test: (TestController) => void }; type Test = { test: (TestController) => void };
let TESTS: ({ name: string } & TestGroup)[] = []; let TESTS: ({ name: string } & TestGroup)[] = [];
@@ -34,26 +34,17 @@ function checkDuplicates(parent: string, names: { name: string }[]) {
class FailNowSentinel {} class FailNowSentinel {}
export type Journal = ({ method: "fail" } | { method: "log", message: string })[]; class TestController {
function formatJournal(journal: Journal): string { #logBuf: string[];
return journal
.filter((e) => e.method === "log")
.map((e) => e.message)
.join("\n");
}
export class TestController {
journal: Journal;
#failed: boolean; #failed: boolean;
constructor() { constructor() {
this.journal = []; this.#logBuf = [];
this.#failed = false; this.#failed = false;
} }
fail() { fail() {
this.#failed = true; this.#failed = true;
this.journal.push({ method: "fail" });
} }
failed(): boolean { failed(): boolean {
@@ -66,7 +57,7 @@ export class TestController {
} }
log(message: string) { log(message: string) {
this.journal.push({ method: "log", message }); this.#logBuf.push(message);
} }
error(message: string) { error(message: string) {
@@ -78,6 +69,10 @@ export class TestController {
this.log(message); this.log(message);
this.failNow(); this.failNow();
} }
getLog(): string {
return this.#logBuf.join("\n");
}
} }
// ============================================================================= // =============================================================================
@@ -85,34 +80,35 @@ export class TestController {
export interface TestResult { export interface TestResult {
success: boolean; success: boolean;
journal: Journal; output: string;
} }
export function run(reporter: Reporter) { function runTests(reporter: Reporter, parents: string[], tree: TestTree) {
reporter.register(TESTS); const path = [...parents, tree.name];
for (const suite of TESTS) { if ("children" in tree) {
for (const c of suite.children) runTests(reporter, [suite.name], c); for (const c of tree.children) runTests(reporter, path, c);
}
reporter.finalize();
}
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; return;
} }
let controller = new TestController(); let controller = new TestController();
let excStr: string; let excStr: string;
try { try {
node.test(controller); tree.test(controller);
} catch (e) { } catch (e) {
if (!(e instanceof FailNowSentinel)) { if (!(e instanceof FailNowSentinel)) {
controller.fail();
excStr = extractExceptionString(e); excStr = extractExceptionString(e);
} }
} }
if (excStr !== undefined) controller.error(excStr); const log = controller.getLog();
reporter.update(path, { success: !controller.failed(), journal: controller.journal }); 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 { function extractExceptionString(e: any): string {
@@ -130,7 +126,6 @@ function extractExceptionString(e: any): string {
// Reporting // Reporting
export interface Reporter { export interface Reporter {
register(suites: TestGroup[]): void
update(path: string[], result: TestResult): void; update(path: string[], result: TestResult): void;
finalize(): void; finalize(): void;
} }
@@ -154,8 +149,6 @@ export class StreamReporter implements Reporter {
this.counts = { successes: 0, failures: 0 }; this.counts = { successes: 0, failures: 0 };
} }
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) { update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty"); if (path.length === 0) throw new RangeError("path is empty");
const pathStr = path.join(SEP); const pathStr = path.join(SEP);
@@ -199,11 +192,10 @@ export class StreamReporter implements Reporter {
#writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) { #writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) {
let output = ""; let output = "";
let logOutput = formatJournal(test.journal); if (test.output) {
if (logOutput) { const lines = test.output.split("\n");
const lines = logOutput.split("\n");
if (lines.length <= 1) { if (lines.length <= 1) {
output = `: ${logOutput}`; output = `: ${test.output}`;
} else { } else {
const padding = nested ? " " : " "; const padding = nested ? " " : " ";
output = ":\n" + lines.map((line) => padding + line).join("\n"); output = ":\n" + lines.map((line) => padding + line).join("\n");
@@ -214,8 +206,6 @@ export class StreamReporter implements Reporter {
} }
export class DOMReporter implements Reporter { export class DOMReporter implements Reporter {
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) { update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty"); if (path.length === 0) throw new RangeError("path is empty");
const counter = document.getElementById(result.success ? "successes" : "failures"); const counter = document.getElementById(result.success ? "successes" : "failures");
@@ -247,10 +237,9 @@ export class DOMReporter implements Reporter {
} }
const p = document.createElement("p"); const p = document.createElement("p");
p.classList.add("test-desc"); p.classList.add("test-desc");
const logOutput = formatJournal(result.journal); if (result.output) {
if (logOutput) {
const pre = document.createElement("pre"); const pre = document.createElement("pre");
pre.appendChild(document.createTextNode(logOutput)); pre.appendChild(document.createTextNode(result.output));
p.appendChild(pre); p.appendChild(pre);
} else { } else {
p.classList.add("italic"); p.classList.add("italic");
@@ -261,32 +250,3 @@ export class DOMReporter implements Reporter {
finalize() {} finalize() {}
} }
interface GoNode {
name: string;
subtests?: GoNode[];
}
// Used to display results via `go test`, via some glue code from the Go side.
export class GoTestReporter implements Reporter {
// Convert a test tree into the one expected by the Go code.
static serialize(node: TestTree): GoNode {
if (!("children" in node)) return { name: node.name };
return {
name: node.name,
subtests: node.children.map(GoTestReporter.serialize),
};
}
register(suites: TestGroup[]) {
console.log(JSON.stringify(suites.map(GoTestReporter.serialize)));
}
update(path: string[], result: TestResult) {
console.log(JSON.stringify({ "path": path, ...result }));
}
finalize() {
console.log("null");
}
}

View File

@@ -18,7 +18,7 @@
<div id="root"> <div id="root">
</div> </div>
<script type="module" src="./static/all_tests.js"></script> <script type="module" src="./static/test_tests.js"></script>
<script type="module"> <script type="module">
import { DOMReporter, run } from "./static/test.js"; import { DOMReporter, run } from "./static/test.js";
run(new DOMReporter()); run(new DOMReporter());