forked from qt-creator/qt-creator
CppEditor: Convert AssignToLocalVariableTest to out-of-line approach
Change-Id: Id31271f4c2cc0b5c783ef44f9ac4b387b2568ff8 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -135,5 +135,34 @@
|
||||
<file>testcases/MoveDeclarationOutOfIfTest/if-else/original_file.cpp</file>
|
||||
<file>testcases/MoveDeclarationOutOfIfTest/if-else-if/expected_file.cpp</file>
|
||||
<file>testcases/MoveDeclarationOutOfIfTest/if-else-if/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/free-function/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/free-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/member-function/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/member-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/new-expression/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/new-expression/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-function-in-expression/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-function-in-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-initialization-list/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-return1/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-return2/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-return3/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-return4/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-return5/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-void-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-void-member-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/no-void-static-member-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/QTCREATORBUG-10355/description.txt</file>
|
||||
<file>testcases/AssignToLocalVariableTest/QTCREATORBUG-10355/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/QTCREATORBUG-10355/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/QTCREATORBUG-10355-2/description.txt</file>
|
||||
<file>testcases/AssignToLocalVariableTest/QTCREATORBUG-10355-2/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/QTCREATORBUG-10355-2/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/static-member-function/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/static-member-function/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/templates/expected_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/templates/expected_file.h</file>
|
||||
<file>testcases/AssignToLocalVariableTest/templates/original_file.cpp</file>
|
||||
<file>testcases/AssignToLocalVariableTest/templates/original_file.h</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
@@ -129,10 +129,6 @@ public:
|
||||
setClangdReplacement({20});
|
||||
}
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
static QObject *createTest();
|
||||
#endif
|
||||
|
||||
private:
|
||||
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
|
||||
{
|
||||
@@ -254,262 +250,12 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
using namespace Tests;
|
||||
|
||||
class AssignToLocalVariableTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void testTemplates()
|
||||
{
|
||||
QList<TestDocumentPtr> testDocuments;
|
||||
QByteArray original;
|
||||
QByteArray expected;
|
||||
|
||||
// Header File
|
||||
original =
|
||||
"template <typename T>\n"
|
||||
"class List {\n"
|
||||
"public:\n"
|
||||
" T first();"
|
||||
"};\n"
|
||||
;
|
||||
expected = original;
|
||||
testDocuments << CppTestDocument::create("file.h", original, expected);
|
||||
|
||||
// Source File
|
||||
original =
|
||||
"#include \"file.h\"\n"
|
||||
"void foo() {\n"
|
||||
" List<int> list;\n"
|
||||
" li@st.first();\n"
|
||||
"}\n";
|
||||
expected =
|
||||
"#include \"file.h\"\n"
|
||||
"void foo() {\n"
|
||||
" List<int> list;\n"
|
||||
" auto localFirst = list.first();\n"
|
||||
"}\n";
|
||||
testDocuments << CppTestDocument::create("file.cpp", original, expected);
|
||||
|
||||
AssignToLocalVariable factory;
|
||||
QuickFixOperationTest(testDocuments, &factory);
|
||||
}
|
||||
|
||||
void test_data()
|
||||
{
|
||||
QTest::addColumn<QByteArray>("original");
|
||||
QTest::addColumn<QByteArray>("expected");
|
||||
|
||||
// Check: Add local variable for a free function.
|
||||
QTest::newRow("freeFunction")
|
||||
<< QByteArray(
|
||||
"int foo() {return 1;}\n"
|
||||
"void bar() {fo@o();}\n")
|
||||
<< QByteArray(
|
||||
"int foo() {return 1;}\n"
|
||||
"void bar() {auto localFoo = foo();}\n");
|
||||
|
||||
// Check: Add local variable for a member function.
|
||||
QTest::newRow("memberFunction")
|
||||
<< QByteArray(
|
||||
"class Foo {public: int* fooFunc();}\n"
|
||||
"void bar() {\n"
|
||||
" Foo *f = new Foo;\n"
|
||||
" @f->fooFunc();\n"
|
||||
"}\n")
|
||||
<< QByteArray(
|
||||
"class Foo {public: int* fooFunc();}\n"
|
||||
"void bar() {\n"
|
||||
" Foo *f = new Foo;\n"
|
||||
" auto localFooFunc = f->fooFunc();\n"
|
||||
"}\n");
|
||||
|
||||
// Check: Add local variable for a member function, cursor in the middle (QTCREATORBUG-10355)
|
||||
QTest::newRow("memberFunction2ndGrade1")
|
||||
<< QByteArray(
|
||||
"struct Foo {int* func();};\n"
|
||||
"struct Baz {Foo* foo();};\n"
|
||||
"void bar() {\n"
|
||||
" Baz *b = new Baz;\n"
|
||||
" b->foo@()->func();\n"
|
||||
"}")
|
||||
<< QByteArray(
|
||||
"struct Foo {int* func();};\n"
|
||||
"struct Baz {Foo* foo();};\n"
|
||||
"void bar() {\n"
|
||||
" Baz *b = new Baz;\n"
|
||||
" auto localFunc = b->foo()->func();\n"
|
||||
"}");
|
||||
|
||||
// Check: Add local variable for a member function, cursor on function call (QTCREATORBUG-10355)
|
||||
QTest::newRow("memberFunction2ndGrade2")
|
||||
<< QByteArray(
|
||||
"struct Foo {int* func();};\n"
|
||||
"struct Baz {Foo* foo();};\n"
|
||||
"void bar() {\n"
|
||||
" Baz *b = new Baz;\n"
|
||||
" b->foo()->f@unc();\n"
|
||||
"}")
|
||||
<< QByteArray(
|
||||
"struct Foo {int* func();};\n"
|
||||
"struct Baz {Foo* foo();};\n"
|
||||
"void bar() {\n"
|
||||
" Baz *b = new Baz;\n"
|
||||
" auto localFunc = b->foo()->func();\n"
|
||||
"}");
|
||||
|
||||
// Check: Add local variable for a static member function.
|
||||
QTest::newRow("staticMemberFunction")
|
||||
<< QByteArray(
|
||||
"class Foo {public: static int* fooFunc();}\n"
|
||||
"void bar() {\n"
|
||||
" Foo::fooF@unc();\n"
|
||||
"}")
|
||||
<< QByteArray(
|
||||
"class Foo {public: static int* fooFunc();}\n"
|
||||
"void bar() {\n"
|
||||
" auto localFooFunc = Foo::fooFunc();\n"
|
||||
"}");
|
||||
|
||||
// Check: Add local variable for a new Expression.
|
||||
QTest::newRow("newExpression")
|
||||
<< QByteArray(
|
||||
"class Foo {}\n"
|
||||
"void bar() {\n"
|
||||
" new Fo@o;\n"
|
||||
"}")
|
||||
<< QByteArray(
|
||||
"class Foo {}\n"
|
||||
"void bar() {\n"
|
||||
" auto localFoo = new Foo;\n"
|
||||
"}");
|
||||
|
||||
// Check: No trigger for function inside member initialization list.
|
||||
QTest::newRow("noInitializationList")
|
||||
<< QByteArray(
|
||||
"class Foo\n"
|
||||
"{\n"
|
||||
" public: Foo : m_i(fooF@unc()) {}\n"
|
||||
" int fooFunc() {return 2;}\n"
|
||||
" int m_i;\n"
|
||||
"};\n")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for void functions.
|
||||
QTest::newRow("noVoidFunction")
|
||||
<< QByteArray(
|
||||
"void foo() {}\n"
|
||||
"void bar() {fo@o();}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for void member functions.
|
||||
QTest::newRow("noVoidMemberFunction")
|
||||
<< QByteArray(
|
||||
"class Foo {public: void fooFunc();}\n"
|
||||
"void bar() {\n"
|
||||
" Foo *f = new Foo;\n"
|
||||
" @f->fooFunc();\n"
|
||||
"}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for void static member functions.
|
||||
QTest::newRow("noVoidStaticMemberFunction")
|
||||
<< QByteArray(
|
||||
"class Foo {public: static void fooFunc();}\n"
|
||||
"void bar() {\n"
|
||||
" Foo::fo@oFunc();\n"
|
||||
"}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions in expressions.
|
||||
QTest::newRow("noFunctionInExpression")
|
||||
<< QByteArray(
|
||||
"int foo(int a) {return a;}\n"
|
||||
"int bar() {return 1;}"
|
||||
"void baz() {foo(@bar() + bar());}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions in functions. (QTCREATORBUG-9510)
|
||||
QTest::newRow("noFunctionInFunction")
|
||||
<< QByteArray(
|
||||
"int foo(int a, int b) {return a + b;}\n"
|
||||
"int bar(int a) {return a;}\n"
|
||||
"void baz() {\n"
|
||||
" int a = foo(ba@r(), bar());\n"
|
||||
"}\n")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions in return statements (classes).
|
||||
QTest::newRow("noReturnClass1")
|
||||
<< QByteArray(
|
||||
"class Foo {public: static void fooFunc();}\n"
|
||||
"Foo* bar() {\n"
|
||||
" return new Fo@o;\n"
|
||||
"}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions in return statements (classes). (QTCREATORBUG-9525)
|
||||
QTest::newRow("noReturnClass2")
|
||||
<< QByteArray(
|
||||
"class Foo {public: int fooFunc();}\n"
|
||||
"int bar() {\n"
|
||||
" return (new Fo@o)->fooFunc();\n"
|
||||
"}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions in return statements (functions).
|
||||
QTest::newRow("noReturnFunc1")
|
||||
<< QByteArray(
|
||||
"class Foo {public: int fooFunc();}\n"
|
||||
"int bar() {\n"
|
||||
" return Foo::fooFu@nc();\n"
|
||||
"}")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions in return statements (functions). (QTCREATORBUG-9525)
|
||||
QTest::newRow("noReturnFunc2")
|
||||
<< QByteArray(
|
||||
"int bar() {\n"
|
||||
" return list.firs@t().foo;\n"
|
||||
"}\n")
|
||||
<< QByteArray();
|
||||
|
||||
// Check: No trigger for functions which does not match in signature.
|
||||
QTest::newRow("noSignatureMatch")
|
||||
<< QByteArray(
|
||||
"int someFunc(int);\n"
|
||||
"\n"
|
||||
"void f()\n"
|
||||
"{\n"
|
||||
" some@Func();\n"
|
||||
"}")
|
||||
<< QByteArray();
|
||||
}
|
||||
|
||||
void test()
|
||||
{
|
||||
QFETCH(QByteArray, original);
|
||||
QFETCH(QByteArray, expected);
|
||||
AssignToLocalVariable factory;
|
||||
QuickFixOperationTest(singleDocument(original, expected), &factory);
|
||||
}
|
||||
};
|
||||
|
||||
QObject *AssignToLocalVariable::createTest() { return new AssignToLocalVariableTest; }
|
||||
|
||||
#endif // WITH_TESTS
|
||||
} // namespace
|
||||
|
||||
void registerAssignToLocalVariableQuickfix()
|
||||
{
|
||||
CppQuickFixFactory::registerFactory<AssignToLocalVariable>();
|
||||
CppQuickFixFactory::registerFactoryWithStandardTest<AssignToLocalVariable>(
|
||||
"AssignToLocalVariableTest");
|
||||
}
|
||||
|
||||
} // namespace CppEditor::Internal
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
#include <assigntolocalvariable.moc>
|
||||
#endif
|
||||
|
@@ -0,0 +1 @@
|
||||
Add local variable for a member function, cursor on function call (QTCREATORBUG-10355)
|
@@ -0,0 +1,6 @@
|
||||
struct Foo {int* func();};
|
||||
struct Baz {Foo* foo();};
|
||||
void bar() {
|
||||
Baz *b = new Baz;
|
||||
auto localFunc = b->foo()->func();
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
struct Foo {int* func();};
|
||||
struct Baz {Foo* foo();};
|
||||
void bar() {
|
||||
Baz *b = new Baz;
|
||||
b->foo()->f@unc();
|
||||
}
|
@@ -0,0 +1 @@
|
||||
Add local variable for a member function, cursor in the middle (QTCREATORBUG-10355)
|
@@ -0,0 +1,6 @@
|
||||
struct Foo {int* func();};
|
||||
struct Baz {Foo* foo();};
|
||||
void bar() {
|
||||
Baz *b = new Baz;
|
||||
auto localFunc = b->foo()->func();
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
struct Foo {int* func();};
|
||||
struct Baz {Foo* foo();};
|
||||
void bar() {
|
||||
Baz *b = new Baz;
|
||||
b->foo@()->func();
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
int foo() {return 1;}
|
||||
void bar() {auto localFoo = foo();}
|
@@ -0,0 +1,2 @@
|
||||
int foo() {return 1;}
|
||||
void bar() {fo@o();}
|
@@ -0,0 +1,5 @@
|
||||
class Foo {public: int* fooFunc();}
|
||||
void bar() {
|
||||
Foo *f = new Foo;
|
||||
auto localFooFunc = f->fooFunc();
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
class Foo {public: int* fooFunc();}
|
||||
void bar() {
|
||||
Foo *f = new Foo;
|
||||
@f->fooFunc();
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {}
|
||||
void bar() {
|
||||
auto localFoo = new Foo;
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {}
|
||||
void bar() {
|
||||
new Fo@o;
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
int foo(int a) {return a;}
|
||||
int bar() {return 1;}
|
||||
void baz() {foo(@bar() + bar());}
|
@@ -0,0 +1,5 @@
|
||||
int foo(int a, int b) {return a + b;}
|
||||
int bar(int a) {return a;}
|
||||
void baz() {
|
||||
int a = foo(ba@r(), bar());
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
class Foo
|
||||
{
|
||||
public: Foo : m_i(fooF@unc()) {}
|
||||
int fooFunc() {return 2;}
|
||||
int m_i;
|
||||
};
|
@@ -0,0 +1,4 @@
|
||||
class Foo {public: static void fooFunc();}
|
||||
Foo* bar() {
|
||||
return new Fo@o;
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {public: int fooFunc();}
|
||||
int bar() {
|
||||
return (new Fo@o)->fooFunc();
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {public: int fooFunc();}
|
||||
int bar() {
|
||||
return Foo::fooFu@nc();
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
int bar() {
|
||||
return list.firs@t().foo;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
int someFunc(int);
|
||||
|
||||
void f()
|
||||
{
|
||||
some@Func();
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
void foo() {}
|
||||
void bar() {fo@o();}
|
@@ -0,0 +1,5 @@
|
||||
class Foo {public: void fooFunc();}
|
||||
void bar() {
|
||||
Foo *f = new Foo;
|
||||
@f->fooFunc();
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {public: static void fooFunc();}
|
||||
void bar() {
|
||||
Foo::fo@oFunc();
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {public: static int* fooFunc();}
|
||||
void bar() {
|
||||
auto localFooFunc = Foo::fooFunc();
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
class Foo {public: static int* fooFunc();}
|
||||
void bar() {
|
||||
Foo::fooF@unc();
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include "file.h"
|
||||
void foo() {
|
||||
List<int> list;
|
||||
auto localFirst = list.first();
|
||||
};
|
@@ -0,0 +1,5 @@
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
T first();
|
||||
};
|
@@ -0,0 +1,5 @@
|
||||
#include "file.h"
|
||||
void foo() {
|
||||
List<int> list;
|
||||
li@st.first();
|
||||
};
|
@@ -0,0 +1,5 @@
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
T first();
|
||||
};
|
Reference in New Issue
Block a user