| 1 |
import os |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
|
| 5 |
sys.path.insert(0, "..") |
| 6 |
from pycparser import parse_file, c_ast |
| 7 |
from tests.test_util import cpp_supported, cpp_path, cpp_args |
| 8 |
|
| 9 |
|
| 10 |
# Test successful parsing |
| 11 |
# |
| 12 |
class TestParsing(unittest.TestCase): |
| 13 |
def _find_file(self, name): |
| 14 |
"""Find a c file by name, taking into account the current dir can be |
| 15 |
in a couple of typical places |
| 16 |
""" |
| 17 |
testdir = os.path.dirname(__file__) |
| 18 |
name = os.path.join(testdir, "c_files", name) |
| 19 |
assert os.path.exists(name) |
| 20 |
return name |
| 21 |
|
| 22 |
def test_without_cpp(self): |
| 23 |
ast = parse_file(self._find_file("example_c_file.c")) |
| 24 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 25 |
|
| 26 |
@unittest.skipUnless(cpp_supported(), "cpp only works on Unix") |
| 27 |
def test_with_cpp(self): |
| 28 |
memmgr_path = self._find_file("memmgr.c") |
| 29 |
c_files_path = os.path.dirname(memmgr_path) |
| 30 |
ast = parse_file( |
| 31 |
memmgr_path, |
| 32 |
use_cpp=True, |
| 33 |
cpp_path=cpp_path(), |
| 34 |
cpp_args=cpp_args(f"-I{c_files_path}"), |
| 35 |
) |
| 36 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 37 |
|
| 38 |
fake_libc = os.path.join(c_files_path, "..", "..", "utils", "fake_libc_include") |
| 39 |
ast2 = parse_file( |
| 40 |
self._find_file("year.c"), |
| 41 |
use_cpp=True, |
| 42 |
cpp_path=cpp_path(), |
| 43 |
cpp_args=cpp_args(f"-I{fake_libc}"), |
| 44 |
) |
| 45 |
|
| 46 |
self.assertIsInstance(ast2, c_ast.FileAST) |
| 47 |
|
| 48 |
@unittest.skipUnless(cpp_supported(), "cpp only works on Unix") |
| 49 |
def test_cpp_funkydir(self): |
| 50 |
# This test contains Windows specific path escapes |
| 51 |
if sys.platform != "win32": |
| 52 |
return |
| 53 |
|
| 54 |
c_files_path = os.path.join("tests", "c_files") |
| 55 |
ast = parse_file( |
| 56 |
self._find_file("simplemain.c"), |
| 57 |
use_cpp=True, |
| 58 |
cpp_path=cpp_path(), |
| 59 |
cpp_args=cpp_args(f"-I{c_files_path}"), |
| 60 |
) |
| 61 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 62 |
|
| 63 |
@unittest.skipUnless(cpp_supported(), "cpp only works on Unix") |
| 64 |
def test_no_real_content_after_cpp(self): |
| 65 |
ast = parse_file( |
| 66 |
self._find_file("empty.h"), |
| 67 |
use_cpp=True, |
| 68 |
cpp_path=cpp_path(), |
| 69 |
cpp_args=cpp_args(), |
| 70 |
) |
| 71 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 72 |
|
| 73 |
@unittest.skipUnless(cpp_supported(), "cpp only works on Unix") |
| 74 |
def test_c11_with_cpp(self): |
| 75 |
c_files_path = os.path.join("tests", "c_files") |
| 76 |
fake_libc = os.path.join(c_files_path, "..", "..", "utils", "fake_libc_include") |
| 77 |
ast = parse_file( |
| 78 |
self._find_file("c11.c"), |
| 79 |
use_cpp=True, |
| 80 |
cpp_path=cpp_path(), |
| 81 |
cpp_args=cpp_args(f"-I{fake_libc}"), |
| 82 |
) |
| 83 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__": |
| 87 |
unittest.main() |