test.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 test:
  11. if '=>' in line:
  12. test, expect = line.strip().strip(';').split('=>')
  13. lines.append('res = {test};'.format(test=test.strip()))
  14. lines.append('test_assert("{name}", res, {expect});'.format(
  15. name = re.match('\w*', test.strip()).group(),
  16. expect = expect.strip()))
  17. else:
  18. lines.append(line.strip())
  19. with open('test.c', 'w') as file:
  20. file.write(template.format(tests='\n'.join(4*' ' + l for l in lines)))
  21. def compile():
  22. os.environ['DEBUG'] = '1'
  23. os.environ['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:
  29. with open(test) as file:
  30. generate(file)
  31. else:
  32. generate(sys.stdin)
  33. compile()
  34. execute()
  35. if __name__ == "__main__":
  36. main(*sys.argv[1:])