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