forked from rosa/hakurei
Compare commits
15 Commits
d209323773
...
c3229d02e6
| Author | SHA1 | Date | |
|---|---|---|---|
|
c3229d02e6
|
|||
|
a26fd08c02
|
|||
|
1bea21d404
|
|||
|
0aa49dba1e
|
|||
|
af518a080d
|
|||
|
31cbab277c
|
|||
|
f2e4a20d1a
|
|||
|
cfd2284e8d
|
|||
|
9b7b4419a3
|
|||
|
90ee17b861
|
|||
|
877be3308e
|
|||
|
e0f32ead04
|
|||
|
7fa87e69fe
|
|||
|
2e6f562dd5
|
|||
|
7438de2e58
|
@@ -3,5 +3,5 @@
|
||||
// provides faster iteration, especially for those acclimated to test-driven
|
||||
// development.
|
||||
import "./all_tests.js";
|
||||
import { run, StreamReporter } from "./test.js";
|
||||
run(new StreamReporter({ writeln: console.log }));
|
||||
import { StreamReporter, TESTS } from "./test.js";
|
||||
TESTS.run(new StreamReporter({ writeln: console.log }));
|
||||
|
||||
@@ -5,15 +5,36 @@ type TestTree = TestGroup | Test;
|
||||
type TestGroup = { name: string, children: TestTree[] };
|
||||
type Test = { name: string, test: (TestController) => void };
|
||||
|
||||
let TESTS: ({ name: string } & TestGroup)[] = [];
|
||||
export class TestRegistrar {
|
||||
#suites: TestGroup[];
|
||||
|
||||
constructor() {
|
||||
this.#suites = [];
|
||||
}
|
||||
|
||||
suite(name: string, children: TestTree[]) {
|
||||
checkDuplicates(name, children)
|
||||
this.#suites.push({ name, children });
|
||||
}
|
||||
|
||||
run(reporter: Reporter) {
|
||||
reporter.register(this.#suites);
|
||||
for (const suite of this.#suites) {
|
||||
for (const c of suite.children) runTests(reporter, [suite.name], c);
|
||||
}
|
||||
reporter.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
export let TESTS = new TestRegistrar();
|
||||
|
||||
// Register a suite in the global registrar.
|
||||
export function suite(name: string, children: TestTree[]) {
|
||||
checkDuplicates(name, children)
|
||||
TESTS.push({ name, children });
|
||||
TESTS.suite(name, children);
|
||||
}
|
||||
|
||||
export function context(name: string, children: TestTree[]): TestTree {
|
||||
checkDuplicates(name, children)
|
||||
checkDuplicates(name, children);
|
||||
return { name, children };
|
||||
}
|
||||
export const group = context;
|
||||
@@ -35,11 +56,11 @@ function checkDuplicates(parent: string, names: { name: string }[]) {
|
||||
class FailNowSentinel {}
|
||||
|
||||
export class TestController {
|
||||
#logs: string[];
|
||||
logs: string[];
|
||||
#failed: boolean;
|
||||
|
||||
constructor() {
|
||||
this.#logs = [];
|
||||
this.logs = [];
|
||||
this.#failed = false;
|
||||
}
|
||||
|
||||
@@ -57,7 +78,7 @@ export class TestController {
|
||||
}
|
||||
|
||||
log(message: string) {
|
||||
this.#logs.push(message);
|
||||
this.logs.push(message);
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
@@ -69,10 +90,6 @@ export class TestController {
|
||||
this.log(message);
|
||||
this.failNow();
|
||||
}
|
||||
|
||||
getLog(): string[] {
|
||||
return this.#logs;
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -83,14 +100,6 @@ export interface TestResult {
|
||||
logs: string[];
|
||||
}
|
||||
|
||||
export function run(reporter: Reporter) {
|
||||
reporter.register(TESTS);
|
||||
for (const suite of TESTS) {
|
||||
for (const c of suite.children) runTests(reporter, [suite.name], c);
|
||||
}
|
||||
reporter.finalize();
|
||||
}
|
||||
|
||||
function runTests(reporter: Reporter, parents: string[], node: TestTree) {
|
||||
const path = [...parents, node.name];
|
||||
if ("children" in node) {
|
||||
@@ -105,7 +114,7 @@ function runTests(reporter: Reporter, parents: string[], node: TestTree) {
|
||||
controller.error(extractExceptionString(e));
|
||||
}
|
||||
}
|
||||
reporter.update(path, { success: !controller.failed(), logs: controller.getLog() });
|
||||
reporter.update(path, { success: !controller.failed(), logs: controller.logs });
|
||||
}
|
||||
|
||||
function extractExceptionString(e: any): string {
|
||||
@@ -128,6 +137,30 @@ export interface Reporter {
|
||||
finalize(): void;
|
||||
}
|
||||
|
||||
export class NoOpReporter implements Reporter {
|
||||
suites: TestGroup[];
|
||||
results: ({ path: string[] } & TestResult)[];
|
||||
finalized: boolean;
|
||||
|
||||
constructor() {
|
||||
this.suites = [];
|
||||
this.results = [];
|
||||
this.finalized = false;
|
||||
}
|
||||
|
||||
register(suites: TestGroup[]) {
|
||||
this.suites = suites;
|
||||
}
|
||||
|
||||
update(path: string[], result: TestResult) {
|
||||
this.results.push({ path, ...result });
|
||||
}
|
||||
|
||||
finalize() {
|
||||
this.finalized = true;
|
||||
}
|
||||
}
|
||||
|
||||
export interface Stream {
|
||||
writeln(s: string): void;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { context, group, suite, test } from "./test.js";
|
||||
import { NoOpReporter, TestRegistrar, context, group, suite, test } from "./test.js";
|
||||
|
||||
suite("dog", [
|
||||
group("tail", [
|
||||
@@ -37,4 +37,45 @@ suite("cat", [
|
||||
test("likes headpats", (t) => {
|
||||
t.log("meow");
|
||||
}),
|
||||
test("tester tester", (t) => {
|
||||
const r = new TestRegistrar();
|
||||
r.suite("explod", [
|
||||
test("with yarn", (t) => {
|
||||
t.log("YAY");
|
||||
}),
|
||||
]);
|
||||
const reporter = new NoOpReporter();
|
||||
r.run(reporter);
|
||||
if (reporter.suites.length !== 1) {
|
||||
t.fatal(`incorrect number of suites registered got=${reporter.suites.length} want=1`);
|
||||
}
|
||||
const suite = reporter.suites[0];
|
||||
if (suite.name !== "explod") {
|
||||
t.error(`suite name incorrect got='${suite.name}' want='explod'`);
|
||||
}
|
||||
if (suite.children.length !== 1) {
|
||||
t.fatal(`incorrect number of suite children got=${suite.children.length} want=1`);
|
||||
}
|
||||
const test_ = suite.children[0];
|
||||
if (test_.name !== "with yarn") {
|
||||
t.error(`incorrect test name got='${test_.name}' want='with yarn'`);
|
||||
}
|
||||
if ("children" in test_) {
|
||||
t.error(`expected leaf node, got group of ${test_.children.length} children`);
|
||||
}
|
||||
if (!reporter.finalized) t.error(`expected reporter to have been finalized`);
|
||||
if (reporter.results.length !== 1) {
|
||||
t.fatal(`incorrect result count got=${reporter.results.length} want=1`);
|
||||
}
|
||||
const result = reporter.results[0];
|
||||
if (!(result.path.length === 2 &&
|
||||
result.path[0] === "explod" &&
|
||||
result.path[1] === "with yarn")) {
|
||||
t.error(`incorrect result path got=${result.path} want=["explod", "with yarn"]`);
|
||||
}
|
||||
if (!result.success) t.error(`expected test to succeed`);
|
||||
if (!(result.logs.length === 1 && result.logs[0] === "YAY")) {
|
||||
t.error(`incorrect result logs got=${result.logs} want=["YAY"]`);
|
||||
}
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
<div id="root">
|
||||
</div>
|
||||
|
||||
<script type="module" src="./static/all_tests.js"></script>
|
||||
<script type="module">
|
||||
import { DOMReporter, run } from "./static/test.js";
|
||||
run(new DOMReporter());
|
||||
import "./static/all_tests.js";
|
||||
import { DOMReporter, TESTS } from "./static/test.js";
|
||||
TESTS.run(new DOMReporter());
|
||||
</script>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user