From ef00b3a91c3d5ad19e1cebbe02f8cd2fe336ac16 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sat, 11 Nov 2023 12:02:37 +0100 Subject: [PATCH] Started on a test generation framework --- .gitignore | 2 + Makefile | 5 +- tests/integration/test_lang/generator.md | 19 +++++ tests/integration/test_lang/generator.py | 85 +++++++++++++++++++ .../integration/test_lang/generator_u32.json | 4 + 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 tests/integration/test_lang/generator.md create mode 100644 tests/integration/test_lang/generator.py create mode 100644 tests/integration/test_lang/generator_u32.json diff --git a/.gitignore b/.gitignore index b8a823a..29c7ccf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ /.coverage /venv +/tests/integration/test_lang/test_generated_*.py + __pycache__ diff --git a/Makefile b/Makefile index c371232..3fd5cf5 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ WASM2C := $(WABT_DIR)/bin/wasm2c examples: venv/.done $(subst .py,.wasm,$(wildcard examples/*.py)) $(subst .py,.wat.html,$(wildcard examples/*.py)) $(subst .py,.py.html,$(wildcard examples/*.py)) venv/bin/python3 -m http.server --directory examples -test: venv/.done +test: venv/.done tests/integration/test_lang/test_generated_u32.py venv/bin/pytest tests $(TEST_FLAGS) lint: venv/.done @@ -39,6 +39,9 @@ venv/.done: requirements.txt venv/bin/python3 -m pip install -r $^ touch $@ +tests/integration/test_lang/test_generated_u32.py: venv/.done tests/integration/test_lang/generator.py tests/integration/test_lang/generator.md tests/integration/test_lang/generator_u32.json + venv/bin/python3 tests/integration/test_lang/generator.py tests/integration/test_lang/generator.md tests/integration/test_lang/generator_u32.json > $@ + clean-examples: rm -f examples/*.wat examples/*.wasm examples/*.wat.html examples/*.py.html diff --git a/tests/integration/test_lang/generator.md b/tests/integration/test_lang/generator.md new file mode 100644 index 0000000..24949a4 --- /dev/null +++ b/tests/integration/test_lang/generator.md @@ -0,0 +1,19 @@ +# module_constant_def_ok + +```py +CONSTANT: $TYPE = $VAL0 + +@exported +def testEntry() -> i32: + return 0 +``` + +# module_constant_def_bad + +```py +CONSTANT: $OTHER_TYPE = $VAL0 + +@exported +def testEntry() -> i32: + return 0 +``` diff --git a/tests/integration/test_lang/generator.py b/tests/integration/test_lang/generator.py new file mode 100644 index 0000000..443a17d --- /dev/null +++ b/tests/integration/test_lang/generator.py @@ -0,0 +1,85 @@ +import json +import sys + +import marko + +def get_tests(template): + test_data = None + for el in template.children: + if isinstance(el, marko.block.BlankLine): + continue + + if isinstance(el, marko.block.Heading): + if test_data is not None: + yield test_data + + test_data = [] + test_data.append(el) + continue + + if test_data is not None: + test_data.append(el) + + if test_data is not None: + yield test_data + +def apply_settings(settings, txt): + for k, v in settings.items(): + txt = txt.replace(f'${k}', v) + return txt + +def generate_code(template, settings): + type_name = settings['TYPE_NAME'] + + print('"""') + print('AUTO GENERATED') + print() + print('TEMPLATE:', sys.argv[1]) + print('SETTINGS:', sys.argv[2]) + print('"""') + print('import pytest') + print() + print('from ..helpers import Suite') + print() + + for test in get_tests(template): + assert len(test) == 2, test + heading, code_block = test + + assert isinstance(heading, marko.block.Heading) + assert isinstance(code_block, marko.block.FencedCode) + + test_id = apply_settings(settings, heading.children[0].children) + code = apply_settings(settings, code_block.children[0].children) + + code = code.rstrip('\n') + + + print('@pytest.mark.integration_test') + print(f'def test_{type_name}_{test_id}():') + print(' code_py = """') + print(code) + print('"""') + print() + print(' result = Suite(code_py).run_code()') + print() + print(' assert 24 == result.returned_value') + print() + + +def main(): + with open(sys.argv[1], 'r', encoding='utf-8') as fil: + template = marko.Markdown().parse(fil.read()) + + with open(sys.argv[2], 'r', encoding='utf-8') as fil: + settings = json.load(fil) + + if 'TYPE_NAME' not in settings: + settings['TYPE_NAME'] = settings['TYPE'] + + settings['OTHER_TYPE'] = '(u32, )' + + generate_code(template, settings) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/tests/integration/test_lang/generator_u32.json b/tests/integration/test_lang/generator_u32.json new file mode 100644 index 0000000..5843ed9 --- /dev/null +++ b/tests/integration/test_lang/generator_u32.json @@ -0,0 +1,4 @@ +{ + "TYPE": "u32", + "VAL0": "1000000" +}