-
Notifications
You must be signed in to change notification settings - Fork 5
/
test-deduce.py
137 lines (117 loc) · 4.85 KB
/
test-deduce.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
import os
import sys
parsers = ['--recursive-descent', '--lalr']
lib_dir = './lib'
pass_dir = './test/should-pass'
error_dir = './test/should-error'
def test_deduce(parsers, deduce_call, path, expected_return = 0, extra_arguments=""):
deduce_call += ' ' + path
for parser in parsers:
call = deduce_call + ' ' + parser + ' ' + extra_arguments #+ ' --traceback'
print('Testing:', call)
return_code = os.system(call) // 256 # Why does it multiply the return code by 256???
if return_code != expected_return:
if expected_return == 0:
print('\nTest failed!')
else:
print('\nDeduce failed to catch an error!')
exit(1)
def generate_deduce_errors(deduce_call, path):
# We don't pass in the --error flag so we can generate error messages
# However, that means we can't levarage deduces already existed directory stuff
# So we manually do it here
if os.path.isfile(path):
test_deduce(['--recursive-descent'], deduce_call, path, 1, '> ' + path + '.err')
elif os.path.isdir(path):
if path[-1] != '/' or path[-1] != '\\': # Windows moment
path += '/'
for file in os.listdir(path):
if os.path.isfile(path + file):
if file[-3:] == '.pf':
generate_deduce_errors(deduce_call, path + file)
elif os.path.isdir(path + file):
# TODO: recursive directories
pass
else:
print(path, 'was not found!')
exit(1)
def test_deduce_errors(deduce_call, path):
if os.path.isfile(path):
if not os.path.isfile(path + '.err'):
print("Couldn't find an expected error for", path)
print("Did you mean to generate it? If so, use generate_deduce_errors")
exit(1)
temp_file = './actual_error.tmp'
test_deduce(['--recursive-descent'], deduce_call, path + ' > ' + temp_file, 1)
ret_code = os.system('diff --ignore-space-change ' + path + '.err ' + temp_file)
if ret_code == 0:
os.remove(temp_file)
else:
print("The error message for", path, "has changed! See actual_error.tmp")
exit(1)
else:
if path[-1] != '/' or path[-1] != '\\': # Windows moment
path += '/'
for file in os.listdir(path):
if os.path.isfile(path + file):
if file[-3:] == '.pf':
test_deduce_errors(deduce_call, path + file)
elif os.path.isdir(path + file):
# TODO: recursive directories?
pass
if __name__ == "__main__":
# Check command line arguments
extra_arguments = []
regenerables = []
generate_errors = False
test_lib = False
test_passable = False
test_errors = False
already_processed_next = False
for i in range(1, len(sys.argv)):
if already_processed_next:
already_processed_next = False
continue
argument = sys.argv[i]
if argument == '--regenerate-errors':
generate_errors = True
elif argument == '--generate-error':
regenerables.append(sys.argv[i + 1])
already_processed_next = True
elif argument == '--lib':
test_lib = True
elif argument == '--passable':
test_passable = True
elif argument == '--errors':
test_errors = True
else:
extra_arguments.append(argument)
python_path = ""
for i in range(14, 10, -1):
python_path = os.popen("command -v python3." + str(i)).read()[0: -1] # strip the newline character with the splicing
if python_path != "" and os.system(python_path + " -m pip list | grep lark > /dev/null") == 0:
break
if python_path == "":
print("Could not find a python version at or above 3.11 with lark installed")
exit(1)
deduce_call = python_path + " ./deduce.py " + " --dir " + lib_dir + " " + " ".join(extra_arguments)
if generate_errors:
print('Regenerating ALL errors')
generate_deduce_errors(deduce_call, error_dir)
else:
for generable in regenerables:
print('Generating error for:', generable)
generate_deduce_errors(deduce_call, generable)
generate_errors = True # So we don't run ALL tests
if test_lib:
test_deduce(parsers, deduce_call, lib_dir)
if test_passable:
test_deduce(parsers, deduce_call, pass_dir)
if test_errors:
test_deduce_errors(deduce_call, error_dir)
if not (test_lib or test_passable or test_errors or generate_errors):
test_deduce(parsers, deduce_call, lib_dir)
test_deduce(parsers, deduce_call, pass_dir)
test_deduce_errors(deduce_call, error_dir)
os.system("rm -f ./lib/*.thm")
os.system("rm -f ./test/should-pass/*.thm")