test_translation_unit.py revision 265e6b2d17ae348ce73961866979f574c65b56f4
1from clang.cindex import *
2import os
3
4kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
5
6def 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
12def 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
20def 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
28def test_reparse_arguments():
29    path = os.path.join(kInputsDir, 'parse_arguments.c')
30    index = Index.create()
31    tu = index.parse(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi'])
32    tu.reparse()
33    spellings = [c.spelling for c in tu.cursor.get_children()]
34    assert spellings[-2] == 'hello'
35    assert spellings[-1] == 'hi'
36
37def test_unsaved_files():
38    index = Index.create()
39    tu = index.parse('fake.c', ['-I./'], unsaved_files = [
40            ('fake.c', """
41#include "fake.h"
42int x;
43int SOME_DEFINE;
44"""),
45            ('./fake.h', """
46#define SOME_DEFINE y
47""")
48            ])
49    spellings = [c.spelling for c in tu.cursor.get_children()]
50    assert spellings[-2] == 'x'
51    assert spellings[-1] == 'y'
52
53def test_unsaved_files_2():
54    import StringIO
55    index = Index.create()
56    tu = index.parse('fake.c', unsaved_files = [
57            ('fake.c', StringIO.StringIO('int x;'))])
58    spellings = [c.spelling for c in tu.cursor.get_children()]
59    assert spellings[-1] == 'x'
60
61
62def test_includes():
63    def eq(expected, actual):
64        if not actual.is_input_file:
65            return expected[0] == actual.source.name and \
66                   expected[1] == actual.include.name
67        else:
68            return expected[1] == actual.include.name
69
70    src = os.path.join(kInputsDir, 'include.cpp')
71    h1 = os.path.join(kInputsDir, "header1.h")
72    h2 = os.path.join(kInputsDir, "header2.h")
73    h3 = os.path.join(kInputsDir, "header3.h")
74    inc = [(None, src), (src, h1), (h1, h3), (src, h2), (h2, h3)]
75
76    index = Index.create()
77    tu = index.parse(src)
78    for i in zip(inc, tu.get_includes()):
79        assert eq(i[0], i[1])
80
81
82