forked from rosa/hakurei
Compare commits
13 Commits
21b08dae56
...
04f97b6e9e
| Author | SHA1 | Date | |
|---|---|---|---|
|
04f97b6e9e
|
|||
|
4d9e5117c0
|
|||
|
a57db19204
|
|||
|
cafd0e5f3b
|
|||
|
fbb92fb221
|
|||
|
c73f34ecfa
|
|||
|
4bbd5316e4
|
|||
|
d221564419
|
|||
|
9a261fc4e8
|
|||
|
ff2db48f8c
|
|||
|
31a9e9d014
|
|||
|
8cf2421f30
|
|||
|
6bc11098b1
|
@@ -34,17 +34,26 @@ function checkDuplicates(parent: string, names: { name: string }[]) {
|
||||
|
||||
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 {
|
||||
#logs: string[];
|
||||
journal: Journal;
|
||||
#failed: boolean;
|
||||
|
||||
constructor() {
|
||||
this.#logs = [];
|
||||
this.journal = [];
|
||||
this.#failed = false;
|
||||
}
|
||||
|
||||
fail() {
|
||||
this.#failed = true;
|
||||
this.journal.push({ method: "fail" });
|
||||
}
|
||||
|
||||
failed(): boolean {
|
||||
@@ -57,7 +66,7 @@ export class TestController {
|
||||
}
|
||||
|
||||
log(message: string) {
|
||||
this.#logs.push(message);
|
||||
this.journal.push({ method: "log", message });
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
@@ -69,10 +78,6 @@ export class TestController {
|
||||
this.log(message);
|
||||
this.failNow();
|
||||
}
|
||||
|
||||
getLog(): string[] {
|
||||
return this.#logs;
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -80,7 +85,7 @@ export class TestController {
|
||||
|
||||
export interface TestResult {
|
||||
success: boolean;
|
||||
logs: string[];
|
||||
journal: Journal;
|
||||
}
|
||||
|
||||
export function run(reporter: Reporter) {
|
||||
@@ -98,14 +103,16 @@ function runTests(reporter: Reporter, parents: string[], node: TestTree) {
|
||||
return;
|
||||
}
|
||||
let controller = new TestController();
|
||||
let excStr: string;
|
||||
try {
|
||||
node.test(controller);
|
||||
} catch (e) {
|
||||
if (!(e instanceof FailNowSentinel)) {
|
||||
controller.error(extractExceptionString(e));
|
||||
excStr = extractExceptionString(e);
|
||||
}
|
||||
}
|
||||
reporter.update(path, { success: !controller.failed(), logs: controller.getLog() });
|
||||
if (excStr !== undefined) controller.error(excStr);
|
||||
reporter.update(path, { success: !controller.failed(), journal: controller.journal });
|
||||
}
|
||||
|
||||
function extractExceptionString(e: any): string {
|
||||
@@ -192,13 +199,11 @@ export class StreamReporter implements Reporter {
|
||||
|
||||
#writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) {
|
||||
let output = "";
|
||||
if (test.logs.length) {
|
||||
// Individual logs might span multiple lines, so join them together
|
||||
// then split it again.
|
||||
const logStr = test.logs.join("\n");
|
||||
const lines = logStr.split("\n");
|
||||
let logOutput = formatJournal(test.journal);
|
||||
if (logOutput) {
|
||||
const lines = logOutput.split("\n");
|
||||
if (lines.length <= 1) {
|
||||
output = `: ${logStr}`;
|
||||
output = `: ${logOutput}`;
|
||||
} else {
|
||||
const padding = nested ? " " : " ";
|
||||
output = ":\n" + lines.map((line) => padding + line).join("\n");
|
||||
@@ -242,9 +247,10 @@ export class DOMReporter implements Reporter {
|
||||
}
|
||||
const p = document.createElement("p");
|
||||
p.classList.add("test-desc");
|
||||
if (result.logs.length) {
|
||||
const logOutput = formatJournal(result.journal);
|
||||
if (logOutput) {
|
||||
const pre = document.createElement("pre");
|
||||
pre.appendChild(document.createTextNode(result.logs.join("\n")));
|
||||
pre.appendChild(document.createTextNode(logOutput));
|
||||
p.appendChild(pre);
|
||||
} else {
|
||||
p.classList.add("italic");
|
||||
@@ -265,9 +271,10 @@ interface GoNode {
|
||||
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, subtests: null };
|
||||
return {
|
||||
name: node.name,
|
||||
subtests: "children" in node ? node.children.map(GoTestReporter.serialize) : null,
|
||||
subtests: node.children.map(GoTestReporter.serialize),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -276,7 +283,7 @@ export class GoTestReporter implements Reporter {
|
||||
}
|
||||
|
||||
update(path: string[], result: TestResult) {
|
||||
console.log(JSON.stringify({ path: path, ...result }));
|
||||
console.log(JSON.stringify({ "path": path, ...result }));
|
||||
}
|
||||
|
||||
finalize() {
|
||||
|
||||
Reference in New Issue
Block a user