ยปCore Development>Code coverage>Lib/test/test_symbol.py

Python code coverage for Lib/test/test_symbol.py

#countcontent
1n/aimport unittest
2n/afrom test import support
3n/aimport os
4n/aimport sys
5n/aimport subprocess
6n/a
7n/a
8n/aSYMBOL_FILE = support.findfile('symbol.py')
9n/aGRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
10n/a '..', '..', 'Include', 'graminit.h')
11n/aTEST_PY_FILE = 'symbol_test.py'
12n/a
13n/a
14n/aclass TestSymbolGeneration(unittest.TestCase):
15n/a
16n/a def _copy_file_without_generated_symbols(self, source_file, dest_file):
17n/a with open(source_file) as fp:
18n/a lines = fp.readlines()
19n/a with open(dest_file, 'w') as fp:
20n/a fp.writelines(lines[:lines.index("#--start constants--\n") + 1])
21n/a fp.writelines(lines[lines.index("#--end constants--\n"):])
22n/a
23n/a def _generate_symbols(self, grammar_file, target_symbol_py_file):
24n/a proc = subprocess.Popen([sys.executable,
25n/a SYMBOL_FILE,
26n/a grammar_file,
27n/a target_symbol_py_file], stderr=subprocess.PIPE)
28n/a stderr = proc.communicate()[1]
29n/a return proc.returncode, stderr
30n/a
31n/a def compare_files(self, file1, file2):
32n/a with open(file1) as fp:
33n/a lines1 = fp.readlines()
34n/a with open(file2) as fp:
35n/a lines2 = fp.readlines()
36n/a self.assertEqual(lines1, lines2)
37n/a
38n/a @unittest.skipIf(not os.path.exists(GRAMMAR_FILE),
39n/a 'test only works from source build directory')
40n/a def test_real_grammar_and_symbol_file(self):
41n/a output = support.TESTFN
42n/a self.addCleanup(support.unlink, output)
43n/a
44n/a self._copy_file_without_generated_symbols(SYMBOL_FILE, output)
45n/a
46n/a exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output)
47n/a self.assertEqual(b'', stderr)
48n/a self.assertEqual(0, exitcode)
49n/a
50n/a self.compare_files(SYMBOL_FILE, output)
51n/a
52n/a
53n/aif __name__ == "__main__":
54n/a unittest.main()