CppEditor: Convert ExtractFunctionTest to out-of-line approach

Change-Id: Iee8d96bfd3a0c4f48bb16172e47654d2ffb2f38c
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2025-03-05 16:51:59 +01:00
parent 6aa52193de
commit d614e1aa85
11 changed files with 101 additions and 141 deletions

View File

@@ -321,5 +321,14 @@
<file>testcases/ConvertToMetaMethodCallTest/Q_SLOT/original_file.cpp</file> <file>testcases/ConvertToMetaMethodCallTest/Q_SLOT/original_file.cpp</file>
<file>testcases/ConvertToMetaMethodCallTest/slot/expected_file.cpp</file> <file>testcases/ConvertToMetaMethodCallTest/slot/expected_file.cpp</file>
<file>testcases/ConvertToMetaMethodCallTest/slot/original_file.cpp</file> <file>testcases/ConvertToMetaMethodCallTest/slot/original_file.cpp</file>
<file>testcases/ExtractFunctionTest/basic/expected_file.h</file>
<file>testcases/ExtractFunctionTest/basic/original_file.h</file>
<file>testcases/ExtractFunctionTest/class-in-namespace/description.txt</file>
<file>testcases/ExtractFunctionTest/class-in-namespace/expected_file.h</file>
<file>testcases/ExtractFunctionTest/class-in-namespace/original_file.h</file>
<file>testcases/ExtractFunctionTest/member-function/expected_file.h</file>
<file>testcases/ExtractFunctionTest/member-function/original_file.h</file>
<file>testcases/ExtractFunctionTest/if-block/expected_file.h</file>
<file>testcases/ExtractFunctionTest/if-block/original_file.h</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@@ -480,16 +480,6 @@ public:
//! Extracts the selected code and puts it to a function //! Extracts the selected code and puts it to a function
class ExtractFunction : public CppQuickFixFactory class ExtractFunction : public CppQuickFixFactory
{ {
public:
ExtractFunction(FunctionNameGetter functionNameGetter = FunctionNameGetter())
: m_functionNameGetter(functionNameGetter)
{}
#ifdef WITH_TESTS
static QObject *createTest();
#endif
private:
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{ {
const CppRefactoringFilePtr file = interface.currentFile(); const CppRefactoringFilePtr file = interface.currentFile();
@@ -616,148 +606,22 @@ private:
// The current implementation doesn't try to be too smart since it preserves the original form // The current implementation doesn't try to be too smart since it preserves the original form
// of the declarations. This might be or not the desired effect. An improvement would be to // of the declarations. This might be or not the desired effect. An improvement would be to
// let the user somehow customize the function interface. // let the user somehow customize the function interface.
FunctionNameGetter nameGetter;
if (testMode())
nameGetter = []() { return QLatin1String("extracted"); };
result << new ExtractFunctionOperation(interface, result << new ExtractFunctionOperation(interface,
analyser.m_extractionStart, analyser.m_extractionStart,
analyser.m_extractionEnd, analyser.m_extractionEnd,
refFuncDef, funcReturn, relevantDecls, refFuncDef, funcReturn, relevantDecls,
m_functionNameGetter); nameGetter);
}
private:
FunctionNameGetter m_functionNameGetter; // For tests to avoid GUI pop-up.
};
#ifdef WITH_TESTS
using namespace Tests;
class ExtractFunctionTest : public QObject
{
Q_OBJECT
private slots:
void test_data()
{
QTest::addColumn<QByteArray>("original");
QTest::addColumn<QByteArray>("expected");
QTest::newRow("basic")
<< QByteArray("// Documentation for f\n"
"void f()\n"
"{\n"
" @{start}g();@{end}\n"
"}\n")
<< QByteArray("inline void extracted()\n"
"{\n"
" g();\n"
"}\n"
"\n"
"// Documentation for f\n"
"void f()\n"
"{\n"
" extracted();\n"
"}\n");
QTest::newRow("class function")
<< QByteArray("class Foo\n"
"{\n"
"private:\n"
" void bar();\n"
"};\n\n"
"void Foo::bar()\n"
"{\n"
" @{start}g();@{end}\n"
"}\n")
<< QByteArray("class Foo\n"
"{\n"
"public:\n"
" void extracted();\n\n"
"private:\n"
" void bar();\n"
"};\n\n"
"inline void Foo::extracted()\n"
"{\n"
" g();\n"
"}\n\n"
"void Foo::bar()\n"
"{\n"
" extracted();\n"
"}\n");
QTest::newRow("class in namespace")
<< QByteArray("namespace NS {\n"
"class C {\n"
" void f(C &c);\n"
"};\n"
"}\n"
"void NS::C::f(NS::C &c)\n"
"{\n"
" @{start}C *c2 = &c;@{end}\n"
"}\n")
<< QByteArray("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"
"inline void NS::C::extracted(NS::C &c)\n"
"{\n"
" C *c2 = &c;\n"
"}\n"
"\n"
"void NS::C::f(NS::C &c)\n"
"{\n"
" extracted(c);\n"
"}\n");
QTest::newRow("if-block")
<< QByteArray("inline void func()\n"
"{\n"
" int dummy = 0;\n"
" @{start}if@{end} (dummy < 10) {\n"
" ++dummy;\n"
" }\n"
"}\n")
<< QByteArray("inline void extracted(int dummy)\n"
"{\n"
" if (dummy < 10) {\n"
" ++dummy;\n"
" }\n"
"}\n\n"
"inline void func()\n"
"{\n"
" int dummy = 0;\n"
" extracted(dummy);\n"
"}\n");
}
void test()
{
QFETCH(QByteArray, original);
QFETCH(QByteArray, expected);
QList<TestDocumentPtr> testDocuments;
testDocuments << CppTestDocument::create("file.h", original, expected);
ExtractFunction factory([]() { return QLatin1String("extracted"); });
QuickFixOperationTest(testDocuments, &factory);
} }
}; };
QObject *ExtractFunction::createTest() { return new ExtractFunctionTest; }
#endif // WITH_TESTS
} // namespace } // namespace
void registerExtractFunctionQuickfix() void registerExtractFunctionQuickfix()
{ {
CppQuickFixFactory::registerFactory<ExtractFunction>(); CppQuickFixFactory::registerFactoryWithStandardTest<ExtractFunction>("ExtractFunctionTest");
} }
} // namespace CppEditor::Internal } // namespace CppEditor::Internal
#ifdef WITH_TESTS
#include <extractfunction.moc>
#endif

