Started on a test generation framework
This commit is contained in:
parent
fd3939a680
commit
ef00b3a91c
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,4 +3,6 @@
|
||||
/.coverage
|
||||
/venv
|
||||
|
||||
/tests/integration/test_lang/test_generated_*.py
|
||||
|
||||
__pycache__
|
||||
|
||||
5
Makefile
5
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
|
||||
|
||||
|
||||
19
tests/integration/test_lang/generator.md
Normal file
19
tests/integration/test_lang/generator.md
Normal 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
|
||||
```
|
||||
85
tests/integration/test_lang/generator.py
Normal file
85
tests/integration/test_lang/generator.py
Normal 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()
|
||||
4
tests/integration/test_lang/generator_u32.json
Normal file
4
tests/integration/test_lang/generator_u32.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"TYPE": "u32",
|
||||
"VAL0": "1000000"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user