blob: 4475bbcac4b047fbc662ce3796e733b57973e5ab [file] [log] [blame]
Yury Selivanov26f70572015-05-28 14:06:12 -04001import unittest
2from test import support
3import filecmp
4import os
5import sys
6import 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):
18 with open(source_file, 'rb') as fp:
19 lines = fp.readlines()
20 nl = lines[0][len(lines[0].rstrip()):]
21 with open(dest_file, 'wb') as fp:
22 fp.writelines(lines[:lines.index(b"#--start constants--" + nl) + 1])
23 fp.writelines(lines[lines.index(b"#--end constants--" + nl):])
24
25 def _generate_symbols(self, grammar_file, target_symbol_py_file):
26 proc = subprocess.Popen([sys.executable,
27 SYMBOL_FILE,
28 grammar_file,
29 target_symbol_py_file], stderr=subprocess.PIPE)
30 stderr = proc.communicate()[1]
31 return proc.returncode, stderr
32
33 @unittest.skipIf(not os.path.exists(GRAMMAR_FILE),
34 'test only works from source build directory')
35 def test_real_grammar_and_symbol_file(self):
36 self._copy_file_without_generated_symbols(SYMBOL_FILE, TEST_PY_FILE)
37 self.addCleanup(support.unlink, TEST_PY_FILE)
38 self.assertFalse(filecmp.cmp(SYMBOL_FILE, TEST_PY_FILE))
39 self.assertEqual((0, b''), self._generate_symbols(GRAMMAR_FILE,
40 TEST_PY_FILE))
41 self.assertTrue(filecmp.cmp(SYMBOL_FILE, TEST_PY_FILE))
42
43
44if __name__ == "__main__":
45 unittest.main()