29 lines
635 B
JavaScript
29 lines
635 B
JavaScript
/** The smallest test harness that gives a useful failure message. */
|
|
|
|
let failures = 0;
|
|
let checks = 0;
|
|
|
|
export function ok(cond, msg) {
|
|
checks++;
|
|
if (!cond) {
|
|
failures++;
|
|
console.error(' FAIL ' + msg);
|
|
}
|
|
}
|
|
|
|
export function near(actual, expected, tol, msg) {
|
|
ok(Math.abs(actual - expected) <= tol, `${msg} (got ${actual}, want ${expected} ±${tol})`);
|
|
}
|
|
|
|
export function section(name) {
|
|
console.log('· ' + name);
|
|
}
|
|
|
|
export function done(name) {
|
|
if (failures) {
|
|
console.error(`\n${name}: ${failures} of ${checks} checks failed`);
|
|
process.exit(1);
|
|
}
|
|
console.log(`${name}: ${checks} checks passed`);
|
|
}
|