View File

@@ -0,0 +1,10 @@
inline void extracted()
{
g();
}
// Documentation for f
void f()
{
extracted();
}

View File

@@ -0,0 +1,5 @@
// Documentation for f
void f()
{
@{start}g();@{end}
}

View File

@@ -0,0 +1 @@
class in namespace (TODO: Remove redundant qualification)

View File

@@ -0,0 +1,17 @@
namespace NS {
class C {
void f(C &c);
public:
void extracted(NS::C &c);
};
}
inline void NS::C::extracted(NS::C &c)
{
C *c2 = &c;
}
void NS::C::f(NS::C &c)
{
extracted(c);
}

View File

@@ -0,0 +1,9 @@
namespace NS {
class C {
void f(C &c);
};
}
void NS::C::f(NS::C &c)
{
@{start}C *c2 = &c;@{end}
}

View File

@@ -0,0 +1,12 @@
inline void extracted(int dummy)
{
if (dummy < 10) {
++dummy;
}
}
inline void func()
{
int dummy = 0;
extracted(dummy);
}

View File

@@ -0,0 +1,7 @@
inline void func()
{
int dummy = 0;
@{start}if@{end} (dummy < 10) {
++dummy;
}
}

View File

@@ -0,0 +1,17 @@
class Foo
{
public:
void extracted();
private:
void bar();
};
inline void Foo::extracted()
{
g();
}
void Foo::bar()
{
extracted();
}

View File

@@ -0,0 +1,9 @@
class Foo
{
private:
void bar();
};
void Foo::bar()
{
@{start}g();@{end}
}