test.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. import re
  3. import sys
  4. import subprocess
  5. import os
  6. def generate(test):
  7. with open("tests/template.fmt") as file:
  8. template = file.read()
  9. lines = []
  10. for line in re.split('(?<=[;{}])\n', test.read()):
  11. match = re.match('(?: *\n)*( *)(.*)=>(.*);', line, re.MULTILINE)
  12. if match:
  13. tab, test, expect = match.groups()
  14. lines.append(tab+'res = {test};'.format(test=test.strip()))
  15. lines.append(tab+'test_assert("{name}", res, {expect});'.format(
  16. name = re.match('\w*', test.strip()).group(),
  17. expect = expect.strip()))
  18. else:
  19. lines.append(line)
  20. with open('test.c', 'w') as file:
  21. file.write(template.format(tests='\n'.join(lines)))
  22. def compile():
  23. os.environ['CFLAGS'] = os.environ.get('CFLAGS', '') + ' -Werror'
  24. subprocess.check_call(['make', '--no-print-directory', '-s'], env=os.environ)
  25. def execute():
  26. subprocess.check_call(["./lfs"])
  27. def main(test=None):
  28. if test and not test.startswith('-'):
  29. with open(test) as file:
  30. generate(file)
  31. else:
  32. generate(sys.stdin)
  33. compile()
  34. if test == '-s':
  35. sys.exit(1)
  36. execute()
  37. if __name__ == "__main__":
  38. main(*sys.argv[1:])