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