1
0
forked from rosa/hakurei

14 Commits

View File

@@ -34,26 +34,17 @@ function checkDuplicates(parent: string, names: { name: string }[]) {
class FailNowSentinel {} class FailNowSentinel {}
export type Journal = ({ method: "fail" } | { method: "log", message: string })[];
function formatJournal(journal: Journal): string {
return journal
.filter((e) => e.method === "log")
.map((e) => e.message)
.join("\n");
}
export class TestController { export class TestController {
journal: Journal; #logs: string[];
#failed: boolean; #failed: boolean;
constructor() { constructor() {
this.journal = []; this.#logs = [];
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.#logs.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.#logs;
}
} }
// ============================================================================= // =============================================================================
@@ -85,7 +80,7 @@ export class TestController {
export interface TestResult { export interface TestResult {
success: boolean; success: boolean;
journal: Journal; logs: string[];
} }
export function run(reporter: Reporter) { export function run(reporter: Reporter) {
@@ -103,16 +98,14 @@ function runTests(reporter: Reporter, parents: string[], node: TestTree) {
return; return;
} }
let controller = new TestController(); let controller = new TestController();
let excStr: string;
try { try {
node.test(controller); node.test(controller);
} catch (e) { } catch (e) {
if (!(e instanceof FailNowSentinel)) { if (!(e instanceof FailNowSentinel)) {
excStr = extractExceptionString(e); controller.error(extractExceptionString(e));
} }
} }
if (excStr !== undefined) controller.error(excStr); reporter.update(path, { success: !controller.failed(), logs: controller.getLog() });
reporter.update(path, { success: !controller.failed(), journal: controller.journal });
} }
function extractExceptionString(e: any): string { function extractExceptionString(e: any): string {
@@ -199,11 +192,13 @@ 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.logs.length) {
if (logOutput) { // Individual logs might span multiple lines, so join them together
const lines = logOutput.split("\n"); // then split it again.
const logStr = test.logs.join("\n");
const lines = logStr.split("\n");
if (lines.length <= 1) { if (lines.length <= 1) {
output = `: ${logOutput}`; output = `: ${logStr}`;
} 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");
@@ -247,10 +242,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.logs.length) {
if (logOutput) {
const pre = document.createElement("pre"); const pre = document.createElement("pre");
pre.appendChild(document.createTextNode(logOutput)); pre.appendChild(document.createTextNode(result.logs.join("\n")));
p.appendChild(pre); p.appendChild(pre);
} else { } else {
p.classList.add("italic"); p.classList.add("italic");
@@ -271,10 +265,9 @@ interface GoNode {
export class GoTestReporter implements Reporter { export class GoTestReporter implements Reporter {
// Convert a test tree into the one expected by the Go code. // Convert a test tree into the one expected by the Go code.
static serialize(node: TestTree): GoNode { static serialize(node: TestTree): GoNode {
if (!("children" in node)) return { name: node.name, subtests: null };
return { return {
name: node.name, name: node.name,
subtests: node.children.map(GoTestReporter.serialize), subtests: "children" in node ? node.children.map(GoTestReporter.serialize) : null,
}; };
} }
@@ -283,7 +276,7 @@ export class GoTestReporter implements Reporter {
} }
update(path: string[], result: TestResult) { update(path: string[], result: TestResult) {
console.log(JSON.stringify({ "path": path, ...result })); console.log(JSON.stringify({ path: path, ...result }));
} }
finalize() { finalize() {