test02_transliteration.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. def sample_s2r(self):
  17. """
  18. Test S2R transliteration for one CSV sample.
  19. This function name won't start with `test_` otherwise will be
  20. automatically run without parameters.
  21. """
  22. config = scriptshifter.tables.load_table(self.tbl)
  23. if "script_to_roman" in config:
  24. txl = transliterate(self.script, self.tbl)
  25. self.assertEqual(
  26. txl, self.roman,
  27. f"S2R transliteration error for {self.tbl}!")
  28. def sample_r2s(self):
  29. """
  30. Test R2S transliteration for one CSV sample.
  31. This function name won't start with `test_` otherwise will be
  32. automatically run without parameters.
  33. """
  34. config = scriptshifter.tables.load_table(self.tbl)
  35. if "roman_to_script" in config:
  36. txl = transliterate(self.roman, self.tbl, r2s=True)
  37. self.assertEqual(
  38. txl, self.script,
  39. f"R2S transliteration error for {self.tbl}!")
  40. def make_suite():
  41. """
  42. Build parametrized test cases.
  43. """
  44. if "TXL_CONFIG_TABLE_DIR" in environ:
  45. del environ["TXL_CONFIG_TABLE_DIR"]
  46. reload_tables()
  47. suite = TestSuite()
  48. with open(
  49. path.join(TEST_DATA_DIR, "sample_strings.csv"),
  50. newline="") as fh:
  51. csv = reader(fh)
  52. csv.__next__() # Discard header row.
  53. for row in csv:
  54. if len(row[2]):
  55. # Inject transliteration info in the test case.
  56. for tname in ("sample_s2r", "sample_r2s"):
  57. tcase = TestTrans(tname)
  58. tcase.tbl = row[2]
  59. tcase.script = row[3]
  60. tcase.roman = row[4]
  61. suite.addTest(tcase)
  62. return suite
  63. TextTestRunner().run(make_suite())