Commit 6cafc424 authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen

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: default avatarChristian Stenger <christian.stenger@theqtcompany.com>
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@theqtcompany.com>
parent cb3c7538
......@@ -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();
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::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);
......@@ -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 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(), 4U);
Document::Ptr headerDocument = createDocumentAndFile(&temporaryDir, "file.h", headerText, 1U);
QVERIFY(headerDocument);
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 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"));