__init__.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from csv import reader
  2. from difflib import ndiff
  3. from importlib import reload
  4. from os import path
  5. import scriptshifter.tables
  6. from scriptshifter.trans import transliterate
  7. TEST_DIR = path.dirname(path.realpath(__file__))
  8. TEST_DATA_DIR = path.join(TEST_DIR, "data")
  9. def reload_tables():
  10. reload(scriptshifter.tables) # Reload new config dir.
  11. from scriptshifter import tables
  12. tables.list_tables.cache_clear()
  13. tables.load_table.cache_clear()
  14. return tables
  15. def test_sample(dset):
  16. """
  17. Test an individual sample set and produce a human-readable report.
  18. Used outside of automated tests.
  19. @param dset (str): sample set name (without the .csv extension) found in
  20. the `data/script_samples` directory.
  21. """
  22. deltas = []
  23. dset_fpath = path.join(TEST_DATA_DIR, "script_samples", dset + ".csv")
  24. log_fpath = path.join(TEST_DATA_DIR, f"test_{dset}.log")
  25. with open(dset_fpath, newline="") as fh:
  26. csv = reader(fh)
  27. for row in csv:
  28. lang, script, rom = row[:3]
  29. opts = row[3] if len(row) > 3 and row[3] else {}
  30. trans, warnings = transliterate(script, lang, "s2r", opts)
  31. if (trans == rom):
  32. print(".", end="")
  33. else:
  34. print("F", end="")
  35. deltas.append((script, ndiff([trans], [rom])))
  36. with open(log_fpath, "w") as fh:
  37. # If no deltas, just truncate the file.
  38. for script, delta in deltas:
  39. fh.write(f"Original: {script}\n")
  40. for dline in delta:
  41. fh.write(dline.strip() + "\n")
  42. fh.write("\n\n")
  43. ct = len(deltas)
  44. if ct > 0:
  45. print(f"{ct} failed tests. See report at {log_fpath}")
  46. else:
  47. print("All tests passed.")