Commit 49e27b35 authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

CppEditor: Tests: Add Switch Between Method Decl/Def tests



Change-Id: I60fa4ed46178fd4053d83b9a5c6675a0029d2cc2
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 532dc1aa
...@@ -43,7 +43,7 @@ equals(TEST, 1) { ...@@ -43,7 +43,7 @@ equals(TEST, 1) {
cppquickfix_test.cpp \ cppquickfix_test.cpp \
cppdoxygen_test.cpp \ cppdoxygen_test.cpp \
fileandtokenactions_test.cpp \ fileandtokenactions_test.cpp \
followsymbolundercursor_test.cpp followsymbol_switchmethoddecldef_test.cpp
DEFINES += SRCDIR=\\\"$$PWD\\\" DEFINES += SRCDIR=\\\"$$PWD\\\"
} }
...@@ -61,7 +61,7 @@ QtcPlugin { ...@@ -61,7 +61,7 @@ QtcPlugin {
"cppdoxygen_test.cpp", "cppdoxygen_test.cpp",
"cppquickfix_test.cpp", "cppquickfix_test.cpp",
"fileandtokenactions_test.cpp", "fileandtokenactions_test.cpp",
"followsymbolundercursor_test.cpp" "followsymbol_switchmethoddecldef_test.cpp"
] ]
cpp.defines: outer.concat(['SRCDIR="' + FileInfo.path(filePath) + '"']) cpp.defines: outer.concat(['SRCDIR="' + FileInfo.path(filePath) + '"'])
......
...@@ -93,6 +93,10 @@ private slots: ...@@ -93,6 +93,10 @@ private slots:
#ifdef WITH_TESTS #ifdef WITH_TESTS
private slots: private slots:
// The following tests expect that no projects are loaded on start-up. // The following tests expect that no projects are loaded on start-up.
void test_SwitchMethodDeclarationDefinition_fromFunctionDeclarationSymbol();
void test_SwitchMethodDeclarationDefinition_fromFunctionDefinitionSymbol();
void test_SwitchMethodDeclarationDefinition_fromFunctionBody();
void test_FollowSymbolUnderCursor_globalVarFromFunction(); void test_FollowSymbolUnderCursor_globalVarFromFunction();
void test_FollowSymbolUnderCursor_funLocalVarHidesClassMember(); void test_FollowSymbolUnderCursor_funLocalVarHidesClassMember();
void test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemberIntroducedByUsingDirective(); void test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemberIntroducedByUsingDirective();
......
...@@ -38,14 +38,14 @@ ...@@ -38,14 +38,14 @@
/*! /*!
Tests for Follow Symbol Under Cursor Tests for Follow Symbol Under Cursor and Switch Between Method Declaration/Definition
Section numbers refer to Section numbers refer to
Working Draft, Standard for Programming Language C++ Working Draft, Standard for Programming Language C++
Document Number: N3242=11-0012 Document Number: N3242=11-0012
You can find potential test code on the bottom of this file. You can find potential test code for Follow Symbol Under Cursor on the bottom of this file.
*/ */
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace CppEditor; using namespace CppEditor;
...@@ -80,12 +80,16 @@ public: ...@@ -80,12 +80,16 @@ public:
QVERIFY(initialCursorPosition != targetCursorPosition); QVERIFY(initialCursorPosition != targetCursorPosition);
if (initialCursorPosition > targetCursorPosition) { if (initialCursorPosition > targetCursorPosition) {
source.remove(initialCursorPosition, 1); source.remove(initialCursorPosition, 1);
source.remove(targetCursorPosition, 1); if (targetCursorPosition != -1) {
--initialCursorPosition; source.remove(targetCursorPosition, 1);
--initialCursorPosition;
}
} else { } else {
source.remove(targetCursorPosition, 1); source.remove(targetCursorPosition, 1);
source.remove(initialCursorPosition, 1); if (initialCursorPosition != -1) {
--targetCursorPosition; source.remove(initialCursorPosition, 1);
--targetCursorPosition;
}
} }
} }
...@@ -124,14 +128,19 @@ public: ...@@ -124,14 +128,19 @@ public:
/** /**
* Encapsulates the whole process of setting up several editors, positioning the cursor, * Encapsulates the whole process of setting up several editors, positioning the cursor,
* executing Follow Symbol Under Cursor and checking the result. * executing Follow Symbol Under Cursor or Switch Between Method Declaration/Definition
* and checking the result.
*/ */
struct TestCase class TestCase
{ {
QList<TestDocumentPtr> testFiles; public:
enum CppEditorAction {
FollowSymbolUnderCursor,
SwitchBetweenMethodDeclarationDefinition
};
TestCase(const QByteArray &source); TestCase(CppEditorAction action, const QByteArray &source);
TestCase(const QList<TestDocumentPtr> theTestFiles); TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles);
~TestCase(); ~TestCase();
void run(); void run();
...@@ -144,13 +153,18 @@ private: ...@@ -144,13 +153,18 @@ private:
TestDocumentPtr testFileWithInitialCursorMarker(); TestDocumentPtr testFileWithInitialCursorMarker();
TestDocumentPtr testFileWithTargetCursorMarker(); TestDocumentPtr testFileWithTargetCursorMarker();
private:
CppEditorAction m_action;
QList<TestDocumentPtr> m_testFiles;
}; };
/// Convenience function for creating a TestDocument. /// Convenience function for creating a TestDocument.
/// See TestDocument. /// See TestDocument.
TestCase::TestCase(const QByteArray &source) TestCase::TestCase(CppEditorAction action, const QByteArray &source)
: m_action(action)
{ {
testFiles << TestDocument::create(source, QLatin1String("file.cpp")); m_testFiles << TestDocument::create(source, QLatin1String("file.cpp"));
init(); init();
} }
...@@ -158,8 +172,9 @@ TestCase::TestCase(const QByteArray &source) ...@@ -158,8 +172,9 @@ TestCase::TestCase(const QByteArray &source)
/// Exactly one test document must be provided that contains '@', the initial position marker. /// Exactly one test document must be provided that contains '@', the initial position marker.
/// Exactly one test document must be provided that contains '$', the target position marker. /// Exactly one test document must be provided that contains '$', the target position marker.
/// It can be the same document. /// It can be the same document.
TestCase::TestCase(const QList<TestDocumentPtr> theTestFiles) TestCase::TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles)
: testFiles(theTestFiles) : m_action(action)
, m_testFiles(theTestFiles)
{ {
init(); init();
} }
...@@ -174,14 +189,14 @@ void TestCase::init() ...@@ -174,14 +189,14 @@ void TestCase::init()
// Write files to disk // Write files to disk
const QString directoryPath = QDir::tempPath(); const QString directoryPath = QDir::tempPath();
foreach (TestDocumentPtr testFile, testFiles) { foreach (TestDocumentPtr testFile, m_testFiles) {
testFile->directoryPath = directoryPath; testFile->directoryPath = directoryPath;
testFile->writeToDisk(); testFile->writeToDisk();
} }
// Update Code Model // Update Code Model
QStringList filePaths; QStringList filePaths;
foreach (const TestDocumentPtr &testFile, testFiles) foreach (const TestDocumentPtr &testFile, m_testFiles)
filePaths << testFile->filePath(); filePaths << testFile->filePath();
CppTools::CppModelManagerInterface::instance()->updateSourceFiles(filePaths); CppTools::CppModelManagerInterface::instance()->updateSourceFiles(filePaths);
...@@ -200,7 +215,7 @@ void TestCase::init() ...@@ -200,7 +215,7 @@ void TestCase::init()
} }
// Open Files // Open Files
foreach (TestDocumentPtr testFile, testFiles) { foreach (TestDocumentPtr testFile, m_testFiles) {
testFile->editor testFile->editor
= dynamic_cast<CPPEditor *>(EditorManager::openEditor(testFile->filePath())); = dynamic_cast<CPPEditor *>(EditorManager::openEditor(testFile->filePath()));
QVERIFY(testFile->editor); QVERIFY(testFile->editor);
...@@ -232,7 +247,7 @@ TestCase::~TestCase() ...@@ -232,7 +247,7 @@ TestCase::~TestCase()
{ {
// Close editors // Close editors
QList<Core::IEditor *> editorsToClose; QList<Core::IEditor *> editorsToClose;
foreach (const TestDocumentPtr testFile, testFiles) { foreach (const TestDocumentPtr testFile, m_testFiles) {
if (testFile->editor) if (testFile->editor)
editorsToClose << testFile->editor; editorsToClose << testFile->editor;
} }
...@@ -247,7 +262,7 @@ TestCase::~TestCase() ...@@ -247,7 +262,7 @@ TestCase::~TestCase()
TestDocumentPtr TestCase::testFileWithInitialCursorMarker() TestDocumentPtr TestCase::testFileWithInitialCursorMarker()
{ {
foreach (const TestDocumentPtr testFile, testFiles) { foreach (const TestDocumentPtr testFile, m_testFiles) {
if (testFile->hasInitialCursorMarker()) if (testFile->hasInitialCursorMarker())
return testFile; return testFile;
} }
...@@ -256,7 +271,7 @@ TestDocumentPtr TestCase::testFileWithInitialCursorMarker() ...@@ -256,7 +271,7 @@ TestDocumentPtr TestCase::testFileWithInitialCursorMarker()
TestDocumentPtr TestCase::testFileWithTargetCursorMarker() TestDocumentPtr TestCase::testFileWithTargetCursorMarker()
{ {
foreach (const TestDocumentPtr testFile, testFiles) { foreach (const TestDocumentPtr testFile, m_testFiles) {
if (testFile->hasTargetCursorMarker()) if (testFile->hasTargetCursorMarker())
return testFile; return testFile;
} }
...@@ -270,11 +285,25 @@ void TestCase::run() ...@@ -270,11 +285,25 @@ void TestCase::run()
TestDocumentPtr targetTestFile = testFileWithTargetCursorMarker(); TestDocumentPtr targetTestFile = testFileWithTargetCursorMarker();
QVERIFY(targetTestFile); QVERIFY(targetTestFile);
// Trigger Follow Symbol Under Cursor // Activate editor of initial test file
EditorManager::activateEditor(initialTestFile->editor);
initialTestFile->editor->setCursorPosition(initialTestFile->initialCursorPosition); initialTestFile->editor->setCursorPosition(initialTestFile->initialCursorPosition);
// qDebug() << "Initial line:" << initialTestFile->editor->currentLine(); // qDebug() << "Initial line:" << initialTestFile->editor->currentLine();
// qDebug() << "Initial column:" << initialTestFile->editor->currentColumn() - 1; // qDebug() << "Initial column:" << initialTestFile->editor->currentColumn() - 1;
initialTestFile->editorWidget->openLinkUnderCursor();
// Trigger the action
switch (m_action) {
case FollowSymbolUnderCursor:
initialTestFile->editorWidget->openLinkUnderCursor();
break;
case SwitchBetweenMethodDeclarationDefinition:
CppEditorPlugin::instance()->switchDeclarationDefinition();
break;
default:
QFAIL("Unknown test action");
break;
}
QCoreApplication::processEvents(); QCoreApplication::processEvents();
...@@ -295,6 +324,102 @@ void TestCase::run() ...@@ -295,6 +324,102 @@ void TestCase::run()
} // anonymous namespace } // anonymous namespace
void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromFunctionDeclarationSymbol()
{
QList<TestDocumentPtr> testFiles;
const QByteArray headerContents =
"class C\n"
"{\n"
"public:\n"
" C();\n"
" int @function();\n" // Line 5
"};\n"
;
testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));
const QByteArray sourceContents =
"#include \"file.h\"\n"
"\n"
"C::C()\n"
"{\n"
"}\n" // Line 5
"\n"
"int C::$function()\n"
"{\n"
" return 1 + 1;\n"
"}\n" // Line 10
;
testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));
TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
test.run();
}
void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromFunctionDefinitionSymbol()
{
QList<TestDocumentPtr> testFiles;
const QByteArray headerContents =
"class C\n"
"{\n"
"public:\n"
" C();\n"
" int $function();\n"
"};\n"
;
testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));
const QByteArray sourceContents =
"#include \"file.h\"\n"
"\n"
"C::C()\n"
"{\n"
"}\n"
"\n"
"int C::@function()\n"
"{\n"
" return 1 + 1;\n"
"}\n"
;
testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));
TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
test.run();
}
void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromFunctionBody()
{
QList<TestDocumentPtr> testFiles;
const QByteArray headerContents =
"class C\n"
"{\n"
"public:\n"
" C();\n"
" int $function();\n"
"};\n"
;
testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));
const QByteArray sourceContents =
"#include \"file.h\"\n"
"\n"
"C::C()\n"
"{\n"
"}\n" // Line 5
"\n"
"int C::function()\n"
"{\n"
" return @1 + 1;\n"
"}\n" // Line 10
;
testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));
TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
test.run();
}
/// Check ... /// Check ...
void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromFunction() void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromFunction()
{ {
...@@ -306,8 +431,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromFunction() ...@@ -306,8 +431,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromFunction()
"}\n" // Line 5 "}\n" // Line 5
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesClassMember() void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesClassMember()
...@@ -324,8 +449,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesClassMember() ...@@ -324,8 +449,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesClassMember()
"};\n" "};\n"
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemberIntroducedByUsingDirective() void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemberIntroducedByUsingDirective()
...@@ -344,8 +469,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemb ...@@ -344,8 +469,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemb
"}\n" // Line 10 "}\n" // Line 10
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVariable1() void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVariable1()
...@@ -362,8 +487,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVa ...@@ -362,8 +487,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVa
"}\n"; "}\n";
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVariable2() void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVariable2()
...@@ -380,8 +505,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVa ...@@ -380,8 +505,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVa
"}\n"; "}\n";
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_subsequentDefinedClassMember() void CppEditorPlugin::test_FollowSymbolUnderCursor_subsequentDefinedClassMember()
...@@ -394,8 +519,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_subsequentDefinedClassMember( ...@@ -394,8 +519,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_subsequentDefinedClassMember(
"};\n" "};\n"
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_classMemberHidesOuterTypeDef() void CppEditorPlugin::test_FollowSymbolUnderCursor_classMemberHidesOuterTypeDef()
...@@ -410,8 +535,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_classMemberHidesOuterTypeDef( ...@@ -410,8 +535,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_classMemberHidesOuterTypeDef(
"};\n" // Line 5 "};\n" // Line 5
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromEnum() void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromEnum()
...@@ -425,8 +550,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromEnum() ...@@ -425,8 +550,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromEnum()
"}\n" // Line 5 "}\n" // Line 5
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_selfInitialization() void CppEditorPlugin::test_FollowSymbolUnderCursor_selfInitialization()
...@@ -440,8 +565,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_selfInitialization() ...@@ -440,8 +565,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_selfInitialization()
"}\n" // Line 5 "}\n" // Line 5
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_pointerToClassInClassDefinition() void CppEditorPlugin::test_FollowSymbolUnderCursor_pointerToClassInClassDefinition()
...@@ -453,8 +578,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_pointerToClassInClassDefiniti ...@@ -453,8 +578,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_pointerToClassInClassDefiniti
"};\n" "};\n"
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_previouslyDefinedMemberFromArrayDefinition() void CppEditorPlugin::test_FollowSymbolUnderCursor_previouslyDefinedMemberFromArrayDefinition()
...@@ -467,8 +592,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_previouslyDefinedMemberFromAr ...@@ -467,8 +592,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_previouslyDefinedMemberFromAr
"};\n" "};\n"
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_outerStaticMemberVariableFromInsideSubclass() void CppEditorPlugin::test_FollowSymbolUnderCursor_outerStaticMemberVariableFromInsideSubclass()
...@@ -489,8 +614,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_outerStaticMemberVariableFrom ...@@ -489,8 +614,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_outerStaticMemberVariableFrom
"};\n" "};\n"
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingDotOperator() void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingDotOperator()
...@@ -509,8 +634,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingDotOpe ...@@ -509,8 +634,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingDotOpe
"}\n" // Line 10 "}\n" // Line 10
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingArrowOperator() void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingArrowOperator()
...@@ -529,8 +654,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingArrowO ...@@ -529,8 +654,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingArrowO
"}\n" // Line 10 "}\n" // Line 10
; ;
TestCase data(source); TestCase test(TestCase::FollowSymbolUnderCursor, source);
data.run(); test.run();
} }
void CppEditorPlugin::test_FollowSymbolUnderCursor_staticMemberVariableFollowingScopeOperator() void CppEditorPlugin::test_FollowSymbolUnderCursor_staticMemberVariableFollowingScopeOperator()
...@@ -548,8 +673,8 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_staticMemberVariableFollowing ...@@ -548