test.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.DOTALL | re.MULTILINE)
  12. if match:
  13. tab, test, expect = match.groups()
  14. lines.append(tab+'test = {test};'.format(test=test.strip()))
  15. lines.append(tab+'test_assert("{name}", test, {expect});'.format(
  16. name = re.match('\w*', test.strip()).group(),
  17. expect = expect.strip()))
  18. else:
  19. lines.append(line)
  20. # Create test file
  21. with open('test.c', 'w') as file:
  22. file.write(template.format(tests='\n'.join(lines)))
  23. # Remove build artifacts to force rebuild
  24. try:
  25. os.remove('test.o')
  26. os.remove('lfs')
  27. except OSError:
  28. pass
  29. def compile():
  30. subprocess.check_call(['make', '--no-print-directory', '-s'])
  31. def execute():
  32. subprocess.check_call(["./lfs"])
  33. def main(test=None):
  34. if test and not test.startswith('-'):
  35. with open(test) as file:
  36. generate(file)
  37. else:
  38. generate(sys.stdin)
  39. compile()
  40. if test == '-s':
  41. sys.exit(1)
  42. execute()
  43. if __name__ == "__main__":
  44. main(*sys.argv[1:])