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