run_tests.py 2.5 KB

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