forked from qt-creator/qt-creator
CppEditor/CppTools: Introduce Test{Case,Document}
Move common functionality of the 12 test classes into base classes. Change-Id: If64d3cec876807ac6f991151189860a99b8ff4ca Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
@@ -35,6 +35,14 @@
|
||||
|
||||
#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 Internal {
|
||||
namespace Tests {
|
||||
|
||||
@@ -28,12 +28,13 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "cppeditor.h"
|
||||
#include "cppeditorplugin.h"
|
||||
#include "cppeditortestcase.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <cppeditor/cppeditor.h>
|
||||
#include <cppeditor/cppeditorplugin.h>
|
||||
#include <cpptools/cppmodelmanagerinterface.h>
|
||||
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
@@ -58,46 +59,31 @@ typedef QByteArray _;
|
||||
* Encapsulates the whole process of setting up an editor,
|
||||
* pressing ENTER and checking the result.
|
||||
*/
|
||||
struct TestCase
|
||||
class TestCase : public CppEditor::Internal::Tests::TestCase
|
||||
{
|
||||
QByteArray originalText;
|
||||
int pos;
|
||||
CPPEditor *editor;
|
||||
CPPEditorWidget *editorWidget;
|
||||
|
||||
public:
|
||||
TestCase(const QByteArray &input);
|
||||
~TestCase();
|
||||
|
||||
void run(const QByteArray &expected, int undoCount = 1);
|
||||
|
||||
private:
|
||||
TestCase(const TestCase &);
|
||||
TestCase &operator=(const TestCase &);
|
||||
CppEditor::Internal::Tests::TestDocument testDocument;
|
||||
};
|
||||
|
||||
/// The '|' in the input denotes the cursor position.
|
||||
TestCase::TestCase(const QByteArray &input)
|
||||
: originalText(input)
|
||||
: testDocument("file.cpp", input, '|')
|
||||
{
|
||||
pos = originalText.indexOf('|');
|
||||
QVERIFY(pos != -1);
|
||||
originalText.remove(pos, 1);
|
||||
QString fileName(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
Utils::FileSaver srcSaver(fileName);
|
||||
srcSaver.write(originalText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(testDocument.hasCursorMarker());
|
||||
testDocument.m_source.remove(testDocument.m_cursorPosition, 1);
|
||||
QVERIFY(testDocument.writeToDisk());
|
||||
|
||||
// Update Code Model
|
||||
CppTools::CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance();
|
||||
mmi->updateSourceFiles(QStringList(fileName)).waitForFinished();
|
||||
QCoreApplication::processEvents();
|
||||
QVERIFY(mmi->snapshot().contains(fileName));
|
||||
QVERIFY(parseFiles(testDocument.filePath()));
|
||||
|
||||
// Open Editor
|
||||
editor = dynamic_cast<CPPEditor *>(EditorManager::openEditor(fileName));
|
||||
QVERIFY(editor);
|
||||
editorWidget = dynamic_cast<CPPEditorWidget *>(editor->editorWidget());
|
||||
QVERIFY(editorWidget);
|
||||
QVERIFY(openCppEditor(testDocument.filePath(), &testDocument.m_editor,
|
||||
&testDocument.m_editorWidget));
|
||||
closeEditorAtEndOfTestCase(testDocument.m_editor);
|
||||
|
||||
// We want to test documents that start with a comment. By default, the
|
||||
// editor will fold the very first comment it encounters, assuming
|
||||
@@ -105,39 +91,26 @@ TestCase::TestCase(const QByteArray &input)
|
||||
// expected (some blocks are still hidden in some test cases, so the
|
||||
// cursor movements are not as expected). For the time being, we just
|
||||
// prepend a declaration before the initial test comment.
|
||||
// editorWidget->unfoldAll();
|
||||
editor->setCursorPosition(pos);
|
||||
// testDocument.m_editorWidget->unfoldAll();
|
||||
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
|
||||
CppTools::CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance();
|
||||
mmi->GC();
|
||||
QCOMPARE(mmi->snapshot().size(), 0);
|
||||
waitForRehighlightedSemanticDocument(testDocument.m_editorWidget);
|
||||
}
|
||||
|
||||
void TestCase::run(const QByteArray &expected, int undoCount)
|
||||
{
|
||||
// Send 'ENTER' key press
|
||||
QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
|
||||
QCoreApplication::sendEvent(editorWidget, &event);
|
||||
const QByteArray result = editorWidget->document()->toPlainText().toUtf8();
|
||||
QCoreApplication::sendEvent(testDocument.m_editorWidget, &event);
|
||||
const QByteArray result = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
|
||||
|
||||
QCOMPARE(QLatin1String(result), QLatin1String(expected));
|
||||
|
||||
for (int i = 0; i < undoCount; ++i)
|
||||
editorWidget->undo();
|
||||
const QByteArray contentsAfterUndo = editorWidget->document()->toPlainText().toUtf8();
|
||||
QCOMPARE(contentsAfterUndo, originalText);
|
||||
testDocument.m_editorWidget->undo();
|
||||
const QByteArray contentsAfterUndo
|
||||
= testDocument.m_editorWidget->document()->toPlainText().toUtf8();
|
||||
QCOMPARE(contentsAfterUndo, testDocument.m_source);
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
|
||||
@@ -67,9 +67,11 @@ RESOURCES += \
|
||||
|
||||
equals(TEST, 1) {
|
||||
HEADERS += \
|
||||
cppeditortestcase.h \
|
||||
cppquickfix_test_utils.h
|
||||
SOURCES += \
|
||||
cppdoxygen_test.cpp \
|
||||
cppeditortestcase.cpp \
|
||||
cppincludehierarchy_test.cpp \
|
||||
cppquickfix_test.cpp \
|
||||
cppquickfix_test_utils.cpp \
|
||||
|
||||
@@ -51,6 +51,7 @@ QtcPlugin {
|
||||
condition: project.testsEnabled
|
||||
files: [
|
||||
"cppdoxygen_test.cpp",
|
||||
"cppeditortestcase.cpp", "cppeditortestcase.h",
|
||||
"cppincludehierarchy_test.cpp",
|
||||
"cppquickfix_test.cpp",
|
||||
"cppquickfix_test_utils.cpp",
|
||||
|
||||
97
src/plugins/cppeditor/cppeditortestcase.cpp
Normal file
97
src/plugins/cppeditor/cppeditortestcase.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
75
src/plugins/cppeditor/cppeditortestcase.h
Normal file
75
src/plugins/cppeditor/cppeditortestcase.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "cppeditorplugin.h"
|
||||
#include "cppeditortestcase.h"
|
||||
#include "cppincludehierarchymodel.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
@@ -44,12 +45,10 @@ using namespace CppEditor::Internal;
|
||||
using namespace CppTools;
|
||||
|
||||
namespace {
|
||||
class TestCase
|
||||
class TestCase : public CppEditor::Internal::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
TestCase(const QList<QByteArray> &sourceList)
|
||||
: m_cmm(CppModelManagerInterface::instance())
|
||||
, m_editor(0)
|
||||
{
|
||||
QStringList filePaths;
|
||||
const int sourceListSize = sourceList.size();
|
||||
@@ -59,48 +58,28 @@ public:
|
||||
// Write source to file
|
||||
const QString fileName = QString::fromLatin1("%1/file%2.h").arg(QDir::tempPath())
|
||||
.arg(i+1);
|
||||
Utils::FileSaver srcSaver(fileName);
|
||||
srcSaver.write(source);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(writeFile(fileName, source));
|
||||
|
||||
filePaths << fileName;
|
||||
}
|
||||
|
||||
// Update Code Model
|
||||
m_cmm->updateSourceFiles(filePaths).waitForFinished();
|
||||
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());
|
||||
QVERIFY(parseFiles(filePaths));
|
||||
}
|
||||
|
||||
void run(int includesCount, int includedByCount)
|
||||
{
|
||||
const QString fileName = QDir::tempPath() + QLatin1String("/file1.h");
|
||||
|
||||
m_editor = qobject_cast<CPPEditor *>(Core::EditorManager::openEditor(fileName));
|
||||
QVERIFY(m_editor);
|
||||
CPPEditor *editor;
|
||||
QVERIFY(openCppEditor(fileName, &editor));
|
||||
closeEditorAtEndOfTestCase(editor);
|
||||
|
||||
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(1, 0)), includedByCount);
|
||||
}
|
||||
|
||||
private:
|
||||
CppModelManagerInterface *m_cmm;
|
||||
CPPEditor *m_editor;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -27,26 +27,28 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <cplusplus/TranslationUnit.h>
|
||||
#include "cppeditor.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 <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/cpptoolsplugin.h>
|
||||
#include <extensionsystem/pluginmanager.h>
|
||||
#include <projectexplorer/projectexplorer.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/projectexplorer.h>
|
||||
#include <texteditor/basetextdocument.h>
|
||||
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <cplusplus/TranslationUnit.h>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QtAlgorithms>
|
||||
#include <QTextDocument>
|
||||
#include <QtAlgorithms>
|
||||
#include <QtTest>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
@@ -81,7 +83,7 @@ using namespace TextEditor;
|
||||
|
||||
namespace {
|
||||
|
||||
class TestActionsTestCase
|
||||
class TestActionsTestCase : public CppEditor::Internal::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
class AbstractAction
|
||||
@@ -95,6 +97,8 @@ public:
|
||||
typedef QList<ActionPointer> Actions;
|
||||
|
||||
public:
|
||||
TestActionsTestCase();
|
||||
|
||||
/// 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.
|
||||
void run(const Actions &tokenActions = Actions(),
|
||||
@@ -132,14 +136,18 @@ bool TestActionsTestCase::allProjectsConfigured = false;
|
||||
typedef TestActionsTestCase::Actions Actions;
|
||||
typedef TestActionsTestCase::ActionPointer ActionPointer;
|
||||
|
||||
TestActionsTestCase::TestActionsTestCase()
|
||||
: CppEditor::Internal::Tests::TestCase(/*runGarbageCollector=*/false)
|
||||
{
|
||||
}
|
||||
|
||||
void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileActions)
|
||||
{
|
||||
CppModelManagerInterface *mm = CppModelManagerInterface::instance();
|
||||
|
||||
// Collect files to process
|
||||
QStringList filesToOpen;
|
||||
QList<QPointer<ProjectExplorer::Project> > projects;
|
||||
const QList<CppModelManagerInterface::ProjectInfo> projectInfos = mm->projectInfos();
|
||||
const QList<CppModelManagerInterface::ProjectInfo> projectInfos
|
||||
= m_modelManager->projectInfos();
|
||||
if (projectInfos.isEmpty())
|
||||
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
|
||||
|
||||
// Open editor
|
||||
QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), 0);
|
||||
CPPEditor *editor = dynamic_cast<CPPEditor *>(EditorManager::openEditor(filePath));
|
||||
QVERIFY(editor);
|
||||
CPPEditor *editor;
|
||||
CPPEditorWidget *editorWidget;
|
||||
QVERIFY(openCppEditor(filePath, &editor, &editorWidget));
|
||||
|
||||
QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), 1);
|
||||
QVERIFY(mm->isCppEditor(editor));
|
||||
QVERIFY(mm->workingCopy().contains(filePath));
|
||||
QVERIFY(m_modelManager->isCppEditor(editor));
|
||||
QVERIFY(m_modelManager->workingCopy().contains(filePath));
|
||||
|
||||
// Rehighlight
|
||||
CPPEditorWidget *editorWidget = dynamic_cast<CPPEditorWidget *>(editor->editorWidget());
|
||||
QVERIFY(editorWidget);
|
||||
editorWidget->semanticRehighlight(true);
|
||||
while (editorWidget->semanticInfo().doc.isNull())
|
||||
QApplication::processEvents();
|
||||
waitForRehighlightedSemanticDocument(editorWidget);
|
||||
|
||||
// Run all file actions
|
||||
executeActionsOnEditorWidget(editorWidget, fileActions);
|
||||
@@ -194,7 +200,7 @@ void TestActionsTestCase::run(const Actions &tokenActions, const Actions &fileAc
|
||||
if (tokenActions.empty())
|
||||
continue;
|
||||
|
||||
Snapshot snapshot = mm->snapshot();
|
||||
const Snapshot snapshot = globalSnapshot();
|
||||
Document::Ptr document = snapshot.preprocessedDocument(
|
||||
editorWidget->document()->toPlainText().toUtf8(), filePath);
|
||||
QVERIFY(document);
|
||||
|
||||
@@ -29,13 +29,15 @@
|
||||
|
||||
#include "cppeditor.h"
|
||||
#include "cppeditorplugin.h"
|
||||
#include "cppeditortestcase.h"
|
||||
#include "cppelementevaluator.h"
|
||||
#include "cppvirtualfunctionassistprovider.h"
|
||||
#include "cppvirtualfunctionproposalitem.h"
|
||||
|
||||
#include <texteditor/codeassist/iassistproposal.h>
|
||||
#include <texteditor/codeassist/iassistprocessor.h>
|
||||
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
|
||||
#include <texteditor/codeassist/iassistprocessor.h>
|
||||
#include <texteditor/codeassist/iassistproposal.h>
|
||||
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
#include <QDebug>
|
||||
@@ -168,66 +170,40 @@ typedef QSharedPointer<TestDocument> TestDocumentPtr;
|
||||
* - a '@' character denotes the initial text cursor position
|
||||
* - a '$' character denotes the target text cursor position
|
||||
*/
|
||||
class TestDocument
|
||||
class TestDocument : public CppEditor::Internal::Tests::TestDocument
|
||||
{
|
||||
public:
|
||||
TestDocument(const QByteArray &theSource, const QString &fileName)
|
||||
: source(theSource)
|
||||
, fileName(fileName)
|
||||
, initialCursorPosition(source.indexOf('@'))
|
||||
, targetCursorPosition(source.indexOf('$'))
|
||||
, editor(0)
|
||||
, editorWidget(0)
|
||||
TestDocument(const QByteArray &source, const QByteArray &fileName)
|
||||
: CppEditor::Internal::Tests::TestDocument(fileName, source)
|
||||
, m_targetCursorPosition(source.indexOf('$'))
|
||||
{
|
||||
if (initialCursorPosition != -1 || targetCursorPosition != -1)
|
||||
QVERIFY(initialCursorPosition != targetCursorPosition);
|
||||
if (m_cursorPosition != -1 || m_targetCursorPosition != -1)
|
||||
QVERIFY(m_cursorPosition != m_targetCursorPosition);
|
||||
|
||||
if (initialCursorPosition > targetCursorPosition) {
|
||||
source.remove(initialCursorPosition, 1);
|
||||
if (targetCursorPosition != -1) {
|
||||
source.remove(targetCursorPosition, 1);
|
||||
--initialCursorPosition;
|
||||
if (m_cursorPosition > m_targetCursorPosition) {
|
||||
m_source.remove(m_cursorPosition, 1);
|
||||
if (m_targetCursorPosition != -1) {
|
||||
m_source.remove(m_targetCursorPosition, 1);
|
||||
--m_cursorPosition;
|
||||
}
|
||||
} else {
|
||||
source.remove(targetCursorPosition, 1);
|
||||
if (initialCursorPosition != -1) {
|
||||
source.remove(initialCursorPosition, 1);
|
||||
--targetCursorPosition;
|
||||
m_source.remove(m_targetCursorPosition, 1);
|
||||
if (m_cursorPosition != -1) {
|
||||
m_source.remove(m_cursorPosition, 1);
|
||||
--m_targetCursorPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TestDocumentPtr create(const QByteArray &theOriginalSource, const QString &fileName)
|
||||
static TestDocumentPtr create(const QByteArray &source, const QByteArray &fileName)
|
||||
{
|
||||
return TestDocumentPtr(new TestDocument(theOriginalSource, fileName));
|
||||
return TestDocumentPtr(new TestDocument(source, fileName));
|
||||
}
|
||||
|
||||
bool hasInitialCursorMarker() const { return initialCursorPosition != -1; }
|
||||
bool hasTargetCursorMarker() const { return targetCursorPosition != -1; }
|
||||
bool hasTargetCursorMarker() const { return m_targetCursorPosition != -1; }
|
||||
|
||||
QString filePath() const
|
||||
{
|
||||
if (directoryPath.isEmpty())
|
||||
qDebug() << "directoryPath not set!";
|
||||
return directoryPath + QLatin1Char('/') + fileName;
|
||||
}
|
||||
|
||||
void writeToDisk() const
|
||||
{
|
||||
Utils::FileSaver srcSaver(filePath());
|
||||
srcSaver.write(source);
|
||||
srcSaver.finalize();
|
||||
}
|
||||
|
||||
QByteArray source;
|
||||
|
||||
const QString fileName;
|
||||
QString directoryPath;
|
||||
int initialCursorPosition;
|
||||
int targetCursorPosition;
|
||||
|
||||
CPPEditor *editor;
|
||||
CPPEditorWidget *editorWidget;
|
||||
public:
|
||||
int m_targetCursorPosition;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -235,7 +211,7 @@ public:
|
||||
* executing Follow Symbol Under Cursor or Switch Between Function Declaration/Definition
|
||||
* and checking the result.
|
||||
*/
|
||||
class TestCase
|
||||
class TestCase : public CppEditor::Internal::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
enum CppEditorAction {
|
||||
@@ -247,7 +223,6 @@ public:
|
||||
const OverrideItemList &expectedVirtualFunctionProposal = OverrideItemList());
|
||||
TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles,
|
||||
const OverrideItemList &expectedVirtualFunctionProposal = OverrideItemList());
|
||||
~TestCase();
|
||||
|
||||
void run();
|
||||
|
||||
@@ -273,7 +248,7 @@ TestCase::TestCase(CppEditorAction action, const QByteArray &source,
|
||||
: m_action(action)
|
||||
, m_expectedVirtualFunctionProposal(expectedVirtualFunctionProposal)
|
||||
{
|
||||
m_testFiles << TestDocument::create(source, QLatin1String("file.cpp"));
|
||||
m_testFiles << TestDocument::create(source, "file.cpp");
|
||||
init();
|
||||
}
|
||||
|
||||
@@ -299,74 +274,39 @@ void TestCase::init()
|
||||
"No test file with target cursor marker is provided.");
|
||||
|
||||
// Write files to disk
|
||||
const QString directoryPath = QDir::tempPath();
|
||||
foreach (TestDocumentPtr testFile, m_testFiles) {
|
||||
testFile->directoryPath = directoryPath;
|
||||
testFile->writeToDisk();
|
||||
}
|
||||
foreach (TestDocumentPtr testFile, m_testFiles)
|
||||
QVERIFY(testFile->writeToDisk());
|
||||
|
||||
// Update Code Model
|
||||
QStringList filePaths;
|
||||
foreach (const TestDocumentPtr &testFile, m_testFiles)
|
||||
filePaths << testFile->filePath();
|
||||
CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance();
|
||||
mmi->updateSourceFiles(filePaths).waitForFinished();
|
||||
QCoreApplication::processEvents();
|
||||
const Snapshot snapshot = mmi->snapshot();
|
||||
QVERIFY(!snapshot.isEmpty());
|
||||
foreach (const QString &filePath, filePaths)
|
||||
QVERIFY(snapshot.contains(filePath));
|
||||
QVERIFY(parseFiles(filePaths));
|
||||
|
||||
// Open Files
|
||||
foreach (TestDocumentPtr testFile, m_testFiles) {
|
||||
testFile->editor
|
||||
= dynamic_cast<CPPEditor *>(EditorManager::openEditor(testFile->filePath()));
|
||||
QVERIFY(testFile->editor);
|
||||
|
||||
testFile->editorWidget = dynamic_cast<CPPEditorWidget *>(testFile->editor->editorWidget());
|
||||
QVERIFY(testFile->editorWidget);
|
||||
QVERIFY(openCppEditor(testFile->filePath(), &testFile->m_editor,
|
||||
&testFile->m_editorWidget));
|
||||
closeEditorAtEndOfTestCase(testFile->m_editor);
|
||||
|
||||
// Wait until the indexer processed the just opened file.
|
||||
// The file is "Full Checked" since it is in the working copy now,
|
||||
// that is the function bodies are processed.
|
||||
forever {
|
||||
Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
|
||||
if (Document::Ptr document = snapshot.document(testFile->filePath())) {
|
||||
if (document->checkMode() == Document::FullCheck)
|
||||
break;
|
||||
QCoreApplication::processEvents();
|
||||
}
|
||||
const Document::Ptr document = waitForFileInGlobalSnapshot(testFile->filePath());
|
||||
if (document->checkMode() == Document::FullCheck)
|
||||
break;
|
||||
}
|
||||
|
||||
// Rehighlight
|
||||
testFile->editorWidget->semanticRehighlight(true);
|
||||
// Wait for the semantic info from the future
|
||||
while (testFile->editorWidget->semanticInfo().doc.isNull())
|
||||
QCoreApplication::processEvents();
|
||||
waitForRehighlightedSemanticDocument(testFile->m_editorWidget);
|
||||
}
|
||||
}
|
||||
|
||||
TestCase::~TestCase()
|
||||
{
|
||||
// Close editors
|
||||
QList<Core::IEditor *> editorsToClose;
|
||||
foreach (const TestDocumentPtr testFile, m_testFiles) {
|
||||
if (testFile->editor)
|
||||
editorsToClose << testFile->editor;
|
||||
}
|
||||
EditorManager::closeEditors(editorsToClose, false);
|
||||
QCoreApplication::processEvents(); // process any pending events
|
||||
|
||||
// Remove the test files from the code-model
|
||||
CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance();
|
||||
mmi->GC();
|
||||
QCOMPARE(mmi->snapshot().size(), 0);
|
||||
}
|
||||
|
||||
TestDocumentPtr TestCase::testFileWithInitialCursorMarker()
|
||||
{
|
||||
foreach (const TestDocumentPtr testFile, m_testFiles) {
|
||||
if (testFile->hasInitialCursorMarker())
|
||||
if (testFile->hasCursorMarker())
|
||||
return testFile;
|
||||
}
|
||||
return TestDocumentPtr();
|
||||
@@ -389,9 +329,9 @@ void TestCase::run()
|
||||
QVERIFY(targetTestFile);
|
||||
|
||||
// Activate editor of initial test file
|
||||
EditorManager::activateEditor(initialTestFile->editor);
|
||||
EditorManager::activateEditor(initialTestFile->m_editor);
|
||||
|
||||
initialTestFile->editor->setCursorPosition(initialTestFile->initialCursorPosition);
|
||||
initialTestFile->m_editor->setCursorPosition(initialTestFile->m_cursorPosition);
|
||||
// qDebug() << "Initial line:" << initialTestFile->editor->currentLine();
|
||||
// qDebug() << "Initial column:" << initialTestFile->editor->currentColumn() - 1;
|
||||
|
||||
@@ -401,7 +341,7 @@ void TestCase::run()
|
||||
// Trigger the action
|
||||
switch (m_action) {
|
||||
case FollowSymbolUnderCursorAction: {
|
||||
CPPEditorWidget *widget = initialTestFile->editorWidget;
|
||||
CPPEditorWidget *widget = initialTestFile->m_editorWidget;
|
||||
FollowSymbolUnderCursor *delegate = widget->followSymbolUnderCursorDelegate();
|
||||
VirtualFunctionAssistProvider *original = delegate->virtualFunctionAssistProvider();
|
||||
|
||||
@@ -409,7 +349,7 @@ void TestCase::run()
|
||||
QScopedPointer<VirtualFunctionTestAssistProvider> testProvider(
|
||||
new VirtualFunctionTestAssistProvider(widget));
|
||||
delegate->setVirtualFunctionAssistProvider(testProvider.data());
|
||||
initialTestFile->editorWidget->openLinkUnderCursor();
|
||||
initialTestFile->m_editorWidget->openLinkUnderCursor();
|
||||
immediateVirtualSymbolResults = testProvider->m_immediateItems;
|
||||
finalVirtualSymbolResults = testProvider->m_finalItems;
|
||||
|
||||
@@ -434,7 +374,7 @@ void TestCase::run()
|
||||
|
||||
QCOMPARE(currentTextEditor->document()->filePath(), targetTestFile->filePath());
|
||||
int expectedLine, expectedColumn;
|
||||
currentTextEditor->convertPosition(targetTestFile->targetCursorPosition,
|
||||
currentTextEditor->convertPosition(targetTestFile->m_targetCursorPosition,
|
||||
&expectedLine, &expectedColumn);
|
||||
// qDebug() << "Expected line:" << expectedLine;
|
||||
// qDebug() << "Expected column:" << expectedColumn;
|
||||
@@ -550,8 +490,8 @@ void CppEditorPlugin::test_SwitchMethodDeclarationDefinition()
|
||||
QFETCH(QByteArray, source);
|
||||
|
||||
QList<TestDocumentPtr> testFiles;
|
||||
testFiles << TestDocument::create(header, QLatin1String("file.h"));
|
||||
testFiles << TestDocument::create(source, QLatin1String("file.cpp"));
|
||||
testFiles << TestDocument::create(header, "file.h");
|
||||
testFiles << TestDocument::create(source, "file.cpp");
|
||||
|
||||
TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinitionAction, testFiles);
|
||||
test.run();
|
||||
@@ -924,18 +864,18 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_multipleDocuments_data()
|
||||
|
||||
QTest::newRow("skipForwardDeclarationBasic") << (QList<TestDocumentPtr>()
|
||||
<< TestDocument::create("class $Foo {};\n",
|
||||
QLatin1String("defined.h"))
|
||||
"defined.h")
|
||||
<< TestDocument::create("class Foo;\n"
|
||||
"@Foo foo;\n",
|
||||
QLatin1String("forwardDeclaredAndUsed.h"))
|
||||
"forwardDeclaredAndUsed.h")
|
||||
);
|
||||
|
||||
QTest::newRow("skipForwardDeclarationTemplates") << (QList<TestDocumentPtr>()
|
||||
<< TestDocument::create("template <class E> class $Container {};\n",
|
||||
QLatin1String("defined.h"))
|
||||
"defined.h")
|
||||
<< TestDocument::create("template <class E> class Container;\n"
|
||||
"@Container<int> container;\n",
|
||||
QLatin1String("forwardDeclaredAndUsed.h"))
|
||||
"forwardDeclaredAndUsed.h")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1371,13 +1311,13 @@ void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_multipleD
|
||||
{
|
||||
QList<TestDocumentPtr> testFiles = QList<TestDocumentPtr>()
|
||||
<< TestDocument::create("struct A { virtual void virt(int) = 0; };\n",
|
||||
QLatin1String("a.h"))
|
||||
"a.h")
|
||||
<< TestDocument::create("#include \"a.h\"\n"
|
||||
"struct B : A { void virt(int) {} };\n",
|
||||
QLatin1String("b.h"))
|
||||
"b.h")
|
||||
<< TestDocument::create("#include \"a.h\"\n"
|
||||
"void f(A *o) { o->$@virt(42); }\n",
|
||||
QLatin1String("u.cpp"))
|
||||
"u.cpp")
|
||||
;
|
||||
|
||||
const OverrideItemList finalResults = OverrideItemList()
|
||||
|
||||
@@ -27,8 +27,9 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "insertionpointlocator.h"
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
#include "insertionpointlocator.h"
|
||||
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
@@ -361,9 +362,7 @@ void CppToolsPlugin::test_codegen_definition_empty_class()
|
||||
"\n";
|
||||
|
||||
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
|
||||
Utils::FileSaver srcSaver(src->fileName());
|
||||
srcSaver.write(srcText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
|
||||
src->setUtf8Source(srcText);
|
||||
src->parse();
|
||||
src->check();
|
||||
@@ -371,9 +370,7 @@ void CppToolsPlugin::test_codegen_definition_empty_class()
|
||||
QCOMPARE(src->globalSymbolCount(), 1U);
|
||||
|
||||
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
Utils::FileSaver dstSaver(dst->fileName());
|
||||
dstSaver.write(dstText);
|
||||
dstSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
|
||||
dst->setUtf8Source(dstText);
|
||||
dst->parse();
|
||||
dst->check();
|
||||
@@ -429,9 +426,7 @@ void CppToolsPlugin::test_codegen_definition_first_member()
|
||||
"int y;\n").arg(QDir::tempPath()).toLatin1();
|
||||
|
||||
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
|
||||
Utils::FileSaver srcSaver(src->fileName());
|
||||
srcSaver.write(srcText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
|
||||
src->setUtf8Source(srcText);
|
||||
src->parse();
|
||||
src->check();
|
||||
@@ -441,9 +436,7 @@ void CppToolsPlugin::test_codegen_definition_first_member()
|
||||
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
|
||||
Client::IncludeLocal));
|
||||
Utils::FileSaver dstSaver(dst->fileName());
|
||||
dstSaver.write(dstText);
|
||||
dstSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
|
||||
dst->setUtf8Source(dstText);
|
||||
dst->parse();
|
||||
dst->check();
|
||||
@@ -499,9 +492,7 @@ void CppToolsPlugin::test_codegen_definition_last_member()
|
||||
"int y;\n").arg(QDir::tempPath()).toLatin1();
|
||||
|
||||
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
|
||||
Utils::FileSaver srcSaver(src->fileName());
|
||||
srcSaver.write(srcText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
|
||||
src->setUtf8Source(srcText);
|
||||
src->parse();
|
||||
src->check();
|
||||
@@ -511,9 +502,7 @@ void CppToolsPlugin::test_codegen_definition_last_member()
|
||||
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
|
||||
Client::IncludeLocal));
|
||||
Utils::FileSaver dstSaver(dst->fileName());
|
||||
dstSaver.write(dstText);
|
||||
dstSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
|
||||
dst->setUtf8Source(dstText);
|
||||
dst->parse();
|
||||
dst->check();
|
||||
@@ -575,9 +564,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member()
|
||||
"int y;\n").arg(QDir::tempPath()).toLatin1();
|
||||
|
||||
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
|
||||
Utils::FileSaver srcSaver(src->fileName());
|
||||
srcSaver.write(srcText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
|
||||
src->setUtf8Source(srcText);
|
||||
src->parse();
|
||||
src->check();
|
||||
@@ -587,9 +574,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member()
|
||||
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
|
||||
Client::IncludeLocal));
|
||||
Utils::FileSaver dstSaver(dst->fileName());
|
||||
dstSaver.write(dstText);
|
||||
dstSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
|
||||
dst->setUtf8Source(dstText);
|
||||
dst->parse();
|
||||
dst->check();
|
||||
@@ -647,9 +632,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefin
|
||||
"int y;\n").arg(QDir::tempPath()).toLatin1();
|
||||
|
||||
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
|
||||
Utils::FileSaver srcSaver(src->fileName());
|
||||
srcSaver.write(srcText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
|
||||
src->setUtf8Source(srcText);
|
||||
src->parse();
|
||||
src->check();
|
||||
@@ -659,9 +642,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefin
|
||||
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
|
||||
Client::IncludeLocal));
|
||||
Utils::FileSaver dstSaver(dst->fileName());
|
||||
dstSaver.write(dstText);
|
||||
dstSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
|
||||
dst->setUtf8Source(dstText);
|
||||
dst->parse();
|
||||
dst->check();
|
||||
@@ -722,9 +703,7 @@ void CppToolsPlugin::test_codegen_definition_member_specific_file()
|
||||
"int y;\n").arg(QDir::tempPath()).toLatin1();
|
||||
|
||||
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
|
||||
Utils::FileSaver srcSaver(src->fileName());
|
||||
srcSaver.write(srcText);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
|
||||
src->setUtf8Source(srcText);
|
||||
src->parse();
|
||||
src->check();
|
||||
@@ -734,9 +713,7 @@ void CppToolsPlugin::test_codegen_definition_member_specific_file()
|
||||
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
|
||||
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
|
||||
Client::IncludeLocal));
|
||||
Utils::FileSaver dstSaver(dst->fileName());
|
||||
dstSaver.write(dstText);
|
||||
dstSaver.finalize();
|
||||
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
|
||||
dst->setUtf8Source(dstText);
|
||||
dst->parse();
|
||||
dst->check();
|
||||
|
||||
@@ -27,21 +27,22 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cppcompletionassist.h"
|
||||
#include "cppmodelmanager.h"
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
|
||||
#include <texteditor/plaintexteditor.h>
|
||||
#include <texteditor/codeassist/iassistproposal.h>
|
||||
#include <texteditor/convenience.h>
|
||||
#include <texteditor/plaintexteditor.h>
|
||||
|
||||
#include <utils/changeset.h>
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
#include <QtTest>
|
||||
#include <QDebug>
|
||||
#include <QTextDocument>
|
||||
#include <QDir>
|
||||
#include <QTextDocument>
|
||||
#include <QtTest>
|
||||
|
||||
/*!
|
||||
Tests for code completion.
|
||||
@@ -52,15 +53,13 @@ using namespace CppTools::Internal;
|
||||
using namespace TextEditor;
|
||||
using namespace Core;
|
||||
|
||||
namespace {
|
||||
typedef QByteArray _;
|
||||
namespace { typedef QByteArray _; }
|
||||
|
||||
class CompletionTestCase
|
||||
class CompletionTestCase : public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
CompletionTestCase(const QByteArray &sourceText, const QByteArray &textToInsert = QByteArray())
|
||||
: position(-1), editorWidget(0), textDocument(0), editor(0),
|
||||
cmm(CppModelManager::instance())
|
||||
: position(-1), editorWidget(0), textDocument(0), editor(0)
|
||||
{
|
||||
source = sourceText;
|
||||
position = source.indexOf('@');
|
||||
@@ -69,22 +68,20 @@ public:
|
||||
|
||||
// Write source to file
|
||||
const QString fileName = QDir::tempPath() + QLatin1String("/file.h");
|
||||
Utils::FileSaver srcSaver(fileName);
|
||||
srcSaver.write(source);
|
||||
srcSaver.finalize();
|
||||
QVERIFY(writeFile(fileName, source));
|
||||
|
||||
// Open in editor
|
||||
editor = EditorManager::openEditor(fileName);
|
||||
QVERIFY(editor);
|
||||
closeEditorAtEndOfTestCase(editor);
|
||||
editorWidget = qobject_cast<TextEditor::BaseTextEditorWidget *>(editor->widget());
|
||||
QVERIFY(editorWidget);
|
||||
|
||||
textDocument = editorWidget->document();
|
||||
|
||||
// Get Document
|
||||
while (!cmm->snapshot().contains(fileName))
|
||||
QCoreApplication::processEvents();
|
||||
Document::Ptr document = cmm->snapshot().document(fileName);
|
||||
waitForFileInGlobalSnapshot(fileName);
|
||||
const Document::Ptr document = globalSnapshot().document(fileName);
|
||||
|
||||
snapshot.insert(document);
|
||||
|
||||
@@ -92,13 +89,6 @@ public:
|
||||
insertText(textToInsert);
|
||||
}
|
||||
|
||||
~CompletionTestCase()
|
||||
{
|
||||
EditorManager::closeEditor(editor, /*askAboutModifiedEditors=*/ false);
|
||||
cmm->GC();
|
||||
QVERIFY(cmm->snapshot().isEmpty());
|
||||
}
|
||||
|
||||
QStringList getCompletions(bool *replaceAccessOperator = 0) const
|
||||
{
|
||||
QStringList completions;
|
||||
@@ -151,12 +141,8 @@ private:
|
||||
BaseTextEditorWidget *editorWidget;
|
||||
QTextDocument *textDocument;
|
||||
IEditor *editor;
|
||||
|
||||
CppModelManager *cmm;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void CppToolsPlugin::test_completion_basic_1()
|
||||
{
|
||||
const QByteArray source =
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "cppfunctionsfilter.h"
|
||||
#include "cpplocatorfilter.h"
|
||||
#include "cppmodelmanager.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/testdatadir.h>
|
||||
@@ -58,69 +59,53 @@ Q_DECLARE_METATYPE(ILocatorFilter *)
|
||||
|
||||
namespace {
|
||||
|
||||
class MyTestDataDir : public Core::Internal::Tests::TestDataDir
|
||||
{
|
||||
public:
|
||||
MyTestDataDir(const QString &testDataDirectory)
|
||||
: TestDataDir(QLatin1String(SRCDIR "/../../../tests/cpplocators/") + testDataDirectory) {}
|
||||
};
|
||||
QTC_DECLARE_MYTESTDATADIR("../../../tests/cpplocators/")
|
||||
|
||||
class CppLocatorFilterTest : public BasicLocatorFilterTest
|
||||
inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
class CppLocatorFilterTest : public BasicLocatorFilterTest, public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
CppLocatorFilterTest(ILocatorFilter *filter, const QString &fileName)
|
||||
: BasicLocatorFilterTest(filter)
|
||||
, m_modelManager(CppModelManager::instance())
|
||||
, m_fileName(fileName)
|
||||
{
|
||||
QVERIFY(!m_fileName.isEmpty());
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
QVERIFY(garbageCollectGlobalSnapshot());
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void doBeforeLocatorRun()
|
||||
{
|
||||
m_modelManager->updateSourceFiles(QStringList() << m_fileName).waitForFinished();
|
||||
QCoreApplication::processEvents();
|
||||
QVERIFY(m_modelManager->snapshot().contains(m_fileName));
|
||||
}
|
||||
virtual void doBeforeLocatorRun() { QVERIFY(parseFiles(m_fileName)); }
|
||||
virtual void doAfterLocatorRun() { QVERIFY(garbageCollectGlobalSnapshot()); }
|
||||
|
||||
virtual void doAfterLocatorRun()
|
||||
{
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
}
|
||||
|
||||
CppModelManager *m_modelManager;
|
||||
const QString m_fileName;
|
||||
};
|
||||
|
||||
class CppCurrentDocumentFilterTest : public BasicLocatorFilterTest
|
||||
class CppCurrentDocumentFilterTest
|
||||
: public BasicLocatorFilterTest
|
||||
, public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
CppCurrentDocumentFilterTest(const QString &fileName)
|
||||
: BasicLocatorFilterTest(PluginManager::getObject<CppCurrentDocumentFilter>())
|
||||
, m_modelManager(CppModelManager::instance())
|
||||
, m_editor(0)
|
||||
, m_fileName(fileName)
|
||||
{
|
||||
QVERIFY(!m_fileName.isEmpty());
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void doBeforeLocatorRun()
|
||||
{
|
||||
QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
QVERIFY(garbageCollectGlobalSnapshot());
|
||||
|
||||
m_editor = EditorManager::openEditor(m_fileName);
|
||||
QVERIFY(m_editor);
|
||||
while (!m_modelManager->snapshot().contains(m_fileName))
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
waitForFileInGlobalSnapshot(m_fileName);
|
||||
}
|
||||
|
||||
virtual void doAfterLocatorRun()
|
||||
@@ -128,19 +113,13 @@ private:
|
||||
EditorManager::closeEditor(m_editor, /*askAboutModifiedEditors=*/ false);
|
||||
QCoreApplication::processEvents();
|
||||
QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
QVERIFY(garbageCollectGlobalSnapshot());
|
||||
}
|
||||
|
||||
CppModelManager *m_modelManager;
|
||||
IEditor *m_editor;
|
||||
const QString m_fileName;
|
||||
};
|
||||
|
||||
inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter()
|
||||
{
|
||||
QFETCH(QString, testFile);
|
||||
|
||||
@@ -27,9 +27,10 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpppreprocessor.h"
|
||||
#include "cpptoolseditorsupport.h"
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
#include "modelmanagertesthelper.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
@@ -181,19 +182,15 @@ public:
|
||||
return isFetchOk;
|
||||
}
|
||||
|
||||
void writeContents(const QByteArray &contents) const
|
||||
bool writeContents(const QByteArray &contents) const
|
||||
{
|
||||
Utils::FileSaver fileSaver(m_filePath);
|
||||
fileSaver.write(contents);
|
||||
fileSaver.finalize();
|
||||
return CppTools::Tests::TestCase::writeFile(m_filePath, contents);
|
||||
}
|
||||
|
||||
private:
|
||||
void restoreContents() const
|
||||
{
|
||||
Utils::FileSaver fileSaver(m_filePath);
|
||||
fileSaver.write(m_originalFileContents);
|
||||
fileSaver.finalize();
|
||||
CppTools::Tests::TestCase::writeFile(m_filePath, m_originalFileContents);
|
||||
}
|
||||
|
||||
QByteArray m_originalFileContents;
|
||||
@@ -554,7 +551,7 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
|
||||
QByteArray originalContents;
|
||||
QVERIFY(fileChangerAndRestorer.readContents(&originalContents));
|
||||
const QByteArray newFileContentes = originalContents + "\nint addedOtherGlobal;";
|
||||
fileChangerAndRestorer.writeContents(newFileContentes);
|
||||
QVERIFY(fileChangerAndRestorer.writeContents(newFileContentes));
|
||||
|
||||
// Add or remove source file. The configuration stays the same.
|
||||
part->files.clear();
|
||||
|
||||
@@ -268,6 +268,8 @@ public:
|
||||
virtual void setIndexingSupport(CppTools::CppIndexingSupport *indexingSupport) = 0;
|
||||
virtual CppIndexingSupport *indexingSupport() = 0;
|
||||
|
||||
virtual void setIncludePaths(const QStringList &includePaths) = 0;
|
||||
|
||||
signals:
|
||||
/// Project data might be locked while this is emitted.
|
||||
void aboutToRemoveFiles(const QStringList &files);
|
||||
|
||||
@@ -27,9 +27,10 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpppointerdeclarationformatter.h"
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpptoolsplugin.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
|
||||
#include <texteditor/plaintexteditor.h>
|
||||
|
||||
@@ -63,8 +64,9 @@ static QString stripCursor(const QString &source)
|
||||
return copy;
|
||||
}
|
||||
|
||||
struct TestEnvironment
|
||||
class TestEnvironment : public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
QByteArray source;
|
||||
Snapshot snapshot;
|
||||
CppRefactoringFilePtr cppRefactoringFile;
|
||||
@@ -88,9 +90,7 @@ struct TestEnvironment
|
||||
// Write source to temprorary file
|
||||
const QString filePath = QDir::tempPath() + QLatin1String("/file.h");
|
||||
document = Document::create(filePath);
|
||||
Utils::FileSaver documentSaver(document->fileName());
|
||||
documentSaver.write(sourceWithoutCursorMarker.toLatin1());
|
||||
documentSaver.finalize();
|
||||
QVERIFY(writeFile(document->fileName(), sourceWithoutCursorMarker.toLatin1()));
|
||||
|
||||
// Preprocess source
|
||||
Preprocessor preprocess(0, &env);
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "cppmodelmanager.h"
|
||||
#include "cpppreprocessertesthelper.h"
|
||||
#include "cpppreprocessor.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <utils/fileutils.h>
|
||||
@@ -62,9 +63,7 @@ public:
|
||||
if (QFileInfo(fileName).exists())
|
||||
return Document::Ptr(); // Test file was not removed.
|
||||
|
||||
Utils::FileSaver srcSaver(fileName);
|
||||
srcSaver.write(source);
|
||||
srcSaver.finalize();
|
||||
CppTools::Tests::TestCase::writeFile(fileName, source);
|
||||
|
||||
CppPreprocessor pp((QPointer<CppModelManager>(m_cmm)));
|
||||
pp.setIncludePaths(QStringList(TestIncludePaths::directoryOfTestFile()));
|
||||
|
||||
@@ -115,6 +115,7 @@ FORMS += \
|
||||
equals(TEST, 1) {
|
||||
HEADERS += \
|
||||
cpppreprocessertesthelper.h \
|
||||
cpptoolstestcase.h \
|
||||
modelmanagertesthelper.h
|
||||
|
||||
SOURCES += \
|
||||
@@ -126,6 +127,7 @@ equals(TEST, 1) {
|
||||
cpppointerdeclarationformatter_test.cpp \
|
||||
cpppreprocessertesthelper.cpp \
|
||||
cpppreprocessor_test.cpp \
|
||||
cpptoolstestcase.cpp \
|
||||
modelmanagertesthelper.cpp \
|
||||
symbolsearcher_test.cpp \
|
||||
typehierarchybuilder_test.cpp
|
||||
|
||||
@@ -87,6 +87,7 @@ QtcPlugin {
|
||||
"cpppointerdeclarationformatter_test.cpp",
|
||||
"cpppreprocessertesthelper.cpp", "cpppreprocessertesthelper.h",
|
||||
"cpppreprocessor_test.cpp",
|
||||
"cpptoolstestcase.cpp", "cpptoolstestcase.h",
|
||||
"modelmanagertesthelper.cpp", "modelmanagertesthelper.h",
|
||||
"symbolsearcher_test.cpp",
|
||||
"typehierarchybuilder_test.cpp",
|
||||
|
||||
159
src/plugins/cpptools/cpptoolstestcase.cpp
Normal file
159
src/plugins/cpptools/cpptoolstestcase.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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 "cpptoolstestcase.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
static bool snapshotContains(const CPlusPlus::Snapshot &snapshot, const QStringList &filePaths)
|
||||
{
|
||||
foreach (const QString &filePath, filePaths) {
|
||||
if (!snapshot.contains(filePath)) {
|
||||
const QString warning = QLatin1String("Missing file in snapshot: ") + filePath;
|
||||
QWARN(qPrintable(warning));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace CppTools {
|
||||
namespace Tests {
|
||||
|
||||
TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker)
|
||||
: m_fileName(fileName), m_source(source), m_cursorMarker(cursorMarker)
|
||||
{}
|
||||
|
||||
QString TestDocument::filePath() const
|
||||
{
|
||||
const QString fileNameAsString = QString::fromUtf8(m_fileName);
|
||||
if (!QFileInfo(fileNameAsString).isAbsolute())
|
||||
return QDir::tempPath() + QLatin1Char('/') + fileNameAsString;
|
||||
return fileNameAsString;
|
||||
}
|
||||
|
||||
bool TestDocument::writeToDisk() const
|
||||
{
|
||||
return TestCase::writeFile(filePath(), m_source);
|
||||
}
|
||||
|
||||
TestCase::TestCase(bool runGarbageCollector)
|
||||
: m_modelManager(CppModelManagerInterface::instance())
|
||||
, m_runGarbageCollector(runGarbageCollector)
|
||||
{
|
||||
if (m_runGarbageCollector)
|
||||
QVERIFY(garbageCollectGlobalSnapshot());
|
||||
}
|
||||
|
||||
TestCase::~TestCase()
|
||||
{
|
||||
QVERIFY(Core::EditorManager::closeEditors(m_editorsToClose, false));
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
if (m_runGarbageCollector)
|
||||
QVERIFY(garbageCollectGlobalSnapshot());
|
||||
}
|
||||
|
||||
CPlusPlus::Snapshot TestCase::globalSnapshot()
|
||||
{
|
||||
return CppModelManagerInterface::instance()->snapshot();
|
||||
}
|
||||
|
||||
bool TestCase::garbageCollectGlobalSnapshot()
|
||||
{
|
||||
CppModelManagerInterface::instance()->GC();
|
||||
return globalSnapshot().isEmpty();
|
||||
}
|
||||
|
||||
bool TestCase::parseFiles(const QStringList &filePaths)
|
||||
{
|
||||
CppModelManagerInterface::instance()->updateSourceFiles(filePaths).waitForFinished();
|
||||
QCoreApplication::processEvents();
|
||||
const CPlusPlus::Snapshot snapshot = globalSnapshot();
|
||||
if (snapshot.isEmpty()) {
|
||||
QWARN("After parsing: snapshot is empty.");
|
||||
return false;
|
||||
}
|
||||
if (!snapshotContains(snapshot, filePaths)) {
|
||||
QWARN("After parsing: snapshot does not contain all expected files.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestCase::parseFiles(const QString &filePath)
|
||||
{
|
||||
return parseFiles(QStringList(filePath));
|
||||
}
|
||||
|
||||
void TestCase::closeEditorAtEndOfTestCase(Core::IEditor *editor)
|
||||
{
|
||||
if (editor && !m_editorsToClose.contains(editor))
|
||||
m_editorsToClose.append(editor);
|
||||
}
|
||||
|
||||
CPlusPlus::Document::Ptr TestCase::waitForFileInGlobalSnapshot(const QString &filePath)
|
||||
{
|
||||
return waitForFilesInGlobalSnapshot(QStringList(filePath)).first();
|
||||
}
|
||||
|
||||
QList<CPlusPlus::Document::Ptr> TestCase::waitForFilesInGlobalSnapshot(
|
||||
const QStringList &filePaths)
|
||||
{
|
||||
QList<CPlusPlus::Document::Ptr> result;
|
||||
foreach (const QString &filePath, filePaths) {
|
||||
forever {
|
||||
if (CPlusPlus::Document::Ptr document = globalSnapshot().document(filePath)) {
|
||||
result.append(document);
|
||||
break;
|
||||
}
|
||||
QCoreApplication::processEvents();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool TestCase::writeFile(const QString &filePath, const QByteArray &contents)
|
||||
{
|
||||
Utils::FileSaver saver(filePath);
|
||||
if (!saver.write(contents) || !saver.finalize()) {
|
||||
const QString warning = QLatin1String("Failed to write file to disk: ") + filePath;
|
||||
QWARN(qPrintable(warning));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Tests
|
||||
} // namespace CppTools
|
||||
98
src/plugins/cpptools/cpptoolstestcase.h
Normal file
98
src/plugins/cpptools/cpptoolstestcase.h
Normal file
@@ -0,0 +1,98 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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 CPPTOOLSTESTCASE_H
|
||||
#define CPPTOOLSTESTCASE_H
|
||||
|
||||
#include "cppmodelmanagerinterface.h"
|
||||
#include "cpptools_global.h"
|
||||
|
||||
#include <coreplugin/editormanager/ieditor.h>
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
namespace CPlusPlus {
|
||||
class Document;
|
||||
class Snapshot;
|
||||
}
|
||||
namespace Core {
|
||||
class IEditor;
|
||||
}
|
||||
|
||||
namespace CppTools {
|
||||
namespace Tests {
|
||||
|
||||
class CPPTOOLS_EXPORT TestDocument
|
||||
{
|
||||
public:
|
||||
TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker = '@');
|
||||
|
||||
QString filePath() const;
|
||||
bool writeToDisk() const;
|
||||
|
||||
public:
|
||||
QByteArray m_fileName;
|
||||
QByteArray m_source;
|
||||
char m_cursorMarker;
|
||||
};
|
||||
|
||||
class CPPTOOLS_EXPORT TestCase
|
||||
{
|
||||
Q_DISABLE_COPY(TestCase)
|
||||
|
||||
public:
|
||||
TestCase(bool runGarbageCollector = true);
|
||||
~TestCase();
|
||||
|
||||
void closeEditorAtEndOfTestCase(Core::IEditor *editor);
|
||||
|
||||
static bool parseFiles(const QString &filePath);
|
||||
static bool parseFiles(const QStringList &filePaths);
|
||||
|
||||
static CPlusPlus::Snapshot globalSnapshot();
|
||||
static bool garbageCollectGlobalSnapshot();
|
||||
|
||||
static CPlusPlus::Document::Ptr waitForFileInGlobalSnapshot(const QString &filePath);
|
||||
static QList<CPlusPlus::Document::Ptr> waitForFilesInGlobalSnapshot(
|
||||
const QStringList &filePaths);
|
||||
|
||||
static bool writeFile(const QString &filePath, const QByteArray &contents);
|
||||
|
||||
protected:
|
||||
CppModelManagerInterface *m_modelManager;
|
||||
|
||||
private:
|
||||
QList<Core::IEditor *> m_editorsToClose;
|
||||
bool m_runGarbageCollector;
|
||||
};
|
||||
|
||||
} // namespace Tests
|
||||
} // namespace CppTools
|
||||
|
||||
#endif // CPPTOOLSTESTCASE_H
|
||||
@@ -31,6 +31,7 @@
|
||||
|
||||
#include "builtinindexingsupport.h"
|
||||
#include "cppmodelmanager.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
#include "searchsymbols.h"
|
||||
|
||||
#include <coreplugin/testdatadir.h>
|
||||
@@ -43,14 +44,7 @@ using namespace CppTools::Internal;
|
||||
|
||||
namespace {
|
||||
|
||||
class MyTestDataDir : public Core::Internal::Tests::TestDataDir
|
||||
{
|
||||
public:
|
||||
MyTestDataDir(const QString &testDataDirectory)
|
||||
: TestDataDir(QLatin1String(SRCDIR "/../../../tests/cppsymbolsearcher/")
|
||||
+ testDataDirectory)
|
||||
{}
|
||||
};
|
||||
QTC_DECLARE_MYTESTDATADIR("../../../tests/cppsymbolsearcher/")
|
||||
|
||||
class ResultData
|
||||
{
|
||||
@@ -90,21 +84,16 @@ public:
|
||||
|
||||
typedef ResultData::ResultDataList ResultDataList;
|
||||
|
||||
class SymbolSearcherTest
|
||||
class SymbolSearcherTest : public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
/// Takes no ownership of indexingSupportToUse
|
||||
SymbolSearcherTest(const QString &testFile, CppIndexingSupport *indexingSupportToUse)
|
||||
: m_modelManager(CppModelManager::instance())
|
||||
, m_indexingSupportToUse(indexingSupportToUse)
|
||||
: m_indexingSupportToUse(indexingSupportToUse)
|
||||
, m_testFile(testFile)
|
||||
{
|
||||
QVERIFY(m_indexingSupportToUse);
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
m_modelManager->updateSourceFiles(QStringList(m_testFile)).waitForFinished();
|
||||
QCoreApplication::processEvents();
|
||||
QVERIFY(m_modelManager->snapshot().contains(m_testFile));
|
||||
|
||||
QVERIFY(parseFiles(m_testFile));
|
||||
m_indexingSupportToRestore = m_modelManager->indexingSupport();
|
||||
m_modelManager->setIndexingSupport(m_indexingSupportToUse);
|
||||
}
|
||||
@@ -124,12 +113,9 @@ public:
|
||||
~SymbolSearcherTest()
|
||||
{
|
||||
m_modelManager->setIndexingSupport(m_indexingSupportToRestore);
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
}
|
||||
|
||||
private:
|
||||
CppModelManager *m_modelManager;
|
||||
CppIndexingSupport *m_indexingSupportToRestore;
|
||||
CppIndexingSupport *m_indexingSupportToUse;
|
||||
const QString m_testFile;
|
||||
|
||||
@@ -30,10 +30,11 @@
|
||||
#include "cpptoolsplugin.h"
|
||||
|
||||
#include "cppmodelmanagerinterface.h"
|
||||
#include "cpptoolstestcase.h"
|
||||
#include "typehierarchybuilder.h"
|
||||
|
||||
#include <cplusplus/SymbolVisitor.h>
|
||||
#include <cplusplus/Overview.h>
|
||||
#include <cplusplus/SymbolVisitor.h>
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
#include <QDir>
|
||||
@@ -92,46 +93,25 @@ private:
|
||||
Class *m_clazz;
|
||||
};
|
||||
|
||||
struct TestDocument
|
||||
class TestCase : public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
TestDocument(const QString &fileName, const QString &contents)
|
||||
: fileName(fileName), contents(contents) {}
|
||||
|
||||
QString fileName;
|
||||
QString contents;
|
||||
};
|
||||
|
||||
class TestCase
|
||||
{
|
||||
public:
|
||||
TestCase(const QList<TestDocument> &documents, const QString &expectedHierarchy)
|
||||
: m_modelManager(CppModelManagerInterface::instance())
|
||||
, m_documents(documents)
|
||||
, m_expectedHierarchy(expectedHierarchy)
|
||||
{
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
}
|
||||
TestCase(const QList<Tests::TestDocument> &documents, const QString &expectedHierarchy)
|
||||
: m_documents(documents), m_expectedHierarchy(expectedHierarchy)
|
||||
{}
|
||||
|
||||
void run()
|
||||
{
|
||||
// Write files
|
||||
QStringList filePaths;
|
||||
foreach (const TestDocument &document, m_documents) {
|
||||
const QString filePath = QDir::tempPath() + QLatin1Char('/') + document.fileName;
|
||||
Utils::FileSaver documentSaver(filePath);
|
||||
documentSaver.write(document.contents.toUtf8());
|
||||
documentSaver.finalize();
|
||||
filePaths << filePath;
|
||||
foreach (const Tests::TestDocument &document, m_documents) {
|
||||
QVERIFY(document.writeToDisk());
|
||||
filePaths << document.filePath();
|
||||
}
|
||||
|
||||
// Parse files
|
||||
m_modelManager->updateSourceFiles(filePaths).waitForFinished();
|
||||
QCoreApplication::processEvents();
|
||||
const Snapshot snapshot = m_modelManager->snapshot();
|
||||
QVERIFY(!snapshot.isEmpty());
|
||||
foreach (const QString &filePath, filePaths)
|
||||
QVERIFY(snapshot.contains(filePath));
|
||||
QVERIFY(parseFiles(filePaths));
|
||||
const Snapshot snapshot = globalSnapshot();
|
||||
|
||||
// Get class for which to generate the hierarchy
|
||||
const Document::Ptr firstDocument = snapshot.document(filePaths.first());
|
||||
@@ -148,37 +128,30 @@ public:
|
||||
QCOMPARE(actualHierarchy, m_expectedHierarchy);
|
||||
}
|
||||
|
||||
~TestCase()
|
||||
{
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
}
|
||||
|
||||
private:
|
||||
CppModelManagerInterface *m_modelManager;
|
||||
QList<TestDocument> m_documents;
|
||||
QList<Tests::TestDocument> m_documents;
|
||||
QString m_expectedHierarchy;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
Q_DECLARE_METATYPE(QList<TestDocument>)
|
||||
Q_DECLARE_METATYPE(QList<Tests::TestDocument>)
|
||||
|
||||
void CppToolsPlugin::test_typehierarchy_data()
|
||||
{
|
||||
QTest::addColumn<QList<TestDocument> >("documents");
|
||||
QTest::addColumn<QList<Tests::TestDocument> >("documents");
|
||||
QTest::addColumn<QString>("expectedHierarchy");
|
||||
|
||||
typedef QLatin1String _;
|
||||
typedef Tests::TestDocument TestDocument;
|
||||
|
||||
QTest::newRow("basic-single-document")
|
||||
<< (QList<TestDocument>()
|
||||
<< TestDocument(_("a.h"),
|
||||
_("class A {};\n"
|
||||
"class B : public A {};\n"
|
||||
"class C1 : public B {};\n"
|
||||
"class C2 : public B {};\n"
|
||||
"class D : public C1 {};\n")))
|
||||
<< TestDocument("a.h",
|
||||
"class A {};\n"
|
||||
"class B : public A {};\n"
|
||||
"class C1 : public B {};\n"
|
||||
"class C2 : public B {};\n"
|
||||
"class D : public C1 {};\n"))
|
||||
<< QString::fromLatin1(
|
||||
"A\n"
|
||||
" B\n"
|
||||
@@ -188,20 +161,20 @@ void CppToolsPlugin::test_typehierarchy_data()
|
||||
|
||||
QTest::newRow("basic-multiple-documents")
|
||||
<< (QList<TestDocument>()
|
||||
<< TestDocument(_("a.h"),
|
||||
_("class A {};"))
|
||||
<< TestDocument(_("b.h"),
|
||||
_("#include \"a.h\"\n"
|
||||
"class B : public A {};"))
|
||||
<< TestDocument(_("c1.h"),
|
||||
_("#include \"b.h\"\n"
|
||||
"class C1 : public B {};"))
|
||||
<< TestDocument(_("c2.h"),
|
||||
_("#include \"b.h\"\n"
|
||||
"class C2 : public B {};"))
|
||||
<< TestDocument(_("d.h"),
|
||||
_("#include \"c1.h\"\n"
|
||||
"class D : public C1 {};")))
|
||||
<< TestDocument("a.h",
|
||||
"class A {};")
|
||||
<< TestDocument("b.h",
|
||||
"#include \"a.h\"\n"
|
||||
"class B : public A {};")
|
||||
<< TestDocument("c1.h",
|
||||
"#include \"b.h\"\n"
|
||||
"class C1 : public B {};")
|
||||
<< TestDocument("c2.h",
|
||||
"#include \"b.h\"\n"
|
||||
"class C2 : public B {};")
|
||||
<< TestDocument("d.h",
|
||||
"#include \"c1.h\"\n"
|
||||
"class D : public C1 {};"))
|
||||
<< QString::fromLatin1(
|
||||
"A\n"
|
||||
" B\n"
|
||||
@@ -213,7 +186,7 @@ void CppToolsPlugin::test_typehierarchy_data()
|
||||
|
||||
void CppToolsPlugin::test_typehierarchy()
|
||||
{
|
||||
QFETCH(QList<TestDocument>, documents);
|
||||
QFETCH(QList<Tests::TestDocument>, documents);
|
||||
QFETCH(QString, expectedHierarchy);
|
||||
|
||||
TestCase testCase(documents, expectedHierarchy);
|
||||
|
||||
@@ -34,10 +34,11 @@
|
||||
#else
|
||||
#include "formeditorw.h"
|
||||
|
||||
#include <coreplugin/testdatadir.h>
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/testdatadir.h>
|
||||
#include <cpptools/cppmodelmanager.h>
|
||||
#include <cpptools/cpptoolseditorsupport.h>
|
||||
#include <cpptools/cpptoolstestcase.h>
|
||||
|
||||
#include <cplusplus/CppDocument.h>
|
||||
#include <cplusplus/Overview.h>
|
||||
@@ -57,15 +58,7 @@ using namespace Designer::Internal;
|
||||
|
||||
namespace {
|
||||
|
||||
class MyTestDataDir : public Core::Internal::Tests::TestDataDir {
|
||||
public:
|
||||
MyTestDataDir()
|
||||
: TestDataDir(QString())
|
||||
{}
|
||||
MyTestDataDir(const QString &dir)
|
||||
: TestDataDir(QLatin1String(SRCDIR "/../../../tests/designer/") + dir)
|
||||
{}
|
||||
};
|
||||
QTC_DECLARE_MYTESTDATADIR("../../../tests/designer/")
|
||||
|
||||
class DocumentContainsFunctionDefinition: protected SymbolVisitor
|
||||
{
|
||||
@@ -157,25 +150,23 @@ bool documentContainsMemberFunctionDeclaration(const Document::Ptr &document,
|
||||
return DocumentContainsDeclaration()(document->globalNamespace(), declaration);
|
||||
}
|
||||
|
||||
class GoToSlotTest
|
||||
class GoToSlotTest : public CppTools::Tests::TestCase
|
||||
{
|
||||
public:
|
||||
GoToSlotTest(const QStringList &files)
|
||||
: m_files(files)
|
||||
, m_modelManager(CppModelManagerInterface::instance())
|
||||
{
|
||||
QCOMPARE(files.size(), 3);
|
||||
cleanup();
|
||||
}
|
||||
~GoToSlotTest() { cleanup(); }
|
||||
|
||||
void run() const
|
||||
void run()
|
||||
{
|
||||
QList<TextEditor::BaseTextEditor *> editors;
|
||||
foreach (const QString &file, m_files) {
|
||||
IEditor *editor = EditorManager::openEditor(file);
|
||||
TextEditor::BaseTextEditor *e = qobject_cast<TextEditor::BaseTextEditor *>(editor);
|
||||
QVERIFY(e);
|
||||
closeEditorAtEndOfTestCase(editor);
|
||||
editors << e;
|
||||
}
|
||||
TextEditor::BaseTextEditor *cppFileEditor = editors.at(0);
|
||||
@@ -185,10 +176,7 @@ public:
|
||||
const QString hFile = m_files.at(1);
|
||||
|
||||
QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), m_files.size());
|
||||
while (!m_modelManager->snapshot().contains(cppFile)
|
||||
|| !m_modelManager->snapshot().contains(hFile)) {
|
||||
QApplication::processEvents();
|
||||
}
|
||||
waitForFilesInGlobalSnapshot(QStringList() << cppFile << hFile);
|
||||
|
||||
// Execute "Go To Slot"
|
||||
FormEditorW *few = FormEditorW::instance();
|
||||
@@ -219,19 +207,8 @@ public:
|
||||
QLatin1String("Form::on_pushButton_clicked")));
|
||||
}
|
||||
|
||||
private:
|
||||
void cleanup()
|
||||
{
|
||||
EditorManager::closeAllEditors(/*askAboutModifiedEditors =*/ false);
|
||||
QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
|
||||
|
||||
m_modelManager->GC();
|
||||
QVERIFY(m_modelManager->snapshot().isEmpty());
|
||||
}
|
||||
|
||||
private:
|
||||
QStringList m_files;
|
||||
CppModelManagerInterface *m_modelManager;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
@@ -44,12 +44,7 @@ using namespace Locator::Internal::Tests;
|
||||
|
||||
namespace {
|
||||
|
||||
class MyTestDataDir : public Core::Internal::Tests::TestDataDir
|
||||
{
|
||||
public:
|
||||
MyTestDataDir(const QString &testDataDirectory)
|
||||
: TestDataDir(QLatin1String(SRCDIR "/../../../tests/locators/") + testDataDirectory) {}
|
||||
};
|
||||
QTC_DECLARE_MYTESTDATADIR("../../../tests/locators/")
|
||||
|
||||
class MyBaseFileFilter : public Locator::BaseFileFilter
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user