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