blob: ed86aec36b873cd56b39cae43c539a655129af33 [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
5import subprocess
6
7
8SYMBOL_FILE = support.findfile('symbol.py')
Serhiy Storchaka8ac65812018-12-22 11:18:40 +02009GEN_SYMBOL_FILE = os.path.join(os.path.dirname(__file__),
10 '..', '..', 'Tools', 'scripts',
11 'generate_symbol_py.py')
Yury Selivanov26f70572015-05-28 14:06:12 -040012GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
13 '..', '..', 'Include', 'graminit.h')
14TEST_PY_FILE = 'symbol_test.py'
15
16
17class TestSymbolGeneration(unittest.TestCase):
18
19 def _copy_file_without_generated_symbols(self, source_file, dest_file):
Victor Stinnerf08fea92015-09-02 14:23:40 +020020 with open(source_file) as fp:
Yury Selivanov26f70572015-05-28 14:06:12 -040021 lines = fp.readlines()
Victor Stinnerf08fea92015-09-02 14:23:40 +020022 with open(dest_file, 'w') as fp:
23 fp.writelines(lines[:lines.index("#--start constants--\n") + 1])
24 fp.writelines(lines[lines.index("#--end constants--\n"):])
Yury Selivanov26f70572015-05-28 14:06:12 -040025
26 def _generate_symbols(self, grammar_file, target_symbol_py_file):
27 proc = subprocess.Popen([sys.executable,
Serhiy Storchaka8ac65812018-12-22 11:18:40 +020028 GEN_SYMBOL_FILE,
Yury Selivanov26f70572015-05-28 14:06:12 -040029 grammar_file,
30 target_symbol_py_file], stderr=subprocess.PIPE)
31 stderr = proc.communicate()[1]
32 return proc.returncode, stderr
33
Victor Stinnerf08fea92015-09-02 14:23:40 +020034 def compare_files(self, file1, file2):
35 with open(file1) as fp:
36 lines1 = fp.readlines()
37 with open(file2) as fp:
38 lines2 = fp.readlines()
39 self.assertEqual(lines1, lines2)
40
Yury Selivanov26f70572015-05-28 14:06:12 -040041 @unittest.skipIf(not os.path.exists(GRAMMAR_FILE),
42 'test only works from source build directory')
43 def test_real_grammar_and_symbol_file(self):
Victor Stinnerf08fea92015-09-02 14:23:40 +020044 output = support.TESTFN
45 self.addCleanup(support.unlink, output)
46
47 self._copy_file_without_generated_symbols(SYMBOL_FILE, output)
48
49 exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output)
50 self.assertEqual(b'', stderr)
51 self.assertEqual(0, exitcode)
52
53 self.compare_files(SYMBOL_FILE, output)
Yury Selivanov26f70572015-05-28 14:06:12 -040054
55
56if __name__ == "__main__":
57 unittest.main()