blob: 087681a2ab3bc7318dbef929ef899596c2f83fbc [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 Bendersky5b09f552011-10-16 06:01:54 +020028 self.failUnless(isinstance(ast, c_ast.FileAST))
29
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 Bendersky5b09f552011-10-16 06:01:54 +020035 self.failUnless(isinstance(ast, c_ast.FileAST))
36
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
43 self.failUnless(isinstance(ast2, c_ast.FileAST))
44
Eli Bendersky5a0abdf2012-07-07 06:52:23 +030045 def test_no_real_content_after_cpp(self):
46 ast = parse_file(self._find_file('empty.h'), use_cpp=True,
47 cpp_path=CPPPATH)
48 self.failUnless(isinstance(ast, c_ast.FileAST))
Eli Bendersky5b09f552011-10-16 06:01:54 +020049
50if __name__ == '__main__':
51 unittest.main()
52
Eli Bendersky3921e8e2010-05-21 09:05:39 +030053
Eli Bendersky5b09f552011-10-16 06:01:54 +020054
55
56