CppEditor: More convenience for quickfix test creation

Now with no boilerplate code whatsoever.

Change-Id: Iab1f9b8b6ed7beba97067327b9f2ad5602648b68
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2025-03-03 18:20:12 +01:00
parent 18f6456503
commit 5a78d8882b
5 changed files with 44 additions and 85 deletions

View File

@@ -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);

View File

@@ -13,6 +13,10 @@
#include <optional>
#ifdef WITH_TESTS
#include "cppquickfix_test.h"
#endif
namespace CppEditor {
namespace Internal {
class CppQuickFixInterface;
@@ -70,6 +74,18 @@ public:
#endif
}
template<class Factory> static void registerFactoryWithStandardTest(const QString &testName)
{
new Factory;
#ifdef WITH_TESTS
cppEditor()->addTestCreator([testName] {
const auto obj = new Internal::Tests::CppQuickFixTestObject(std::make_unique<Factory>());
obj->setObjectName(testName);
return obj;
});
#endif
}
private:
/*!
Implement this function to match and create the appropriate

View File

@@ -8,6 +8,7 @@
#include "../cppmodelmanager.h"
#include "../cppsourceprocessertesthelper.h"
#include "../cpptoolssettings.h"
#include "cppquickfix.h"
#include "cppquickfixassistant.h"
#include <projectexplorer/kitmanager.h>
@@ -245,13 +246,21 @@ void QuickFixOperationTest::run(const QList<TestDocumentPtr> &testDocuments,
QuickFixOperationTest(testDocuments, factory, headerPaths, operationIndex);
}
CppQuickFixTestObject::~CppQuickFixTestObject() = default;
CppQuickFixTestObject::CppQuickFixTestObject(std::unique_ptr<CppQuickFixFactory> &&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) {

View File

@@ -5,7 +5,6 @@
#include "../cpptoolstestcase.h"
#include "../cppcodestylesettings.h"
#include "cppquickfix.h"
#include "cppquickfixsettings.h"
#include <projectexplorer/headerpath.h>
@@ -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<CppQuickFixFactory> &&factory)
: m_factory(std::move(factory)) {}
public:
CppQuickFixTestObject(std::unique_ptr<CppQuickFixFactory> &&factory);
~CppQuickFixTestObject();
private slots:
void initTestCase();

View File

@@ -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<AST *> &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<AST *> &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<AST *> 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<MoveDeclarationOutOfIf>())
{}
};
class MoveDeclarationOutOfWhileTest : public CppQuickFixTestObject
{
Q_OBJECT
public:
MoveDeclarationOutOfWhileTest()
: CppQuickFixTestObject(std::make_unique<MoveDeclarationOutOfWhile>())
{}
};
class OptimizeForLoopTest : public CppQuickFixTestObject
{
Q_OBJECT
public:
OptimizeForLoopTest() : CppQuickFixTestObject(std::make_unique<OptimizeForLoop>()) {}
};
class AddBracesToControlStatementTest : public CppQuickFixTestObject
{
Q_OBJECT
public:
AddBracesToControlStatementTest()
: CppQuickFixTestObject(std::make_unique<AddBracesToControlStatement>())
{}
};
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<AddBracesToControlStatement>();
CppQuickFixFactory::registerFactory<MoveDeclarationOutOfIf>();
CppQuickFixFactory::registerFactory<MoveDeclarationOutOfWhile>();
CppQuickFixFactory::registerFactory<OptimizeForLoop>();
CppQuickFixFactory::registerFactoryWithStandardTest<AddBracesToControlStatement>(
"AddBracesToControlStatementTest");
CppQuickFixFactory::registerFactoryWithStandardTest<MoveDeclarationOutOfIf>(
"MoveDeclarationOutOfIfTest");
CppQuickFixFactory::registerFactoryWithStandardTest<MoveDeclarationOutOfWhile>(
"MoveDeclarationOutOfWhileTest");
CppQuickFixFactory::registerFactoryWithStandardTest<OptimizeForLoop>("OptimizeForLoopTest");
CppQuickFixFactory::registerFactory<SplitIfStatement>();
}
} // namespace CppEditor::Internal
#ifdef WITH_TESTS
#include <rewritecontrolstatements.moc>
#endif