diff options
Diffstat (limited to 'src/plugins/cppeditor/cppquickfix_test.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppquickfix_test.cpp | 321 |
1 files changed, 321 insertions, 0 deletions
diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index e75464d2b5..cf66662e09 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -310,6 +310,23 @@ private: const QString m_include; }; +class AddForwardDeclForUndefinedIdentifierTestFactory : public CppQuickFixFactory +{ +public: + AddForwardDeclForUndefinedIdentifierTestFactory(const QString &className, int symbolPos) + : m_className(className), m_symbolPos(symbolPos) {} + + void match(const CppQuickFixInterface &cppQuickFixInterface, QuickFixOperations &result) + { + result << new AddForwardDeclForUndefinedIdentifierOp(cppQuickFixInterface, 0, + m_className, m_symbolPos); + } + +private: + const QString m_className; + const int m_symbolPos; +}; + } // namespace Tests } // namespace Internal @@ -352,6 +369,56 @@ void CppEditorPlugin::test_quickfix_data() "}\n" ); + // Checks: All enum values are added as case statements for a blank switch when + // the variable is declared alongside the enum definition. + QTest::newRow("CompleteSwitchCaseStatement_basic1_enum_with_declaration") + << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _( + "enum EnumType { V1, V2 } t;\n" + "\n" + "void f()\n" + "{\n" + " @switch (t) {\n" + " }\n" + "}\n" + ) << _( + "enum EnumType { V1, V2 } t;\n" + "\n" + "void f()\n" + "{\n" + " switch (t) {\n" + " case V1:\n" + " break;\n" + " case V2:\n" + " break;\n" + " }\n" + "}\n" + ); + + // Checks: All enum values are added as case statements for a blank switch + // for anonymous enums. + QTest::newRow("CompleteSwitchCaseStatement_basic1_anonymous_enum") + << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _( + "enum { V1, V2 } t;\n" + "\n" + "void f()\n" + "{\n" + " @switch (t) {\n" + " }\n" + "}\n" + ) << _( + "enum { V1, V2 } t;\n" + "\n" + "void f()\n" + "{\n" + " switch (t) {\n" + " case V1:\n" + " break;\n" + " case V2:\n" + " break;\n" + " }\n" + "}\n" + ); + // Checks: All enum values are added as case statements for a blank switch with a default case. QTest::newRow("CompleteSwitchCaseStatement_basic2") << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _( @@ -1515,6 +1582,40 @@ void CppEditorPlugin::test_quickfix_data() "};\n" ); + QTest::newRow("InsertQtPropertyMembersPrivateBeforePublic") + << CppQuickFixFactoryPtr(new InsertQtPropertyMembers) + << _("class XmarksTheSpot {\n" + "private:\n" + " @Q_PROPERTY(int it READ getIt WRITE setIt NOTIFY itChanged)\n" + "public:\n" + " void find();\n" + "};\n" + ) + << _("class XmarksTheSpot {\n" + "private:\n" + " Q_PROPERTY(int it READ getIt WRITE setIt NOTIFY itChanged)\n" + " int m_it;\n" + "\n" + "public:\n" + " void find();\n" + " int getIt() const\n" + " {\n" + " return m_it;\n" + " }\n" + "public slots:\n" + " void setIt(int it)\n" + " {\n" + " if (m_it == it)\n" + " return;\n" + "\n" + " m_it = it;\n" + " emit itChanged(m_it);\n" + " }\n" + "signals:\n" + " void itChanged(int it);\n" + "};\n" + ); + // Escape String Literal as UTF-8 (no-trigger) QTest::newRow("EscapeStringLiteral_notrigger") << CppQuickFixFactoryPtr(new EscapeStringLiteral) @@ -2842,6 +2943,19 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateFunction() QuickFixOperationTest(singleDocument(original, expected), &factory); } +void CppEditorPlugin::test_quickfix_InsertDefFromDecl_notTriggeredForFriendFunc() +{ + const QByteArray contents = + "class Foo\n" + "{\n" + " friend void f@unc();\n" + "};\n" + "\n"; + + InsertDefFromDecl factory; + QuickFixOperationTest(singleDocument(contents, ""), &factory); +} + // Function for one of InsertDeclDef section cases void insertToSectionDeclFromDef(const QByteArray §ion, int sectionIndex) { @@ -3691,6 +3805,20 @@ void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_data() << TestIncludePaths::globalQtCoreIncludePath() << testDocuments << firstRefactoringOperation << ""; testDocuments.clear(); + + original = + "std::s@tring s;\n" + ; + expected = + "#include <string>\n" + "\n" + "std::string s;\n" + ; + testDocuments << QuickFixTestDocument::create("file.cpp", original, expected); + QTest::newRow("inserting_std::string") + << TestIncludePaths::globalIncludePath() + << testDocuments << firstRefactoringOperation << ""; + testDocuments.clear(); } void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier() @@ -3742,6 +3870,171 @@ void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_noDoubleQtH QuickFixOfferedOperationsTest(testDocuments, &factory, headerPaths, expectedOperations); } +void CppEditorPlugin::test_quickfix_AddForwardDeclForUndefinedIdentifier_data() +{ + QTest::addColumn<QuickFixTestDocuments>("testDocuments"); + QTest::addColumn<QString>("symbol"); + QTest::addColumn<int>("symbolPos"); + + QByteArray original; + QByteArray expected; + + original = + "#pragma once\n" + "\n" + "void f(const Blu@bb &b)\n" + "{\n" + "}\n" + ; + expected = + "#pragma once\n" + "\n" + "\n" + "class Blubb;\n" + "void f(const Blubb &b)\n" + "{\n" + "}\n" + ; + QTest::newRow("unqualified symbol") + << QuickFixTestDocuments{QuickFixTestDocument::create("theheader.h", original, expected)} + << "Blubb" << original.indexOf('@'); + + original = + "#pragma once\n" + "\n" + "namespace NS {\n" + "class C;\n" + "}\n" + "void f(const NS::Blu@bb &b)\n" + "{\n" + "}\n" + ; + expected = + "#pragma once\n" + "\n" + "namespace NS {\n" + "\n" + "class Blubb;\n" + "class C;\n" + "}\n" + "void f(const NS::Blubb &b)\n" + "{\n" + "}\n" + ; + QTest::newRow("qualified symbol, full namespace present") + << QuickFixTestDocuments{QuickFixTestDocument::create("theheader.h", original, expected)} + << "NS::Blubb" << original.indexOf('@'); + + original = + "#pragma once\n" + "\n" + "namespace NS {\n" + "class C;\n" + "}\n" + "void f(const NS::NS2::Blu@bb &b)\n" + "{\n" + "}\n" + ; + expected = + "#pragma once\n" + "\n" + "namespace NS {\n" + "\n" + "namespace NS2 { class Blubb; }\n" + "class C;\n" + "}\n" + "void f(const NS::NS2::Blubb &b)\n" + "{\n" + "}\n" + ; + QTest::newRow("qualified symbol, partial namespace present") + << QuickFixTestDocuments{QuickFixTestDocument::create("theheader.h", original, expected)} + << "NS::NS2::Blubb" << original.indexOf('@'); + + original = + "#pragma once\n" + "\n" + "namespace NS {\n" + "class C;\n" + "}\n" + "void f(const NS2::Blu@bb &b)\n" + "{\n" + "}\n" + ; + expected = + "#pragma once\n" + "\n" + "\n" + "namespace NS2 { class Blubb; }\n" + "namespace NS {\n" + "class C;\n" + "}\n" + "void f(const NS2::Blubb &b)\n" + "{\n" + "}\n" + ; + QTest::newRow("qualified symbol, other namespace present") + << QuickFixTestDocuments{QuickFixTestDocument::create("theheader.h", original, expected)} + << "NS2::Blubb" << original.indexOf('@'); + + original = + "#pragma once\n" + "\n" + "void f(const NS2::Blu@bb &b)\n" + "{\n" + "}\n" + ; + expected = + "#pragma once\n" + "\n" + "\n" + "namespace NS2 { class Blubb; }\n" + "void f(const NS2::Blubb &b)\n" + "{\n" + "}\n" + ; + QTest::newRow("qualified symbol, no namespace present") + << QuickFixTestDocuments{QuickFixTestDocument::create("theheader.h", original, expected)} + << "NS2::Blubb" << original.indexOf('@'); + + original = + "#pragma once\n" + "\n" + "void f(const NS2::Blu@bb &b)\n" + "{\n" + "}\n" + "namespace NS2 {}\n" + ; + expected = + "#pragma once\n" + "\n" + "\n" + "namespace NS2 { class Blubb; }\n" + "void f(const NS2::Blubb &b)\n" + "{\n" + "}\n" + "namespace NS2 {}\n" + ; + QTest::newRow("qualified symbol, existing namespace after symbol") + << QuickFixTestDocuments{QuickFixTestDocument::create("theheader.h", original, expected)} + << "NS2::Blubb" << original.indexOf('@'); +} + +void CppEditorPlugin::test_quickfix_AddForwardDeclForUndefinedIdentifier() +{ + QFETCH(QuickFixTestDocuments, testDocuments); + QFETCH(QString, symbol); + QFETCH(int, symbolPos); + + CppTools::Tests::TemporaryDir temporaryDir; + QVERIFY(temporaryDir.isValid()); + testDocuments.first()->setBaseDirectory(temporaryDir.path()); + + QScopedPointer<CppQuickFixFactory> factory( + new AddForwardDeclForUndefinedIdentifierTestFactory(symbol, symbolPos)); + QuickFixOperationTest::run({testDocuments}, factory.data(), ".", 0); +} + /// Check: Move definition from header to cpp. void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp() { @@ -4943,6 +5236,34 @@ void CppEditorPlugin::test_quickfix_ExtractFunction_data() "{\n" " extracted();\n" "}\n"); + + QTest::newRow("class in namespace") + << _("namespace NS {\n" + "class C {\n" + " void f(C &c);\n" + "};\n" + "}\n" + "void NS::C::f(NS::C &c)\n" + "{\n" + " @{start}C *c = &c;@{end}\n" + "}\n") + << _("namespace NS {\n" + "class C {\n" + " void f(C &c);\n" + "\n" + "public:\n" + " void extracted(NS::C &c);\n" // TODO: Remove non-required qualification + "};\n" + "}\n" + "void NS::C::extracted(NS::C &c)\n" + "{\n" + " C *c = &c;\n" + "}\n" + "\n" + "void NS::C::f(NS::C &c)\n" + "{\n" + " extracted(c);\n" + "}\n"); } void CppEditorPlugin::test_quickfix_ExtractFunction() |