Skip to content

Commit ade1203

Browse files
authored
[libclang/python] Add tests for equality operators. (#138132)
Adds tests for SourceRange, SourceLocation and Cursor. This is a follow-up to #138074
1 parent 099a0fa commit ade1203

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23

34
from clang.cindex import (
45
Config,
@@ -16,6 +17,8 @@
1617

1718
from .util import get_cursor, get_tu
1819

20+
INPUTS_DIR = Path(__file__).parent / "INPUTS"
21+
1922
BASE_INPUT = "int one;\nint two;\n"
2023

2124

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

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23

34
from clang.cindex import Config, SourceLocation, SourceRange, TranslationUnit
45

@@ -9,6 +10,8 @@
910

1011
from .util import get_tu
1112

13+
INPUTS_DIR = Path(__file__).parent / "INPUTS"
14+
1215

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

0 commit comments

Comments
 (0)