• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2import platform
3import sys
4import unittest
5
6sys.path.insert(0, '..')
7from pycparser import parse_file, c_ast
8
9CPPPATH = 'cpp'
10
11
12# Test successful parsing
13#
14class TestParsing(unittest.TestCase):
15    def _find_file(self, name):
16        """ Find a c file by name, taking into account the current dir can be
17            in a couple of typical places
18        """
19        testdir = os.path.dirname(__file__)
20        name = os.path.join(testdir, 'c_files', name)
21        assert os.path.exists(name)
22        return name
23
24    def test_without_cpp(self):
25        ast = parse_file(self._find_file('example_c_file.c'))
26        self.assertIsInstance(ast, c_ast.FileAST)
27
28    @unittest.skipUnless(platform.system() == 'Linux',
29                         'cpp only works on Linux')
30    def test_with_cpp(self):
31        memmgr_path = self._find_file('memmgr.c')
32        c_files_path = os.path.dirname(memmgr_path)
33        ast = parse_file(memmgr_path, use_cpp=True,
34            cpp_path=CPPPATH,
35            cpp_args='-I%s' % c_files_path)
36        self.assertIsInstance(ast, c_ast.FileAST)
37
38        fake_libc = os.path.join(c_files_path, '..', '..',
39                                 'utils', 'fake_libc_include')
40        ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
41            cpp_path=CPPPATH,
42            cpp_args=[r'-I%s' % fake_libc])
43
44        self.assertIsInstance(ast2, c_ast.FileAST)
45
46    @unittest.skipUnless(platform.system() == 'Linux',
47                         'cpp only works on Linux')
48    def test_cpp_funkydir(self):
49        # This test contains Windows specific path escapes
50        if sys.platform != 'win32':
51            return
52
53        c_files_path = os.path.join('tests', 'c_files')
54        ast = parse_file(self._find_file('simplemain.c'), use_cpp=True,
55            cpp_path=CPPPATH, cpp_args='-I%s' % c_files_path)
56        self.assertIsInstance(ast, c_ast.FileAST)
57
58    @unittest.skipUnless(platform.system() == 'Linux',
59                         'cpp only works on Linux')
60    def test_no_real_content_after_cpp(self):
61        ast = parse_file(self._find_file('empty.h'), use_cpp=True,
62            cpp_path=CPPPATH)
63        self.assertIsInstance(ast, c_ast.FileAST)
64
65
66if __name__ == '__main__':
67    unittest.main()
68