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): |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 18 | with open(source_file) as fp: |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 19 | lines = fp.readlines() |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 20 | with open(dest_file, 'w') as fp: |
| 21 | fp.writelines(lines[:lines.index("#--start constants--\n") + 1]) |
| 22 | fp.writelines(lines[lines.index("#--end constants--\n"):]) |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 23 | |
| 24 | def _generate_symbols(self, grammar_file, target_symbol_py_file): |
| 25 | proc = subprocess.Popen([sys.executable, |
| 26 | SYMBOL_FILE, |
| 27 | grammar_file, |
| 28 | target_symbol_py_file], stderr=subprocess.PIPE) |
| 29 | stderr = proc.communicate()[1] |
| 30 | return proc.returncode, stderr |
| 31 | |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 32 | def compare_files(self, file1, file2): |
| 33 | with open(file1) as fp: |
| 34 | lines1 = fp.readlines() |
| 35 | with open(file2) as fp: |
| 36 | lines2 = fp.readlines() |
| 37 | self.assertEqual(lines1, lines2) |
| 38 | |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 39 | @unittest.skipIf(not os.path.exists(GRAMMAR_FILE), |
| 40 | 'test only works from source build directory') |
| 41 | def test_real_grammar_and_symbol_file(self): |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 42 | output = support.TESTFN |
| 43 | self.addCleanup(support.unlink, output) |
| 44 | |
| 45 | self._copy_file_without_generated_symbols(SYMBOL_FILE, output) |
| 46 | |
| 47 | exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output) |
| 48 | self.assertEqual(b'', stderr) |
| 49 | self.assertEqual(0, exitcode) |
| 50 | |
| 51 | self.compare_files(SYMBOL_FILE, output) |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 52 | |
| 53 | |
| 54 | if __name__ == "__main__": |
| 55 | unittest.main() |