blob: afa4d5eaeb44f1e24d87925efd9eb0304b05d820 [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 Benderskyd0973782012-01-19 08:09:33 +020031 ast = parse_file(self._find_file('memmgr.c'), use_cpp=True,
Eli Bendersky5b09f552011-10-16 06:01:54 +020032 cpp_path=CPPPATH,
Eli Benderskyd0973782012-01-19 08:09:33 +020033 cpp_args=r'-Iutils/fake_libc_include')
Eli Bendersky5b09f552011-10-16 06:01:54 +020034 self.failUnless(isinstance(ast, c_ast.FileAST))
35
Eli Benderskyd0973782012-01-19 08:09:33 +020036 ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
Eli Bendersky5b09f552011-10-16 06:01:54 +020037 cpp_path=CPPPATH,
Eli Benderskyd0973782012-01-19 08:09:33 +020038 cpp_args=r'-Iutils/fake_libc_include')
Eli Bendersky5b09f552011-10-16 06:01:54 +020039
40 self.failUnless(isinstance(ast2, c_ast.FileAST))
41
42
43if __name__ == '__main__':
44 unittest.main()
45
Eli Bendersky3921e8e2010-05-21 09:05:39 +030046
Eli Bendersky5b09f552011-10-16 06:01:54 +020047
48
49