Started on a test generation framework

This commit is contained in:
Johan B.W. de Vries 2023-11-11 12:02:37 +01:00
parent fd3939a680
commit ef00b3a91c
5 changed files with 114 additions and 1 deletions

2
.gitignore vendored
View File

@ -3,4 +3,6 @@
/.coverage
/venv
/tests/integration/test_lang/test_generated_*.py
__pycache__

View File

@ -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

View File

@ -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
```

View File

@ -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()

View File

@ -0,0 +1,4 @@
{
"TYPE": "u32",
"VAL0": "1000000"
}