summaryrefslogtreecommitdiff
path: root/tests/node-driver.js
blob: 3393dca4ddc3e146a80c3899ee1e15807fc088c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import assert  from "node:assert/strict";
import process from "node:process";



const red    = s => `\x1b[31m${s}\x1b[0m`;
const green  = s => `\x1b[32m${s}\x1b[0m`;
const yellow = s => `\x1b[33m${s}\x1b[0m`;

const isAsync = f =>
	typeof f.then === 'function' &&
	f[Symbol.toStringTag] === 'Promise';

const t = {
	assert,
	tap: x => {
		process.stderr.write(`tap: ${x}\n`);
		return x;
	},
	start: msg => {
		process.stderr.write(`${msg}:\n`);
	},
	test: async (msg, fn) => {
		process.stderr.write(`${yellow("testing")}: ${msg}... `);
		try {
			if (isAsync(fn)) {
				await fn();
			} else {
				fn();
			}
		} catch (e) {
			process.stderr.write(`${red("ERR")}.\n}`);
			throw e;
		}
		process.stderr.write(`${green("OK")}.\n`);
	},
};

const runTests = async tests => {
	for (const testFn of tests) {
		await testFn(t);
	}
};

const main = async (path = process.argv[2]) => {
	const module = await import("../" + path);
	await runTests(module.allTests);
};



await main();