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