Skip to content

Commit 48e931e

Browse files
committed
tools/tinytest-codegen.py: Generate code for upytesthelper.
The way tinytest was used in qemu-arm test target is that it didn't test much. MicroPython tests are based on matching the test output against reference output, but qemu-arm's implementation didn't do that, it effectively tested just that there was no exception during test execution. "upytesthelper" wrapper was introduce to fix it, and so test generator is now switched to generate test code for it. Also, fix PEP8 and other codestyle issues.
1 parent 140bbce commit 48e931e

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

tools/tinytest-codegen.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
#! /usr/bin/env python3
1+
#!/usr/bin/env python3
22

33
import os, sys
44
from glob import glob
55
from re import sub
66

77
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]))
8+
s = s.decode()
9+
lookup = {
10+
'\0': '\\0',
11+
'\t': '\\t',
12+
'\n': '\\n\"\n\"',
13+
'\r': '\\r',
14+
'\\': '\\\\',
15+
'\"': '\\\"',
16+
}
17+
return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s]))
1718

1819
def chew_filename(t):
19-
return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] }
20+
return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] }
2021

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
22+
def script_to_map(test_file):
23+
r = {"name": chew_filename(test_file)["func"]}
24+
with open(test_file, "rb") as f:
25+
r["script"] = escape(f.read())
26+
with open(test_file + ".exp", "rb") as f:
27+
r["output"] = escape(f.read())
28+
return r
2529

2630
test_function = (
2731
"void {name}(void* data) {{\n"
28-
" const char * pystr = {script};\n"
29-
" do_str(pystr);\n"
32+
" static const char pystr[] = {script};\n"
33+
" static const char exp[] = {output};\n"
34+
" upytest_set_expected_output(exp, sizeof(exp) - 1);\n"
35+
" upytest_execute_test(pystr);\n"
3036
"}}"
3137
)
3238

@@ -57,10 +63,10 @@ def script_to_map(t):
5763
output = []
5864

5965
for group in test_dirs:
60-
tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests]
61-
output.extend([test_function.format(**script_to_map(test)) for test in tests])
62-
testcase_members = [testcase_member.format(**chew_filename(test)) for test in tests]
63-
output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members)))
66+
tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests]
67+
output.extend([test_function.format(**script_to_map(test)) for test in tests])
68+
testcase_members = [testcase_member.format(**chew_filename(test)) for test in tests]
69+
output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members)))
6470

6571
testgroup_members = [testgroup_member.format(name=group) for group in test_dirs]
6672

0 commit comments

Comments
 (0)