-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
124 lines (101 loc) · 3.36 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
"""
Script to test Yai agains the test cases present in folder "tests"
"""
import os
import subprocess
def print_in_color(color):
cmd = {'red':'\033[91m', 'green':'\033[92m', 'cyan':'\033[96m'}
def print_func(*args, **kwargs):
for arg in args:
print(cmd[color], arg, '\033[0m ', sep='', end='')
print(**kwargs)
return print_func
class Test:
def __init__(self, filepath):
self.filepath = filepath
with open(filepath, 'r') as file:
code = file.readlines()
stdout = False
stderr = False
self.expected_stdout = []
self.expected_stderr = []
for line in code:
if line.startswith('// stdout'):
stdout = True
continue
elif line.startswith('// stderr'):
stdout = False
stderr = True
continue
if stdout:
if line.startswith('*/'):
break
self.expected_stdout.append(line)
elif stderr:
if line.startswith('*/'):
break
self.expected_stderr.append(line)
def run(self):
process = subprocess.run(
[cmd, self.filepath],
capture_output=True,
bufsize=1,
universal_newlines=True)
self.stdout = process.stdout
self.stderr = process.stderr
self.passed = False
if self.match(True) and self.match(False):
self.passed = True
def match(self, is_stdout):
expected = self.expected_stdout
got = self.stdout
if not is_stdout:
expected = self.expected_stderr
got = self.stderr
got = got.strip().split('\n')
if len(expected) == 0 and len(got) == 1 and got[0] == '':
return True
if(len(expected) != len(got)): return False
for e, g in zip(expected, got):
if e.replace('\n', '') != g:
return False
return True
def main():
total_tests = 0
tests_passed = 0
tests_failed = 0
tests = []
# get all the test files and create objects
for file in sorted(os.listdir(test_directory)):
base = os.path.join(test_directory, file)
if os.path.isdir(base):
for test in os.listdir(base):
path = os.path.join(base, test)
tests.append(Test(path))
else:
tests.append(Test(base))
# run all the tests and print their status
total_tests = len(tests)
for test in tests:
# print(''.join(test.expected_stdout))
# print(''.join(test.expected_stderr))
print('{0: <45}'.format(test.filepath), sep='', end=' ')
test.run()
if test.passed:
tests_passed += 1
green('OK')
else:
red('FAIL')
tests_failed = total_tests - tests_passed
print()
print('Total tests:', total_tests)
green('Tests passed:', tests_passed)
red('Tests failed:', tests_failed)
if total_tests == tests_passed:
green('All passed (OK)')
if __name__ == '__main__':
test_directory = 'tests'
red = print_in_color('red')
green = print_in_color('green')
cmd = './yai'
main()