forked from McMartin/KLTypeList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-features.py
executable file
·308 lines (235 loc) · 9.05 KB
/
test-features.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# Copyright (c) 2014 Alain Martin
import argparse
import ast
import os
import re
import subprocess
import sys
import tempfile
FEATURE_EXT = '.feature'
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
def compiler_arg_choices():
compilers_dir = os.path.join(REPO_ROOT, 'compilers')
return [os.path.basename(file_name)
for file_name in os.listdir(compilers_dir)]
def parse_args():
def existing_dir_or_file(path):
if not os.path.exists(path):
message = 'No such file or directory %s' % os.path.abspath(path)
raise argparse.ArgumentTypeError(message)
return path
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-c', '--compiler',
required=True,
choices=compiler_arg_choices())
arg_parser.add_argument('input_path',
type=existing_dir_or_file,
nargs='?',
default=os.path.curdir)
return arg_parser.parse_args(sys.argv[1:])
class Compiler(object):
@staticmethod
def from_file(compiler_file_path):
with open(compiler_file_path, 'r') as compiler_file:
settings = ast.literal_eval(compiler_file.read())
return Compiler(
settings['exe'], settings['options'], settings['env'])
def __init__(self, exe, options, env):
self.exe = exe
self.options = options
self.env = env
def call_env(self):
call_env = os.environ.copy()
for key in self.env:
if key not in call_env:
call_env[key] = ''
for path in self.env[key]:
call_env[key] += os.pathsep + path
return call_env
def compile(self, source_file_path):
compiler_cmd = [self.exe, source_file_path] + self.options
call_env = self.call_env()
return_code = 0
output = ''
try:
output = subprocess.check_output(
compiler_cmd, env=call_env, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
return_code = error.returncode
output = error.output
return return_code, output
class Status(object):
ERROR = 'ERROR'
PASSED = 'PASSED'
FAILED = 'FAILED'
def get_return_type(result):
if result is None:
return 'DNC'
if result in ('true', 'false'):
return 'Boolean'
if result.isdigit():
return 'Integer'
if result.startswith('TypeList<'):
return 'TypeList'
if len(result) == 1:
return 'Type'
class Feature(object):
def __init__(self, line, name, has_arguments, return_type):
self.line = line
self.name = name
self.has_arguments = has_arguments is not None
self.return_type = return_type
@staticmethod
def from_declaration(line):
feature_declaration_regex = re.compile(r'^(.+?)(?:<(.*)>)? -> (.+)$')
match = feature_declaration_regex.search(line)
if match:
name, has_arguments, return_type = match.groups()
return Feature(line, name, has_arguments, return_type)
def run_test(self, feature_test, compiler):
if (self.name != feature_test.feature_name
or self.has_arguments != (feature_test.arguments is not None)
or (self.return_type != get_return_type(feature_test.result)
and feature_test.result is not None)):
print '[ %-6s ] %s\ndoes not match %s' % (
'ERROR', feature_test.line, self.line)
return Status.ERROR
return feature_test.run(self, compiler)
test_code_skeleton = '''
#include "KL/TypeList.hpp"
#include <type_traits>
using A = {A};
using B = {B};
using C = {C};
using namespace KL;
class Test{feature_name}
{{
void test()
{{
{result_type} Result = TypeList<{pack}>::{feature_name}{arguments};
static_assert({assertion}, "!");
}}
}};
'''
def get_result_type(return_type):
if return_type in ('Boolean', 'Integer'):
return 'const auto'
if return_type in ('Type', 'TypeList'):
return 'using'
def get_assertion(return_type, result):
if result is None:
return 'true'
if return_type in ('Boolean', 'Integer'):
return '%s == Result' % result
if return_type in ('TypeList', 'Type'):
return 'std::is_same<%s, Result>::value' % result
class FeatureTest(object):
def __init__(self, line, feature_name, pack, arguments, result):
self.line = line
self.feature_name = feature_name
self.pack = pack
self.arguments = arguments
self.result = result
@staticmethod
def from_declaration(line):
feature_test_declaration_regex = re.compile(
r'^TypeList<(.*)>::(.+?)(?:<(.*)>)?'
r' (?:NOT COMPILE|== (.+))$')
match = feature_test_declaration_regex.search(line)
if match:
pack, feature_name, arguments, result = match.groups()
return FeatureTest(line, feature_name, pack, arguments, result)
def run(self, feature, compiler):
arguments = ''
if feature.has_arguments:
arguments += '<' + self.arguments + '>'
if feature.return_type in ('Boolean', 'Integer'):
arguments += '::value'
test_code = test_code_skeleton.format(
feature_name=feature.name,
result_type=get_result_type(feature.return_type),
pack=self.pack,
arguments=arguments,
assertion=get_assertion(feature.return_type, self.result),
A='void',
B='bool',
C='char',
)
temp_file_descriptor = None
temp_file_path = None
temp_file = None
return_code = None
try:
temp_file_descriptor, temp_file_path = tempfile.mkstemp(
suffix='.cpp')
temp_file = os.fdopen(temp_file_descriptor, 'w')
temp_file.write(test_code)
temp_file.close()
return_code, output = compiler.compile(temp_file_path)
finally:
if temp_file:
temp_file.close()
elif temp_file_descriptor:
os.close(temp_file_descriptor)
if temp_file_path:
os.remove(temp_file_path)
if return_code is not None:
if (return_code == 0) == (self.result is not None):
print '[ %-6s ] %s' % ('PASS', self.line)
return Status.PASSED
else:
print '[ %-6s ] %s' % ('FAIL!', self.line)
print output
return Status.FAILED
return Status.ERROR
def test_feature_file(feature_file_path, compiler):
feature = None
status = []
with open(feature_file_path, 'r') as feature_file:
for line in feature_file:
if not line.isspace():
line = line.rstrip()
if not feature:
feature = Feature.from_declaration(line)
if feature:
print '[--------] %s' % feature.line
else:
print 'Failed to parse feature "%s" in %s' % (
line, feature_file_path)
return [Status.ERROR]
else:
test = FeatureTest.from_declaration(line)
if test:
status.append(feature.run_test(test, compiler))
else:
print 'Failed to parse feature test "%s" in %s' % (
line, feature_file_path)
status.append(Status.ERROR)
print ('[--------] %s passed' % status.count(Status.PASSED)
+ ', %s failed' % status.count(Status.FAILED)
+ ', %s errored\n' % status.count(Status.ERROR))
return status
def find_feature_files(path):
if os.path.isfile(path) and os.path.splitext(path)[1] == FEATURE_EXT:
yield path
return
for root, _, file_names in os.walk(path):
for file_name in file_names:
file_path = os.path.join(root, file_name)
if os.path.splitext(file_path)[1] == FEATURE_EXT:
yield file_path
def test_features(compiler, input_path):
compiler_file_path = os.path.join(REPO_ROOT, 'compilers', compiler)
compiler = Compiler.from_file(compiler_file_path)
feature_files = find_feature_files(input_path)
status = []
for feature_file_path in feature_files:
status += test_feature_file(feature_file_path, compiler)
print '[ TOTAL ] %s error%s, %s failed test%s, %s passed test%s' % (
status.count(Status.ERROR), 's'[status.count(Status.ERROR) == 1:],
status.count(Status.FAILED), 's'[status.count(Status.FAILED) == 1:],
status.count(Status.PASSED), 's'[status.count(Status.PASSED) == 1:])
return 1 if Status.ERROR in status else status.count(Status.FAILED)
if __name__ == '__main__':
sys.exit(test_features(**vars(parse_args())))