blob: 645d8f43b6cd9d3f1790f69efd7928f63e31e827 [file] [log] [blame]
Yury Selivanov26f70572015-05-28 14:06:12 -04001import unittest
2from test import support
Yury Selivanov26f70572015-05-28 14:06:12 -04003import os
4import sys
Steve Dower28f6cb32019-01-22 10:49:52 -08005import sysconfig
Yury Selivanov26f70572015-05-28 14:06:12 -04006import subprocess
7
8
9SYMBOL_FILE = support.findfile('symbol.py')
Serhiy Storchaka8ac65812018-12-22 11:18:40 +020010GEN_SYMBOL_FILE = os.path.join(os.path.dirname(__file__),
11 '..', '..', 'Tools', 'scripts',
12 'generate_symbol_py.py')
Yury Selivanov26f70572015-05-28 14:06:12 -040013GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
14 '..', '..', 'Include', 'graminit.h')
15TEST_PY_FILE = 'symbol_test.py'
16
17
18class TestSymbolGeneration(unittest.TestCase):
19
20 def _copy_file_without_generated_symbols(self, source_file, dest_file):
Victor Stinnerf08fea92015-09-02 14:23:40 +020021 with open(source_file) as fp:
Yury Selivanov26f70572015-05-28 14:06:12 -040022 lines = fp.readlines()
Victor Stinnerf08fea92015-09-02 14:23:40 +020023 with open(dest_file, 'w') as fp:
24 fp.writelines(lines[:lines.index("#--start constants--\n") + 1])
25 fp.writelines(lines[lines.index("#--end constants--\n"):])
Yury Selivanov26f70572015-05-28 14:06:12 -040026
27 def _generate_symbols(self, grammar_file, target_symbol_py_file):
28 proc = subprocess.Popen([sys.executable,
Serhiy Storchaka8ac65812018-12-22 11:18:40 +020029 GEN_SYMBOL_FILE,
Yury Selivanov26f70572015-05-28 14:06:12 -040030 grammar_file,
31 target_symbol_py_file], stderr=subprocess.PIPE)
32 stderr = proc.communicate()[1]
33 return proc.returncode, stderr
34
Victor Stinnerf08fea92015-09-02 14:23:40 +020035 def compare_files(self, file1, file2):
36 with open(file1) as fp:
37 lines1 = fp.readlines()
38 with open(file2) as fp:
39 lines2 = fp.readlines()
40 self.assertEqual(lines1, lines2)
41
Steve Dower28f6cb32019-01-22 10:49:52 -080042 @unittest.skipUnless(sysconfig.is_python_build(),
43 'test only works from source build directory')
Yury Selivanov26f70572015-05-28 14:06:12 -040044 def test_real_grammar_and_symbol_file(self):
Victor Stinnerf08fea92015-09-02 14:23:40 +020045 output = support.TESTFN
46 self.addCleanup(support.unlink, output)
47
48 self._copy_file_without_generated_symbols(SYMBOL_FILE, output)
49
50 exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output)
51 self.assertEqual(b'', stderr)
52 self.assertEqual(0, exitcode)
53
54 self.compare_files(SYMBOL_FILE, output)
Yury Selivanov26f70572015-05-28 14:06:12 -040055
56
57if __name__ == "__main__":
58 unittest.main()