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:
Nikolai Kosjar
2013-12-16 16:02:45 +01:00
parent 5aa8a63f90
commit 5c8df5fa4e
25 changed files with 814 additions and 659 deletions

View File

@@ -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();

View File

@@ -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 =

View File

@@ -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);

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);

View File

@@ -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()));

View File

@@ -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

View File

@@ -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",

View 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

View 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

View File

@@ -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;

View File

@@ -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);