From 93269f3ecddd1fd6746123b7e78f981b4e9e6fc2 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Thu, 25 Oct 2018 10:03:21 +0200 Subject: [PATCH] Clang: Add unit-tests for function overloads completion order Make sure that the same function overloads with different priorities come together in the completions list. Fixes the case when we complete the method without '.' or '->'. Change-Id: Icaf7ea47f5e58b3ae5cc9764ad79c857f6f6e231 Reviewed-by: Nikolai Kosjar --- .../source/codecompletionsextractor.cpp | 4 +- tests/unit/unittest/codecompleter-test.cpp | 45 +++++++++++++++++++ .../unit/unittest/data/completions_order.cpp | 19 ++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/tools/clangbackend/source/codecompletionsextractor.cpp b/src/tools/clangbackend/source/codecompletionsextractor.cpp index f9c019b66d9..b42520c7e50 100644 --- a/src/tools/clangbackend/source/codecompletionsextractor.cpp +++ b/src/tools/clangbackend/source/codecompletionsextractor.cpp @@ -115,7 +115,9 @@ static void adaptOverloadsPriorities(CodeCompletions &codeCompletions) std::map> cachedOverloads; for (CodeCompletion ¤tCompletion : codeCompletions) { if (currentCompletion.completionKind != CodeCompletion::ConstructorCompletionKind - && currentCompletion.completionKind != CodeCompletion::FunctionCompletionKind) { + && currentCompletion.completionKind != CodeCompletion::FunctionCompletionKind + && currentCompletion.completionKind + != CodeCompletion::FunctionDefinitionCompletionKind) { continue; } diff --git a/tests/unit/unittest/codecompleter-test.cpp b/tests/unit/unittest/codecompleter-test.cpp index f72a9eb8281..539afba9b67 100644 --- a/tests/unit/unittest/codecompleter-test.cpp +++ b/tests/unit/unittest/codecompleter-test.cpp @@ -580,6 +580,51 @@ TEST_F(CodeCompleterSlowTest, ConstructorHasOverloadCompletions) ASSERT_THAT(constructorsCount, 2); } +TEST_F(CodeCompleterSlowTest, FunctionOverloadsNoParametersOrder) +{ + auto myCompleter = setupCompleter(completionsOrder); + const ClangBackEnd::CodeCompletions completions = myCompleter.complete(27, 7); + + int firstIndex = Utils::indexOf(completions, [](const CodeCompletion &codeCompletion) { + return codeCompletion.text == "foo"; + }); + int secondIndex = Utils::indexOf(completions, [i = 0, firstIndex](const CodeCompletion &codeCompletion) mutable { + return (i++) > firstIndex && codeCompletion.text == "foo"; + }); + + ASSERT_THAT(abs(firstIndex - secondIndex), 1); +} + +TEST_F(CodeCompleterSlowTest, FunctionOverloadsWithParametersOrder) +{ + auto myCompleter = setupCompleter(completionsOrder); + const ClangBackEnd::CodeCompletions completions = myCompleter.complete(27, 7); + + int firstIndex = Utils::indexOf(completions, [](const CodeCompletion &codeCompletion) { + return codeCompletion.text == "bar"; + }); + int secondIndex = Utils::indexOf(completions, [i = 0, firstIndex](const CodeCompletion &codeCompletion) mutable { + return (i++) > firstIndex && codeCompletion.text == "bar"; + }); + + ASSERT_THAT(abs(firstIndex - secondIndex), 1); +} + +TEST_F(CodeCompleterSlowTest, FunctionOverloadsWithoutDotOrArrowOrder) +{ + auto myCompleter = setupCompleter(completionsOrder); + const ClangBackEnd::CodeCompletions completions = myCompleter.complete(21, 1); + + int firstIndex = Utils::indexOf(completions, [](const CodeCompletion &codeCompletion) { + return codeCompletion.text == "bar"; + }); + int secondIndex = Utils::indexOf(completions, [i = 0, firstIndex](const CodeCompletion &codeCompletion) mutable { + return (i++) > firstIndex && codeCompletion.text == "bar"; + }); + + ASSERT_THAT(abs(firstIndex - secondIndex), 1); +} + ClangBackEnd::CodeCompleter CodeCompleter::setupCompleter( const ClangBackEnd::FileContainer &fileContainer) { diff --git a/tests/unit/unittest/data/completions_order.cpp b/tests/unit/unittest/data/completions_order.cpp index 6ede7573d85..6c363a6c504 100644 --- a/tests/unit/unittest/data/completions_order.cpp +++ b/tests/unit/unittest/data/completions_order.cpp @@ -7,3 +7,22 @@ public: void testConstructor() { } + +class Base { + virtual void bar(int a) const; +}; + +class DifferentPriorities : public Base { +public: + void foo(); + void foo() const; + void bar(int a) const override; + void testBar() { + + } +}; + +void testPriorities() { + DifferentPriorities d; + d. +}