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