CppTools: Tests: Stop writing into source dir

Instead, always write into a unique temporary directory in
QDir::tempPath().

Where applicable, read the test source from files instead of first
writing the file.

Some clean ups in test_codegen*.

Change-Id: Id48dc50c6ca16252edfd9fc8a86ba0de9f9be486
Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@theqtcompany.com>
This commit is contained in:
Nikolai Kosjar
2014-12-09 18:42:15 +01:00
committed by Erik Verbruggen
parent cb3c7538be
commit 6cafc424e8
17 changed files with 359 additions and 376 deletions

View File

@@ -33,6 +33,7 @@
#include "insertionpointlocator.h"
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QtTest>
#include <QDebug>
@@ -46,6 +47,34 @@ using namespace CPlusPlus;
using namespace CppTools;
using namespace CppTools::Internal;
namespace {
Document::Ptr createDocument(const QString filePath, const QByteArray text,
unsigned expectedGlobalSymbolCount)
{
Document::Ptr document = Document::create(filePath);
document->setUtf8Source(text);
document->check();
QTC_ASSERT(document->diagnosticMessages().isEmpty(), return Document::Ptr());
QTC_ASSERT(document->globalSymbolCount() == expectedGlobalSymbolCount, return Document::Ptr());
return document;
}
Document::Ptr createDocumentAndFile(Tests::TemporaryDir *temporaryDir,
const QByteArray relativeFilePath,
const QByteArray text,
unsigned expectedGlobalSymbolCount)
{
QTC_ASSERT(temporaryDir, return Document::Ptr());
const QString absoluteFilePath = temporaryDir->createFile(relativeFilePath, text);
QTC_ASSERT(!absoluteFilePath.isEmpty(), return Document::Ptr());
return createDocument(absoluteFilePath, text, expectedGlobalSymbolCount);
}
} // anonymous namespace
/*!
Should insert at line 3, column 1, with "public:\n" as prefix and without suffix.
*/
@@ -56,14 +85,8 @@ void CppToolsPlugin::test_codegen_public_in_empty_class()
"{\n"
"};\n"
"\n";
Document::Ptr doc = Document::create(QLatin1String("public_in_empty_class"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Document::Ptr doc = createDocument(QLatin1String("public_in_empty_class"), src, 1U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
@@ -96,14 +119,8 @@ void CppToolsPlugin::test_codegen_public_in_nonempty_class()
"public:\n" // line 3
"};\n" // line 4
"\n";
Document::Ptr doc = Document::create(QLatin1String("public_in_nonempty_class"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Document::Ptr doc = createDocument(QLatin1String("public_in_nonempty_class"), src, 1U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
@@ -136,14 +153,8 @@ void CppToolsPlugin::test_codegen_public_before_protected()
"protected:\n" // line 3
"};\n"
"\n";
Document::Ptr doc = Document::create(QLatin1String("public_before_protected"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Document::Ptr doc = createDocument(QLatin1String("public_before_protected"), src, 1U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
@@ -177,14 +188,8 @@ void CppToolsPlugin::test_codegen_private_after_protected()
"protected:\n" // line 3
"};\n"
"\n";
Document::Ptr doc = Document::create(QLatin1String("private_after_protected"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Document::Ptr doc = createDocument(QLatin1String("private_after_protected"), src, 1U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
@@ -218,14 +223,8 @@ void CppToolsPlugin::test_codegen_protected_in_nonempty_class()
"public:\n" // line 3
"};\n" // line 4
"\n";
Document::Ptr doc = Document::create(QLatin1String("protected_in_nonempty_class"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Document::Ptr doc = createDocument(QLatin1String("protected_in_nonempty_class"), src, 1U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
@@ -259,14 +258,8 @@ void CppToolsPlugin::test_codegen_protected_between_public_and_private()
"private:\n" // line 4
"};\n" // line 5
"\n";
Document::Ptr doc = Document::create(QLatin1String("protected_betwee_public_and_private"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Document::Ptr doc = createDocument(QLatin1String("protected_betwee_public_and_private"), src, 1U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
@@ -321,13 +314,8 @@ void CppToolsPlugin::test_codegen_qtdesigner_integration()
"\n"
"#endif // MAINWINDOW_H\n";
Document::Ptr doc = Document::create(QLatin1String("qtdesigner_integration"));
doc->setUtf8Source(src);
doc->parse();
doc->check();
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 2U);
Document::Ptr doc = createDocument(QLatin1String("qtdesigner_integration"), src, 2U);
QVERIFY(doc);
Class *foo = doc->globalSymbolAt(1)->asClass();
QVERIFY(foo);
@@ -351,38 +339,29 @@ void CppToolsPlugin::test_codegen_qtdesigner_integration()
void CppToolsPlugin::test_codegen_definition_empty_class()
{
const QByteArray srcText = "\n"
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QByteArray headerText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
"};\n"
"\n";
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 1U);
QVERIFY(headerDocument);
const QByteArray dstText = "\n"
const QByteArray sourceText = "\n"
"int x;\n" // line 1
"\n";
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 1U);
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 1U);
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 1U);
QVERIFY(sourceDocument);
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
snapshot.insert(headerDocument);
snapshot.insert(sourceDocument);
Class *foo = src->globalSymbolAt(0)->asClass();
Class *foo = headerDocument->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
@@ -397,7 +376,7 @@ void CppToolsPlugin::test_codegen_definition_empty_class()
QList<InsertionLocation> locList = find.methodDefinition(decl);
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.fileName(), sourceDocument->fileName());
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
QCOMPARE(loc.suffix(), QString());
QCOMPARE(loc.line(), 3U);
@@ -406,49 +385,41 @@ void CppToolsPlugin::test_codegen_definition_empty_class()
void CppToolsPlugin::test_codegen_definition_first_member()
{
const QByteArray srcText = "\n"
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QByteArray headerText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
"void bar();\n" // line 4
"};\n"
"\n";
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 1U);
QVERIFY(headerDocument);
const QByteArray dstText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::bar()\n" // line 4
"{\n"
"\n"
"}\n"
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 1U);
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
Client::IncludeLocal));
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 3U);
const QByteArray sourceText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::bar()\n" // line 4
"{\n"
"\n"
"}\n"
"\n"
"int y;\n").arg(temporaryDir.path()).toLatin1();
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 3U);
QVERIFY(sourceDocument);
sourceDocument->addIncludeFile(Document::Include(QLatin1String("file.h"),
headerDocument->fileName(), 1,
Client::IncludeLocal));
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
snapshot.insert(headerDocument);
snapshot.insert(sourceDocument);
Class *foo = src->globalSymbolAt(0)->asClass();
Class *foo = headerDocument->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
@@ -463,7 +434,7 @@ void CppToolsPlugin::test_codegen_definition_first_member()
QList<InsertionLocation> locList = find.methodDefinition(decl);
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.fileName(), sourceDocument->fileName());
QCOMPARE(loc.line(), 4U);
QCOMPARE(loc.column(), 1U);
QCOMPARE(loc.suffix(), QLatin1String("\n\n"));
@@ -472,49 +443,42 @@ void CppToolsPlugin::test_codegen_definition_first_member()
void CppToolsPlugin::test_codegen_definition_last_member()
{
const QByteArray srcText = "\n"
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QByteArray headerText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
"void bar();\n" // line 4
"};\n"
"\n";
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 1U);
QVERIFY(headerDocument);
const QByteArray dstText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
const QByteArray sourceText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"int y;\n").arg(temporaryDir.path()).toLatin1();
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 1U);
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
Client::IncludeLocal));
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 3U);
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 3U);
QVERIFY(sourceDocument);
sourceDocument->addIncludeFile(Document::Include(QLatin1String("file.h"),
headerDocument->fileName(), 1,
Client::IncludeLocal));
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
snapshot.insert(headerDocument);
snapshot.insert(sourceDocument);
Class *foo = src->globalSymbolAt(0)->asClass();
Class *foo = headerDocument->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
@@ -529,7 +493,7 @@ void CppToolsPlugin::test_codegen_definition_last_member()
QList<InsertionLocation> locList = find.methodDefinition(decl);
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.fileName(), sourceDocument->fileName());
QCOMPARE(loc.line(), 7U);
QCOMPARE(loc.column(), 2U);
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
@@ -538,7 +502,10 @@ void CppToolsPlugin::test_codegen_definition_last_member()
void CppToolsPlugin::test_codegen_definition_middle_member()
{
const QByteArray srcText = "\n"
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QByteArray headerText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
@@ -547,46 +514,37 @@ void CppToolsPlugin::test_codegen_definition_middle_member()
"};\n"
"\n";
const QByteArray dstText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"void Foo::car()\n" // line 9
"{\n"
"\n"
"}\n"
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 1U);
QVERIFY(headerDocument);
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 1U);
const QByteArray sourceText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"void Foo::car()\n" // line 9
"{\n"
"\n"
"}\n"
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
Client::IncludeLocal));
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 4U);
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 4U);
QVERIFY(sourceDocument);
sourceDocument->addIncludeFile(Document::Include(QLatin1String("file.h"),
headerDocument->fileName(), 1,
Client::IncludeLocal));
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
snapshot.insert(headerDocument);
snapshot.insert(sourceDocument);
Class *foo = src->globalSymbolAt(0)->asClass();
Class *foo = headerDocument->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
@@ -601,7 +559,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member()
QList<InsertionLocation> locList = find.methodDefinition(decl);
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.fileName(), sourceDocument->fileName());
QCOMPARE(loc.line(), 7U);
QCOMPARE(loc.column(), 2U);
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
@@ -610,7 +568,10 @@ void CppToolsPlugin::test_codegen_definition_middle_member()
void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefined()
{
const QByteArray srcText = "\n"
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QByteArray headerText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
@@ -619,8 +580,10 @@ void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefin
"void car();\n" // line 6
"};\n"
"\n";
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 1U);
QVERIFY(headerDocument);
const QByteArray dstText = QString::fromLatin1(
const QByteArray sourceText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
@@ -630,31 +593,18 @@ void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefin
"\n"
"}\n"
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 1U);
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
Client::IncludeLocal));
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 3U);
"int y;\n").arg(temporaryDir.path()).toLatin1();
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 3U);
QVERIFY(sourceDocument);
sourceDocument->addIncludeFile(Document::Include(QLatin1String("file.h"),
headerDocument->fileName(), 1,
Client::IncludeLocal));
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
snapshot.insert(headerDocument);
snapshot.insert(sourceDocument);
Class *foo = src->globalSymbolAt(0)->asClass();
Class *foo = headerDocument->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
@@ -669,7 +619,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefin
QList<InsertionLocation> locList = find.methodDefinition(decl);
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.fileName(), sourceDocument->fileName());
QCOMPARE(loc.line(), 4U);
QCOMPARE(loc.column(), 1U);
QCOMPARE(loc.prefix(), QString());
@@ -678,7 +628,10 @@ void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefin
void CppToolsPlugin::test_codegen_definition_member_specific_file()
{
const QByteArray srcText = "\n"
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QByteArray headerText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
@@ -690,42 +643,31 @@ void CppToolsPlugin::test_codegen_definition_member_specific_file()
"{\n"
"\n"
"}\n";
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 2U);
QVERIFY(headerDocument);
const QByteArray dstText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 2U);
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
Client::IncludeLocal));
QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 3U);
const QByteArray sourceText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"int y;\n").arg(temporaryDir.path()).toLatin1();
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 3U);
QVERIFY(sourceDocument);
sourceDocument->addIncludeFile(Document::Include(QLatin1String("file.h"),
headerDocument->fileName(), 1,
Client::IncludeLocal));
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
snapshot.insert(headerDocument);
snapshot.insert(sourceDocument);
Class *foo = src->globalSymbolAt(0)->asClass();
Class *foo = headerDocument->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
@@ -737,10 +679,10 @@ void CppToolsPlugin::test_codegen_definition_member_specific_file()
CppRefactoringChanges changes(snapshot);
InsertionPointLocator find(changes);
QList<InsertionLocation> locList = find.methodDefinition(decl, true, dst->fileName());
QList<InsertionLocation> locList = find.methodDefinition(decl, true, sourceDocument->fileName());
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.fileName(), sourceDocument->fileName());
QCOMPARE(loc.line(), 7U);
QCOMPARE(loc.column(), 2U);
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));

