Skip to content

Commit be86596

Browse files
Ilya Dmitrichenkopfalcon
authored andcommitted
tools: inline test suite generator.
1 parent b1442e0 commit be86596

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tools/tinytest-codegen.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#! /usr/bin/env python3
2+
3+
import os, sys
4+
from glob import glob
5+
from re import sub
6+
7+
def escape(s):
8+
lookup = {
9+
'\0': '\\0',
10+
'\t': '\\t',
11+
'\n': '\\n\"\n\"',
12+
'\r': '\\r',
13+
'\\': '\\\\',
14+
'\"': '\\\"',
15+
}
16+
return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s]))
17+
18+
def chew_filename(t):
19+
return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] }
20+
21+
def script_to_map(t):
22+
r = { 'name': chew_filename(t)['func'] }
23+
with open(t) as f: r['script'] = escape(''.join(f.readlines()))
24+
return r
25+
26+
test_function = (
27+
"void {name}(void* data) {{\n"
28+
" const char * pystr = {script};\n"
29+
" do_str(pystr);\n"
30+
"}}"
31+
)
32+
33+
testcase_struct = (
34+
"struct testcase_t {name}_tests[] = {{\n{body}\n END_OF_TESTCASES\n}};"
35+
)
36+
testcase_member = (
37+
" {{ \"{desc}\", {func}, TT_ENABLED_, 0, 0 }},"
38+
)
39+
40+
testgroup_struct = (
41+
"struct testgroup_t groups[] = {{\n{body}\n END_OF_GROUPS\n}};"
42+
)
43+
testgroup_member = (
44+
" {{ \"{name}/\", {name}_tests }},"
45+
)
46+
47+
## XXX: may be we could have `--without <groups>` argument...
48+
test_dirs = ('basics', 'float', 'import', 'io', 'misc')
49+
50+
output = []
51+
52+
for group in test_dirs:
53+
tests = glob('{}/*.py'.format(group))
54+
output.extend([test_function.format(**script_to_map(test)) for test in tests])
55+
testcase_members = [testcase_member.format(**chew_filename(test)) for test in tests]
56+
output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members)))
57+
58+
testgroup_members = [testgroup_member.format(name=group) for group in test_dirs]
59+
60+
output.append(testgroup_struct.format(body='\n'.join(testgroup_members)))
61+
62+
## XXX: may be we could have `--output <filename>` argument...
63+
print('\n\n'.join(output))

0 commit comments

Comments
 (0)