Files
strans/tests/mkhanja_test.py

110 lines
3.2 KiB
Python

#!/usr/bin/env python3
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
IMPORT = ROOT / "map" / "libhangul2hanja"
GENERATE = ROOT / "map" / "mkhanja"
def run(program, source):
return subprocess.run(
[sys.executable, "-B", str(program), str(source)],
capture_output=True,
text=True,
check=False,
)
class MkhanjaTest(unittest.TestCase):
def source(self, text):
tmp = tempfile.TemporaryDirectory()
path = Path(tmp.name) / "hanja.txt"
path.write_text(text, encoding="utf-8")
self.addCleanup(tmp.cleanup)
return path
def test_imports_single_bmp_hanja_rows(self):
result = run(
IMPORT,
self.source(
"# Copyright holder\n"
"# BSD license\n"
"한:漢:first\n"
"가:㐀:extension A\n"
"김:金:compatibility\n"
"방학:放:word reading\n"
"학:學校:word value\n"
"한:𠀀:astral\n"
"ㄱ:加:jamo\n"
),
)
self.assertEqual(result.returncode, 0, result.stderr)
lines = result.stdout.splitlines()
self.assertEqual(lines[:2], ["# Copyright holder", "# BSD license"])
self.assertEqual(lines[2:], ["", "\t", "\t", "\t"])
def test_import_rejects_malformed_and_duplicate_rows(self):
bad = [
"한 漢\n",
"한:漢:first\n한:漢:again\n",
]
for text in bad:
with self.subTest(text=repr(text)):
self.assertEqual(run(IMPORT, self.source(text)).returncode, 1)
def test_groups_readings_and_preserves_order(self):
result = run(
GENERATE,
self.source(
"# Copyright holder\n"
"# BSD license\n"
"\t\n"
"\t\n"
"\t\n"
"\t\n"
"\t\n"
),
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(
result.stdout.splitlines(),
[
";; Copyright holder",
";; BSD license",
"",
"\t漢 韓",
"\t㐀 家",
"\t",
],
)
def test_generator_rejects_bad_rows(self):
bad = [
"漢韓\t\n",
"\t한자\n",
"𠀀\t\n",
"\t\n\t\n",
"漢 한\n",
]
for text in bad:
with self.subTest(text=repr(text)):
self.assertEqual(run(GENERATE, self.source(text)).returncode, 1)
def test_generator_keeps_runtime_candidate_limit(self):
rows = "".join(f"{chr(0x4E00 + n)}\t\n" for n in range(33))
result = run(GENERATE, self.source(rows))
self.assertEqual(result.returncode, 0, result.stderr)
candidates = result.stdout.rstrip().split("\t", 1)[1].split()
self.assertEqual(len(candidates), 32)
self.assertEqual(candidates[-1], chr(0x4E00 + 31))
if __name__ == "__main__":
unittest.main()