test02_transliteration.py 2.2 KB

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