test.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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([
  31. os.environ.get('MAKE', 'make'),
  32. '--no-print-directory', '-s'])
  33. def execute():
  34. if 'EXEC' in os.environ:
  35. subprocess.check_call([os.environ['EXEC'], "./lfs"])
  36. else:
  37. subprocess.check_call(["./lfs"])
  38. def main(test=None):
  39. if test and not test.startswith('-'):
  40. with open(test) as file:
  41. generate(file)
  42. else:
  43. generate(sys.stdin)
  44. compile()
  45. if test == '-s':
  46. sys.exit(1)
  47. execute()
  48. if __name__ == "__main__":
  49. main(*sys.argv[1:])