diff --git a/src/libs/extensionsystem/pluginmanager.cpp b/src/libs/extensionsystem/pluginmanager.cpp index c0cb96081e7..5e4c92a6260 100644 --- a/src/libs/extensionsystem/pluginmanager.cpp +++ b/src/libs/extensionsystem/pluginmanager.cpp @@ -1207,7 +1207,9 @@ static QStringList matchingTestFunctions(const QStringList &testFunctions, static QObject *objectWithClassName(const QObjectList &objects, const QString &className) { return Utils::findOr(objects, nullptr, [className] (QObject *object) -> bool { - QString candidate = QString::fromUtf8(object->metaObject()->className()); + QString candidate = object->objectName(); + if (candidate.isEmpty()) + candidate = QString::fromUtf8(object->metaObject()->className()); const int colonIndex = candidate.lastIndexOf(QLatin1Char(':')); if (colonIndex != -1 && colonIndex < candidate.size() - 1) candidate = candidate.mid(colonIndex + 1); diff --git a/src/plugins/cppeditor/quickfixes/cppquickfix.h b/src/plugins/cppeditor/quickfixes/cppquickfix.h index 3d654661bc6..d7c95c104eb 100644 --- a/src/plugins/cppeditor/quickfixes/cppquickfix.h +++ b/src/plugins/cppeditor/quickfixes/cppquickfix.h @@ -13,6 +13,10 @@ #include +#ifdef WITH_TESTS +#include "cppquickfix_test.h" +#endif + namespace CppEditor { namespace Internal { class CppQuickFixInterface; @@ -70,6 +74,18 @@ public: #endif } + template static void registerFactoryWithStandardTest(const QString &testName) + { + new Factory; +#ifdef WITH_TESTS + cppEditor()->addTestCreator([testName] { + const auto obj = new Internal::Tests::CppQuickFixTestObject(std::make_unique()); + obj->setObjectName(testName); + return obj; + }); +#endif + } + private: /*! Implement this function to match and create the appropriate diff --git a/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp b/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp index 5162025d224..fd1e85bf667 100644 --- a/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp @@ -8,6 +8,7 @@ #include "../cppmodelmanager.h" #include "../cppsourceprocessertesthelper.h" #include "../cpptoolssettings.h" +#include "cppquickfix.h" #include "cppquickfixassistant.h" #include @@ -245,13 +246,21 @@ void QuickFixOperationTest::run(const QList &testDocuments, QuickFixOperationTest(testDocuments, factory, headerPaths, operationIndex); } +CppQuickFixTestObject::~CppQuickFixTestObject() = default; + +CppQuickFixTestObject::CppQuickFixTestObject(std::unique_ptr &&factory) + : m_factory(std::move(factory)) {} + void CppQuickFixTestObject::initTestCase() { - const QStringList classNameComponents - = QString::fromLatin1(metaObject()->className()).split("::", Qt::SkipEmptyParts); - QVERIFY(!classNameComponents.isEmpty()); - - const QDir testDir(QLatin1String(":/cppeditor/testcases/") + classNameComponents.last()); + QString testName = objectName(); + if (testName.isEmpty()) { + const QStringList classNameComponents + = QString::fromLatin1(metaObject()->className()).split("::", Qt::SkipEmptyParts); + QVERIFY(!classNameComponents.isEmpty()); + testName = classNameComponents.last(); + } + const QDir testDir(QLatin1String(":/cppeditor/testcases/") + testName); QVERIFY2(testDir.exists(), qPrintable(testDir.absolutePath())); const QStringList subDirs = testDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name); for (const QString &subDir : subDirs) { diff --git a/src/plugins/cppeditor/quickfixes/cppquickfix_test.h b/src/plugins/cppeditor/quickfixes/cppquickfix_test.h index 1077550c057..b824bc5bfcc 100644 --- a/src/plugins/cppeditor/quickfixes/cppquickfix_test.h +++ b/src/plugins/cppeditor/quickfixes/cppquickfix_test.h @@ -5,7 +5,6 @@ #include "../cpptoolstestcase.h" #include "../cppcodestylesettings.h" -#include "cppquickfix.h" #include "cppquickfixsettings.h" #include @@ -22,6 +21,7 @@ namespace TextEditor { class QuickFixOperation; } namespace CppEditor { +class CppQuickFixFactory; namespace Internal { namespace Tests { @@ -92,9 +92,9 @@ public: class CppQuickFixTestObject : public QObject { Q_OBJECT -protected: - CppQuickFixTestObject(std::unique_ptr &&factory) - : m_factory(std::move(factory)) {} +public: + CppQuickFixTestObject(std::unique_ptr &&factory); + ~CppQuickFixTestObject(); private slots: void initTestCase(); diff --git a/src/plugins/cppeditor/quickfixes/rewritecontrolstatements.cpp b/src/plugins/cppeditor/quickfixes/rewritecontrolstatements.cpp index 6f6125f0a5a..4b18a5d0011 100644 --- a/src/plugins/cppeditor/quickfixes/rewritecontrolstatements.cpp +++ b/src/plugins/cppeditor/quickfixes/rewritecontrolstatements.cpp @@ -405,12 +405,6 @@ private: */ class MoveDeclarationOutOfIf: public CppQuickFixFactory { -#ifdef WITH_TESTS -public: - static QObject *createTest(); -#endif - -private: void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override { const QList &path = interface.path(); @@ -451,12 +445,6 @@ private: */ class MoveDeclarationOutOfWhile: public CppQuickFixFactory { -#ifdef WITH_TESTS -public: - static QObject *createTest(); -#endif - -private: void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override { const QList &path = interface.path(); @@ -583,12 +571,6 @@ private: */ class AddBracesToControlStatement : public CppQuickFixFactory { -#ifdef WITH_TESTS -public: - static QObject *createTest(); -#endif - -private: void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override { if (interface.path().isEmpty()) @@ -607,12 +589,6 @@ private: */ class OptimizeForLoop : public CppQuickFixFactory { -#ifdef WITH_TESTS -public: - static QObject *createTest(); -#endif - -private: void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override { const QList path = interface.path(); @@ -694,62 +670,18 @@ private: } }; -#ifdef WITH_TESTS -using namespace Tests; - -class MoveDeclarationOutOfIfTest : public CppQuickFixTestObject -{ - Q_OBJECT -public: - MoveDeclarationOutOfIfTest() - : CppQuickFixTestObject(std::make_unique()) - {} -}; - -class MoveDeclarationOutOfWhileTest : public CppQuickFixTestObject -{ - Q_OBJECT -public: - MoveDeclarationOutOfWhileTest() - : CppQuickFixTestObject(std::make_unique()) - {} -}; - -class OptimizeForLoopTest : public CppQuickFixTestObject -{ - Q_OBJECT -public: - OptimizeForLoopTest() : CppQuickFixTestObject(std::make_unique()) {} -}; - -class AddBracesToControlStatementTest : public CppQuickFixTestObject -{ - Q_OBJECT -public: - AddBracesToControlStatementTest() - : CppQuickFixTestObject(std::make_unique()) - {} -}; - -QObject *MoveDeclarationOutOfIf::createTest() { return new MoveDeclarationOutOfIfTest; } -QObject *MoveDeclarationOutOfWhile::createTest() { return new MoveDeclarationOutOfWhileTest; } -QObject *OptimizeForLoop::createTest() { return new OptimizeForLoopTest; } -QObject *AddBracesToControlStatement::createTest() { return new AddBracesToControlStatementTest; } - -#endif // WITH_TESTS } // namespace void registerRewriteControlStatementQuickfixes() { - CppQuickFixFactory::registerFactory(); - CppQuickFixFactory::registerFactory(); - CppQuickFixFactory::registerFactory(); - CppQuickFixFactory::registerFactory(); + CppQuickFixFactory::registerFactoryWithStandardTest( + "AddBracesToControlStatementTest"); + CppQuickFixFactory::registerFactoryWithStandardTest( + "MoveDeclarationOutOfIfTest"); + CppQuickFixFactory::registerFactoryWithStandardTest( + "MoveDeclarationOutOfWhileTest"); + CppQuickFixFactory::registerFactoryWithStandardTest("OptimizeForLoopTest"); CppQuickFixFactory::registerFactory(); } } // namespace CppEditor::Internal - -#ifdef WITH_TESTS -#include -#endif