blob: 587284a44c53e08b25f99189c112127ffde06e52 [file] [log] [blame]
Eli Benderskyd0973782012-01-19 08:09:33 +02001import sys, os
Eli Bendersky5b09f552011-10-16 06:01:54 +02002import unittest
3
4sys.path.insert(0, '..')
5from pycparser import parse_file, c_ast
6
7# Portable cpp path for Windows and Linux/Unix
Eli Benderskyd0973782012-01-19 08:09:33 +02008CPPPATH = 'utils/cpp.exe' if sys.platform == 'win32' else 'cpp'
Eli Bendersky5b09f552011-10-16 06:01:54 +02009
10
11# Test successful parsing
12#
13class TestParsing(unittest.TestCase):
Eli Benderskyd0973782012-01-19 08:09:33 +020014 def _find_file(self, name):
15 """ Find a c file by name, taking into account the current dir can be
16 in a couple of typical places
17 """
18 fullnames = [
19 os.path.join('c_files', name),
20 os.path.join('tests', 'c_files', name)]
21 for fullname in fullnames:
22 if os.path.exists(fullname):
23 return fullname
24 assert False, "Unreachable"
25
Eli Bendersky3921e8e2010-05-21 09:05:39 +030026 def test_without_cpp(self):
Eli Benderskyd0973782012-01-19 08:09:33 +020027 ast = parse_file(self._find_file('example_c_file.c'))
Eli Benderskyc4701a32012-08-10 08:07:39 +030028 self.assertTrue(isinstance(ast, c_ast.FileAST))
Eli Bendersky5b09f552011-10-16 06:01:54 +020029
Eli Bendersky3921e8e2010-05-21 09:05:39 +030030 def test_with_cpp(self):
eli.bendersky665a0bd2012-01-20 09:24:32 +020031 c_files_path = os.path.join('tests', 'c_files')
Eli Benderskyd0973782012-01-19 08:09:33 +020032 ast = parse_file(self._find_file('memmgr.c'), use_cpp=True,
Eli Bendersky5b09f552011-10-16 06:01:54 +020033 cpp_path=CPPPATH,
eli.bendersky665a0bd2012-01-20 09:24:32 +020034 cpp_args='-I%s' % c_files_path)
Eli Benderskyc4701a32012-08-10 08:07:39 +030035 self.assertTrue(isinstance(ast, c_ast.FileAST))
Eli Bendersky5b09f552011-10-16 06:01:54 +020036
Eli Benderskyd0973782012-01-19 08:09:33 +020037 ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
Eli Bendersky5b09f552011-10-16 06:01:54 +020038 cpp_path=CPPPATH,
Eli Benderskya2bafde2012-03-14 06:20:58 +020039 cpp_args=[
40 r'-Iutils/fake_libc_include',
41 r'-I../utils/fake_libc_include'])
Eli Bendersky5b09f552011-10-16 06:01:54 +020042
Eli Benderskyc4701a32012-08-10 08:07:39 +030043 self.assertTrue(isinstance(ast2, c_ast.FileAST))
44
45 def test_cpp_funkydir(self):
Eli Benderskyd9649812012-08-10 08:27:12 +030046 # This test contains Windows specific path escapes
47 if sys.platform != 'win32':
48 return
49
Eli Benderskyc4701a32012-08-10 08:07:39 +030050 c_files_path = os.path.join('tests', 'c_files')
51 ast = parse_file(self._find_file('simplemain.c'), use_cpp=True,
52 cpp_path=CPPPATH, cpp_args='-I%s' % c_files_path)
53 self.assertTrue(isinstance(ast, c_ast.FileAST))
Eli Bendersky5b09f552011-10-16 06:01:54 +020054
Eli Bendersky5a0abdf2012-07-07 06:52:23 +030055 def test_no_real_content_after_cpp(self):
56 ast = parse_file(self._find_file('empty.h'), use_cpp=True,
57 cpp_path=CPPPATH)
Eli Benderskyc4701a32012-08-10 08:07:39 +030058 self.assertTrue(isinstance(ast, c_ast.FileAST))
59
Eli Bendersky5b09f552011-10-16 06:01:54 +020060
61if __name__ == '__main__':
62 unittest.main()
63
Eli Bendersky3921e8e2010-05-21 09:05:39 +030064
Eli Bendersky5b09f552011-10-16 06:01:54 +020065
66
67