• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from test import support
3import os
4import sys
5import sysconfig
6import subprocess
7
8
9SYMBOL_FILE              = support.findfile('symbol.py')
10GEN_SYMBOL_FILE          = os.path.join(os.path.dirname(__file__),
11                                        '..', '..', 'Tools', 'scripts',
12                                        'generate_symbol_py.py')
13GRAMMAR_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):
21        with open(source_file) as fp:
22            lines = fp.readlines()
23        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"):])
26
27    def _generate_symbols(self, grammar_file, target_symbol_py_file):
28        proc = subprocess.Popen([sys.executable,
29                                 GEN_SYMBOL_FILE,
30                                 grammar_file,
31                                 target_symbol_py_file], stderr=subprocess.PIPE)
32        stderr = proc.communicate()[1]
33        return proc.returncode, stderr
34
35    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
42    @unittest.skipUnless(sysconfig.is_python_build(),
43                         'test only works from source build directory')
44    def test_real_grammar_and_symbol_file(self):
45        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)
55
56
57if __name__ == "__main__":
58    unittest.main()
59