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.
This commit is contained in:
Paul Sokolovsky 2017-12-11 19:52:06 +02:00
parent 140bbced6f
commit 48e931e1d3
1 changed files with 27 additions and 21 deletions

View File

@ -1,32 +1,38 @@
#! /usr/bin/env python3 #!/usr/bin/env python3
import os, sys import os, sys
from glob import glob from glob import glob
from re import sub from re import sub
def escape(s): def escape(s):
lookup = { s = s.decode()
'\0': '\\0', lookup = {
'\t': '\\t', '\0': '\\0',
'\n': '\\n\"\n\"', '\t': '\\t',
'\r': '\\r', '\n': '\\n\"\n\"',
'\\': '\\\\', '\r': '\\r',
'\"': '\\\"', '\\': '\\\\',
} '\"': '\\\"',
return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s])) }
return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s]))
def chew_filename(t): def chew_filename(t):
return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] } return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] }
def script_to_map(t): def script_to_map(test_file):
r = { 'name': chew_filename(t)['func'] } r = {"name": chew_filename(test_file)["func"]}
with open(t) as f: r['script'] = escape(''.join(f.readlines())) with open(test_file, "rb") as f:
return r r["script"] = escape(f.read())
with open(test_file + ".exp", "rb") as f:
r["output"] = escape(f.read())
return r
test_function = ( test_function = (
"void {name}(void* data) {{\n" "void {name}(void* data) {{\n"
" const char * pystr = {script};\n" " static const char pystr[] = {script};\n"
" do_str(pystr);\n" " static const char exp[] = {output};\n"
" upytest_set_expected_output(exp, sizeof(exp) - 1);\n"
" upytest_execute_test(pystr);\n"
"}}" "}}"
) )
@ -57,10 +63,10 @@ exclude_tests = (
output = [] output = []
for group in test_dirs: for group in test_dirs:
tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests] tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests]
output.extend([test_function.format(**script_to_map(test)) for test in tests]) output.extend([test_function.format(**script_to_map(test)) for test in tests])
testcase_members = [testcase_member.format(**chew_filename(test)) for test in tests] testcase_members = [testcase_member.format(**chew_filename(test)) for test in tests]
output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members))) output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members)))
testgroup_members = [testgroup_member.format(name=group) for group in test_dirs] testgroup_members = [testgroup_member.format(name=group) for group in test_dirs]