2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-03-13 10:57:32 -04:00
|
|
|
|
2021-02-02 06:05:46 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
} from "../../../test_util/std/testing/asserts.ts";
|
|
|
|
import * as colors from "../../../test_util/std/fmt/colors.ts";
|
2020-06-12 11:58:04 -04:00
|
|
|
export { colors };
|
2021-02-02 06:05:46 -05:00
|
|
|
import { resolve } from "../../../test_util/std/path/mod.ts";
|
2019-03-06 20:48:46 -05:00
|
|
|
export {
|
|
|
|
assert,
|
2019-05-17 14:03:01 -04:00
|
|
|
assertEquals,
|
2019-08-27 11:33:39 -04:00
|
|
|
assertMatch,
|
2019-05-17 14:03:01 -04:00
|
|
|
assertNotEquals,
|
2021-09-05 16:43:46 -04:00
|
|
|
assertRejects,
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStrictEquals,
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes,
|
2020-09-27 06:22:32 -04:00
|
|
|
assertThrows,
|
2020-03-28 13:03:49 -04:00
|
|
|
fail,
|
2021-02-22 07:26:17 -05:00
|
|
|
unimplemented,
|
2020-09-27 06:22:32 -04:00
|
|
|
unreachable,
|
2021-02-02 06:05:46 -05:00
|
|
|
} from "../../../test_util/std/testing/asserts.ts";
|
|
|
|
export { deferred } from "../../../test_util/std/async/deferred.ts";
|
2021-04-11 19:40:42 -04:00
|
|
|
export type { Deferred } from "../../../test_util/std/async/deferred.ts";
|
2021-06-24 21:44:14 -04:00
|
|
|
export { delay } from "../../../test_util/std/async/delay.ts";
|
2021-02-02 06:05:46 -05:00
|
|
|
export { readLines } from "../../../test_util/std/io/bufio.ts";
|
|
|
|
export { parse as parseArgs } from "../../../test_util/std/flags/mod.ts";
|
2018-08-23 19:47:43 -04:00
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
interface UnitTestPermissions {
|
2021-09-22 19:50:50 -04:00
|
|
|
env?: "inherit" | boolean | string[];
|
|
|
|
hrtime?: "inherit" | boolean;
|
|
|
|
net?: "inherit" | boolean | string[];
|
|
|
|
ffi?: "inherit" | boolean;
|
|
|
|
read?: "inherit" | boolean | Array<string | URL>;
|
|
|
|
run?: "inherit" | boolean | Array<string | URL>;
|
|
|
|
write?: "inherit" | boolean | Array<string | URL>;
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
interface UnitTestOptions {
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore?: boolean;
|
2020-06-12 11:58:04 -04:00
|
|
|
only?: boolean;
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions?: UnitTestPermissions;
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2020-04-01 04:47:23 -04:00
|
|
|
type TestFunction = () => void | Promise<void>;
|
|
|
|
|
|
|
|
export function unitTest(fn: TestFunction): void;
|
|
|
|
export function unitTest(options: UnitTestOptions, fn: TestFunction): void;
|
2020-03-04 11:31:14 -05:00
|
|
|
export function unitTest(
|
2020-04-01 04:47:23 -04:00
|
|
|
optionsOrFn: UnitTestOptions | TestFunction,
|
2020-07-14 15:24:17 -04:00
|
|
|
maybeFn?: TestFunction,
|
2020-03-04 11:31:14 -05:00
|
|
|
): void {
|
|
|
|
assert(optionsOrFn, "At least one argument is required");
|
|
|
|
|
|
|
|
let options: UnitTestOptions;
|
|
|
|
let name: string;
|
2020-04-01 04:47:23 -04:00
|
|
|
let fn: TestFunction;
|
2020-03-04 11:31:14 -05:00
|
|
|
|
|
|
|
if (typeof optionsOrFn === "function") {
|
|
|
|
options = {};
|
|
|
|
fn = optionsOrFn;
|
|
|
|
name = fn.name;
|
|
|
|
assert(name, "Missing test function name");
|
|
|
|
} else {
|
|
|
|
options = optionsOrFn;
|
|
|
|
assert(maybeFn, "Missing test function definition");
|
|
|
|
assert(
|
|
|
|
typeof maybeFn === "function",
|
2020-07-14 15:24:17 -04:00
|
|
|
"Second argument should be test function definition",
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
fn = maybeFn;
|
|
|
|
name = fn.name;
|
|
|
|
assert(name, "Missing test function name");
|
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2021-04-27 07:14:01 -04:00
|
|
|
const testDefinition: Deno.TestDefinition = {
|
2020-03-04 11:31:14 -05:00
|
|
|
name,
|
2020-03-18 19:25:55 -04:00
|
|
|
fn,
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: !!options.ignore,
|
2020-06-12 11:58:04 -04:00
|
|
|
only: !!options.only,
|
2021-04-27 07:14:01 -04:00
|
|
|
permissions: Object.assign({
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
net: false,
|
|
|
|
env: false,
|
|
|
|
run: false,
|
2021-08-06 17:28:10 -04:00
|
|
|
ffi: false,
|
2021-04-27 07:14:01 -04:00
|
|
|
hrtime: false,
|
2021-09-22 19:50:50 -04:00
|
|
|
}, options.permissions),
|
2020-03-04 11:31:14 -05:00
|
|
|
};
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2021-04-27 07:14:01 -04:00
|
|
|
Deno.test(testDefinition);
|
2020-04-01 04:47:23 -04:00
|
|
|
}
|
2020-03-13 10:57:32 -04:00
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
export function pathToAbsoluteFileUrl(path: string): URL {
|
|
|
|
path = resolve(path);
|
|
|
|
|
|
|
|
return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`);
|
|
|
|
}
|