test_translation_unit.py revision db59a7700e5e42e8b5f6f8e327067a969540ee14
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_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"
34int x;
35int 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
45def 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