diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/base.ts | 33 | ||||
-rw-r--r-- | test/reading/test.ts | 20 |
2 files changed, 47 insertions, 6 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)); +} + diff --git a/test/reading/test.ts b/test/reading/test.ts index c1e7de4..e178543 100644 --- a/test/reading/test.ts +++ b/test/reading/test.ts @@ -1,12 +1,20 @@ -import { api, assertEquals, formatCaseIndex } from "../base.ts"; +import YomikunError from "../../util/error.ts"; +import { api, assertStrDiffMsg, formatCaseIndex } from "../base.ts"; import cases from "./cases.ts"; -cases.forEach(({input, output}, i) => { +cases.forEach((test, i) => { // if (i != 1) return; - Deno.test(`Sentence reading ${formatCaseIndex(i, cases.length)} - ${input}`, async () => { - // TODO: use sentence reading and tags - var sentence = await api.sentence(input); - assertEquals(sentence.furigana("refold-tools"), output); + Deno.test(`Sentence reading ${formatCaseIndex(i, cases.length)} - ${test.input}`, async () => { + // TODO: use domain/series tags + var sentence = await api.sentence(test.input); + + const actual = sentence.furigana("refold-tools"); + const expected = test.output; + + // reading-only check (skip split reading across multiple kanji check) + if (sentence.reading != test.reading && actual != expected) { + throw new YomikunError(assertStrDiffMsg(expected, actual)); + } }); }); |