test_location.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from clang.cindex import Cursor
  2. from clang.cindex import File
  3. from clang.cindex import SourceLocation
  4. from clang.cindex import SourceRange
  5. from .util import get_cursor
  6. from .util import get_tu
  7. baseInput="int one;\nint two;\n"
  8. def assert_location(loc, line, column, offset):
  9. assert loc.line == line
  10. assert loc.column == column
  11. assert loc.offset == offset
  12. def test_location():
  13. tu = get_tu(baseInput)
  14. one = get_cursor(tu, 'one')
  15. two = get_cursor(tu, 'two')
  16. assert one is not None
  17. assert two is not None
  18. assert_location(one.location,line=1,column=5,offset=4)
  19. assert_location(two.location,line=2,column=5,offset=13)
  20. # adding a linebreak at top should keep columns same
  21. tu = get_tu('\n' + baseInput)
  22. one = get_cursor(tu, 'one')
  23. two = get_cursor(tu, 'two')
  24. assert one is not None
  25. assert two is not None
  26. assert_location(one.location,line=2,column=5,offset=5)
  27. assert_location(two.location,line=3,column=5,offset=14)
  28. # adding a space should affect column on first line only
  29. tu = get_tu(' ' + baseInput)
  30. one = get_cursor(tu, 'one')
  31. two = get_cursor(tu, 'two')
  32. assert_location(one.location,line=1,column=6,offset=5)
  33. assert_location(two.location,line=2,column=5,offset=14)
  34. # define the expected location ourselves and see if it matches
  35. # the returned location
  36. tu = get_tu(baseInput)
  37. file = File.from_name(tu, 't.c')
  38. location = SourceLocation.from_position(tu, file, 1, 5)
  39. cursor = Cursor.from_location(tu, location)
  40. one = get_cursor(tu, 'one')
  41. assert one is not None
  42. assert one == cursor
  43. # Ensure locations referring to the same entity are equivalent.
  44. location2 = SourceLocation.from_position(tu, file, 1, 5)
  45. assert location == location2
  46. location3 = SourceLocation.from_position(tu, file, 1, 4)
  47. assert location2 != location3
  48. offset_location = SourceLocation.from_offset(tu, file, 5)
  49. cursor = Cursor.from_location(tu, offset_location)
  50. verified = False
  51. for n in [n for n in tu.cursor.get_children() if n.spelling == 'one']:
  52. assert n == cursor
  53. verified = True
  54. assert verified
  55. def test_extent():
  56. tu = get_tu(baseInput)
  57. one = get_cursor(tu, 'one')
  58. two = get_cursor(tu, 'two')
  59. assert_location(one.extent.start,line=1,column=1,offset=0)
  60. assert_location(one.extent.end,line=1,column=8,offset=7)
  61. assert baseInput[one.extent.start.offset:one.extent.end.offset] == "int one"
  62. assert_location(two.extent.start,line=2,column=1,offset=9)
  63. assert_location(two.extent.end,line=2,column=8,offset=16)
  64. assert baseInput[two.extent.start.offset:two.extent.end.offset] == "int two"
  65. file = File.from_name(tu, 't.c')
  66. location1 = SourceLocation.from_position(tu, file, 1, 1)
  67. location2 = SourceLocation.from_position(tu, file, 1, 8)
  68. range1 = SourceRange.from_locations(location1, location2)
  69. range2 = SourceRange.from_locations(location1, location2)
  70. assert range1 == range2
  71. location3 = SourceLocation.from_position(tu, file, 1, 6)
  72. range3 = SourceRange.from_locations(location1, location3)
  73. assert range1 != range3