diff options
Diffstat (limited to 'test/base.ts')
-rw-r--r-- | test/base.ts | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/base.ts b/test/base.ts index 8369bfe..064ed57 100644 --- a/test/base.ts +++ b/test/base.ts @@ -1,14 +1,19 @@ export { assertEquals } from "https://deno.land/std@0.193.0/testing/asserts.ts"; +import { diffCharacters } from "https://deno.land/x/diff@v0.3.5/mod.ts"; +import { bold, gray, green, red } from "https://deno.land/std@0.193.0/fmt/colors.ts"; + import Yomikun from "../api/yomikun.ts"; import DirectCoreClient from '../core/direct/client.ts'; import { Wrap } from "../util/wrap.ts"; +import YomikunError from "../util/error.ts"; export const core = new DirectCoreClient(); export const api = new Yomikun(core); await api.ready; +/** @summary print padded test case index (e.g. "(001/452)") */ export function formatCaseIndex(i: number, total: number) { let out = ""; out += (i+1).toString().padStart(Math.log10(total) + 1, '0'); @@ -16,3 +21,31 @@ export function formatCaseIndex(i: number, total: number) { out += total.toString(); return out.wrap(Wrap.parenthesis); } + +/** @summary diff a (single line) string character by character and insert ANSI color codes */ +export function strDiff(expected: string, actual: string): string { + let out = ""; + const diff = diffCharacters(expected, actual); + out += "expected: "; + out += diff.filter(c => c.wasAdded == false).map(c => c.wasRemoved ? red(c.character) : bold(gray(c.character))).join(""); + out += "\n"; + out += " actual: "; + out += diff.filter(c => c.wasRemoved == false).map(c => c.wasAdded ? green(c.character) : bold(gray(c.character))).join(""); + return out; +} + +export function assertStrDiffMsg(expected: string, actual: string): string { + let msg = "Values are not equal!\n\n"; + msg += " "; + msg += bold(gray("[diff]")) + " " + `(${red("removed")}/${green("added")})`; + msg += "\n"; + msg += strDiff(expected, actual); + return msg; +} + +/** @summary like deno std assertEquals but character by character */ +export function assertStrDiff(expected: string, actual: string): void | never { + if (expected == actual) return; + throw new YomikunError(assertStrDiffMsg(expected, actual)); +} + |