View File

@@ -73,8 +73,10 @@ public:
m_source[m_position] = ' ';
// Write source to file
const QString fileName = QDir::tempPath() + QLatin1String("/file.h");
QVERIFY(writeFile(fileName, m_source));
m_temporaryDir.reset(new Tests::TemporaryDir());
QVERIFY(m_temporaryDir->isValid());
const QString fileName = m_temporaryDir->createFile("file.h", m_source);
QVERIFY(!fileName.isEmpty());
// Open in editor
m_editor = EditorManager::openEditor(fileName);
@@ -150,6 +152,7 @@ private:
QByteArray m_source;
int m_position;
Snapshot m_snapshot;
QScopedPointer<Tests::TemporaryDir> m_temporaryDir;
TextEditorWidget *m_editorWidget;
QTextDocument *m_textDocument;
IEditor *m_editor;

View File

@@ -30,6 +30,7 @@
#include "cpptoolsplugin.h"
#include "cpptoolsreuse.h"
#include "cpptoolstestcase.h"
#include "cppfilesettingspage.h"
#include <utils/fileutils.h>
@@ -60,15 +61,16 @@ void CppToolsPlugin::test_headersource()
QFETCH(QString, sourceFileName);
QFETCH(QString, headerFileName);
bool wasHeader;
const QString baseDir = baseTestDir();
QDir path = QDir(baseDir + _(QTest::currentDataTag()));
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QDir path = QDir(temporaryDir.path() + QLatin1Char('/') + _(QTest::currentDataTag()));
const QString sourcePath = path.absoluteFilePath(sourceFileName);
const QString headerPath = path.absoluteFilePath(headerFileName);
createTempFile(sourcePath);
createTempFile(headerPath);
bool wasHeader;
clearHeaderSourceCache();
QCOMPARE(correspondingHeaderOrSource(sourcePath, &wasHeader), headerPath);
QVERIFY(!wasHeader);

