Skip to content

Commit 74cf17d

Browse files
committed
[libclang/python] Add tests for equality operators.
Adds tests for SourceRange, SourceLocation and Cursor. This is a follow-up to #138074
1 parent 4994174 commit 74cf17d

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

clang/bindings/python/tests/cindex/test_cursor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,3 +1035,18 @@ def test_specialized_template(self):
10351035
self.assertNotEqual(foos[0], foos[1])
10361036
self.assertEqual(foos[0], prime_foo)
10371037
self.assertIsNone(tu.cursor.specialized_template)
1038+
1039+
def test_equality(self):
1040+
tu = get_tu(CHILDREN_TEST, lang="cpp")
1041+
cursor1 = get_cursor(tu, "s0")
1042+
cursor1_2 = get_cursor(tu, "s0")
1043+
cursor2 = get_cursor(tu, "f0")
1044+
1045+
self.assertIsNotNone(cursor1)
1046+
self.assertIsNotNone(cursor1_2)
1047+
self.assertIsNotNone(cursor2)
1048+
1049+
self.assertEqual(cursor1, cursor1)
1050+
self.assertEqual(cursor1, cursor1_2)
1051+
self.assertNotEqual(cursor1, cursor2)
1052+
self.assertNotEqual(cursor1, "foo")

clang/bindings/python/tests/cindex/test_location.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from .util import get_cursor, get_tu
1818

19+
INPUTS_DIR = os.path.join(os.path.dirname(__file__), "INPUTS")
20+
1921
BASE_INPUT = "int one;\nint two;\n"
2022

2123

@@ -151,3 +153,21 @@ def test_operator_lt(self):
151153
assert l_t1_12 < l_t2_13 < l_t1_14
152154
assert not l_t2_13 < l_t1_12
153155
assert not l_t1_14 < l_t2_13
156+
157+
def test_equality(self):
158+
path = os.path.join(INPUTS_DIR, "testfile.c")
159+
path_a = os.path.join(INPUTS_DIR, "a.inc")
160+
tu = TranslationUnit.from_source(path)
161+
main_file = File.from_name(tu, path)
162+
a_file = File.from_name(tu, path_a)
163+
164+
location1 = SourceLocation.from_position(tu, main_file, 1, 3)
165+
location2 = SourceLocation.from_position(tu, main_file, 2, 2)
166+
location1_2 = SourceLocation.from_position(tu, main_file, 1, 3)
167+
file2_location1 = SourceLocation.from_position(tu, a_file, 1, 3)
168+
169+
self.assertEqual(location1, location1)
170+
self.assertEqual(location1, location1_2)
171+
self.assertNotEqual(location1, location2)
172+
self.assertNotEqual(location1, file2_location1)
173+
self.assertNotEqual(location1, "foo")

clang/bindings/python/tests/cindex/test_source_range.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from .util import get_tu
1111

12+
INPUTS_DIR = os.path.join(os.path.dirname(__file__), "INPUTS")
13+
1214

1315
def create_range(tu, line1, column1, line2, column2):
1416
return SourceRange.from_locations(
@@ -83,3 +85,16 @@ def test_contains(self):
8385
r_curly = create_range(tu2, 1, 11, 3, 1)
8486
l_f2 = SourceLocation.from_position(tu2, tu2.get_file("./numbers.inc"), 4, 1)
8587
assert l_f2 in r_curly
88+
89+
def test_equality(self):
90+
path = os.path.join(INPUTS_DIR, "testfile.c")
91+
tu = TranslationUnit.from_source(path)
92+
93+
r1 = create_range(tu, 1, 1, 2, 2)
94+
r2 = create_range(tu, 1, 2, 2, 2)
95+
r1_2 = create_range(tu, 1, 1, 2, 2)
96+
97+
self.assertEqual(r1, r1)
98+
self.assertEqual(r1, r1_2)
99+
self.assertNotEqual(r1, r2)
100+
self.assertNotEqual(r1, "foo")

0 commit comments

Comments
 (0)