-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
205 lines (163 loc) · 5.53 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
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
#!/bin/python3
"""
This module contains the test functions for the luvascript compiler
"""
import os
import sys
import glob
import subprocess
TEST_DIR = "./tests/"
# format: ['test-name', ['test-output', ...], exitcode]
EXPECTED_OUTPUTS: list = [
["Test 1", [], 55],
["Test 2", [], 26],
["Test 3", [], 14],
["Test 4", [], 5],
["Test 5", [], 69],
["Test 6", [], 18],
["Test 7", [], 12],
["Test 8", [], 3],
["Test 9", [], 50],
["Test 10", [], 3],
["Test 11", [], 40],
["Test 12", [], 1],
["Test 13", [], 32],
["Test 14", [], 8],
["Test 15", [], 12],
["Test 16", [], 13],
["Test 17", [], 0],
["Test 18", [], 1],
["Test 19", [], 1],
["Test 20", [], 0],
["Test 21", [], 1],
["Test 22", [], 1],
["Test 23", [], 20],
["Test 24", [], 10],
["Test 25", [], 8],
["Test 26", [], 55],
["Test 27", [], 22],
["Test 28", [], 97],
["Test 29", [], 63],
["Test 30", [], ord(',')],
["Test 31", [], 12],
["Test 32", ["Hello, World!"], 14],
["Test 33", [], 22],
["Test 34", [], 3],
["Test 35", [], 20],
["Test 36", [], 15],
["Test 37", [], ord(',')],
["Test 38", [], ord('s')]]
# ------------------------------------------------------------------------
def main():
'''main function'''
# build debug version of the luvascript compiler
build_debug()
# change working directory to test directory
os.chdir(TEST_DIR)
# parse command line args
files, options = parse_args()
index = 0
successfull_tests = 0
failed_builds = 0
failed_tests = 0
for file in files:
# built the test file
if not build_test(file, options):
failed_builds += 1
index += 1
continue
#execute the generated file
if run_test('./a.out', EXPECTED_OUTPUTS[index]):
successfull_tests += 1
else:
failed_tests += 1
index += 1
# print summary
print('Stats:')
print(' - successfull tests: ', colored(0, 255, 0, successfull_tests))
print(' - failed tests: ', colored(255, 0, 0, failed_tests)
if failed_tests != 0 else colored(0, 255, 0, failed_tests))
print(' - failed builds: ', colored(255, 0, 0, failed_builds)
if failed_builds != 0 else colored(0, 255, 0, failed_builds))
print()
if failed_tests == 0 and failed_builds == 0:
print('[', colored(0, 255, 0, 'All tests finshed successfully'), ']')
print()
else:
sys.exit(1)
# ------------------------------------------------------------------------
def parse_args():
'''
Parsing the command line arguments
returns a list of files to build/run + a list of options
'''
all_test_files = sorted(filter(os.path.isfile, glob.glob('./*.lv')))
if len(sys.argv) > 1:
# look for options
options = []
files = []
for arg in sys.argv[1:]:
if arg.startswith('-'):
options.append(arg)
elif arg in all_test_files:
files.append(arg)
else:
print(colored(255, 0, 0, f'{arg} is not a valid test file'))
sys.exit(1)
return files, options
return all_test_files, []
# ------------------------------------------------------------------------
def build_debug():
"""builds the debug version"""
print()
print(colored(0, 255, 0, 'Building debug version...'))
subprocess.run(['make', 'debug'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
# ------------------------------------------------------------------------
def build_test(file, options):
"""builds the test file"""
# build the test file
compile_proc = subprocess.Popen(
['../bin/debug/lvc', file],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = compile_proc.communicate()
if compile_proc.returncode != 0 or not os.path.exists('./a.out'):
# the build failed
# print error message
build_failed_str = colored(255, 0, 0, 'build failed')
stdout_msg = stdout.decode() if stdout else ''
print(f"{file}: {build_failed_str}")
print(f'{file}: build output:\n{stdout_msg}')
# print error if stderr is not empty
if stderr is not None and stderr.decode() != '':
print(f'stderr:\n{stderr.decode()}')
return False
if stdout and ('--verbose' in options or '-v' in options):
print(f'{file}: build output:\n{stdout.decode()}')
return True
# ------------------------------------------------------------------------
def run_test(file, expected):
"""runs the test file"""
result = subprocess.run([file], capture_output=True, text=True, check=False)
# check if test was successful or failed
if result.returncode == expected[2]:
print(
f'{expected[0]}:',
colored(0, 255, 0, 'test finished successfully'))
return True
else:
print(
f'{expected[0]}:',
colored(255, 0, 0, 'test failed'))
print(
f"\texpected exitcode \'{expected[2]}\'"
f" but got \'{result.returncode}\' instead")
print()
return False
# ------------------------------------------------------------------------
def colored(red: int, green: int, blue: int, text: str):
"""colors the text with the given RGB values"""
return f'\033[38;2;{red};{green};{blue}m{text}\033[0m'
# ------------------------------------------------------------------------
if __name__ == '__main__':
main()