Skip to content

[libclang/python] Add tests for equality operators. #138132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions clang/bindings/python/tests/cindex/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,18 @@ def test_specialized_template(self):
self.assertNotEqual(foos[0], foos[1])
self.assertEqual(foos[0], prime_foo)
self.assertIsNone(tu.cursor.specialized_template)

def test_equality(self):
tu = get_tu(CHILDREN_TEST, lang="cpp")
cursor1 = get_cursor(tu, "s0")
cursor1_2 = get_cursor(tu, "s0")
cursor2 = get_cursor(tu, "f0")

self.assertIsNotNone(cursor1)
self.assertIsNotNone(cursor1_2)
self.assertIsNotNone(cursor2)

self.assertEqual(cursor1, cursor1)
self.assertEqual(cursor1, cursor1_2)
self.assertNotEqual(cursor1, cursor2)
self.assertNotEqual(cursor1, "foo")
21 changes: 21 additions & 0 deletions clang/bindings/python/tests/cindex/test_location.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path

from clang.cindex import (
Config,
Expand All @@ -16,6 +17,8 @@

from .util import get_cursor, get_tu

INPUTS_DIR = Path(__file__).parent / "INPUTS"

BASE_INPUT = "int one;\nint two;\n"


Expand Down Expand Up @@ -151,3 +154,21 @@ def test_operator_lt(self):
assert l_t1_12 < l_t2_13 < l_t1_14
assert not l_t2_13 < l_t1_12
assert not l_t1_14 < l_t2_13

def test_equality(self):
path = INPUTS_DIR / "testfile.c"
path_a = INPUTS_DIR / "a.inc"
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)

location1 = SourceLocation.from_position(tu, main_file, 1, 3)
location2 = SourceLocation.from_position(tu, main_file, 2, 2)
location1_2 = SourceLocation.from_position(tu, main_file, 1, 3)
file2_location1 = SourceLocation.from_position(tu, a_file, 1, 3)

self.assertEqual(location1, location1)
self.assertEqual(location1, location1_2)
self.assertNotEqual(location1, location2)
self.assertNotEqual(location1, file2_location1)
self.assertNotEqual(location1, "foo")
16 changes: 16 additions & 0 deletions clang/bindings/python/tests/cindex/test_source_range.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path

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

Expand All @@ -9,6 +10,8 @@

from .util import get_tu

INPUTS_DIR = Path(__file__).parent / "INPUTS"


def create_range(tu, line1, column1, line2, column2):
return SourceRange.from_locations(
Expand Down Expand Up @@ -83,3 +86,16 @@ def test_contains(self):
r_curly = create_range(tu2, 1, 11, 3, 1)
l_f2 = SourceLocation.from_position(tu2, tu2.get_file("./numbers.inc"), 4, 1)
assert l_f2 in r_curly

def test_equality(self):
path = INPUTS_DIR / "testfile.c"
tu = TranslationUnit.from_source(path)

r1 = create_range(tu, 1, 1, 2, 2)
r2 = create_range(tu, 1, 2, 2, 2)
r1_2 = create_range(tu, 1, 1, 2, 2)

self.assertEqual(r1, r1)
self.assertEqual(r1, r1_2)
self.assertNotEqual(r1, r2)
self.assertNotEqual(r1, "foo")