forked from rosa/hakurei
Compare commits
5 Commits
2762e26718
...
6f78444b11
| Author | SHA1 | Date | |
|---|---|---|---|
|
6f78444b11
|
|||
|
2a3f6f5384
|
|||
|
ef8663461b
|
|||
|
0b3be27b9a
|
|||
|
61a25c88ae
|
@@ -29,6 +29,8 @@ func serveStaticContent(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFileFS(w, r, content, "ui/static/test.js")
|
||||
case "/static/test.css":
|
||||
http.ServeFileFS(w, r, content, "ui/static/test.css")
|
||||
case "/static/test_tests.js":
|
||||
http.ServeFileFS(w, r, content, "ui/static/test_tests.js")
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
|
||||
|
||||
4
cmd/pkgserver/ui/static/run_tests.ts
Normal file
4
cmd/pkgserver/ui/static/run_tests.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
import "./test_tests.js";
|
||||
import { run, StreamReporter } from "./test.js";
|
||||
run(new StreamReporter({ writeln: console.log }));
|
||||
@@ -17,6 +17,9 @@ details.test-node {
|
||||
p.test-desc {
|
||||
margin: 0 0 0 1rem;
|
||||
padding: 2px 0;
|
||||
> pre {
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.italic {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -18,116 +137,114 @@ export interface Stream {
|
||||
export class StreamReporter implements Reporter {
|
||||
stream: Stream;
|
||||
verbose: boolean;
|
||||
failures: ({ path: string[] } & TestResult)[];
|
||||
#failures: ({ path: string[] } & TestResult)[];
|
||||
counts: { successes: number, failures: number };
|
||||
|
||||
constructor(stream: Stream, verbose: boolean = false) {
|
||||
this.stream = stream;
|
||||
this.verbose = verbose;
|
||||
this.failures = [];
|
||||
this.#failures = [];
|
||||
this.counts = { successes: 0, failures: 0 };
|
||||
}
|
||||
|
||||
update(path: string[], result: TestResult) {
|
||||
if (path.length === 0) throw new RangeError("path is empty");
|
||||
const pathStr = path.join(' ❯ ');
|
||||
const pathStr = path.join(" ❯ ");
|
||||
if (result.success) {
|
||||
this.counts.successes++;
|
||||
if (this.verbose) this.stream.writeln(`✅️ ${pathStr}`);
|
||||
} else {
|
||||
this.counts.failures++;
|
||||
this.stream.writeln(`⚠️ ${pathStr}`);
|
||||
this.failures.push({ path, ...result });
|
||||
this.#failures.push({ path, ...result });
|
||||
}
|
||||
}
|
||||
|
||||
finalize() {
|
||||
// Transform [{ path: ['a', 'b', 'c'] }, { path: ['a', 'b', 'd'] }]
|
||||
// into { 'a ❯ b': ['c', 'd'] }.
|
||||
// Transform [{ path: ["a", "b", "c"] }, { path: ["a", "b", "d"] }]
|
||||
// into { "a ❯ b": ["c", "d"] }.
|
||||
let pathMap = new Map<string, ({ name: string } & TestResult)[]>();
|
||||
for (const f of this.failures) {
|
||||
const key = f.path.slice(0, -1).join(' ❯ ');
|
||||
for (const f of this.#failures) {
|
||||
const key = f.path.slice(0, -1).join(" ❯ ");
|
||||
if (!pathMap.has(key)) pathMap.set(key, []);
|
||||
pathMap.get(key).push({ name: f.path.at(-1), ...f });
|
||||
}
|
||||
|
||||
this.stream.writeln('');
|
||||
this.stream.writeln('FAILURES');
|
||||
this.stream.writeln('========');
|
||||
this.stream.writeln("");
|
||||
this.stream.writeln("FAILURES");
|
||||
this.stream.writeln("========");
|
||||
|
||||
for (const [path, tests] of pathMap) {
|
||||
if (tests.length === 1) {
|
||||
const t = tests[0];
|
||||
const pathStr = path ? `${path} ❯ ` : '';
|
||||
const output = t.output ? `: ${t.output}` : '';
|
||||
this.stream.writeln(`${pathStr}${t.name}${output}`);
|
||||
this.#writeOutput(tests[0], path ? `${path} ❯ ` : "", false);
|
||||
} else {
|
||||
this.stream.writeln(path);
|
||||
for (const t of tests) {
|
||||
const output = t.output ? `: ${t.output}` : '';
|
||||
this.stream.writeln(` - ${t.name}${output}`);
|
||||
}
|
||||
for (const t of tests) this.#writeOutput(t, " - ", true);
|
||||
}
|
||||
}
|
||||
|
||||
this.stream.writeln('');
|
||||
this.stream.writeln("");
|
||||
const { successes, failures } = this.counts;
|
||||
this.stream.writeln(`${successes} succeeded, ${failures} failed`);
|
||||
}
|
||||
|
||||
#writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) {
|
||||
let output = "";
|
||||
if (test.output) {
|
||||
const lines = test.output.split("\n");
|
||||
if (lines.length <= 1) {
|
||||
output = `: ${test.output}`;
|
||||
} else {
|
||||
const padding = nested ? " " : " ";
|
||||
output = ":\n" + lines.map((line) => padding + line).join("\n");
|
||||
}
|
||||
}
|
||||
this.stream.writeln(`${prefix}${test.name}${output}`);
|
||||
}
|
||||
}
|
||||
|
||||
export class DOMReporter implements Reporter {
|
||||
update(path: string[], result: TestResult) {
|
||||
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");
|
||||
counter.innerText = (Number(counter.innerText) + 1).toString();
|
||||
let parent = document.getElementById('root');
|
||||
let parent = document.getElementById("root");
|
||||
for (const node of path) {
|
||||
let child = null;
|
||||
outer: for (const d of parent.children) {
|
||||
for (const s of d.children) {
|
||||
if (!(s instanceof HTMLElement)) continue;
|
||||
if (s.tagName !== 'SUMMARY' || s.innerText !== node) continue;
|
||||
if (s.tagName !== "SUMMARY" || s.innerText !== node) continue;
|
||||
child = d;
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
if (child === null) {
|
||||
child = document.createElement('details');
|
||||
child.className = 'test-node';
|
||||
const summary = document.createElement('summary');
|
||||
child = document.createElement("details");
|
||||
child.className = "test-node";
|
||||
const summary = document.createElement("summary");
|
||||
summary.appendChild(document.createTextNode(node));
|
||||
child.appendChild(summary);
|
||||
parent.appendChild(child);
|
||||
}
|
||||
if (!result.success) {
|
||||
child.open = true;
|
||||
child.classList.add('failure');
|
||||
child.classList.add("failure");
|
||||
}
|
||||
parent = child;
|
||||
}
|
||||
const p = document.createElement('p');
|
||||
p.classList.add('test-desc');
|
||||
const p = document.createElement("p");
|
||||
p.classList.add("test-desc");
|
||||
if (result.output) {
|
||||
const code = document.createElement('code');
|
||||
code.appendChild(document.createTextNode(result.output));
|
||||
p.appendChild(code);
|
||||
const pre = document.createElement("pre");
|
||||
pre.appendChild(document.createTextNode(result.output));
|
||||
p.appendChild(pre);
|
||||
} else {
|
||||
p.classList.add('italic');
|
||||
p.appendChild(document.createTextNode('No output.'));
|
||||
p.classList.add("italic");
|
||||
p.appendChild(document.createTextNode("No output."));
|
||||
}
|
||||
parent.appendChild(p);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
40
cmd/pkgserver/ui/static/test_tests.ts
Normal file
40
cmd/pkgserver/ui/static/test_tests.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { context, group, suite, test } from "./test.js";
|
||||
|
||||
suite("dog", [
|
||||
group("tail", [
|
||||
test("wags when happy", (t) => {
|
||||
if (0 / 0 !== Infinity / Infinity) {
|
||||
t.fatal("undefined must not be defined");
|
||||
}
|
||||
}),
|
||||
test("idle when down", (t) => {
|
||||
t.log("test test");
|
||||
t.error("dog whining noises go here");
|
||||
}),
|
||||
]),
|
||||
test("likes headpats", (t) => {
|
||||
if (2 !== 2) {
|
||||
t.error("IEEE 754 violated: 2 is NaN");
|
||||
}
|
||||
}),
|
||||
context("near cat", [
|
||||
test("is ecstatic", (t) => {
|
||||
if (("b" + "a" + + "a" + "a").toLowerCase() == "banana") {
|
||||
t.error("🍌🍌🍌");
|
||||
t.error("🍌🍌🍌");
|
||||
t.error("🍌🍌🍌");
|
||||
t.failNow();
|
||||
}
|
||||
}),
|
||||
test("playfully bites cats' tails", (t) => {
|
||||
t.log("arf!");
|
||||
throw new Error("nom");
|
||||
}),
|
||||
]),
|
||||
]);
|
||||
|
||||
suite("cat", [
|
||||
test("likes headpats", (t) => {
|
||||
t.log("meow");
|
||||
}),
|
||||
]);
|
||||
@@ -18,7 +18,11 @@
|
||||
<div id="root">
|
||||
</div>
|
||||
|
||||
<script type="module" src="./static/test.js"></script>
|
||||
<script type="module" src="./static/test_tests.js"></script>
|
||||
<script type="module">
|
||||
import { DOMReporter, run } from "./static/test.js";
|
||||
run(new DOMReporter());
|
||||
</script>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user