Home | History | Annotate | Download | only in cindex
      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_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 
     37 def test_unsaved_files():
     38     index = Index.create()
     39     tu = index.parse('fake.c', ['-I./'], unsaved_files = [
     40             ('fake.c', """
     41 #include "fake.h"
     42 int x;
     43 int 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 
     53 def 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 def normpaths_equal(path1, path2):
     62     """ Compares two paths for equality after normalizing them with
     63         os.path.normpath
     64     """
     65     return os.path.normpath(path1) == os.path.normpath(path2)
     66 
     67 def test_includes():
     68     def eq(expected, actual):
     69         if not actual.is_input_file:
     70             return  normpaths_equal(expected[0], actual.source.name) and \
     71                     normpaths_equal(expected[1], actual.include.name)
     72         else:
     73             return normpaths_equal(expected[1], actual.include.name)
     74 
     75     src = os.path.join(kInputsDir, 'include.cpp')
     76     h1 = os.path.join(kInputsDir, "header1.h")
     77     h2 = os.path.join(kInputsDir, "header2.h")
     78     h3 = os.path.join(kInputsDir, "header3.h")
     79     inc = [(src, h1), (h1, h3), (src, h2), (h2, h3)]
     80 
     81     index = Index.create()
     82     tu = index.parse(src)
     83     for i in zip(inc, tu.get_includes()):
     84         assert eq(i[0], i[1])
     85