-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
58 lines (44 loc) · 1.65 KB
/
Copy pathrun_tests.py
File metadata and controls
58 lines (44 loc) · 1.65 KB
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
"""Run all the file comparison tests"""
import difflib
import os
import os.path
import partial_pp
import re
import sys
def get_test_inputs(tests_dir):
"""Return the list of input files for testing in tests_dir"""
match_inputs = re.compile('.+\\.c$')
return [f for f in os.listdir(tests_dir) if match_inputs.match(f)]
def get_defines_and_undefines_from_input(input):
"""Takes the input file and extracts the preprocessor definitions that it
should be tested with.
"""
first_line = input.split('\n', 1)[0]
cmd_args = re.match('// RUN: (.+)$', first_line).group(1).split(' ')
return partial_pp.parse_args(cmd_args)
def run_test(test_path):
"""Runs the preprocessor on the file and checks for correctness."""
with open(test_path, 'r') as input_file:
input = input_file.read().rstrip()
with open(test_path+'.out', 'r') as output_file:
reference = output_file.read().rstrip()
(defines, undefines) = get_defines_and_undefines_from_input(input)
output = partial_pp.process(input, defines, undefines)
if reference != output:
print('[!!!] {} failed:'.format(test_path))
print('[!!!] Input:')
print(input)
print('[!!!] Output:')
print(output)
print('[!!!] Diff:')
diff = difflib.ndiff(reference.splitlines(1), output.splitlines(1))
print(''.join(diff))
else:
print('[===] {} succeeded.'.format(test_path))
def main(argv):
test_dir = os.path.join(os.getcwd(), 'tests')
for file in get_test_inputs(test_dir):
run_test(os.path.join('tests/', file))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))