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

Conversation

DeinAlptraum
Copy link
Contributor

Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to #138074

Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to llvm#138074
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels May 1, 2025
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

Changes

Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to #138074


Full diff: https://github.com/llvm/llvm-project/pull/138132.diff

3 Files Affected:

  • (modified) clang/bindings/python/tests/cindex/test_cursor.py (+15)
  • (modified) clang/bindings/python/tests/cindex/test_location.py (+20)
  • (modified) clang/bindings/python/tests/cindex/test_source_range.py (+15)
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 82f40c60afa59..b90a0495ca7be 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -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")
diff --git a/clang/bindings/python/tests/cindex/test_location.py b/clang/bindings/python/tests/cindex/test_location.py
index 21c6169bb0a6f..f1a4589738f52 100644
--- a/clang/bindings/python/tests/cindex/test_location.py
+++ b/clang/bindings/python/tests/cindex/test_location.py
@@ -16,6 +16,8 @@
 
 from .util import get_cursor, get_tu
 
+INPUTS_DIR = os.path.join(os.path.dirname(__file__), "INPUTS")
+
 BASE_INPUT = "int one;\nint two;\n"
 
 
@@ -151,3 +153,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 = os.path.join(INPUTS_DIR, "testfile.c")
+        path_a = os.path.join(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")
diff --git a/clang/bindings/python/tests/cindex/test_source_range.py b/clang/bindings/python/tests/cindex/test_source_range.py
index 81c0a9b05cff8..87b17caaac69b 100644
--- a/clang/bindings/python/tests/cindex/test_source_range.py
+++ b/clang/bindings/python/tests/cindex/test_source_range.py
@@ -9,6 +9,8 @@
 
 from .util import get_tu
 
+INPUTS_DIR = os.path.join(os.path.dirname(__file__), "INPUTS")
+
 
 def create_range(tu, line1, column1, line2, column2):
     return SourceRange.from_locations(
@@ -83,3 +85,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 = os.path.join(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")

@DeinAlptraum
Copy link
Contributor Author

DeinAlptraum commented May 1, 2025

@Endilll I added the tests as discussed on #138074 .
I'm just not sure how/if we can reasonably test the else path for briefComment: this requires a libclang.so built before LLVM 3.2 (the check was added 13 years ago in b947e82). In fact, I'm not entirely sure what the point here is: being able to use the same libclang.so with a newer cindex.py and then returning nonsense instead of erroring out surely can't be the goal, right? Perhaps we're better off removing the if/else part entirely?

@Endilll
Copy link
Contributor

Endilll commented May 1, 2025

@Endilll I added the tests as discussed on #138074 .

Thank you!

I'm just not sure how/if we can reasonably test the else path for briefComment: this requires a libclang.so built before LLVM 3.2 (the check was added 13 years ago in b947e82). In fact, I'm not entirely sure what the point here is: being able to use the same libclang.so with a newer cindex.py and then returning nonsense instead of erroring out surely can't be the goal, right? Perhaps we're better off removing the if/else part entirely?

I believe Python bindings are designed to work with the matching libclang version, and we don't have to support quirks of older libclang versions. Users of older versions should use matching bindings version instead.

@DeinAlptraum
Copy link
Contributor Author

DeinAlptraum commented May 1, 2025

@Endilll Thank you, sounds good. I put the removal of the compatibility stuff in a separate PR here: #138135

@DeinAlptraum DeinAlptraum merged commit ade1203 into llvm:main May 2, 2025
13 checks passed
@DeinAlptraum DeinAlptraum deleted the typing-pre-tests branch May 2, 2025 04:24
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to llvm#138074
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to llvm#138074
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to llvm#138074
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Adds tests for SourceRange, SourceLocation and Cursor.
This is a follow-up to llvm#138074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants