xref: /llvm-project/clang/bindings/python/tests/cindex/test_comment.py (revision 71cfa381ef8c4fe659c67e8b2901d767e10f2aff)
1import os
2
3from clang.cindex import Config, TranslationUnit
4
5if "CLANG_LIBRARY_PATH" in os.environ:
6    Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
7
8import unittest
9
10from .util import get_cursor
11
12
13class TestComment(unittest.TestCase):
14    def test_comment(self):
15        files = [
16            (
17                "fake.c",
18                """
19/// Aaa.
20int test1;
21
22/// Bbb.
23/// x
24void test2(void);
25
26void f() {
27
28}
29""",
30            )
31        ]
32        # make a comment-aware TU
33        tu = TranslationUnit.from_source(
34            "fake.c",
35            ["-std=c99"],
36            unsaved_files=files,
37            options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
38        )
39        test1 = get_cursor(tu, "test1")
40        self.assertIsNotNone(test1, "Could not find test1.")
41        self.assertTrue(test1.type.is_pod())
42        raw = test1.raw_comment
43        brief = test1.brief_comment
44        self.assertEqual(raw, """/// Aaa.""")
45        self.assertEqual(brief, """Aaa.""")
46
47        test2 = get_cursor(tu, "test2")
48        raw = test2.raw_comment
49        brief = test2.brief_comment
50        self.assertEqual(raw, """/// Bbb.\n/// x""")
51        self.assertEqual(brief, """Bbb. x""")
52
53        f = get_cursor(tu, "f")
54        raw = f.raw_comment
55        brief = f.brief_comment
56        self.assertEqual(raw, "")
57        self.assertEqual(brief, "")
58