Commit 5c8df5fa authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppEditor/CppTools: Introduce Test{Case,Document}

Move common functionality of the 12 test classes into base classes.

Change-Id: If64d3cec876807ac6f991151189860a99b8ff4ca
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 5aa8a63f
...@@ -35,6 +35,14 @@ ...@@ -35,6 +35,14 @@
#include <QString> #include <QString>
#define QTC_DECLARE_MYTESTDATADIR(PATH) \
class MyTestDataDir : public Core::Internal::Tests::TestDataDir \
{ \
public: \
MyTestDataDir(const QString &testDataDirectory = QString()) \
: TestDataDir(QLatin1String(SRCDIR "/" PATH) + testDataDirectory) {} \
};
namespace Core { namespace Core {
namespace Internal { namespace Internal {
namespace Tests { namespace Tests {
......
...@@ -28,12 +28,13 @@ ...@@ -28,12 +28,13 @@
****************************************************************************/ ****************************************************************************/
#include "cppeditor.h" #include "cppeditor.h"
#include "cppeditorplugin.h"
#include "cppeditortestcase.h"
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <cplusplus/CppDocument.h>
#include <cppeditor/cppeditor.h>
#include <cppeditor/cppeditorplugin.h>
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <cplusplus/CppDocument.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QCoreApplication> #include <QCoreApplication>
...@@ -58,46 +59,31 @@ typedef QByteArray _; ...@@ -58,46 +59,31 @@ typedef QByteArray _;
* Encapsulates the whole process of setting up an editor, * Encapsulates the whole process of setting up an editor,
* pressing ENTER and checking the result. * pressing ENTER and checking the result.
*/ */
struct TestCase class TestCase : public CppEditor::Internal::Tests::TestCase
{ {
QByteArray originalText; public:
int pos;
CPPEditor *editor;
CPPEditorWidget *editorWidget;
TestCase(const QByteArray &input); TestCase(const QByteArray &input);
~TestCase();
void run(const QByteArray &expected, int undoCount = 1); void run(const QByteArray &expected, int undoCount = 1);
private: private:
TestCase(const TestCase &); CppEditor::Internal::Tests::TestDocument testDocument;
TestCase &operator=(const TestCase &);
}; };
/// The '|' in the input denotes the cursor position. /// The '|' in the input denotes the cursor position.
TestCase::TestCase(const QByteArray &input) TestCase::TestCase(const QByteArray &input)
: originalText(input) : testDocument("file.cpp", input, '|')
{ {
pos = originalText.indexOf('|'); QVERIFY(testDocument.hasCursorMarker());
QVERIFY(pos != -1); testDocument.m_source.remove(testDocument.m_cursorPosition, 1);
originalText.remove(pos, 1); QVERIFY(testDocument.writeToDisk());
QString fileName(QDir::tempPath() + QLatin1String("/file.cpp"));
Utils::FileSaver srcSaver(fileName);
srcSaver.write(originalText);
srcSaver.finalize();
// Update Code Model // Update Code Model
CppTools::CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance(); QVERIFY(parseFiles(testDocument.filePath()));
mmi->updateSourceFiles(QStringList(fileName)).waitForFinished();
QCoreApplication::processEvents();
QVERIFY(mmi->snapshot().contains(fileName));
// Open Editor // Open Editor
editor = dynamic_cast<CPPEditor *>(EditorManager::openEditor(fileName)); QVERIFY(openCppEditor(testDocument.filePath(), &testDocument.m_editor,
QVERIFY(editor); &testDocument.m_editorWidget));
editorWidget = dynamic_cast<CPPEditorWidget *>(editor->editorWidget()); closeEditorAtEndOfTestCase(testDocument.m_editor);
QVERIFY(editorWidget);
// We want to test documents that start with a comment. By default, the // We want to test documents that start with a comment. By default, the
// editor will fold the very first comment it encounters, assuming // editor will fold the very first comment it encounters, assuming
...@@ -105,39 +91,26 @@ TestCase::TestCase(const QByteArray &input) ...@@ -105,39 +91,26 @@ TestCase::TestCase(const QByteArray &input)
// expected (some blocks are still hidden in some test cases, so the // expected (some blocks are still hidden in some test cases, so the
// cursor movements are not as expected). For the time being, we just // cursor movements are not as expected). For the time being, we just
// prepend a declaration before the initial test comment. // prepend a declaration before the initial test comment.
// editorWidget->unfoldAll(); // testDocument.m_editorWidget->unfoldAll();
editor->setCursorPosition(pos); testDocument.m_editor->setCursorPosition(testDocument.m_cursorPosition);
editorWidget->semanticRehighlight(true);
// Wait for the semantic info from the future:
while (editorWidget->semanticInfo().doc.isNull())
QCoreApplication::processEvents();
}
TestCase::~TestCase()
{
EditorManager::closeEditor(editor, false);
QCoreApplication::processEvents(); // process any pending events
// Remove the test file from the code-model waitForRehighlightedSemanticDocument(testDocument.m_editorWidget);
CppTools::CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance();
mmi->GC();
QCOMPARE(mmi->snapshot().size(), 0);
} }
void TestCase::run(const QByteArray &expected, int undoCount) void TestCase::run(const QByteArray &expected, int undoCount)
{ {
// Send 'ENTER' key press // Send 'ENTER' key press
QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QCoreApplication::sendEvent(editorWidget, &event); QCoreApplication::sendEvent(testDocument.m_editorWidget, &event);
const QByteArray result = editorWidget->document()->toPlainText().toUtf8(); const QByteArray result = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
QCOMPARE(QLatin1String(result), QLatin1String(expected)); QCOMPARE(QLatin1String(result), QLatin1String(expected));
for (int i = 0; i < undoCount; ++i) for (int i = 0; i < undoCount; ++i)
editorWidget->undo(); testDocument.m_editorWidget->undo();
const QByteArray contentsAfterUndo = editorWidget->document()->toPlainText().toUtf8(); const QByteArray contentsAfterUndo
QCOMPARE(contentsAfterUndo, originalText); = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
QCOMPARE(contentsAfterUndo, testDocument.m_source);
} }
} // anonymous namespace } // anonymous namespace
......
...@@ -67,9 +67,11 @@ RESOURCES += \ ...@@ -67,9 +67,11 @@ RESOURCES += \
equals(TEST, 1) { equals(TEST, 1) {
HEADERS += \ HEADERS += \
cppeditortestcase.h \
cppquickfix_test_utils.h cppquickfix_test_utils.h
SOURCES += \ SOURCES += \
cppdoxygen_test.cpp \ cppdoxygen_test.cpp \
cppeditortestcase.cpp \
cppincludehierarchy_test.cpp \ cppincludehierarchy_test.cpp \
cppquickfix_test.cpp \ cppquickfix_test.cpp \
cppquickfix_test_utils.cpp \ cppquickfix_test_utils.cpp \
......
...@@ -51,6 +51,7 @@ QtcPlugin { ...@@ -51,6 +51,7 @@ QtcPlugin {
condition: project.testsEnabled condition: project.testsEnabled
files: [ files: [
"cppdoxygen_test.cpp", "cppdoxygen_test.cpp",
"cppeditortestcase.cpp", "cppeditortestcase.h",
"cppincludehierarchy_test.cpp", "cppincludehierarchy_test.cpp",
"cppquickfix_test.cpp", "cppquickfix_test.cpp",
"cppquickfix_test_utils.cpp", "cppquickfix_test_utils.cpp",
......
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cppeditortestcase.h"
#include "cppeditor.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cplusplus/CppDocument.h>
#include <QDir>
namespace CppEditor {
namespace Internal {
namespace Tests {
TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker)
: CppTools::Tests::TestDocument(fileName, source, cursorMarker)
, m_cursorPosition(source.indexOf(m_cursorMarker))
, m_editor(0)
, m_editorWidget(0)
{
}
bool TestDocument::hasCursorMarker() const { return m_cursorPosition != -1; }
TestCase::TestCase(bool runGarbageCollector)
: CppTools::Tests::TestCase(runGarbageCollector)
{
}
TestCase::~TestCase()
{
}
bool TestCase::openCppEditor(const QString &fileName,
Internal::CPPEditor **editor,
Internal::CPPEditorWidget **editorWidget)
{
using namespace CppEditor::Internal;
if (CPPEditor *e = dynamic_cast<CPPEditor *>(Core::EditorManager::openEditor(fileName))) {
if (editor)
*editor = e;
if (editorWidget) {
if (CPPEditorWidget *w = dynamic_cast<CPPEditorWidget *>(e->editorWidget())) {
*editorWidget = w;
return true;
} else {
return false; // no or wrong widget
}
} else {
return true; // ok since no widget requested
}
} else {
return false; // no or wrong editor
}
}
CPlusPlus::Document::Ptr TestCase::waitForRehighlightedSemanticDocument(
Internal::CPPEditorWidget *editorWidget)
{
editorWidget->semanticRehighlight(true);
while (editorWidget->semanticInfo().doc.isNull())
QCoreApplication::processEvents();
return editorWidget->semanticInfo().doc;
}
} // namespace Tests
} // namespace Internal
} // namespace CppEditor
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPPEDITORTESTCASE_H
#define CPPEDITORTESTCASE_H
#include <cpptools/cpptoolstestcase.h>
namespace CppEditor {
namespace Internal {
class CPPEditor;
class CPPEditorWidget;
namespace Tests {
class TestDocument : public CppTools::Tests::TestDocument
{
public:
TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker = '@');
bool hasCursorMarker() const;
public:
int m_cursorPosition;
Internal::CPPEditor *m_editor;
Internal::CPPEditorWidget *m_editorWidget;
};
class TestCase : public CppTools::Tests::TestCase
{
public:
TestCase(bool runGarbageCollector = true);
~TestCase();
static bool openCppEditor(const QString &fileName,
Internal::CPPEditor **editor,
Internal::CPPEditorWidget **editorWidget = 0);
static CPlusPlus::Document::Ptr waitForRehighlightedSemanticDocument(
Internal::CPPEditorWidget *editorWidget);
};
} // namespace Tests
} // namespace Internal
} // namespace CppEditor
#endif // CPPEDITORTESTCASE_H
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
****************************************************************************/ ****************************************************************************/
#include "cppeditorplugin.h" #include "cppeditorplugin.h"
#include "cppeditortestcase.h"
#include "cppincludehierarchymodel.h" #include "cppincludehierarchymodel.h"
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
...@@ -44,12 +45,10 @@ using namespace CppEditor::Internal; ...@@ -44,12 +45,10 @@ using namespace CppEditor::Internal;
using namespace CppTools; using namespace CppTools;
namespace { namespace {
class TestCase class TestCase : public CppEditor::Internal::Tests::TestCase
{ {
public: public:
TestCase(const QList<QByteArray> &sourceList) TestCase(const QList<QByteArray> &sourceList)
: m_cmm(CppModelManagerInterface::instance())
, m_editor(0)
{ {
QStringList filePaths; QStringList filePaths;
const int sourceListSize = sourceList.size(); const int sourceListSize = sourceList.size();
...@@ -59,48 +58,28 @@ public: ...@@ -59,48 +58,28 @@ public:
// Write source to file // Write source to file
const QString fileName = QString::fromLatin1("%1/file%2.h").arg(QDir::tempPath()) const QString fileName = QString::fromLatin1("%1/file%2.h").arg(QDir::tempPath())
.arg(i+1); .arg(i+1);
Utils::FileSaver srcSaver(fileName); QVERIFY(writeFile(fileName, source));
srcSaver.write(source);
srcSaver.finalize();
filePaths << fileName; filePaths << fileName;
} }
// Update Code Model // Update Code Model
m_cmm->updateSourceFiles(filePaths).waitForFinished(); QVERIFY(parseFiles(filePaths));
QCoreApplication::processEvents();
const Snapshot snapshot = m_cmm->snapshot();
QVERIFY(!snapshot.isEmpty());
foreach (const QString &filePath, filePaths)
QVERIFY(snapshot.contains(filePath));
}
~TestCase()
{
// Close editor
if (m_editor)
Core::EditorManager::closeEditor(m_editor, false);
m_cmm->GC();
QVERIFY(m_cmm->snapshot().isEmpty());
} }
void run(int includesCount, int includedByCount) void run(int includesCount, int includedByCount)
{ {
const QString fileName = QDir::tempPath() + QLatin1String("/file1.h"); const QString fileName = QDir::tempPath() + QLatin1String("/file1.h");
m_editor = qobject_cast<CPPEditor *>(Core::EditorManager::openEditor(fileName)); CPPEditor *editor;
QVERIFY(m_editor); QVERIFY(openCppEditor(fileName, &editor));
closeEditorAtEndOfTestCase(editor);
CppIncludeHierarchyModel model(0); CppIncludeHierarchyModel model(0);
model.buildHierarchy(m_editor, fileName); model.buildHierarchy(editor, fileName);
QCOMPARE(model.rowCount(model.index(0, 0)), includesCount); QCOMPARE(model.rowCount(model.index(0, 0)), includesCount);
QCOMPARE(model.rowCount(model.index(1, 0)), includedByCount); QCOMPARE(model.rowCount(model.index(1, 0)), includedByCount);
} }
private:
CppModelManagerInterface *m_cmm;
CPPEditor *m_editor;
}; };
} }
......
This diff is collapsed.
...@@ -27,26 +27,28 @@ ...@@ -27,26 +27,28 @@
** **
****************************************************************************/ ****************************************************************************/
#include <cplusplus/CppDocument.h> #include "cppeditor.h"
#include <cplusplus/TranslationUnit.h> #include "cppeditorplugin.h"
#include "cppeditortestcase.h"
#include "cppquickfix.h"
#include "cppquickfix_test_utils.h"
#include "cppquickfixassistant.h"
#include "cppquickfixes.h"
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppeditor.h>
#include <cppeditor/cppeditorplugin.h>
#include <cppeditor/cppquickfixassistant.h>
#include <cppeditor/cppquickfixes.h>
#include <cppeditor/cppquickfix.h>
#include <cppeditor/cppquickfix_test_utils.h>
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <cpptools/cpptoolsplugin.h> #include <cpptools/cpptoolsplugin.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <texteditor/basetextdocument.h> #include <texteditor/basetextdocument.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/TranslationUnit.h>
#include <QDebug> #include <QDebug>
#include <QtAlgorithms>
#include <QTextDocument> #include <QTextDocument>
#include <QtAlgorithms>
#include <QtTest> #include <QtTest>
#if QT_VERSION >= 0x050000 #if QT_VERSION >= 0x050000
...@@ -81,7 +83,7 @@ using namespace TextEditor; ...@@ -81,7 +83,7 @@ using namespace TextEditor;
namespace { namespace {
class TestActionsTestCase class TestActionsTestCase : public CppEditor::Internal::Tests::TestCase
{ {
public: public:
class AbstractAction class AbstractAction
...@@ -95,6 +97,8 @@ public: ...@@ -95,6 +97,8 @@ public:
typedef QList<ActionPointer> Actions; typedef QList<ActionPointer> Actions;
public: public:
TestActionsTestCase();
/// Run the given fileActions for each file and the given tokenActions for each token. /// Run the given fileActions for each file and the given tokenActions for each token.
/// The cursor is positioned on the very first character of each token. /// The cursor is positioned on the very first character of each token.
void run(const Actions &tokenActions = Actions(), void run(const Actions &tokenActions = Actions(),
...@@ -132,14 +136,18 @@ bool TestActionsTestCase::allProjectsConfigured = false; ...@@ -132,14 +136,18 @@ bool TestActionsTestCase::allProjectsConfigured = false;
typedef TestActionsTestCase::Actions Actions; typedef TestActionsTestCase::Actions Actions;
typedef TestActionsTestCase::ActionPointer ActionPointer; typedef TestActionsTestCase::ActionPointer ActionPointer;
void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileActions) TestActionsTestCase::TestActionsTestCase()
: CppEditor::Internal::Tests::TestCase(/*runGarbageCollector=*/false)
{ {
CppModelManagerInterface *mm = CppModelManagerInterface::instance(); }
void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileActions)
{
// Collect files to process // Collect files to process
QStringList filesToOpen; QStringList filesToOpen;
QList<QPointer<ProjectExplorer::Project> > projects; QList<QPointer<ProjectExplorer::Project> > projects;
const QList<CppModelManagerInterface::ProjectInfo> projectInfos = mm->projectInfos(); const QList<CppModelManagerInterface::ProjectInfo> projectInfos
= m_modelManager->projectInfos();
if (projectInfos.isEmpty()) if (projectInfos.isEmpty())
MSKIP_SINGLE("No project(s) loaded. Test operates only on loaded projects."); MSKIP_SINGLE("No project(s) loaded. Test operates only on loaded projects.");
...@@ -175,18 +183,16 @@ void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileAc ...@@ -175,18 +183,16 @@ void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileAc
// Open editor // Open editor
QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), 0); QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), 0);
CPPEditor *editor = dynamic_cast<CPPEditor *>(EditorManager::openEditor(filePath)); CPPEditor *editor;
QVERIFY(editor); CPPEditorWidget *editorWidget;
QVERIFY(openCppEditor(filePath, &editor, &editorWidget));
QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), 1); QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), 1);
QVERIFY(mm->isCppEditor(editor)); QVERIFY(m_modelManager->isCppEditor(editor));
QVERIFY(mm->workingCopy().contains(filePath)); QVERIFY(m_modelManager->workingCopy().contains(filePath));
// Rehighlight // Rehighlight
CPPEditorWidget *editorWidget = dynamic_cast<CPPEditorWidget *>(editor->editorWidget()); waitForRehighlightedSemanticDocument(editorWidget);
QVERIFY(editorWidget);
editorWidget->semanticRehighlight(true);
while (editorWidget->semanticInfo().doc.isNull())
QApplication::processEvents();
// Run all file actions // Run all file actions
executeActionsOnEditorWidget(editorWidget, fileActions); executeActionsOnEditorWidget(editorWidget, fileActions);
...@@ -194,7 +200,7 @@ void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileAc ...@@ -194,7 +200,7 @@ void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileAc
if (tokenActions.empty()) if (tokenActions.empty())
continue; continue;
Snapshot snapshot = mm->snapshot(); const Snapshot snapshot = globalSnapshot();
Document::Ptr document = snapshot.preprocessedDocument( Document::Ptr document = snapshot.preprocessedDocument(
editorWidget->document()->toPlainText().toUtf8(), filePath); editorWidget->document()->toPlainText().toUtf8(), filePath);
QVERIFY(document); QVERIFY(document);
......
...@@ -27,8 +27,9 @@ ...@@ -27,8 +27,9 @@
** **
****************************************************************************/ ****************************************************************************/
#include "insertionpointlocator.h"
#include "cpptoolsplugin.h" #include "cpptoolsplugin.h"
#include "cpptoolstestcase.h"
#include "insertionpointlocator.h"
#include <utils/fileutils.h> #include <utils/fileutils.h>
...@@ -361,9 +362,7 @@ void CppToolsPlugin::test_codegen_definition_empty_class() ...@@ -361,9 +362,7 @@ void CppToolsPlugin::test_codegen_definition_empty_class()
"\n"; "\n";
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h")); Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));