summaryrefslogtreecommitdiff
path: root/tests/runner.js
blob: 2e4e2ce031009b13d227abc4ff6b2d182c979c9d (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
53
54
55
56
import { getExit, ARGV } from "../src/compat.js";
import { eq } from "../src/utils.js";

class AssertionError extends Error {}

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 t = {
	tap: x => {
		console.error("tap:", x);
		return x;
	},
	start: msg => {
		console.error(`${msg}:`);
	},
	test: (msg, fn) => {
		try {
			fn();
			console.error(`${yellow("testing")}: ${msg}... ${green("OK")}`);
		} catch (e) {
			console.error(`${yellow("testing")}: ${msg}... ${red("FAIL")}`);
			throw e;
		}
	},
	assertEq: (expected, given, msg = "") => {
		if (!eq(expected, given)) {
			console.error("expected:", expected, "\ngiven:   ", given);
			throw new AssertionError(`${red(msg)}`);
		}
	},
	assert: (x, msg = "") => {
		if (!x) {
			throw new AssertionError(`${red(msg)}`);
		}
	},
};

const main = async () => {
	const { tests } = await import(`../${ARGV[1]}`);
	for (const testFn of tests) {
		await testFn(t);
	}
};


getExit().then(async exit => {
	try {
		await main();
		exit(0);
	} catch (e) {
		console.error(e);
		exit(1);
	}
});