| 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 |
|
| 11 |
class TestParsing(unittest.TestCase): |
| 12 |
def _find_file(self, name): |
| 13 |
""" Find a c file by name, taking into account the current dir can be |
| 14 |
in a couple of typical places |
| 15 |
""" |
| 16 |
testdir = os.path.dirname(__file__) |
| 17 |
name = os.path.join(testdir, 'c_files', name) |
| 18 |
assert os.path.exists(name) |
| 19 |
return name |
| 20 |
|
| 21 |
def test_without_cpp(self): |
| 22 |
ast = parse_file(self._find_file('example_c_file.c')) |
| 23 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 24 |
|
| 25 |
@unittest.skipUnless(cpp_supported(), 'cpp only works on Unix') |
| 26 |
def test_with_cpp(self): |
| 27 |
memmgr_path = self._find_file('memmgr.c') |
| 28 |
c_files_path = os.path.dirname(memmgr_path) |
| 29 |
ast = parse_file(memmgr_path, use_cpp=True, |
| 30 |
cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % c_files_path)) |
| 31 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 32 |
|
| 33 |
fake_libc = os.path.join(c_files_path, '..', '..', |
| 34 |
'utils', 'fake_libc_include') |
| 35 |
ast2 = parse_file(self._find_file('year.c'), use_cpp=True, |
| 36 |
cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % fake_libc)) |
| 37 |
|
| 38 |
self.assertIsInstance(ast2, c_ast.FileAST) |
| 39 |
|
| 40 |
@unittest.skipUnless(cpp_supported(), 'cpp only works on Unix') |
| 41 |
def test_cpp_funkydir(self): |
| 42 |
|
| 43 |
if sys.platform != 'win32': |
| 44 |
return |
| 45 |
|
| 46 |
c_files_path = os.path.join('tests', 'c_files') |
| 47 |
ast = parse_file(self._find_file('simplemain.c'), use_cpp=True, |
| 48 |
cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % c_files_path)) |
| 49 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 50 |
|
| 51 |
@unittest.skipUnless(cpp_supported(), 'cpp only works on Unix') |
| 52 |
def test_no_real_content_after_cpp(self): |
| 53 |
ast = parse_file(self._find_file('empty.h'), use_cpp=True, |
| 54 |
cpp_path=cpp_path(), cpp_args=cpp_args()) |
| 55 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 56 |
|
| 57 |
@unittest.skipUnless(cpp_supported(), 'cpp only works on Unix') |
| 58 |
def test_c11_with_cpp(self): |
| 59 |
c_files_path = os.path.join('tests', 'c_files') |
| 60 |
fake_libc = os.path.join(c_files_path, '..', '..', |
| 61 |
'utils', 'fake_libc_include') |
| 62 |
ast = parse_file(self._find_file('c11.c'), use_cpp=True, |
| 63 |
cpp_path=cpp_path(), cpp_args=cpp_args('-I%s' % fake_libc)) |
| 64 |
self.assertIsInstance(ast, c_ast.FileAST) |
| 65 |
|
| 66 |
if __name__ == '__main__': |
| 67 |
unittest.main() |