summaryrefslogtreecommitdiff
path: root/tests/test_general.py
blob: e6b654e8774172c59ea6e9ea11557bccaf541033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import sys
import unittest

sys.path.insert(0, '..')
from pycparser import parse_file, c_ast
from tests.test_util import cpp_supported, cpp_path, cpp_args

# Test successful parsing
#
class TestParsing(unittest.TestCase):
    def _find_file(self, name):
        """ Find a c file by name, taking into account the current dir can be
            in a couple of typical places
        """
        testdir = os.path.dirname(__file__)
        name = os.path.join(testdir, 'c_files', name)
        assert os.path.exists(name)
        return name

    def test_without_cpp(self):
        ast = parse_file(self._find_file('example_c_file.c'))
        self.assertIsInstance(ast, c_ast.FileAST)

    @unittest.skipUnless(cpp_supported(), 'cpp only works on Unix')
    def test_with_cpp(self):
        memmgr_path = self._find_file('memmgr.c')
        c_files_path = os.path.dirname(memmgr_path)
        ast = parse_file(memmgr_path, use_cpp=True,
            cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % c_files_path))
        self.assertIsInstance(ast, c_ast.FileAST)

        fake_libc = os.path.join(c_files_path, '..', '..',
                                 'utils', 'fake_libc_include')
        ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
            cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % fake_libc))

        self.assertIsInstance(ast2, c_ast.FileAST)

    @unittest.skipUnless(cpp_supported(), 'cpp only works on Unix')
    def test_cpp_funkydir(self):
        # This test contains Windows specific path escapes
        if sys.platform != 'win32':
            return

        c_files_path = os.path.join('tests', 'c_files')
        ast = parse_file(self._find_file('simplemain.c'), use_cpp=True,
            cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % c_files_path))
        self.assertIsInstance(ast, c_ast.FileAST)

    @unittest.skipUnless(cpp_supported(), 'cpp only works on Unix')
    def test_no_real_content_after_cpp(self):
        ast = parse_file(self._find_file('empty.h'), use_cpp=True,
            cpp_path=cpp_path(), cpp_args=cpp_args())
        self.assertIsInstance(ast, c_ast.FileAST)

    @unittest.skipUnless(cpp_supported(), 'cpp only works on Unix')
    def test_c11_with_cpp(self):
        c_files_path = os.path.join('tests', 'c_files')
        fake_libc = os.path.join(c_files_path, '..', '..',
                                 'utils', 'fake_libc_include')
        ast = parse_file(self._find_file('c11.c'), use_cpp=True,
            cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % fake_libc))
        self.assertIsInstance(ast, c_ast.FileAST)

if __name__ == '__main__':
    unittest.main()