diff --git a/packages/testament/package.json b/packages/testament/package.json index d1b6ad5bcd..324a9d8130 100644 --- a/packages/testament/package.json +++ b/packages/testament/package.json @@ -36,15 +36,6 @@ "doc": "typedoc --excludePrivate --out doc --theme ../../tools/doc/typedoc-theme src/index.ts", "pub": "yarn build:release && yarn publish --access public" }, - "dependencies": { - "@thi.ng/api": "^7.2.0", - "@thi.ng/args": "^1.1.1", - "@thi.ng/bench": "^2.1.6", - "@thi.ng/checks": "^2.9.11" - }, - "devDependencies": { - "@thi.ng/testament": "^0.0.1" - }, "keywords": [ "typescript" ], @@ -76,4 +67,4 @@ "status": "alpha", "year": 2021 } -} \ No newline at end of file +} diff --git a/packages/testament/src/api.ts b/packages/testament/src/api.ts index 735b285f10..fdd22438d7 100644 --- a/packages/testament/src/api.ts +++ b/packages/testament/src/api.ts @@ -1,4 +1,10 @@ -import { ConsoleLogger, Fn0, Fn2, ILogger } from "@thi.ng/api"; +export type Fn0 = () => A; +export type Fn = (a: A) => B; +export type Fn2 = (a: A, b: B) => C; + +export type VoidFn = Fn0; + +export type Timestamp = number | bigint; export interface TestOpts { logger: ILogger; @@ -7,15 +13,15 @@ export interface TestOpts { } export interface GroupOpts extends TestOpts { - beforeEach: Fn0; - afterEach: Fn0; + beforeEach: VoidFn; + afterEach: VoidFn; stop: boolean; exit: boolean; } export interface TestCtx { - done: Fn0; - setTimeout: Fn2, number, any>; + done: VoidFn; + setTimeout: Fn2; } export interface TestResult { @@ -24,6 +30,14 @@ export interface TestResult { error?: Error; } -export let LOGGER = new ConsoleLogger("testament"); +export interface ILogger { + level: number; + + fine(...args: any[]): void; + debug(...args: any[]): void; + info(...args: any[]): void; + warn(...args: any[]): void; + severe(...args: any[]): void; +} -export let TIMEOUT = 5000; +export let TIMEOUT = 1000; diff --git a/packages/testament/src/cli.ts b/packages/testament/src/cli.ts index fd943ae183..80b089beb4 100644 --- a/packages/testament/src/cli.ts +++ b/packages/testament/src/cli.ts @@ -1,33 +1,17 @@ -import { flag, parse } from "@thi.ng/args"; -import { isString } from "@thi.ng/checks"; import { readdirSync, statSync } from "fs"; import { resolve } from "path"; +import { isString } from "./utils"; -interface TestamentArgs { - csv: boolean; -} +// interface TestamentArgs { +// csv: boolean; +// } (async () => { - const res = parse( - { csv: flag({ desc: "Output CSV" }) }, - process.argv, - { - start: 2, - usageOpts: { - prefix: ` -@thi.ng/testament test runner -Usage: testament [opts] DIR1 DIR2... - -Options:`, - }, - } - ); - - if (!res) return; + const dirs = process.argv.slice(2); // const cwd = process.argv[1]; - for (let dir of res.rest) { + for (let dir of dirs) { for (let src of files(resolve(dir), ".ts")) { try { await import(src); diff --git a/packages/testament/src/group.ts b/packages/testament/src/group.ts index 74b54b4e45..e71c7c1c14 100644 --- a/packages/testament/src/group.ts +++ b/packages/testament/src/group.ts @@ -1,5 +1,5 @@ -import type { Fn } from "@thi.ng/api"; -import { GroupOpts, LOGGER, TestCtx, TestResult } from "./api"; +import type { Fn, GroupOpts, TestCtx, TestResult } from "./api"; +import { LOGGER } from "./logger"; import { test } from "./test"; export const group = async ( @@ -14,7 +14,9 @@ export const group = async ( }; let results: TestResult[] = []; try { + logger.info("----------"); logger.info(title); + logger.info("----------"); for (let k in tests) { beforeEach && beforeEach(); const res = await test(k, tests[k], opts); diff --git a/packages/testament/src/logger.ts b/packages/testament/src/logger.ts new file mode 100644 index 0000000000..a4e711b230 --- /dev/null +++ b/packages/testament/src/logger.ts @@ -0,0 +1,36 @@ +class Logger { + id: string; + level: number; + + constructor(id = "testament", level = 0) { + this.id = id; + this.level = level; + } + + fine(...args: any[]): void { + this.level <= 0 && this.log("FINE", args); + } + + debug(...args: any[]): void { + this.level <= 1 && this.log("DEBUG", args); + } + + info(...args: any[]): void { + this.level <= 2 && this.log("INFO", args); + } + + warn(...args: any[]): void { + this.level <= 3 && this.log("WARN", args); + } + + severe(...args: any[]): void { + this.level <= 4 && this.log("SEVERE", args); + } + + protected log(_: string, args: any[]) { + // console.log(`[${level}] ${this.id}:`, ...args); + console.log(...args); + } +} + +export let LOGGER = new Logger(); diff --git a/packages/testament/src/test.ts b/packages/testament/src/test.ts index e0a2265e2f..149d6e9cd8 100644 --- a/packages/testament/src/test.ts +++ b/packages/testament/src/test.ts @@ -1,6 +1,6 @@ -import type { Fn } from "@thi.ng/api"; -import { now, timeDiff, Timestamp } from "@thi.ng/bench"; -import { LOGGER, TestCtx, TestOpts, TestResult, TIMEOUT } from "./api"; +import { Fn, TestCtx, TestOpts, TestResult, TIMEOUT, Timestamp } from "./api"; +import { LOGGER } from "./logger"; +import { now, timeDiff } from "./utils"; export const test = async ( title: string, diff --git a/packages/testament/src/utils.ts b/packages/testament/src/utils.ts new file mode 100644 index 0000000000..6e1a65473a --- /dev/null +++ b/packages/testament/src/utils.ts @@ -0,0 +1,43 @@ +import type { Fn0, Fn2, Timestamp } from "./api"; + +/** + * @remarks + * Copied from thi.ng/checks to avoid circular deps + * + * @param x + * + * @internal + */ +export const isString = (x: any): x is string => typeof x === "string"; + +/** + * If available, returns wrapper for `process.hrtime.bigint()` else + * falls back to `Date.now()`. In all cases, returns a nanosec-scale + * timestamp, either as `bigint` or `number`. + * + * @remarks + * Copied from thi.ng/bench to avoid circular deps + */ +export const now: Fn0 = + typeof BigInt !== "undefined" + ? typeof process !== "undefined" && + typeof process.hrtime !== "undefined" && + typeof process.hrtime.bigint === "function" + ? () => process.hrtime.bigint() + : () => BigInt(Date.now() * 1e6) + : () => Date.now() * 1e6; + +/** + * Returns the difference in milliseconds between 2 given + * {@link Timestamp}s. + * + * @remarks + * Copied from thi.ng/bench to avoid circular deps + * + * @param a + * @param b + */ +export const timeDiff: Fn2 = (a, b) => + (typeof BigInt !== "undefined" + ? Number(b - a) + : b - a) * 1e-6;