]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bindings/python/tests/cindex/test_translation_unit.py
Update clang to r96341.
[FreeBSD/FreeBSD.git] / bindings / python / tests / cindex / test_translation_unit.py
1 from clang.cindex import *
2 import os
3
4 kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
5
6 def test_spelling():
7     path = os.path.join(kInputsDir, 'hello.cpp')
8     index = Index.create()
9     tu = index.parse(path)
10     assert tu.spelling == path
11
12 def test_cursor():
13     path = os.path.join(kInputsDir, 'hello.cpp')
14     index = Index.create()
15     tu = index.parse(path)
16     c = tu.cursor
17     assert isinstance(c, Cursor)
18     assert c.kind is CursorKind.TRANSLATION_UNIT
19
20 def test_parse_arguments():
21     path = os.path.join(kInputsDir, 'parse_arguments.c')
22     index = Index.create()
23     tu = index.parse(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi'])
24     spellings = [c.spelling for c in tu.cursor.get_children()]
25     assert spellings[-2] == 'hello'
26     assert spellings[-1] == 'hi'
27
28 def test_unsaved_files():
29     index = Index.create()
30     # FIXME: Why can't we just use "fake.h" here (instead of /tmp/fake.h)?
31     tu = index.parse('fake.c', unsaved_files = [
32             ('fake.c', """
33 #include "/tmp/fake.h"
34 int x;
35 int SOME_DEFINE;
36 """),
37             ('/tmp/fake.h', """
38 #define SOME_DEFINE y
39 """)
40             ])
41     spellings = [c.spelling for c in tu.cursor.get_children()]
42     assert spellings[-2] == 'x'
43     assert spellings[-1] == 'y'
44
45 def test_unsaved_files_2():
46     import StringIO
47     index = Index.create()
48     tu = index.parse('fake.c', unsaved_files = [
49             ('fake.c', StringIO.StringIO('int x;'))])
50     spellings = [c.spelling for c in tu.cursor.get_children()]
51     assert spellings[-1] == 'x'
52
53
54 def test_includes():
55     def eq(expected, actual):
56         if not actual.is_input_file:
57             return expected[0] == actual.source.name and \
58                    expected[1] == actual.include.name
59         else:
60             return expected[1] == actual.include.name
61
62     src = os.path.join(kInputsDir, 'include.cpp')
63     h1 = os.path.join(kInputsDir, "header1.h")
64     h2 = os.path.join(kInputsDir, "header2.h")
65     h3 = os.path.join(kInputsDir, "header3.h")
66     inc = [(None, src), (src, h1), (h1, h3), (src, h2), (h2, h3)]
67
68     index = Index.create()
69     tu = index.parse(src)
70     for i in zip(inc, tu.get_includes()):
71         assert eq(i[0], i[1])
72
73