-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.py
executable file
·337 lines (263 loc) · 10.4 KB
/
run_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python3
import sys
import re
from pathlib import Path
from collections import namedtuple
from itertools import zip_longest
import subprocess
PYLOX_EXE = "./pylox/lox"
CLOX_EXE = "./clox/build/clox"
EXPECTED_OUTPUT_PATTERN = re.compile(r"// expect: ?(.*)")
EXPECTED_ERROR_PATTERN = re.compile(r"// (Error.*)")
ERROR_LINE_PATTERN = re.compile(r"// \[((java|c) )?line (\d+)\] (Error.*)")
EXPECTED_RUNTIME_ERROR_PATTERN = re.compile(r"// expect runtime error: (.+)")
SYNTAX_ERROR_PATTERN = re.compile(r"\[.*line (\d+)\] (Error.+)")
STACK_TRACE_PATTERN = re.compile(r"\[line (\d+)\]")
NONTEST_PATTERN = re.compile(r"// nontest")
_n_passed = 0
_n_failed = 0
_n_skipped = 0
_expectations = 0
Suite = namedtuple("Suite", ["name", "language", "executable", "tests"])
_suite = None # Current suite
_all_suites = {}
_c_suites = []
_py_suites = []
class term:
'''Rudimentary ANSI terminal supporter'''
@staticmethod
def red(text):
return f"\033[31m{text}\033[0m"
@staticmethod
def green(text):
return f"\033[92m{text}\033[0m"
@staticmethod
def gray(text):
return f"\033[30;1m{text}\033[0m"
@staticmethod
def pink(text):
return f"\033[31;1m{text}\033[0m"
@staticmethod
def yellow(text):
return f"\033[33;1m{text}\033[0m"
@staticmethod
def clear_line():
print("\033[1000D\033[0K", end='')
@staticmethod
def update_line(text):
print("\033[1000D\033[0K" + text, end='', flush=True)
ExpectedOutput = namedtuple("ExpectedOutput", ["line", "output"])
class Test:
def __init__(self, path):
self.path = path
self._expected_output = []
self._expected_errors = set()
self._expected_runtime_error = ""
self._runtime_error_line = 0
self._expected_exit_code = 0
self._failures = []
def parse(self) -> bool:
global _suite, _n_skipped, _expectations
state = None # "pass" or "skip"
parts = str(self.path).split("/")
subpart = ""
for part in parts:
if len(subpart):
subpart += "/"
subpart += part
if subpart in _suite.tests:
state = _suite.tests[subpart]
if not state:
raise RuntimeError(f"Unknown state for {self.path}")
elif state == "skip":
_n_skipped += 1
return False
with open(self.path, "r", encoding="utf-8") as f:
for i, line in enumerate(f.readlines(), start=1):
if match := NONTEST_PATTERN.search(line):
return False
if match := EXPECTED_OUTPUT_PATTERN.search(line):
self._expected_output.append(ExpectedOutput(i, match[1]))
_expectations += 1
continue
if match := EXPECTED_ERROR_PATTERN.search(line):
self._expected_errors.add(f"[line {i}] {match[1]}")
self._expected_exit_code = 65
_expectations += 1
continue
if match := ERROR_LINE_PATTERN.search(line):
language = match[2]
if not language or language == _suite.language:
self._expected_errors.add(f"[line {match[3]}] {match[4]}")
self._expected_exit_code = 65
_expectations += 1
continue
if match := EXPECTED_RUNTIME_ERROR_PATTERN.search(line):
self._runtime_error_line = i
self._expected_runtime_error = match[1]
self._expected_exit_code = 70
_expectations += 1
if self._expected_errors and self._expected_runtime_error:
print(f"{term.pink('TEST ERROR')} {self.path}")
print(f" Cannot expect both compile and runtime error.")
print(f"")
return False
return True
def run(self) -> list[str]:
global _suite
result = subprocess.run([_suite.executable, self.path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output_lines = result.stdout.decode("utf-8").split("\n")
error_lines = result.stderr.decode("utf-8").split("\n")
if self._expected_runtime_error:
self._validate_runtime_error(error_lines)
else:
self._validate_compile_error(error_lines)
self._validate_exit_code(result.returncode, error_lines)
self._validate_output(output_lines)
return self._failures
def _validate_runtime_error(self, error_lines):
if len(error_lines) < 2:
self._fail(f"Expected runtime error {self._expected_runtime_error} and got none.")
if error_lines[0] != self._expected_runtime_error:
self._fail(f"Expected runtime error {self._expected_runtime_error} and got:")
self._fail(error_lines[0])
stack_lines = error_lines[1:]
matches = (STACK_TRACE_PATTERN.search(l) for l in stack_lines)
match = next((m for m in matches if m), None)
if not match:
self._fail("Expected stack trace and got: ", stack_lines)
else:
stack_line = int(match[1])
if stack_line != self._runtime_error_line:
self._fail(f"Expected runtime error on line " \
f"{self._runtime_error_line} but was on line " \
f"{stack_line}.")
def _validate_compile_error(self, error_lines):
if len(error_lines) and not error_lines[-1]:
error_lines.pop()
errors_found = set()
unexpected_count = 0
for line in error_lines:
if match := SYNTAX_ERROR_PATTERN.search(line):
error = f"[line {match[1]}] {match[2]}"
if error in self._expected_errors:
errors_found.add(error)
else:
if unexpected_count < 10:
self._fail("Unexpected error:")
self._fail(line)
unexpected_count += 1
elif len(line):
if unexpected_count < 10:
self._fail("Unexpected error:")
self._fail(line)
unexpected_count += 1
if unexpected_count > 10:
self._fail(f"(truncated {unexpected_count - 1} more...)")
for error in self._expected_errors.difference(errors_found):
self._fail(f"Missing expected error: {error}")
def _validate_output(self, output_lines):
if len(output_lines) and output_lines[-1] == "":
output_lines.pop()
for output, expected in zip_longest(output_lines, self._expected_output,
fillvalue=None):
if expected is None:
self._fail(f"Got output {output} when none was expected.")
elif output is None:
self._fail(f"Expected output {expected.output} on line " \
f"{expected.line}.")
elif expected.output != output:
self._fail(f"Expected output {expected.output} on line " \
f"{expected.line} and got {output}")
def _validate_exit_code(self, exit_code, error_lines):
if exit_code == self._expected_exit_code:
return
if len(error_lines) > 10:
error_lines = error_lines[:10]
error_lines.append("(truncated...)")
self._fail(f"Expected return code {self._expected_exit_code} Stderr:",
error_lines)
def _fail(self, message, lines=None):
self._failures.append(message)
if lines:
self._failures.extend(lines)
def run_test(path: Path | str):
global _n_passed, _n_failed, _n_skipped
if "benchmark" in str(path):
return
# Update status line
term.update_line(f"Passed: {term.green(_n_passed)} " \
f"Failed: {term.red(_n_failed)} " \
f"Skipped: {term.yellow(_n_skipped)} " \
f"{term.gray(path)}")
test = Test(path)
if not test.parse():
return
failures = test.run()
if not failures:
_n_passed += 1
else:
_n_failed += 1
term.clear_line()
print(f"{term.red('FAIL')} {path}")
for failure in failures:
print(f" {term.pink(failure)}")
print("")
def run_suite(name: str) -> bool:
global _suite, _all_suites, _n_passed, _n_failed, _n_skipped, _expectations
_suite = _all_suites[name]
_n_passed = 0
_n_failed = 0
_n_skipped = 0
_expectations = 0
if not Path(_suite.executable).exists():
raise ValueError(f"Executable {_suite.executable} does not exist!")
for file_ in Path("./tests").rglob("*.lox"):
run_test(file_)
term.clear_line()
if _n_failed == 0:
print(f"All {term.green(_n_passed)} tests passed ({_expectations} expectations).")
else:
print(f"{term.green(_n_passed)} tests passed. {term.red(_n_failed)} tests failed.")
return _n_failed == 0
def run_suites(names: list[str]) -> bool:
def _run(name: str):
global run_suite
print(f"=== {name} ===")
return run_suite(name)
all_success = all(_run(name) for name in names)
if not all_success:
sys.exit(1)
def _define_test_suites():
def c_suite(name: str, tests: dict[str, str]):
global _all_suites, _c_suite
_all_suites[name] = Suite(
name, language="c", executable=CLOX_EXE, tests=tests)
_c_suites.append(name)
def py_suite(name: str, tests: dict[str, str]):
global _all_suites, _py_suites
_all_suites[name] = Suite(
name, language="java", # pylox is essentially jlox
executable=PYLOX_EXE, tests=tests)
_py_suites.append(name)
all = { "tests": "pass" }
early_chapters = {
"tests/scanning": "skip",
"tests/expressions": "skip",
}
no_limits = { "tests/limit": "skip" }
py_suite("pylox", all | early_chapters | no_limits)
c_suite("clox", all | early_chapters)
def main(args):
global _suite
_define_test_suites()
if len(args) > 2:
sys.exit(f"Usage: {sys.argv[0]} suite")
elif len(args) == 2:
run_suite(args[1])
else:
run_suite("pylox")
if __name__ == "__main__":
main(sys.argv)