Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 1 | import unittest |
| 2 | from test import support |
| 3 | import filecmp |
| 4 | import os |
| 5 | import sys |
| 6 | import subprocess |
| 7 | |
| 8 | |
| 9 | SYMBOL_FILE = support.findfile('symbol.py') |
| 10 | GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), |
| 11 | '..', '..', 'Include', 'graminit.h') |
| 12 | TEST_PY_FILE = 'symbol_test.py' |
| 13 | |
| 14 | |
| 15 | class 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 | |
| 44 | if __name__ == "__main__": |
| 45 | unittest.main() |