test_code_completion.py 2.7 KB

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