Home | History | Annotate | Download | only in cindex
      1 from clang.cindex import Index
      2 
      3 baseInput="int one;\nint two;\n"
      4 
      5 def assert_location(loc, line, column, offset):
      6     assert loc.line == line
      7     assert loc.column == column
      8     assert loc.offset == offset
      9 
     10 def test_location():
     11     index = Index.create()
     12     tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
     13 
     14     for n in tu.cursor.get_children():
     15         if n.spelling == 'one':
     16             assert_location(n.location,line=1,column=5,offset=4)
     17         if n.spelling == 'two':
     18             assert_location(n.location,line=2,column=5,offset=13)
     19 
     20     # adding a linebreak at top should keep columns same
     21     tu = index.parse('t.c', unsaved_files = [('t.c',"\n"+baseInput)])
     22 
     23     for n in tu.cursor.get_children():
     24         if n.spelling == 'one':
     25             assert_location(n.location,line=2,column=5,offset=5)
     26         if n.spelling == 'two':
     27             assert_location(n.location,line=3,column=5,offset=14)
     28 
     29     # adding a space should affect column on first line only
     30     tu = index.parse('t.c', unsaved_files = [('t.c'," "+baseInput)])
     31 
     32     for n in tu.cursor.get_children():
     33         if n.spelling == 'one':
     34             assert_location(n.location,line=1,column=6,offset=5)
     35         if n.spelling == 'two':
     36             assert_location(n.location,line=2,column=5,offset=14)
     37 
     38 def test_extent():
     39     index = Index.create()
     40     tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
     41 
     42     for n in tu.cursor.get_children():
     43         if n.spelling == 'one':
     44             assert_location(n.extent.start,line=1,column=1,offset=0)
     45             assert_location(n.extent.end,line=1,column=8,offset=7)
     46             assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int one"
     47         if n.spelling == 'two':
     48             assert_location(n.extent.start,line=2,column=1,offset=9)
     49             assert_location(n.extent.end,line=2,column=8,offset=16)
     50             assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int two"
     51