-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
81 lines (63 loc) · 2.37 KB
/
test.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
from dataclasses import dataclass
import os
import subprocess
import tempfile
import unittest
write_golden_files = False
@dataclass
class SampleFile:
base: str
input_path: str
output_path: str
class TestEvaluator(unittest.TestCase):
excluded_programs = {
# Infinite programs
"default",
"wave",
}
def sample_programs(self):
program_dir = os.path.join(os.path.dirname(__file__), "test_programs")
samples = []
for filename in os.listdir(program_dir):
base, ext = os.path.splitext(filename)
if base in self.excluded_programs:
continue
if ext != ".art":
continue
input_path = os.path.join(program_dir, filename)
stdout_path = os.path.join(program_dir, base + ".output")
samples.append(SampleFile(base, input_path, stdout_path))
return samples
def execute(self, filename):
eval_path = os.path.join(os.path.dirname(__file__), "main.py")
with open(filename) as inp:
text = inp.read()
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as src:
src.write(text)
# Since we're not closing the file, we need to seek back to the
# start before we try reading it again.
src.seek(0)
capture = subprocess.run(
["python", eval_path, "--tick=0", "--no-clear", src.name],
capture_output=True,
text=True,
)
return capture.stdout
@unittest.skipIf(write_golden_files, "writing golden files")
def test_sample_programs(self):
for program in self.sample_programs():
with self.subTest(program.base):
actual = self.execute(program.input_path)
with open(program.output_path) as out:
expected = out.read()
self.assertEqual(actual, expected)
@unittest.skipIf(not write_golden_files, "not writing golden files")
def test_write_golden_files(self):
for program in self.sample_programs():
print(program.base)
with self.subTest(program.base):
stdout = self.execute(program.input_path)
with open(program.output_path, "w") as out:
out.write(stdout)
if __name__ == "__main__":
unittest.main()