blob: 32564f3df214ca2295bf520058e841714abc13a0 [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 Dower128efca2019-01-22 12:31:30 -08005import sysconfig
Yury Selivanov26f70572015-05-28 14:06:12 -04006import subprocess
7
8
9SYMBOL_FILE = support.findfile('symbol.py')
10GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
11 '..', '..', 'Include', 'graminit.h')
12TEST_PY_FILE = 'symbol_test.py'
13
14
15class TestSymbolGeneration(unittest.TestCase):
16
17 def _copy_file_without_generated_symbols(self, source_file, dest_file):
Victor Stinnerf08fea92015-09-02 14:23:40 +020018 with open(source_file) as fp:
Yury Selivanov26f70572015-05-28 14:06:12 -040019 lines = fp.readlines()
Victor Stinnerf08fea92015-09-02 14:23:40 +020020 with open(dest_file, 'w') as fp:
21 fp.writelines(lines[:lines.index("#--start constants--\n") + 1])
22 fp.writelines(lines[lines.index("#--end constants--\n"):])
Yury Selivanov26f70572015-05-28 14:06:12 -040023
24 def _generate_symbols(self, grammar_file, target_symbol_py_file):
25 proc = subprocess.Popen([sys.executable,
26 SYMBOL_FILE,
27 grammar_file,
28 target_symbol_py_file], stderr=subprocess.PIPE)
29 stderr = proc.communicate()[1]
30 return proc.returncode, stderr
31
Victor Stinnerf08fea92015-09-02 14:23:40 +020032 def compare_files(self, file1, file2):
33 with open(file1) as fp:
34 lines1 = fp.readlines()
35 with open(file2) as fp:
36 lines2 = fp.readlines()
37 self.assertEqual(lines1, lines2)
38
Steve Dower128efca2019-01-22 12:31:30 -080039 @unittest.skipUnless(sysconfig.is_python_build(),
40 'test only works from source build directory')
Yury Selivanov26f70572015-05-28 14:06:12 -040041 def test_real_grammar_and_symbol_file(self):
Victor Stinnerf08fea92015-09-02 14:23:40 +020042 output = support.TESTFN
43 self.addCleanup(support.unlink, output)
44
45 self._copy_file_without_generated_symbols(SYMBOL_FILE, output)
46
47 exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output)
48 self.assertEqual(b'', stderr)
49 self.assertEqual(0, exitcode)
50
51 self.compare_files(SYMBOL_FILE, output)
Yury Selivanov26f70572015-05-28 14:06:12 -040052
53
54if __name__ == "__main__":
55 unittest.main()