فهرست منبع

Fix transliteration tests.

scossu 2 ماه پیش
والد
کامیت
959433870a
2فایلهای تغییر یافته به همراه38 افزوده شده و 78 حذف شده
  1. 30 19
      test/integration.py
  2. 8 59
      test/unittest/test02_transliteration.py

+ 30 - 19
test/integration.py

@@ -10,7 +10,7 @@ from test import TEST_DATA_DIR
 logger = getLogger(__name__)
 
 
-def test_sample(dset):
+def test_sample(dset, report=True):
     """
     Test an individual sample set and produce a human-readable report.
 
@@ -18,8 +18,12 @@ def test_sample(dset):
 
     @param dset (str): sample set name (without the .csv extension) found in
     the `data/script_samples` directory.
+
+    @param report (bool): if True (the default), print fail/success ticks and
+    write out a report to file at the end. Otherwise, raise an exception on
+    the first error encountered.
     """
-    deltas = []
+    deltas = [] if report else None
     dset_fpath = path.join(TEST_DATA_DIR, "script_samples", dset + ".csv")
     log_fpath = path.join(TEST_DATA_DIR, "log", f"test_{dset}.log")
 
@@ -41,20 +45,21 @@ def test_sample(dset):
                 _trans(rom, lang, "r2s", opts, script, deltas)
             i += 1
 
-    with open(log_fpath, "w") as fh:
-        # If no deltas, just truncate the file.
-        for lang, script, delta in deltas:
-            fh.write(f"Language: {lang}\n")
-            fh.write(f"Original: {script}\nDiff (result vs. expected):\n")
-            for dline in delta:
-                fh.write(dline.strip() + "\n")
-            fh.write("\n\n")
+    if report:
+        with open(log_fpath, "w") as fh:
+            # If no deltas, just truncate the file.
+            for lang, script, delta in deltas:
+                fh.write(f"Language: {lang}\n")
+                fh.write(f"Original: {script}\nDiff (result vs. expected):\n")
+                for dline in delta:
+                    fh.write(dline.strip() + "\n")
+                fh.write("\n\n")
 
-    ct = len(deltas)
-    if ct > 0:
-        print(f"\n\n{ct} failed tests. See report at {log_fpath}")
-    else:
-        print("All tests passed.")
+        ct = len(deltas)
+        if ct > 0:
+            print(f"\n\n{ct} failed tests. See report at {log_fpath}")
+        else:
+            print("All tests passed.")
 
 
 def _trans(script, lang, t_dir, opts, rom, deltas):
@@ -62,8 +67,14 @@ def _trans(script, lang, t_dir, opts, rom, deltas):
     trans, warnings = transliterate(
             script, lang, t_dir=t_dir,
             capitalize=opts.get("capitalize"), options=opts)
-    if (trans == rom):
-        print(".", end="")
+    try:
+        assert trans == rom
+    except AssertionError as e:
+        if deltas is not None:
+            print("F", end="")
+            deltas.append((lang, script, ndiff([trans], [rom])))
+        else:
+            raise e
     else:
-        print("F", end="")
-        deltas.append((lang, script, ndiff([trans], [rom])))
+        if deltas:
+            print(".", end="")

+ 8 - 59
test/unittest/test02_transliteration.py

@@ -1,13 +1,10 @@
 import logging
 
-from unittest import TestCase, TestSuite, TextTestRunner
-from csv import reader
-from json import loads as jloads
-from os import environ, path, unlink
+from unittest import TestCase
+from os import environ, unlink
 
-from scriptshifter.trans import transliterate
-from scriptshifter.tables import get_language, init_db
-from test import TEST_DATA_DIR
+from scriptshifter.tables import init_db
+from test.integration import test_sample
 
 
 logger = logging.getLogger(__name__)
@@ -23,58 +20,10 @@ def tearDownModule():
 
 class TestTrans(TestCase):
     """
-    Test S2R transliteration.
+    Test transliteration.
 
-    Modified test case class to run independent tests for each CSV row.
-
-    TODO use a comprehensive sample table and report errors for unsupported
-    languages.
-    """
-    def sample(self):
-        """
-        Test transliteration for one CSV row.
-
-        This function name won't start with `test_` otherwise will be
-        automatically run without parameters.
-        """
-        config = get_language(self.tbl)
-        t_dir = self.options.get("t_dir", "s2r")
-        if (
-                t_dir == "s2r" and config["has_s2r"]
-                or t_dir == "r2s" and config["has_r2s"]):
-            txl = transliterate(
-                    self.script, self.tbl,
-                    t_dir=t_dir,
-                    capitalize=self.options.get("capitalize", False),
-                    options=self.options)[0]
-            self.assertEqual(
-                    txl, self.roman,
-                    f"S2R transliteration error for {self.tbl}!\n"
-                    f"Original: {self.script}")
-
-
-def make_suite():
+    Use "unittest" sample table.
     """
-    Build parametrized test cases.
-    """
-    suite = TestSuite()
-
-    with open(path.join(
-        TEST_DATA_DIR, "script_samples", "unittest.csv"
-    ), newline="") as fh:
-        csv = reader(fh)
-        for row in csv:
-            if len(row[0]):
-                # Inject transliteration info in the test case.
-                tcase = TestTrans("sample")
-                tcase.tbl = row[0]
-                tcase.script = row[1].strip()
-                tcase.roman = row[2].strip()
-                tcase.options = jloads(row[3]) if len(row[3]) else {}
-
-                suite.addTest(tcase)
-
-    return suite
-
 
-TextTestRunner().run(make_suite())
+    def test_integration_sample(self):
+        test_sample("unittest", False)