2019-01-01 22:46:17 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2019-03-02 14:56:53 -05:00
|
|
|
import { green, red } from "../colors/mod.ts";
|
2019-01-22 01:09:51 -05:00
|
|
|
|
2019-01-01 22:46:17 -05:00
|
|
|
export type TestFunction = () => void | Promise<void>;
|
|
|
|
|
|
|
|
export interface TestDefinition {
|
|
|
|
fn: TestFunction;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filterRegExp: RegExp | null;
|
2019-03-28 12:29:27 -04:00
|
|
|
const candidates: TestDefinition[] = [];
|
2019-01-01 22:46:17 -05:00
|
|
|
|
|
|
|
let filtered = 0;
|
|
|
|
|
|
|
|
// Must be called before any test() that needs to be filtered.
|
|
|
|
export function setFilter(s: string): void {
|
|
|
|
filterRegExp = new RegExp(s, "i");
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
function filter(name: string): boolean {
|
|
|
|
if (filterRegExp) {
|
|
|
|
return filterRegExp.test(name);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 22:46:17 -05:00
|
|
|
export function test(t: TestDefinition | TestFunction): void {
|
|
|
|
const fn: TestFunction = typeof t === "function" ? t : t.fn;
|
|
|
|
const name: string = t.name;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
throw new Error("Test function may not be anonymous");
|
|
|
|
}
|
|
|
|
if (filter(name)) {
|
2019-03-28 12:29:27 -04:00
|
|
|
candidates.push({ fn, name });
|
2019-01-01 22:46:17 -05:00
|
|
|
} else {
|
|
|
|
filtered++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
const RED_FAILED = red("FAILED");
|
|
|
|
const GREEN_OK = green("ok");
|
2019-01-01 22:46:17 -05:00
|
|
|
|
2019-03-04 14:19:03 -05:00
|
|
|
interface TestStats {
|
|
|
|
filtered: number;
|
|
|
|
ignored: number;
|
|
|
|
measured: number;
|
|
|
|
passed: number;
|
|
|
|
failed: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestResult {
|
|
|
|
name: string;
|
2019-05-30 08:59:30 -04:00
|
|
|
error?: Error;
|
2019-03-04 14:19:03 -05:00
|
|
|
ok: boolean;
|
|
|
|
printed: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestResults {
|
|
|
|
keys: Map<string, number>;
|
|
|
|
cases: Map<number, TestResult>;
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
function createTestResults(tests: TestDefinition[]): TestResults {
|
2019-03-04 14:19:03 -05:00
|
|
|
return tests.reduce(
|
|
|
|
(acc: TestResults, { name }: TestDefinition, i: number): TestResults => {
|
|
|
|
acc.keys.set(name, i);
|
2019-05-30 08:59:30 -04:00
|
|
|
acc.cases.set(i, { name, printed: false, ok: false, error: undefined });
|
2019-03-04 14:19:03 -05:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{ cases: new Map(), keys: new Map() }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function report(result: TestResult): void {
|
|
|
|
if (result.ok) {
|
2019-03-04 19:53:35 -05:00
|
|
|
console.log(`test ${result.name} ... ${GREEN_OK}`);
|
2019-03-04 14:19:03 -05:00
|
|
|
} else if (result.error) {
|
|
|
|
console.error(
|
2019-03-04 19:53:35 -05:00
|
|
|
`test ${result.name} ... ${RED_FAILED}\n${result.error.stack}`
|
2019-03-04 14:19:03 -05:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(`test ${result.name} ... unresolved`);
|
|
|
|
}
|
|
|
|
result.printed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function printResults(
|
|
|
|
stats: TestStats,
|
|
|
|
results: TestResults,
|
2019-03-26 11:29:12 -04:00
|
|
|
flush: boolean,
|
|
|
|
exitOnFail: boolean
|
2019-03-04 14:19:03 -05:00
|
|
|
): void {
|
|
|
|
if (flush) {
|
|
|
|
for (const result of results.cases.values()) {
|
|
|
|
if (!result.printed) {
|
|
|
|
report(result);
|
|
|
|
if (result.error && exitOnFail) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Attempting to match the output of Rust's test runner.
|
|
|
|
console.log(
|
2019-03-04 19:53:35 -05:00
|
|
|
`\ntest result: ${stats.failed ? RED_FAILED : GREEN_OK}. ` +
|
2019-03-04 14:19:03 -05:00
|
|
|
`${stats.passed} passed; ${stats.failed} failed; ` +
|
|
|
|
`${stats.ignored} ignored; ${stats.measured} measured; ` +
|
|
|
|
`${stats.filtered} filtered out\n`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function previousPrinted(name: string, results: TestResults): boolean {
|
2019-05-30 08:59:30 -04:00
|
|
|
const curIndex: number = results.keys.get(name)!;
|
2019-03-04 14:19:03 -05:00
|
|
|
if (curIndex === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
return results.cases.get(curIndex - 1)!.printed;
|
2019-03-04 14:19:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createTestCase(
|
|
|
|
stats: TestStats,
|
|
|
|
results: TestResults,
|
2019-03-28 12:29:27 -04:00
|
|
|
exitOnFail: boolean,
|
|
|
|
{ fn, name }: TestDefinition
|
2019-03-04 14:19:03 -05:00
|
|
|
): Promise<void> {
|
2019-05-30 08:59:30 -04:00
|
|
|
const result: TestResult = results.cases.get(results.keys.get(name)!)!;
|
2019-03-04 14:19:03 -05:00
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
stats.passed++;
|
|
|
|
result.ok = true;
|
|
|
|
} catch (err) {
|
|
|
|
stats.failed++;
|
|
|
|
result.error = err;
|
|
|
|
if (exitOnFail) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (previousPrinted(name, results)) {
|
|
|
|
report(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initTestCases(
|
|
|
|
stats: TestStats,
|
|
|
|
results: TestResults,
|
2019-03-26 11:29:12 -04:00
|
|
|
tests: TestDefinition[],
|
|
|
|
exitOnFail: boolean
|
2019-03-04 14:19:03 -05:00
|
|
|
): Array<Promise<void>> {
|
2019-03-26 11:29:12 -04:00
|
|
|
return tests.map(createTestCase.bind(null, stats, results, exitOnFail));
|
2019-03-04 14:19:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function runTestsParallel(
|
|
|
|
stats: TestStats,
|
|
|
|
results: TestResults,
|
2019-03-26 11:29:12 -04:00
|
|
|
tests: TestDefinition[],
|
|
|
|
exitOnFail: boolean
|
2019-03-04 14:19:03 -05:00
|
|
|
): Promise<void> {
|
|
|
|
try {
|
2019-03-26 11:29:12 -04:00
|
|
|
await Promise.all(initTestCases(stats, results, tests, exitOnFail));
|
2019-03-04 14:19:03 -05:00
|
|
|
} catch (_) {
|
|
|
|
// The error was thrown to stop awaiting all promises if exitOnFail === true
|
|
|
|
// stats.failed has been incremented and the error stored in results
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runTestsSerial(
|
|
|
|
stats: TestStats,
|
2019-03-26 11:29:12 -04:00
|
|
|
tests: TestDefinition[],
|
|
|
|
exitOnFail: boolean
|
2019-03-04 14:19:03 -05:00
|
|
|
): Promise<void> {
|
|
|
|
for (const { fn, name } of tests) {
|
2019-01-09 10:32:37 -05:00
|
|
|
// See https://github.com/denoland/deno/pull/1452
|
|
|
|
// about this usage of groupCollapsed
|
|
|
|
console.groupCollapsed(`test ${name} `);
|
2019-01-01 22:46:17 -05:00
|
|
|
try {
|
|
|
|
await fn();
|
2019-03-04 14:19:03 -05:00
|
|
|
stats.passed++;
|
2019-03-04 19:53:35 -05:00
|
|
|
console.log("...", GREEN_OK);
|
2019-01-09 10:32:37 -05:00
|
|
|
console.groupEnd();
|
2019-03-04 14:19:03 -05:00
|
|
|
} catch (err) {
|
2019-03-04 19:53:35 -05:00
|
|
|
console.log("...", RED_FAILED);
|
2019-01-09 10:32:37 -05:00
|
|
|
console.groupEnd();
|
2019-03-04 14:19:03 -05:00
|
|
|
console.error(err.stack);
|
|
|
|
stats.failed++;
|
2019-01-01 22:46:17 -05:00
|
|
|
if (exitOnFail) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 14:19:03 -05:00
|
|
|
}
|
2019-01-01 22:46:17 -05:00
|
|
|
|
2019-03-04 14:19:03 -05:00
|
|
|
/** Defines options for controlling execution details of a test suite. */
|
|
|
|
export interface RunOptions {
|
|
|
|
parallel?: boolean;
|
2019-03-26 11:29:12 -04:00
|
|
|
exitOnFail?: boolean;
|
2019-03-28 12:29:27 -04:00
|
|
|
only?: RegExp;
|
|
|
|
skip?: RegExp;
|
2019-03-04 14:19:03 -05:00
|
|
|
}
|
2019-01-01 22:46:17 -05:00
|
|
|
|
2019-03-04 14:19:03 -05:00
|
|
|
/**
|
|
|
|
* Runs specified test cases.
|
|
|
|
* Parallel execution can be enabled via the boolean option; default: serial.
|
|
|
|
*/
|
2019-03-26 11:29:12 -04:00
|
|
|
export async function runTests({
|
|
|
|
parallel = false,
|
2019-03-28 12:29:27 -04:00
|
|
|
exitOnFail = false,
|
|
|
|
only = /[^\s]/,
|
|
|
|
skip = /^\s*$/
|
2019-03-26 11:29:12 -04:00
|
|
|
}: RunOptions = {}): Promise<void> {
|
2019-03-28 12:29:27 -04:00
|
|
|
const tests: TestDefinition[] = candidates.filter(
|
2019-04-24 07:41:23 -04:00
|
|
|
({ name }): boolean => only.test(name) && !skip.test(name)
|
2019-03-28 12:29:27 -04:00
|
|
|
);
|
2019-03-04 14:19:03 -05:00
|
|
|
const stats: TestStats = {
|
|
|
|
measured: 0,
|
2019-03-28 12:29:27 -04:00
|
|
|
ignored: candidates.length - tests.length,
|
2019-03-04 14:19:03 -05:00
|
|
|
filtered: filtered,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0
|
|
|
|
};
|
|
|
|
const results: TestResults = createTestResults(tests);
|
|
|
|
console.log(`running ${tests.length} tests`);
|
|
|
|
if (parallel) {
|
2019-03-26 11:29:12 -04:00
|
|
|
await runTestsParallel(stats, results, tests, exitOnFail);
|
2019-01-01 22:46:17 -05:00
|
|
|
} else {
|
2019-03-26 11:29:12 -04:00
|
|
|
await runTestsSerial(stats, tests, exitOnFail);
|
2019-03-04 14:19:03 -05:00
|
|
|
}
|
2019-03-26 11:29:12 -04:00
|
|
|
printResults(stats, results, parallel, exitOnFail);
|
2019-03-04 14:19:03 -05:00
|
|
|
if (stats.failed) {
|
2019-01-01 22:46:17 -05:00
|
|
|
// Use setTimeout to avoid the error being ignored due to unhandled
|
|
|
|
// promise rejections being swallowed.
|
2019-04-24 07:41:23 -04:00
|
|
|
setTimeout((): void => {
|
2019-03-04 14:19:03 -05:00
|
|
|
console.error(`There were ${stats.failed} test failures.`);
|
2019-02-23 11:24:57 -05:00
|
|
|
Deno.exit(1);
|
2019-01-01 22:46:17 -05:00
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
2019-02-23 11:18:43 -05:00
|
|
|
|
2019-03-04 14:19:03 -05:00
|
|
|
/**
|
|
|
|
* Runs specified test cases if the enclosing script is main.
|
|
|
|
* Execution mode is toggleable via opts.parallel, defaults to false.
|
|
|
|
*/
|
|
|
|
export async function runIfMain(
|
|
|
|
meta: ImportMeta,
|
|
|
|
opts?: RunOptions
|
|
|
|
): Promise<void> {
|
2019-03-01 02:54:21 -05:00
|
|
|
if (meta.main) {
|
2019-03-04 14:19:03 -05:00
|
|
|
return runTests(opts);
|
2019-02-23 11:18:43 -05:00
|
|
|
}
|
|
|
|
}
|