85 lines
1.8 KiB
Python
85 lines
1.8 KiB
Python
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() |