test.py 1.4 KB

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