Files
Jannick Kremer 2f076c383e [libclang/python] Deprecate _CXUnsavedFile, introduce UnsavedFile instead (#187412)
To maintain consistency with other classes mirrored from libclang, the
`_CX` prefix should be removed.
The old name is introduced as a subclass of the renamed class, and
deprecation warnings are added on every attribute access.
2026-03-20 08:33:01 +09:00

81 lines
2.5 KiB
Python

import os
from clang.cindex import _CXUnsavedFile, File, Index, TranslationUnit
import unittest
import warnings
inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")
class TestFile(unittest.TestCase):
def test_file(self):
index = Index.create()
tu = index.parse("t.c", unsaved_files=[("t.c", "")])
file = File.from_name(tu, "t.c")
self.assertEqual(str(file), "t.c")
self.assertEqual(file.name, "t.c")
self.assertEqual(repr(file), "<File: t.c>")
def test_file_eq(self):
path = os.path.join(inputs_dir, "testfile.c")
path_a = os.path.join(inputs_dir, "a.inc")
path_b = os.path.join(inputs_dir, "b.inc")
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)
a_file2 = File.from_name(tu, path_a)
b_file = File.from_name(tu, path_b)
self.assertEqual(a_file, a_file2)
self.assertNotEqual(a_file, b_file)
self.assertNotEqual(main_file, a_file)
self.assertNotEqual(main_file, b_file)
self.assertNotEqual(main_file, "t.c")
def test_file_eq_in_memory(self):
tu = TranslationUnit.from_source(
"testfile.c",
unsaved_files=[
(
"testfile.c",
"""
int a[] = {
#include "a.inc"
};
int b[] = {
#include "b.inc"
};
""",
),
("a.inc", "1,2,3"),
("b.inc", "1,2,3"),
],
)
path = os.path.join(inputs_dir, "testfile.c")
path_a = os.path.join(inputs_dir, "a.inc")
path_b = os.path.join(inputs_dir, "b.inc")
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)
a_file2 = File.from_name(tu, path_a)
b_file = File.from_name(tu, path_b)
self.assertEqual(a_file, a_file2)
self.assertNotEqual(a_file, b_file)
self.assertNotEqual(main_file, a_file)
self.assertNotEqual(main_file, b_file)
self.assertNotEqual(main_file, "a.inc")
class TestUnsavedFile(unittest.TestCase):
def test_deprecation_warning(self):
unsaved_file = _CXUnsavedFile()
with warnings.catch_warnings(record=True) as log:
unsaved_file.name
self.assertEqual(len(log), 1)
for warning in log:
self.assertIsInstance(warning.message, DeprecationWarning)