1010logger = getLogger (__name__ )
1111
1212
13- def test_sample (dset ):
13+ def test_sample (dset , report = True ):
1414 """
1515 Test an individual sample set and produce a human-readable report.
1616
1717 Used outside of automated tests.
1818
1919 @param dset (str): sample set name (without the .csv extension) found in
2020 the `data/script_samples` directory.
21+
22+ @param report (bool): if True (the default), print fail/success ticks and
23+ write out a report to file at the end. Otherwise, raise an exception on
24+ the first error encountered.
2125 """
22- deltas = []
26+ deltas = [] if report else None
2327 dset_fpath = path .join (TEST_DATA_DIR , "script_samples" , dset + ".csv" )
2428 log_fpath = path .join (TEST_DATA_DIR , "log" , f"test_{ dset } .log" )
2529
@@ -41,29 +45,36 @@ def test_sample(dset):
4145 _trans (rom , lang , "r2s" , opts , script , deltas )
4246 i += 1
4347
44- with open (log_fpath , "w" ) as fh :
45- # If no deltas, just truncate the file.
46- for lang , script , delta in deltas :
47- fh .write (f"Language: { lang } \n " )
48- fh .write (f"Original: { script } \n Diff (result vs. expected):\n " )
49- for dline in delta :
50- fh .write (dline .strip () + "\n " )
51- fh .write ("\n \n " )
48+ if report :
49+ with open (log_fpath , "w" ) as fh :
50+ # If no deltas, just truncate the file.
51+ for lang , script , delta in deltas :
52+ fh .write (f"Language: { lang } \n " )
53+ fh .write (f"Original: { script } \n Diff (result vs. expected):\n " )
54+ for dline in delta :
55+ fh .write (dline .strip () + "\n " )
56+ fh .write ("\n \n " )
5257
53- ct = len (deltas )
54- if ct > 0 :
55- print (f"\n \n { ct } failed tests. See report at { log_fpath } " )
56- else :
57- print ("All tests passed." )
58+ ct = len (deltas )
59+ if ct > 0 :
60+ print (f"\n \n { ct } failed tests. See report at { log_fpath } " )
61+ else :
62+ print ("All tests passed." )
5863
5964
6065def _trans (script , lang , t_dir , opts , rom , deltas ):
6166 logger .debug (f"Transliterating { lang } : { t_dir } " )
6267 trans , warnings = transliterate (
6368 script , lang , t_dir = t_dir ,
6469 capitalize = opts .get ("capitalize" ), options = opts )
65- if (trans == rom ):
66- print ("." , end = "" )
70+ try :
71+ assert trans == rom
72+ except AssertionError as e :
73+ if deltas is not None :
74+ print ("F" , end = "" )
75+ deltas .append ((lang , script , ndiff ([trans ], [rom ])))
76+ else :
77+ raise e
6778 else :
68- print ( "F" , end = "" )
69- deltas . append (( lang , script , ndiff ([ trans ], [ rom ])) )
79+ if deltas :
80+ print ( "." , end = "" )
0 commit comments