Skip to content

Commit e543cf3

Browse files
committed
i18n for tests
1 parent a35deb3 commit e543cf3

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

python/code/wypp/i18n.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,22 @@ def tr(key: str, **kws) -> str:
140140
'Constructor of record `{cls}` does not accept keyword argument `{name}`.':
141141
'Konstruktor des Records `{cls}` akzeptiert kein Schlüsselwort-Argument `{name}`.',
142142

143-
'invalid record definition': 'ungültige Record-Definition'
143+
'invalid record definition': 'ungültige Record-Definition',
144+
145+
'Expected {expected}, but the result is {actual}':
146+
'Erwartet wird {expected}, aber das Ergebnis ist {actual}',
147+
'ERROR: ': 'FEHLER: ',
148+
'ERROR in ': 'FEHLER in ',
149+
'File {filename}, line {lineno}: ': 'Datei {filename}, Zeile {lineno}: ',
150+
'Uncovered case': 'Ein Fall ist nicht abgedeckt',
151+
'uncovered case': 'ein Fall ist nicht abgedeckt',
152+
'The impossible happened!': 'Das Unmögliche ist passiert!',
153+
'Stop of execution': 'Abbruch der Ausführung',
154+
'1 successful test': '1 erfolgreicher Test',
155+
'all succesful': 'alle erfolgreich',
156+
'and stop of execution': 'und Abbruch der Ausführung',
157+
'all successful': 'alle erfolgreich'
158+
144159
}
145160

146161
def expectingNoReturn(cn: location.CallableName) -> str:
@@ -346,3 +361,40 @@ def unknownKeywordArgument(cn: location.CallableName, name: str) -> str:
346361
return tr('Constructor of record `{cls}` does not accept keyword argument `{name}`.',
347362
cls=cls, name=name)
348363
raise ValueError(f'Unexpected: {cn}')
364+
365+
def checkExpected(expected: str, actual: str) -> str:
366+
return tr('Expected {expected}, but the result is {actual}', expected=expected,actual=actual)
367+
368+
def numTests(n: int) -> str:
369+
match getLang():
370+
case 'en':
371+
if n == 0:
372+
return 'no tests'
373+
elif n == 1:
374+
return '1 test'
375+
else:
376+
return f'{n} tests'
377+
case 'de':
378+
if n == 0:
379+
return 'keine Tests'
380+
elif n == 1:
381+
return '1 Test'
382+
else:
383+
return f'{n} Tests'
384+
385+
def numFailing(n: int) -> str:
386+
match getLang():
387+
case 'en':
388+
if n == 0:
389+
return 'no errors'
390+
elif n == 1:
391+
return '1 error'
392+
else:
393+
return f'{n} errors'
394+
case 'de':
395+
if n == 0:
396+
return 'keine Fehler'
397+
elif n == 1:
398+
return '1 Fehler'
399+
else:
400+
return f'{n} Fehler'

python/code/wypp/writeYourProgram.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import location
1010
import paths
1111
import utils
12+
import i18n
1213

1314
_DEBUG = False
1415
def _debug(s):
@@ -147,23 +148,21 @@ def printTestResults(prefix='', loadingFailed=False):
147148
failing = _testCount['failing']
148149
bad = '🙁'
149150
good = '😀'
150-
tests = f'{prefix}{total} Tests'
151-
if total == 1:
152-
tests = f'{prefix}{total} Test'
151+
tests = f'{prefix}' + i18n.numTests(total)
153152
if total == 0:
154153
pass
155154
elif failing == 0:
156155
if loadingFailed:
157-
print(f'{tests}, Abbruch der Ausführung {bad}')
156+
print(f'{tests}, {i18n.tr("Stop of execution")} {bad}')
158157
elif total == 1:
159-
print(f'1 erfolgreicher Test {good}')
158+
print(f'{i18n.tr("1 successful test")} {good}')
160159
else:
161-
print(f'{tests}, alle erfolgreich {good}')
160+
print(f'{tests}, {i18n.tr("all successful")} {good}')
162161
else:
163162
if loadingFailed:
164-
print(f'{tests}, {failing} Fehler und Abbruch der Ausführung {bad}')
163+
print(f'{tests}, {i18n.numFailing(failing)} and stop of execution {bad}')
165164
else:
166-
print(f'{tests}, {failing} Fehler {bad}')
165+
print(f'{tests}, {i18n.numFailing(failing)} {bad}')
167166
return {'total': total, 'failing': failing}
168167

169168
def checkEq(actual, expected):
@@ -197,20 +196,20 @@ def checkGeneric(actual, expected, *, structuralObjEq=True, floatEqWithDelta=Tru
197196
frame = stack[2] if len(stack) > 2 else None
198197
if frame:
199198
filename = paths.canonicalizePath(frame.filename)
200-
caller = f"Datei {filename}, Zeile {frame.lineno}: "
199+
caller = i18n.tr('File {filename}, line {lineno}: ',
200+
filename=filename, lineno=frame.lineno)
201201
else:
202202
caller = ""
203203
def fmt(x):
204204
if type(x) == str:
205205
return repr(x)
206206
else:
207207
return str(x)
208-
msg = f"{caller}Erwartet wird {fmt(expected)}, aber das " \
209-
f"Ergebnis ist {fmt(actual)}"
208+
msg = caller + i18n.checkExpected(fmt(expected), fmt(actual))
210209
if _dieOnCheckFailures():
211210
raise Exception(msg)
212211
else:
213-
print("FEHLER in " + msg)
212+
print(i18n.tr('ERROR in ') + msg)
214213

215214
def checkFail(msg: str):
216215
if not _checksEnabled:
@@ -220,15 +219,17 @@ def checkFail(msg: str):
220219
if _dieOnCheckFailures():
221220
raise Exception(msg)
222221
else:
223-
print("FEHLER: " + msg)
222+
print(i18n.tr('ERROR: ') + msg)
224223

225224
def uncoveredCase():
226225
stack = inspect.stack()
227226
if len(stack) > 1:
228227
caller = stack[1]
229-
raise Exception(f"{caller.filename}, Zeile {caller.lineno}: ein Fall ist nicht abgedeckt")
228+
callerStr = i18n.tr('File {filename}, line {lineno}: ',
229+
filename=caller.filename, lineno=caller.lineno)
230+
raise Exception(callerStr + i18n.tr('uncovered case'))
230231
else:
231-
raise Exception(f"Ein Fall ist nicht abgedeckt")
232+
raise Exception(i18n.tr('Uncovered case'))
232233

233234
#
234235
# Deep equality
@@ -328,7 +329,8 @@ def todo(msg=None):
328329

329330
def impossible(msg=None):
330331
if msg is None:
331-
msg = 'Das Unmögliche ist passiert!'
332+
msg = i18n.tr('The impossible happened!')
333+
'Das Unmögliche ist passiert!'
332334
raise errors.ImpossibleError(msg)
333335

334336
# Additional functions and aliases
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Traceback (most recent call last):
22
File "file-test-data/extras/testImpossible.py", line 3, in <module>
33
impossible()
4-
File "code/wypp/writeYourProgram.py", line 332, in impossible
4+
File "code/wypp/writeYourProgram.py", line 334, in impossible
55
raise errors.ImpossibleError(msg)
66

77
Das Unmögliche ist passiert!
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Traceback (most recent call last):
22
File "file-test-data/extras/testTodo.py", line 3, in <module>
33
todo()
4-
File "code/wypp/writeYourProgram.py", line 327, in todo
4+
File "code/wypp/writeYourProgram.py", line 328, in todo
55
raise errors.TodoError(msg)
66

77
TODO

0 commit comments

Comments
 (0)