run_tests.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. import sys
  3. import time
  4. import subprocess
  5. def test_file(filepath, cpython=False):
  6. if cpython:
  7. return os.system("python " + filepath) == 0
  8. if sys.platform == 'win32':
  9. return os.system("main.exe " + filepath) == 0
  10. else:
  11. return os.system("./main " + filepath) == 0
  12. def test_dir(path):
  13. print("Testing directory:", path)
  14. for filename in sorted(os.listdir(path)):
  15. if not filename.endswith('.py'):
  16. continue
  17. filepath = os.path.join(path, filename)
  18. print("> " + filepath, flush=True)
  19. if path == 'benchmarks/':
  20. _0 = time.time()
  21. if not test_file(filepath, cpython=True):
  22. print('cpython run failed')
  23. continue
  24. _1 = time.time()
  25. if not test_file(filepath): exit(1)
  26. _2 = time.time()
  27. print(f' cpython: {_1 - _0:.6f}s (100%)')
  28. print(f' pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
  29. else:
  30. if not test_file(filepath):
  31. print('-' * 50)
  32. print("TEST FAILED! Press any key to continue...")
  33. input()
  34. print('CPython:', str(sys.version).replace('\n', ''))
  35. print('System:', '64-bit' if sys.maxsize > 2**32 else '32-bit')
  36. if len(sys.argv) == 2:
  37. assert 'benchmark' in sys.argv[1]
  38. test_dir('benchmarks/')
  39. else:
  40. test_dir('tests/')
  41. # test interactive mode
  42. print("[REPL Test Enabled]")
  43. if sys.platform in ['linux', 'darwin']:
  44. cmd = './main'
  45. else:
  46. cmd = None
  47. if cmd is not None:
  48. res = subprocess.run([cmd], encoding='utf-8', input=r'''
  49. def add(a, b):
  50. return a + b
  51. class A:
  52. def __init__(self, x):
  53. self.x = x
  54. def get(self):
  55. return self.x
  56. print('ans_1:', add(1, 2))
  57. print('ans_2:', A('abc').get())
  58. exit()
  59. ''', capture_output=True, check=True)
  60. res.check_returncode()
  61. assert 'ans_1: 3' in res.stdout, res.stdout
  62. assert 'ans_2: abc' in res.stdout, res.stdout
  63. print("ALL TESTS PASSED")