#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
#
# SPDX-License-Identifier: MIT
import os, sys
from glob import glob
from re import sub
import argparse
def escape(s):
s = s.decode()
lookup = {
"\0": "\\0",
"\t": "\\t",
"\n": '\\n"\n"',
"\r": "\\r",
"\\": "\\\\",
'"': '\\"',
}
return '""\n"{}"'.format("".join([lookup[x] if x in lookup else x for x in s]))
def chew_filename(t):
return {"func": "test_{}_fn".format(sub(r"/|\.|-", "_", t)), "desc": t}
def script_to_map(test_file):
r = {"name": chew_filename(test_file)["func"]}
with open(test_file, "rb") as test:
script = test.readlines()
# Test for import skip_if and inject it into the test as needed.
if "import skip_if\n" in script:
index = script.index("import skip_if\n")
script.pop(index)
script.insert(index, "class skip_if:\n")
with open("../tests/skip_if.py") as skip_if:
total_lines = 1
for line in skip_if:
stripped = line.strip()
if not stripped or stripped.startswith(("#", '"""')):
continue
script.insert(index + total_lines, "\t" + line)
total_lines += 1
r["script"] = escape(b"".join(script))
with open(test_file + ".exp", "rb") as f:
r["output"] = escape(f.read())
return r
def load_profile(profile_file, test_dirs, exclude_tests):
profile_globals = {"test_dirs": test_dirs, "exclude_tests": exclude_tests}
exec(profile_file.read(), profile_globals)
return profile_globals["test_dirs"], profile_globals["exclude_tests"]
test_function = (
"void {name}(void* data) {{\n"
" static const char pystr[] = {script};\n"
" static const char exp[] = {output};\n"
' printf("\\n");\n'
" upytest_set_expected_output(exp, sizeof(exp) - 1);\n"
" upytest_execute_test(pystr);\n"
' printf("result: ");\n'
"}}"
)
testcase_struct = "struct testcase_t {name}_tests[] = {{\n{body}\n END_OF_TESTCASES\n}};"
testcase_member = ' {{ "{desc}", {func}, TT_ENABLED_, 0, 0 }},'
testgroup_struct = "struct testgroup_t groups[] = {{\n{body}\n END_OF_GROUPS\n}};"
testgroup_member = ' {{ "{name}", {name}_tests }},'
## XXX: may be we could have `--without