Files
clang-p2996/clang/test/bindings/python/tests/cindex/test_enums.py
Rainer Orth 90c9cc2c98 [clang][python][test] Move python binding tests to lit framework (#145855)
As discussed in PR #142353, the current testsuite of the `clang` Python
bindings has several issues:

- If `libclang.so` cannot be loaded into `python` to run the testsuite,
the whole `ninja check-all` aborts.
- The result of running the testsuite isn't report like the `lit`-based
tests, rendering them almost invisible.
- The testsuite is disabled in a non-obvious way (`RUN_PYTHON_TESTS`) in
`tests/CMakeLists.txt`, which again doesn't show up in the test results.

All these issues can be avoided by integrating the Python bindings tests
with `lit`, which is what this patch does:

- The actual test lives in `clang/test/bindings/python/bindings.sh` and
is run by `lit`.
- The current `clang/bindings/python/tests` directory (minus the
now-superfluous `CMakeLists.txt`) is moved into the same directory.
- The check if `libclang` is loadable (originally from PR #142353) is
now handled via a new `lit` feature, `libclang-loadable`.
- The various ways to disable the tests have been turned into `XFAIL`s
as appropriate.
- AArch64 doesn't `FAIL` any longer, so no `XFAIL` is necessary.
- It keeps the `check-clang-python` target for use by the Clang Python
CI.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.
2025-06-26 16:34:10 +02:00

52 lines
1.4 KiB
Python

import unittest
from clang.cindex import (
AccessSpecifier,
AvailabilityKind,
BinaryOperator,
CursorKind,
ExceptionSpecificationKind,
LinkageKind,
RefQualifierKind,
StorageClass,
TemplateArgumentKind,
TLSKind,
TokenKind,
TypeKind,
)
class TestEnums(unittest.TestCase):
enums = [
TokenKind,
CursorKind,
TemplateArgumentKind,
ExceptionSpecificationKind,
AvailabilityKind,
AccessSpecifier,
TypeKind,
RefQualifierKind,
LinkageKind,
TLSKind,
StorageClass,
BinaryOperator,
]
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(2))
max_value = max([variant.value for variant in enum])
with self.assertRaises(ValueError):
enum.from_id(max_value + 1)
with self.assertRaises(ValueError):
enum.from_id(-1)
def test_duplicate_ids(self):
"""Check that no two kinds have the same id"""
# for enum in self.enums:
for enum in self.enums:
num_declared_variants = len(enum._member_map_.keys())
num_unique_variants = len(list(enum))
self.assertEqual(num_declared_variants, num_unique_variants)