aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/sentence.ts6
-rw-r--r--deno.lock1
-rw-r--r--test/base.ts33
-rw-r--r--test/reading/test.ts20
4 files changed, 54 insertions, 6 deletions
diff --git a/api/sentence.ts b/api/sentence.ts
index 2311913..f1d5521 100644
--- a/api/sentence.ts
+++ b/api/sentence.ts
@@ -61,6 +61,12 @@ export default class Sentence extends APIBase {
}, "");
}
+ get reading(): string {
+ return this.words.reduce((out, word) => {
+ return out + word.reading;
+ }, "");
+ }
+
public async forceUpdate() {
// unresolve ready
this.ready = new Promise(res => this._resolveReady = res);
diff --git a/deno.lock b/deno.lock
index 358021a..4bea2b4 100644
--- a/deno.lock
+++ b/deno.lock
@@ -70,6 +70,7 @@
"https://deno.land/std@0.193.0/testing/_diff.ts": "1a3c044aedf77647d6cac86b798c6417603361b66b54c53331b312caeb447aea",
"https://deno.land/std@0.193.0/testing/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7",
"https://deno.land/std@0.193.0/testing/asserts.ts": "056d571baaefc7f13af3e29ad6a66d4dbe5355d3cb2ae130e7d2a1b1e01085e3",
+ "https://deno.land/x/diff@v0.3.5/mod.ts": "87329de713a1158720436eb2034a0a84f5146639241b6b0f15409d7077715193",
"https://deno.land/x/plug@1.0.1/deps.ts": "35ea2acd5e3e11846817a429b7ef4bec47b80f2d988f5d63797147134cbd35c2",
"https://deno.land/x/plug@1.0.1/download.ts": "8d6a023ade0806a0653b48cd5f6f8b15fcfaa1dbf2aa1f4bc90fc5732d27b144",
"https://deno.land/x/plug@1.0.1/mod.ts": "5dec80ee7a3a325be45c03439558531bce7707ac118f4376cebbd6740ff24bfb",
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));
+ }
});
});