-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_meta.py
215 lines (163 loc) · 6.35 KB
/
test_meta.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
import os
import secrets
from zoofc1.lexer import splitSource, tokenize, TT # noqa
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
SOURCE_CODE_DIR = os.path.join(THIS_DIR, "zoofc1")
SNIPPET_DIR = os.path.join(THIS_DIR, "snippets")
class ErrorCode:
def __init__(self, code, filename, linenr):
self.code = code
self.filename = filename
self.linenr = linenr
self.invalid = ""
def collect_error_codes_from_source():
"""Find all error codes in the source."""
error_codes = []
all_codes = set()
for fname in os.listdir(SOURCE_CODE_DIR):
if not fname.endswith((".zf", ".py")):
continue
filename = os.path.join(SOURCE_CODE_DIR, fname)
with open(filename, "rb") as f:
text = f.read().decode()
for linenr, line in enumerate(text.splitlines(), start=1):
i = 0
while True:
i = line.find('"E', i)
if i < 0:
break
i2 = line.find('"', i + 1)
assert i2 > i
s = line[i + 1 : i2]
i = i2
if 3 <= len(s) <= 6 and any(c.isnumeric() for c in s[1:]):
# String looks close enough!
ec = ErrorCode(s, filename, linenr)
error_codes.append(ec)
if not all(c.isnumeric() for c in s[1:]):
ec.invalid = "not numeric"
elif len(s) != 5:
ec.invalid = "incorrect length"
elif s.endswith("000"):
ec.invalid = "is placeholder"
elif s in all_codes:
ec.invalid = "duplicate"
else:
all_codes.add(s)
return error_codes
def collect_error_codes_from_snippets():
"""Find a set of error codes from the snippet outputs."""
all_codes = set()
for fname in os.listdir(SNIPPET_DIR):
if not fname.endswith(".zf"):
continue
filename = os.path.join(SNIPPET_DIR, fname)
with open(filename, "rb") as f:
text = f.read().decode()
i = 0
while True:
i = text.find("Error (E", i)
if i < 0:
break
i2 = text.find(") --", i + 8)
assert i2 > i
s = text[i + 7 : i2]
i = i2
all_codes.add(s)
return all_codes
# %% Tests
def test_error_numbers_are_valid_and_unique():
error_codes = collect_error_codes_from_source()
print(f"Found {len(error_codes)} error codes.")
for ec in error_codes:
if ec.invalid:
print(
f'Invalid ({ec.invalid}): {ec.code} in "{ec.filename}", line {ec.linenr}'
)
assert len(error_codes) > 32
assert not any(ec.invalid for ec in error_codes)
def test_error_codes_appear_in_correct_file():
error_codes = collect_error_codes_from_source()
for ec in error_codes:
firstnum = int(ec.code[1])
fname = os.path.basename(ec.filename)
if firstnum == 1:
assert fname in ("parser.py")
elif firstnum == 2:
assert fname in ("resolver.py", "errors.py")
elif firstnum == 8:
assert fname in ("interpreter.py", "errors.py")
else:
assert False, f"Unknown erorr code prefix {ec.code}"
def test_that_each_error_code_is_in_a_snippet():
excludes = {
"E1361", # Parsing ended before EOF (end of file).
"E1614", # For-expressions will be implemened later.
"E1068", # Cannot have more than 250 arguments.
"E1462", # Cannot have more than 250 parameters.
"E8259", # Unexpected unary operation.
"E8701", # Unexpected binary expression '{optype}'.
"E8092", # Unexpected logical expression '{optype}'.
"E8821", # Unexpected literal: '{expr.token.lexeme}'.
"E8774", # Undefined variable. We already catch this in resolve step
}
error_codes = collect_error_codes_from_source()
source_codes = {ec.code for ec in error_codes if not ec.invalid}
snippet_codes = collect_error_codes_from_snippets()
unknown_codes = snippet_codes.difference(source_codes)
untested_codes = source_codes.difference(snippet_codes)
untested_codes.difference_update(excludes)
if unknown_codes:
print("Unknown codes:", unknown_codes)
if untested_codes:
print("Untested codes:", untested_codes)
assert not unknown_codes
assert not untested_codes
# %% Manual stuff
def zero_out_all_codes():
"""Set all error codes to placeholder codes."""
# Don't make this too easy
res = input(
"Do you really want to nuke all current error codes? Type 'yes' to confirm: "
)
if res != "yes":
print("cancelled")
return
error_codes = collect_error_codes_from_source()
assigned_count = 0
for ec in error_codes:
if not ec.code.endswith("000"):
prefix = ec.code[:2]
new_code = prefix + "000"
assigned_count += 1
_replace_error_code(ec, new_code)
print(f"Zeroed out {assigned_count} error codes.")
def assign_error_codes():
"""Assign a new random error code to each placeholder.
Call this function manually.
"""
error_codes = collect_error_codes_from_source()
all_codes = {ec.code for ec in error_codes if not ec.invalid}
assigned_count = 0
for ec in error_codes:
if ec.code.endswith("000"):
prefix = ec.code[:2]
valid_codes = set([f"{prefix}{i:03d}" for i in range(1, 1000)])
valid_codes.difference_update(all_codes)
new_code = secrets.choice(list(valid_codes))
assigned_count += 1
all_codes.add(new_code)
_replace_error_code(ec, new_code)
print(f"Assigned {assigned_count} error codes.")
def _replace_error_code(ec, new_code):
with open(ec.filename, "rb") as f:
text = f.read().decode()
lines = text.split("\n")
lines[ec.linenr - 1] = lines[ec.linenr - 1].replace(ec.code, new_code)
text = "\n".join(lines)
with open(ec.filename, "wb") as f:
f.write(text.encode())
if __name__ == "__main__":
test_error_numbers_are_valid_and_unique()
test_error_codes_appear_in_correct_file()
test_that_each_error_code_is_in_a_snippet()