View File

@@ -46,7 +46,6 @@
#include <QDebug>
#include <QFileInfo>
#include <QTemporaryDir>
#include <QtTest>
using namespace CppTools;
@@ -79,6 +78,15 @@ public:
{ return directory(_("sources")) + fileName; }
};
QStringList toAbsolutePaths(const QStringList &relativePathList,
const Tests::TemporaryCopiedDir &temporaryDir)
{
QStringList result;
foreach (const QString &file, relativePathList)
result << temporaryDir.absolutePath(file.toUtf8());
return result;
}
// TODO: When possible, use this helper class in all tests
class ProjectCreator
{
@@ -478,8 +486,14 @@ void CppToolsPlugin::test_modelmanager_refresh_added_and_purge_removed()
void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_change()
{
QFETCH(QString, fileToChange);
QFETCH(QList<ProjectFile>, initialProjectFiles);
QFETCH(QList<ProjectFile>, finalProjectFiles);
QFETCH(QStringList, initialProjectFiles);
QFETCH(QStringList, finalProjectFiles);
Tests::TemporaryCopiedDir temporaryDir(
MyTestDataDir(QLatin1String("testdata_refresh2")).path());
fileToChange = temporaryDir.absolutePath(fileToChange.toUtf8());
initialProjectFiles = toAbsolutePaths(initialProjectFiles, temporaryDir);
finalProjectFiles = toAbsolutePaths(finalProjectFiles, temporaryDir);
ModelManagerTestHelper helper;
CppModelManager *mm = CppModelManager::instance();
@@ -490,8 +504,8 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
ProjectPart::Ptr part(new ProjectPart);
part->languageVersion = ProjectPart::CXX14;
part->qtVersion = ProjectPart::Qt5;
foreach (const ProjectFile &file, initialProjectFiles)
part->files.append(file);
foreach (const QString &file, initialProjectFiles)
part->files.append(ProjectFile(file, ProjectFile::CXXSource));
pi = ProjectInfo(project);
pi.appendProjectPart(part);
pi.finish();
@@ -504,9 +518,9 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
QCOMPARE(refreshedFiles.size(), initialProjectFiles.size());
snapshot = mm->snapshot();
foreach (const ProjectFile &file, initialProjectFiles) {
QVERIFY(refreshedFiles.contains(file.path));
QVERIFY(snapshot.contains(file.path));
foreach (const QString &file, initialProjectFiles) {
QVERIFY(refreshedFiles.contains(file));
QVERIFY(snapshot.contains(file));
}
document = snapshot.document(fileToChange);
@@ -524,8 +538,8 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
// Add or remove source file. The configuration stays the same.
part->files.clear();
foreach (const ProjectFile &file, finalProjectFiles)
part->files.append(file);
foreach (const QString &file, finalProjectFiles)
part->files.append(ProjectFile(file, ProjectFile::CXXSource));
pi = ProjectInfo(project);
pi.appendProjectPart(part);
pi.finish();
@@ -534,9 +548,9 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
QCOMPARE(refreshedFiles.size(), finalProjectFiles.size());
snapshot = mm->snapshot();
foreach (const ProjectFile &file, finalProjectFiles) {
QVERIFY(refreshedFiles.contains(file.path));
QVERIFY(snapshot.contains(file.path));
foreach (const QString &file, finalProjectFiles) {
QVERIFY(refreshedFiles.contains(file));
QVERIFY(snapshot.contains(file));
}
document = snapshot.document(fileToChange);
const QDateTime lastModifiedAfter = document->lastModified();
@@ -549,19 +563,15 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_change_data()
{
QTest::addColumn<QString>("fileToChange");
QTest::addColumn<QList<ProjectFile> >("initialProjectFiles");
QTest::addColumn<QList<ProjectFile> >("finalProjectFiles");
QTest::addColumn<QStringList>("initialProjectFiles");
QTest::addColumn<QStringList>("finalProjectFiles");
const MyTestDataDir testDataDir(_("testdata_refresh2"));
const QString testCpp(testDataDir.file(_("source.cpp")));
const QString testCpp2(testDataDir.file(_("source2.cpp")));
const QString testCpp = QLatin1String("source.cpp");
const QString testCpp2 = QLatin1String("source2.cpp");
const QString fileToChange = testCpp;
QList<ProjectFile> projectFiles1 = QList<ProjectFile>()
<< ProjectFile(testCpp, ProjectFile::CXXSource);
QList<ProjectFile> projectFiles2 = QList<ProjectFile>()
<< ProjectFile(testCpp, ProjectFile::CXXSource)
<< ProjectFile(testCpp2, ProjectFile::CXXSource);
const QStringList projectFiles1 = QStringList() << testCpp;
const QStringList projectFiles2 = QStringList() << testCpp << testCpp2;
// Add a file
QTest::newRow("case: add project file") << fileToChange << projectFiles1 << projectFiles2;
@@ -1059,7 +1069,7 @@ void CppToolsPlugin::test_modelmanager_renameIncludes()
} GCHelper;
Q_UNUSED(GCHelper); // do not warn about being unused
QTemporaryDir tmpDir;
TemporaryDir tmpDir;
QVERIFY(tmpDir.isValid());
const QDir workingDir(tmpDir.path());

View File

@@ -87,15 +87,18 @@ public:
}
// Write source to temprorary file
const QString filePath = QDir::tempPath() + QLatin1String("/file.h");
Document::Ptr document = Document::create(filePath);
QVERIFY(writeFile(document->fileName(), sourceWithoutCursorMarker.toUtf8()));
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QString filePath = temporaryDir.createFile("file.h",
sourceWithoutCursorMarker.toUtf8());
QVERIFY(!filePath.isEmpty());
// Preprocess source
Environment env;
Preprocessor preprocess(0, &env);
const QByteArray preprocessedSource = preprocess.run(filePath, sourceWithoutCursorMarker);
Document::Ptr document = Document::create(filePath);
document->setUtf8Source(preprocessedSource);
document->parse(parseMode);
document->check();

View File

@@ -63,22 +63,16 @@ public:
cleanUp();
}
Document::Ptr run(const QByteArray &source)
Document::Ptr run(const QString &filePath)
{
const QString fileName = TestIncludePaths::testFilePath();
FileWriterAndRemover scopedFile(fileName, source);
if (!scopedFile.writtenSuccessfully())
return Document::Ptr();
QScopedPointer<CppSourceProcessor> sourceProcessor(
CppModelManager::createSourceProcessor());
const ProjectPart::HeaderPath hp(TestIncludePaths::directoryOfTestFile(),
ProjectPart::HeaderPath::IncludePath);
sourceProcessor->setHeaderPaths(ProjectPart::HeaderPaths() << hp);
sourceProcessor->run(fileName);
sourceProcessor->run(filePath);
Document::Ptr document = m_cmm->document(fileName);
Document::Ptr document = m_cmm->document(filePath);
return document;
}
@@ -101,14 +95,11 @@ private:
/// Check: Resolved and unresolved includes are properly tracked.
void CppToolsPlugin::test_cppsourceprocessor_includes_resolvedUnresolved()
{
QByteArray source =
"#include \"header.h\"\n"
"#include \"notresolvable.h\"\n"
"\n"
;
const QString testFilePath
= TestIncludePaths::testFilePath(QLatin1String("test_main_resolvedUnresolved.cpp"));
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
Document::Ptr document = processor.run(testFilePath);
QVERIFY(document);
const QList<Document::Include> resolvedIncludes = document->resolvedIncludes();
@@ -167,15 +158,11 @@ void CppToolsPlugin::test_cppsourceprocessor_includes_cyclic()
/// Check: All include errors are reported as diagnostic messages.
void CppToolsPlugin::test_cppsourceprocessor_includes_allDiagnostics()
{
QByteArray source =
"#include <NotResolvable1>\n"
"#include <NotResolvable2>\n"
"#include \"/some/nonexisting/file123.h\"\n"
"\n"
;
const QString testFilePath
= TestIncludePaths::testFilePath(QLatin1String("test_main_allDiagnostics.cpp"));
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
Document::Ptr document = processor.run(testFilePath);
QVERIFY(document);
QCOMPARE(document->resolvedIncludes().size(), 0);
@@ -185,15 +172,11 @@ void CppToolsPlugin::test_cppsourceprocessor_includes_allDiagnostics()
void CppToolsPlugin::test_cppsourceprocessor_macroUses()
{
QByteArray source =
"#define SOMEDEFINE 1\n"
"#if SOMEDEFINE == 1\n"
" int someNumber;\n"
"#endif\n"
;
const QString testFilePath
= TestIncludePaths::testFilePath(QLatin1String("test_main_macroUses.cpp"));
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
Document::Ptr document = processor.run(testFilePath);
QVERIFY(document);
const QList<Document::MacroUse> macroUses = document->macroUses();
QCOMPARE(macroUses.size(), 1);

View File

@@ -77,8 +77,12 @@ TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source,
QString TestDocument::filePath() const
{
if (!m_baseDirectory.isEmpty())
return QDir::cleanPath(m_baseDirectory + QLatin1Char('/') + m_fileName);
if (!QFileInfo(m_fileName).isAbsolute())
return QDir::tempPath() + QLatin1Char('/') + m_fileName;
return m_fileName;
}
@@ -263,9 +267,25 @@ void ProjectOpenerAndCloser::onGcFinished()
m_gcFinished = true;
}
TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath)
TemporaryDir::TemporaryDir()
: m_temporaryDir(QDir::tempPath() + QLatin1String("/qtcreator-tests-XXXXXX"))
, m_isValid(m_temporaryDir.isValid())
{
}
QString TemporaryDir::createFile(const QByteArray &relativePath, const QByteArray &contents)
{
const QString relativePathString = QString::fromUtf8(relativePath);
if (relativePathString.isEmpty() || QFileInfo(relativePathString).isAbsolute())
return QString();
const QString filePath = m_temporaryDir.path() + QLatin1Char('/') + relativePathString;
if (!TestCase::writeFile(filePath, contents))
return QString();
return filePath;
}
TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath)
{
if (!m_isValid)
return;

View File

@@ -60,10 +60,13 @@ class CPPTOOLS_EXPORT TestDocument
public:
TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker = '@');
void setBaseDirectory(const QString &baseDirectory) { m_baseDirectory = baseDirectory; }
QString filePath() const;
bool writeToDisk() const;
public:
QString m_baseDirectory;
QString m_fileName;
QString m_source;
char m_cursorMarker;
@@ -124,18 +127,31 @@ private:
QList<ProjectExplorer::Project *> m_openProjects;
};
class CPPTOOLS_EXPORT TemporaryCopiedDir
class TemporaryDir
{
Q_DISABLE_COPY(TemporaryDir)
public:
TemporaryCopiedDir(const QString &sourceDirPath);
TemporaryDir();
bool isValid() const { return m_isValid; }
QString path() const { return m_temporaryDir.path(); }
QString createFile(const QByteArray &relativePath, const QByteArray &contents);
protected:
QTemporaryDir m_temporaryDir;
bool m_isValid;
};
class CPPTOOLS_EXPORT TemporaryCopiedDir : public TemporaryDir
{
public:
TemporaryCopiedDir(const QString &sourceDirPath);
QString absolutePath(const QByteArray &relativePath) const;
private:
QTemporaryDir m_temporaryDir;
bool m_isValid;
TemporaryCopiedDir();
};
class CPPTOOLS_EXPORT VerifyCleanCppModelManager

View File

@@ -544,14 +544,8 @@ bool IncludeGroup::hasCommonIncludeDir() const
using namespace Tests;
using CppTools::Internal::CppToolsPlugin;
static QList<Include> includesForSource(const QByteArray &source)
static QList<Include> includesForSource(const QString &filePath)
{
const QString fileName = TestIncludePaths::testFilePath();
FileWriterAndRemover scopedFile(fileName, source);
if (!scopedFile.writtenSuccessfully())
return QList<Include>();
using namespace CppTools::Internal;
CppModelManager *cmm = CppModelManager::instance();
cmm->GC();
@@ -560,44 +554,18 @@ static QList<Include> includesForSource(const QByteArray &source)
<< ProjectPart::HeaderPath(
TestIncludePaths::globalIncludePath(),
ProjectPart::HeaderPath::IncludePath));
sourceProcessor->run(fileName);
sourceProcessor->run(filePath);
Document::Ptr document = cmm->document(fileName);
Document::Ptr document = cmm->document(filePath);
return document->resolvedIncludes();
}
void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByNewLines()
{
// Source referencing those files
QByteArray source =
"#include \"header.h\"\n"
"\n"
"#include \"file.h\"\n"
"#include \"fileother.h\"\n"
"\n"
"#include <lib/fileother.h>\n"
"#include <lib/file.h>\n"
"\n"
"#include \"otherlib/file.h\"\n"
"#include \"otherlib/fileother.h\"\n"
"\n"
"#include \"utils/utils.h\"\n"
"\n"
"#include <QDebug>\n"
"#include <QDir>\n"
"#include <QString>\n"
"\n"
"#include <iostream>\n"
"#include <string>\n"
"#include <except>\n"
"\n"
"#include <iostream>\n"
"#include \"stuff\"\n"
"#include <except>\n"
"\n"
;
const QString testFilePath = TestIncludePaths::testFilePath(
QLatin1String("test_main_detectIncludeGroupsByNewLines.cpp"));
QList<Include> includes = includesForSource(source);
QList<Include> includes = includesForSource(testFilePath);
QCOMPARE(includes.size(), 17);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByNewLines(includes);
@@ -636,20 +604,10 @@ void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByNewLines()
void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByIncludeDir()
{
QByteArray source =
"#include \"file.h\"\n"
"#include \"fileother.h\"\n"
"#include <lib/file.h>\n"
"#include <lib/fileother.h>\n"
"#include \"otherlib/file.h\"\n"
"#include \"otherlib/fileother.h\"\n"
"#include <iostream>\n"
"#include <string>\n"
"#include <except>\n"
"\n"
;
const QString testFilePath = TestIncludePaths::testFilePath(
QLatin1String("test_main_detectIncludeGroupsByIncludeDir.cpp"));
QList<Include> includes = includesForSource(source);
QList<Include> includes = includesForSource(testFilePath);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
@@ -670,20 +628,10 @@ void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByIncludeDir()
void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByIncludeType()
{
QByteArray source =
"#include \"file.h\"\n"
"#include \"fileother.h\"\n"
"#include <lib/file.h>\n"
"#include <lib/fileother.h>\n"
"#include \"otherlib/file.h\"\n"
"#include \"otherlib/fileother.h\"\n"
"#include <iostream>\n"
"#include <string>\n"
"#include <except>\n"
"\n"
;
const QString testFilePath = TestIncludePaths::testFilePath(
QLatin1String("test_main_detectIncludeGroupsByIncludeType.cpp"));
QList<Include> includes = includesForSource(source);
QList<Include> includes = includesForSource(testFilePath);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);

View File

@@ -103,11 +103,17 @@ public:
{
QVERIFY(succeededSoFar());
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
QList<Tests::TestDocument> documents_ = documents;
// Write files
QSet<QString> filePaths;
foreach (const Tests::TestDocument &document, documents) {
QVERIFY(document.writeToDisk());
filePaths << document.filePath();
for (int i = 0, size = documents_.size(); i < size; ++i) {
documents_[i].setBaseDirectory(temporaryDir.path());
QVERIFY(documents_[i].writeToDisk());
filePaths << documents_[i].filePath();
}
// Parse files
@@ -115,7 +121,7 @@ public:
const Snapshot snapshot = globalSnapshot();
// Get class for which to generate the hierarchy
const Document::Ptr firstDocument = snapshot.document(documents.first().filePath());
const Document::Ptr firstDocument = snapshot.document(documents_.first().filePath());
QVERIFY(firstDocument);
QVERIFY(firstDocument->diagnosticMessages().isEmpty());
Class *clazz = FindFirstClassInDocument()(firstDocument);