[clang] [libclang/python] Fix bugs in custom enum implementation and add tests (PR #95381)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 13 03:49:13 PDT 2024


================
@@ -0,0 +1,50 @@
+import unittest
+
+from clang.cindex import (
+    CursorKind,
+    TemplateArgumentKind,
+    ExceptionSpecificationKind,
+    AvailabilityKind,
+    AccessSpecifier,
+    TypeKind,
+    RefQualifierKind,
+    LinkageKind,
+    TLSKind,
+    StorageClass,
+)
+
+
+class TestCursorKind(unittest.TestCase):
+    enums = [
+        CursorKind,
+        TemplateArgumentKind,
+        ExceptionSpecificationKind,
+        AvailabilityKind,
+        AccessSpecifier,
+        TypeKind,
+        RefQualifierKind,
+        LinkageKind,
+        TLSKind,
+        StorageClass,
+    ]
+
+    def test_from_id(self):
+        """Check that kinds can be constructed from valid IDs"""
+        for enum in self.enums:
+            self.assertEqual(enum.from_id(2), enum._kinds[2])
+            with self.assertRaises(ValueError):
+                enum.from_id(len(enum._kinds))
+            with self.assertRaises(ValueError):
+                enum.from_id(-1)
+
+    def test_unique_kinds(self):
+        """Check that no kind name has been used multiple times"""
+        for enum in self.enums:
+            seen_names = set()
+            for id in range(len(enum._kinds)):
+                try:
+                    kind_name = enum.from_id(id).name
+                except ValueError:
+                    continue
+                self.assertNotIn(kind_name, seen_names)
+                seen_names.add(id)
----------------
Endilll wrote:

I think you can just add them to the set, and then check whether it has the same length as `enum._kinds`.

https://github.com/llvm/llvm-project/pull/95381


More information about the cfe-commits mailing list