| 1 |
from pathlib import Path |
| 2 |
|
| 3 |
from clang.cindex import SourceLocation, SourceRange, TranslationUnit |
| 4 |
|
| 5 |
|
| 6 |
import unittest |
| 7 |
|
| 8 |
from .util import get_tu |
| 9 |
|
| 10 |
INPUTS_DIR = Path(__file__).parent / "INPUTS" |
| 11 |
|
| 12 |
|
| 13 |
def create_range(tu, line1, column1, line2, column2): |
| 14 |
return SourceRange.from_locations( |
| 15 |
SourceLocation.from_position(tu, tu.get_file(tu.spelling), line1, column1), |
| 16 |
SourceLocation.from_position(tu, tu.get_file(tu.spelling), line2, column2), |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class TestSourceRange(unittest.TestCase): |
| 21 |
def test_contains(self): |
| 22 |
tu = get_tu( |
| 23 |
"""aaaaa |
| 24 |
aaaaa |
| 25 |
aaaaa |
| 26 |
aaaaa""" |
| 27 |
) |
| 28 |
file = tu.get_file(tu.spelling) |
| 29 |
|
| 30 |
l13 = SourceLocation.from_position(tu, file, 1, 3) |
| 31 |
l21 = SourceLocation.from_position(tu, file, 2, 1) |
| 32 |
l22 = SourceLocation.from_position(tu, file, 2, 2) |
| 33 |
l23 = SourceLocation.from_position(tu, file, 2, 3) |
| 34 |
l24 = SourceLocation.from_position(tu, file, 2, 4) |
| 35 |
l25 = SourceLocation.from_position(tu, file, 2, 5) |
| 36 |
l33 = SourceLocation.from_position(tu, file, 3, 3) |
| 37 |
l31 = SourceLocation.from_position(tu, file, 3, 1) |
| 38 |
r22_24 = create_range(tu, 2, 2, 2, 4) |
| 39 |
r23_23 = create_range(tu, 2, 3, 2, 3) |
| 40 |
r24_32 = create_range(tu, 2, 4, 3, 2) |
| 41 |
r14_32 = create_range(tu, 1, 4, 3, 2) |
| 42 |
|
| 43 |
assert l13 not in r22_24 # Line before start |
| 44 |
assert l21 not in r22_24 # Column before start |
| 45 |
assert l22 in r22_24 # Colum on start |
| 46 |
assert l23 in r22_24 # Column in range |
| 47 |
assert l24 in r22_24 # Column on end |
| 48 |
assert l25 not in r22_24 # Column after end |
| 49 |
assert l33 not in r22_24 # Line after end |
| 50 |
|
| 51 |
assert l23 in r23_23 # In one-column range |
| 52 |
|
| 53 |
assert l23 not in r24_32 # Outside range in first line |
| 54 |
assert l33 not in r24_32 # Outside range in last line |
| 55 |
assert l25 in r24_32 # In range in first line |
| 56 |
assert l31 in r24_32 # In range in last line |
| 57 |
|
| 58 |
assert l21 in r14_32 # In range at start of center line |
| 59 |
assert l25 in r14_32 # In range at end of center line |
| 60 |
|
| 61 |
# In range within included file |
| 62 |
tu2 = TranslationUnit.from_source( |
| 63 |
"main.c", |
| 64 |
unsaved_files=[ |
| 65 |
( |
| 66 |
"main.c", |
| 67 |
"""int a[] = { |
| 68 |
#include "numbers.inc" |
| 69 |
}; |
| 70 |
""", |
| 71 |
), |
| 72 |
( |
| 73 |
"./numbers.inc", |
| 74 |
"""1, |
| 75 |
2, |
| 76 |
3, |
| 77 |
4 |
| 78 |
""", |
| 79 |
), |
| 80 |
], |
| 81 |
) |
| 82 |
|
| 83 |
r_curly = create_range(tu2, 1, 11, 3, 1) |
| 84 |
l_f2 = SourceLocation.from_position(tu2, tu2.get_file("./numbers.inc"), 4, 1) |
| 85 |
assert l_f2 in r_curly |
| 86 |
|
| 87 |
def test_equality(self): |
| 88 |
path = INPUTS_DIR / "testfile.c" |
| 89 |
tu = TranslationUnit.from_source(path) |
| 90 |
|
| 91 |
r1 = create_range(tu, 1, 1, 2, 2) |
| 92 |
r2 = create_range(tu, 1, 2, 2, 2) |
| 93 |
r1_2 = create_range(tu, 1, 1, 2, 2) |
| 94 |
|
| 95 |
self.assertEqual(r1, r1) |
| 96 |
self.assertEqual(r1, r1_2) |
| 97 |
self.assertNotEqual(r1, r2) |
| 98 |
self.assertNotEqual(r1, "foo") |