aboutsummaryrefslogtreecommitdiff
path: root/test/base.ts
blob: 064ed57ebb9930be37dcbf51df488ac4320dd041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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');
	out += "/";
	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));
}