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