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