test02_transliteration.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import logging
  2. from unittest import TestCase, TestSuite, TextTestRunner
  3. from csv import reader
  4. from os import environ, path
  5. from tests import TEST_DATA_DIR, reload_tables
  6. from scriptshifter.trans import transliterate
  7. import scriptshifter.tables
  8. logger = logging.getLogger(__name__)
  9. class TestTrans(TestCase):
  10. """
  11. Test S2R transliteration.
  12. Modified test case class to run independent tests for each CSV row.
  13. TODO use a comprehensive sample table and report errors for unsupported
  14. languages.
  15. """
  16. maxDiff = None
  17. def sample_s2r(self):
  18. """
  19. Test S2R transliteration for one CSV sample.
  20. This function name won't start with `test_` otherwise will be
  21. automatically run without parameters.
  22. """
  23. config = scriptshifter.tables.load_table(self.tbl)
  24. if "script_to_roman" in config:
  25. txl = transliterate(self.script, self.tbl)
  26. self.assertEqual(
  27. txl, self.roman,
  28. f"S2R transliteration error for {self.tbl}!")
  29. def sample_r2s(self):
  30. """
  31. Test R2S transliteration for one CSV sample.
  32. This function name won't start with `test_` otherwise will be
  33. automatically run without parameters.
  34. """
  35. config = scriptshifter.tables.load_table(self.tbl)
  36. if "roman_to_script" in config:
  37. txl = transliterate(self.roman, self.tbl, r2s=True)
  38. self.assertEqual(
  39. txl, self.script,
  40. f"R2S transliteration error for {self.tbl}!")
  41. def make_suite():
  42. """
  43. Build parametrized test cases.
  44. """
  45. if "TXL_CONFIG_TABLE_DIR" in environ:
  46. del environ["TXL_CONFIG_TABLE_DIR"]
  47. reload_tables()
  48. suite = TestSuite()
  49. with open(
  50. path.join(TEST_DATA_DIR, "sample_strings.csv"),
  51. newline="") as fh:
  52. csv = reader(fh)
  53. csv.__next__() # Discard header row.
  54. for row in csv:
  55. if len(row[2]):
  56. # Inject transliteration info in the test case.
  57. for tname in ("sample_s2r", "sample_r2s"):
  58. tcase = TestTrans(tname)
  59. tcase.tbl = row[2]
  60. tcase.script = row[3].strip()
  61. tcase.roman = row[4].strip()
  62. suite.addTest(tcase)
  63. return suite
  64. TextTestRunner().run(make_suite())