Changeset View
Changeset View
Standalone View
Standalone View
plugins/clang/tests/test_assistants.cpp
Show All 14 Lines | 1 | /* This file is part of KDevelop | |||
---|---|---|---|---|---|
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | Boston, MA 02110-1301, USA. | 17 | Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "test_assistants.h" | 20 | #include "test_assistants.h" | ||
21 | 21 | | |||
22 | #include "codegen/clangrefactoring.h" | 22 | #include "codegen/clangrefactoring.h" | ||
23 | #include "codegen/adaptsignatureassistant.h" | ||||
23 | 24 | | |||
24 | #include <QTest> | 25 | #include <QTest> | ||
25 | #include <QLoggingCategory> | 26 | #include <QLoggingCategory> | ||
26 | #include <QTemporaryDir> | 27 | #include <QTemporaryDir> | ||
27 | 28 | | |||
28 | #include <KLocalizedString> | 29 | #include <KLocalizedString> | ||
29 | 30 | | |||
30 | #include <KTextEditor/View> | 31 | #include <KTextEditor/View> | ||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Line(s) | 298 | const auto renameProblemIt = std::find_if(problems.cbegin(), problems.cend(), [](const ProblemPointer& p) { | |||
298 | return dynamic_cast<const StaticAssistantProblem*>(p.constData()); | 299 | return dynamic_cast<const StaticAssistantProblem*>(p.constData()); | ||
299 | }); | 300 | }); | ||
300 | if (renameProblemIt != problems.cend()) | 301 | if (renameProblemIt != problems.cend()) | ||
301 | return *renameProblemIt; | 302 | return *renameProblemIt; | ||
302 | 303 | | |||
303 | return {}; | 304 | return {}; | ||
304 | } | 305 | } | ||
305 | 306 | | |||
307 | template <typename T> | ||||
308 | ProblemPointer findProblemWithAssistant(const QVector<ProblemPointer>& problems) | ||||
309 | { | ||||
310 | const auto problemIterator = std::find_if(problems.cbegin(), problems.cend(), [](const ProblemPointer& p) { | ||||
311 | const auto problem = dynamic_cast<const StaticAssistantProblem*>(p.constData()); | ||||
312 | return problem && dynamic_cast<const T*>(problem->solutionAssistant().constData()); | ||||
313 | }); | ||||
314 | if (problemIterator != problems.cend()) | ||||
315 | return *problemIterator; | ||||
316 | | ||||
317 | return {}; | ||||
318 | } | ||||
319 | | ||||
306 | void TestAssistants::testRenameAssistant() | 320 | void TestAssistants::testRenameAssistant() | ||
307 | { | 321 | { | ||
308 | QFETCH(QString, fileContents); | 322 | QFETCH(QString, fileContents); | ||
309 | Testbed testbed(QString(), fileContents); | 323 | Testbed testbed(QString(), fileContents); | ||
310 | 324 | | |||
311 | const auto document = testbed.document(Testbed::CppDoc); | 325 | const auto document = testbed.document(Testbed::CppDoc); | ||
312 | QVERIFY(document); | 326 | QVERIFY(document); | ||
313 | 327 | | |||
▲ Show 20 Lines • Show All 209 Lines • ▼ Show 20 Line(s) | 534 | { | |||
523 | const auto document = testbed.document(stateChange.document); | 537 | const auto document = testbed.document(stateChange.document); | ||
524 | QVERIFY(document); | 538 | QVERIFY(document); | ||
525 | 539 | | |||
526 | DUChainReadLocker lock; | 540 | DUChainReadLocker lock; | ||
527 | 541 | | |||
528 | auto topCtx = DUChain::self()->chainForDocument(document->url()); | 542 | auto topCtx = DUChain::self()->chainForDocument(document->url()); | ||
529 | QVERIFY(topCtx); | 543 | QVERIFY(topCtx); | ||
530 | 544 | | |||
531 | const auto problem = findStaticAssistantProblem(DUChainUtils::allProblemsForContext(topCtx)); | 545 | const auto problem = findProblemWithAssistant<AdaptSignatureAssistant>(DUChainUtils::allProblemsForContext(topCtx)); | ||
532 | if (problem) { | 546 | if (problem) { | ||
533 | assistant = problem->solutionAssistant(); | 547 | assistant = problem->solutionAssistant(); | ||
534 | } | 548 | } | ||
535 | 549 | | |||
536 | if (stateChange.result == SHOULD_ASSIST) { | 550 | if (stateChange.result == SHOULD_ASSIST) { | ||
537 | #if CINDEX_VERSION_MINOR < 35 | 551 | #if CINDEX_VERSION_MINOR < 35 | ||
538 | QEXPECT_FAIL("change_function_type", "Clang sees that return type of out-of-line definition differs from that in the declaration and won't parse the code...", Abort); | 552 | QEXPECT_FAIL("change_function_type", "Clang sees that return type of out-of-line definition differs from that in the declaration and won't parse the code...", Abort); | ||
539 | QEXPECT_FAIL("change_return_type_impl", "Clang sees that return type of out-of-line definition differs from that in the declaration and won't include the function's AST and thus we never get updated about the new return type...", Abort); | 553 | QEXPECT_FAIL("change_return_type_impl", "Clang sees that return type of out-of-line definition differs from that in the declaration and won't include the function's AST and thus we never get updated about the new return type...", Abort); | ||
▲ Show 20 Lines • Show All 253 Lines • ▼ Show 20 Line(s) | 802 | QTest::newRow("move-unexposed-type") | |||
793 | << QualifiedIdentifier(QStringLiteral("move")); | 807 | << QualifiedIdentifier(QStringLiteral("move")); | ||
794 | QTest::newRow("move-constructor") | 808 | QTest::newRow("move-constructor") | ||
795 | << QStringLiteral("class Class{Class(){}\n};") | 809 | << QStringLiteral("class Class{Class(){}\n};") | ||
796 | << QString() | 810 | << QString() | ||
797 | << QStringLiteral("class Class{Class();\n};") | 811 | << QStringLiteral("class Class{Class();\n};") | ||
798 | << QStringLiteral("Class::Class() {}\n") | 812 | << QStringLiteral("Class::Class() {}\n") | ||
799 | << QualifiedIdentifier(QStringLiteral("Class::Class")); | 813 | << QualifiedIdentifier(QStringLiteral("Class::Class")); | ||
800 | } | 814 | } | ||
815 | | ||||
816 | void TestAssistants::testHeaderGuardAssistant() | ||||
817 | { | ||||
818 | CodeRepresentation::setDiskChangesForbidden(false); | ||||
819 | | ||||
820 | QFETCH(QString, filename); | ||||
821 | QFETCH(QString, code); | ||||
822 | QFETCH(QString, pragmaExpected); | ||||
823 | QFETCH(QString, macroExpected); | ||||
824 | | ||||
825 | TestFile pragmaFile (code, QStringLiteral("h")); | ||||
826 | TestFile macroFile (code, QStringLiteral("h"), filename); | ||||
827 | | ||||
828 | QExplicitlySharedDataPointer<IAssistant> pragmaAssistant; | ||||
829 | QExplicitlySharedDataPointer<IAssistant> macroAssistant; | ||||
830 | | ||||
831 | pragmaFile.parse(TopDUContext::Empty); | ||||
832 | macroFile.parse(TopDUContext::Empty); | ||||
833 | QVERIFY(pragmaFile.waitForParsed()); | ||||
834 | QVERIFY(macroFile.waitForParsed()); | ||||
835 | | ||||
836 | DUChainReadLocker lock; | ||||
837 | const auto pragmaTopContext = DUChain::self()->chainForDocument(pragmaFile.url()); | ||||
838 | const auto macroTopContext = DUChain::self()->chainForDocument(macroFile.url()); | ||||
839 | QVERIFY(pragmaTopContext); | ||||
840 | QVERIFY(macroTopContext); | ||||
841 | | ||||
842 | const auto pragmaProblem = findStaticAssistantProblem(DUChainUtils::allProblemsForContext(pragmaTopContext)); | ||||
843 | const auto macroProblem = findStaticAssistantProblem(DUChainUtils::allProblemsForContext(macroTopContext)); | ||||
844 | QVERIFY(pragmaProblem && macroProblem); | ||||
845 | pragmaAssistant = pragmaProblem->solutionAssistant(); | ||||
846 | macroAssistant = macroProblem->solutionAssistant(); | ||||
847 | QVERIFY(pragmaAssistant && macroAssistant); | ||||
848 | | ||||
849 | pragmaAssistant->actions()[0]->execute(); | ||||
850 | macroAssistant->actions()[1]->execute(); | ||||
851 | | ||||
852 | QCOMPARE(pragmaFile.fileContents(), pragmaExpected); | ||||
853 | QCOMPARE(macroFile.fileContents(), macroExpected); | ||||
854 | | ||||
855 | CodeRepresentation::setDiskChangesForbidden(true); | ||||
856 | } | ||||
857 | | ||||
858 | void TestAssistants::testHeaderGuardAssistant_data() | ||||
859 | { | ||||
860 | QTest::addColumn<QString>("filename"); | ||||
861 | QTest::addColumn<QString>("code"); | ||||
862 | QTest::addColumn<QString>("pragmaExpected"); | ||||
863 | QTest::addColumn<QString>("macroExpected"); | ||||
864 | | ||||
865 | QTest::newRow("simple") << QStringLiteral("simpleheaderguard") | ||||
866 | << QStringLiteral("int main()\n{\nreturn 0;\n}\n") | ||||
867 | << QStringLiteral("#pragma once\n\nint main()\n{\nreturn 0;\n}\n") | ||||
868 | << QStringLiteral( | ||||
869 | "#ifndef SIMPLEHEADERGUARD_H_INCLUDED\n" | ||||
870 | "#define SIMPLEHEADERGUARD_H_INCLUDED\n\n" | ||||
871 | "int main()\n{\nreturn 0;\n}\n" | ||||
872 | "#endif // SIMPLEHEADERGUARD_H_INCLUDED" | ||||
873 | ); | ||||
874 | | ||||
875 | QTest::newRow("licensed") << QStringLiteral("licensed-headerguard") | ||||
876 | << QStringLiteral("/* Copyright 3018 John Doe\n *This software may delete everything*/\n" | ||||
877 | "int main()\n{\nreturn 0;\n}\n") | ||||
878 | << QStringLiteral("/* Copyright 3018 John Doe\n *This software may delete everything*/\n" | ||||
879 | "#pragma once\n\n" | ||||
880 | "int main()\n{\nreturn 0;\n}\n") | ||||
881 | << QStringLiteral( | ||||
882 | "/* Copyright 3018 John Doe\n *This software may delete everything*/\n" | ||||
883 | "#ifndef LICENSED_HEADERGUARD_H_INCLUDED\n" | ||||
884 | "#define LICENSED_HEADERGUARD_H_INCLUDED\n\n" | ||||
885 | "int main()\n{\nreturn 0;\n}\n" | ||||
886 | "#endif // LICENSED_HEADERGUARD_H_INCLUDED" | ||||
887 | ); | ||||
888 | } |