Home | History | Annotate | Download | only in test
      1 import unittest
      2 from test import support
      3 import os
      4 import sys
      5 import sysconfig
      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) as fp:
     19             lines = fp.readlines()
     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"):])
     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 
     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 
     39     @unittest.skipUnless(sysconfig.is_python_build(),
     40                          'test only works from source build directory')
     41     def test_real_grammar_and_symbol_file(self):
     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)
     52 
     53 
     54 if __name__ == "__main__":
     55     unittest.main()
     56