12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- from csv import reader
- from difflib import ndiff
- from importlib import reload
- from os import path
- import scriptshifter.tables
- from scriptshifter.trans import transliterate
- TEST_DIR = path.dirname(path.realpath(__file__))
- TEST_DATA_DIR = path.join(TEST_DIR, "data")
- def reload_tables():
- reload(scriptshifter.tables) # Reload new config dir.
- from scriptshifter import tables
- tables.list_tables.cache_clear()
- tables.load_table.cache_clear()
- return tables
- def test_sample(dset):
- """
- Test an individual sample set and produce a human-readable report.
- Used outside of automated tests.
- @param dset (str): sample set name (without the .csv extension) found in
- the `data/script_samples` directory.
- """
- deltas = []
- dset_fpath = path.join(TEST_DATA_DIR, "script_samples", dset + ".csv")
- log_fpath = path.join(TEST_DATA_DIR, f"test_{dset}.log")
- with open(dset_fpath, newline="") as fh:
- csv = reader(fh)
- for row in csv:
- lang, script, rom = row[:3]
- opts = row[3] if len(row) > 3 and row[3] else {}
- trans, warnings = transliterate(script, lang, "s2r", opts)
- if (trans == rom):
- print(".", end="")
- else:
- print("F", end="")
- deltas.append((script, ndiff([trans], [rom])))
- with open(log_fpath, "w") as fh:
- # If no deltas, just truncate the file.
- for script, delta in deltas:
- fh.write(f"Original: {script}\n")
- for dline in delta:
- fh.write(dline.strip() + "\n")
- fh.write("\n\n")
- ct = len(deltas)
- if ct > 0:
- print(f"{ct} failed tests. See report at {log_fpath}")
- else:
- print("All tests passed.")
|