Debian Codegraph @

clang/bindings/python/tests/cindex/test_code_completion.py

raw
1 import os
2 from clang.cindex import Config
3
4 if "CLANG_LIBRARY_PATH" in os.environ:
5 Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
6
7 from clang.cindex import TranslationUnit
8
9 import unittest
10 from .util import skip_if_no_fspath
11 from .util import str_to_path
12
13
14 class TestCodeCompletion(unittest.TestCase):
15 def check_completion_results(self, cr, expected):
16 self.assertIsNotNone(cr)
17 self.assertEqual(len(cr.diagnostics), 0)
18
19 completions = [str(c) for c in cr.results]
20
21 for c in expected:
22 self.assertIn(c, completions)
23
24 def test_code_complete(self):
25 files = [
26 (
27 "fake.c",
28 """
29 /// Aaa.
30 int test1;
31
32 /// Bbb.
33 void test2(void);
34
35 void f() {
36
37 }
38 """,
39 )
40 ]
41
42 tu = TranslationUnit.from_source(
43 "fake.c",
44 ["-std=c99"],
45 unsaved_files=files,
46 options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
47 )
48
49 cr = tu.codeComplete(
50 "fake.c", 9, 1, unsaved_files=files, include_brief_comments=True
51 )
52
53 expected = [
54 "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.",
55 "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.",
56 "{'return', TypedText} | {';', SemiColon} || Priority: 40 || Availability: Available || Brief comment: None",
57 ]
58 self.check_completion_results(cr, expected)
59
60 @skip_if_no_fspath
61 def test_code_complete_pathlike(self):
62 files = [
63 (
64 str_to_path("fake.c"),
65 """
66 /// Aaa.
67 int test1;
68
69 /// Bbb.
70 void test2(void);
71
72 void f() {
73
74 }
75 """,
76 )
77 ]
78
79 tu = TranslationUnit.from_source(
80 str_to_path("fake.c"),
81 ["-std=c99"],
82 unsaved_files=files,
83 options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
84 )
85
86 cr = tu.codeComplete(
87 str_to_path("fake.c"),
88 9,
89 1,
90 unsaved_files=files,
91 include_brief_comments=True,
92 )
93
94 expected = [
95 "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.",
96 "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.",
97 "{'return', TypedText} | {';', SemiColon} || Priority: 40 || Availability: Available || Brief comment: None",
98 ]
99 self.check_completion_results(cr, expected)
100
101 def test_code_complete_availability(self):
102 files = [
103 (
104 "fake.cpp",
105 """
106 class P {
107 protected:
108 int member;
109 };
110
111 class Q : public P {
112 public:
113 using P::member;
114 };
115
116 void f(P x, Q y) {
117 x.; // member is inaccessible
118 y.; // member is accessible
119 }
120 """,
121 )
122 ]
123
124 tu = TranslationUnit.from_source(
125 "fake.cpp", ["-std=c++98"], unsaved_files=files
126 )
127
128 cr = tu.codeComplete("fake.cpp", 12, 5, unsaved_files=files)
129
130 expected = [
131 "{'const', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
132 "{'volatile', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
133 "{'operator', TypedText} || Priority: 40 || Availability: Available || Brief comment: None",
134 "{'P', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
135 "{'Q', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
136 ]
137 self.check_completion_results(cr, expected)
138
139 cr = tu.codeComplete("fake.cpp", 13, 5, unsaved_files=files)
140 expected = [
141 "{'P', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None",
142 "{'P &', ResultType} | {'operator=', TypedText} | {'(', LeftParen} | {'const P &', Placeholder} | {')', RightParen} || Priority: 79 || Availability: Available || Brief comment: None",
143 "{'int', ResultType} | {'member', TypedText} || Priority: 35 || Availability: NotAccessible || Brief comment: None",
144 "{'void', ResultType} | {'~P', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 79 || Availability: Available || Brief comment: None",
145 ]
146 self.check_completion_results(cr, expected)