Commit 9921960a authored by Nikolai Kosjar's avatar Nikolai Kosjar

Cpp{Editor,Tools}: Move tests for IncludeGroups to CppTools

They belong there. This will also kill the CppSourceProcessor dependency
from CppEditor.

Change-Id: Ic2ae6b0c6f0913d913636be61df194846985d1ce
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 50233c0a
......@@ -147,9 +147,6 @@ private slots:
void test_quickfix_InsertDeclFromDef();
void test_quickfix_AddIncludeForUndefinedIdentifier_detectIncludeGroupsByNewLines();
void test_quickfix_AddIncludeForUndefinedIdentifier_detectIncludeGroupsByIncludeDir();
void test_quickfix_AddIncludeForUndefinedIdentifier_detectIncludeGroupsByIncludeType();
void test_quickfix_AddIncludeForUndefinedIdentifier_normal();
void test_quickfix_AddIncludeForUndefinedIdentifier_ignoremoc();
void test_quickfix_AddIncludeForUndefinedIdentifier_sortingTop();
......
......@@ -37,9 +37,7 @@
#include <cpptools/cppcodestylepreferences.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cppsourceprocessertesthelper.h>
#include <cpptools/cppsourceprocessor.h>
#include <cpptools/cpptoolssettings.h>
#include <cpptools/includeutils.h>
#include <utils/fileutils.h>
......@@ -56,7 +54,6 @@ using namespace CppEditor;
using namespace CppEditor::Internal;
using namespace CppEditor::Internal::Tests;
using namespace CppTools;
using namespace IncludeUtils;
using namespace TextEditor;
using CppTools::Tests::TestIncludePaths;
......@@ -1868,162 +1865,6 @@ void CppEditorPlugin::test_quickfix_InsertDeclFromDef()
insertToSectionDeclFromDef("private slots", 5);
}
QList<Include> includesForSource(const QByteArray &source)
{
const QString fileName = TestIncludePaths::testFilePath();
CppTools::Tests::TestCase::writeFile(fileName, source);
using namespace CppTools::Internal;
CppModelManager *cmm = CppModelManager::instance();
cmm->GC();
CppSourceProcessor sourceProcessor((QPointer<CppModelManager>(cmm)));
sourceProcessor.setIncludePaths(QStringList(TestIncludePaths::globalIncludePath()));
sourceProcessor.run(fileName);
Document::Ptr document = cmm->document(fileName);
return document->resolvedIncludes();
}
/// Check: Detection of include groups separated by new lines
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_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"
;
QList<Include> includes = includesForSource(source);
QCOMPARE(includes.size(), 17);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByNewLines(includes);
QCOMPARE(includeGroups.size(), 8);
QCOMPARE(includeGroups.at(0).size(), 1);
QVERIFY(includeGroups.at(0).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(0).hasOnlyIncludesOfType(Client::IncludeLocal));
QVERIFY(includeGroups.at(0).isSorted());
QCOMPARE(includeGroups.at(1).size(), 2);
QVERIFY(!includeGroups.at(1).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(1).hasOnlyIncludesOfType(Client::IncludeLocal));
QVERIFY(includeGroups.at(1).isSorted());
QCOMPARE(includeGroups.at(2).size(), 2);
QVERIFY(!includeGroups.at(2).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(2).hasOnlyIncludesOfType(Client::IncludeGlobal));
QVERIFY(!includeGroups.at(2).isSorted());
QCOMPARE(includeGroups.at(6).size(), 3);
QVERIFY(includeGroups.at(6).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(6).hasOnlyIncludesOfType(Client::IncludeGlobal));
QVERIFY(!includeGroups.at(6).isSorted());
QCOMPARE(includeGroups.at(7).size(), 3);
QVERIFY(includeGroups.at(7).commonPrefix().isEmpty());
QVERIFY(!includeGroups.at(7).hasOnlyIncludesOfType(Client::IncludeLocal));
QVERIFY(!includeGroups.at(7).hasOnlyIncludesOfType(Client::IncludeGlobal));
QVERIFY(!includeGroups.at(7).isSorted());
QCOMPARE(IncludeGroup::filterIncludeGroups(includeGroups, Client::IncludeLocal).size(), 4);
QCOMPARE(IncludeGroup::filterIncludeGroups(includeGroups, Client::IncludeGlobal).size(), 3);
QCOMPARE(IncludeGroup::filterMixedIncludeGroups(includeGroups).size(), 1);
}
/// Check: Detection of include groups separated by include dirs
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_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"
;
QList<Include> includes = includesForSource(source);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
QCOMPARE(includeGroups.size(), 4);
QCOMPARE(includeGroups.at(0).size(), 2);
QVERIFY(includeGroups.at(0).commonIncludeDir().isEmpty());
QCOMPARE(includeGroups.at(1).size(), 2);
QCOMPARE(includeGroups.at(1).commonIncludeDir(), QLatin1String("lib/"));
QCOMPARE(includeGroups.at(2).size(), 2);
QCOMPARE(includeGroups.at(2).commonIncludeDir(), QLatin1String("otherlib/"));
QCOMPARE(includeGroups.at(3).size(), 3);
QCOMPARE(includeGroups.at(3).commonIncludeDir(), QLatin1String(""));
}
/// Check: Detection of include groups separated by include types
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_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"
;
QList<Include> includes = includesForSource(source);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
QCOMPARE(includeGroups.size(), 4);
QCOMPARE(includeGroups.at(0).size(), 2);
QVERIFY(includeGroups.at(0).hasOnlyIncludesOfType(Client::IncludeLocal));
QCOMPARE(includeGroups.at(1).size(), 2);
QVERIFY(includeGroups.at(1).hasOnlyIncludesOfType(Client::IncludeGlobal));
QCOMPARE(includeGroups.at(2).size(), 2);
QVERIFY(includeGroups.at(2).hasOnlyIncludesOfType(Client::IncludeLocal));
QCOMPARE(includeGroups.at(3).size(), 3);
QVERIFY(includeGroups.at(3).hasOnlyIncludesOfType(Client::IncludeGlobal));
}
/// Check: Add include if there is already an include
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_normal()
{
......
......@@ -161,6 +161,10 @@ private slots:
void test_typehierarchy_data();
void test_typehierarchy();
void test_includeGroups_detectIncludeGroupsByNewLines();
void test_includeGroups_detectIncludeGroupsByIncludeDir();
void test_includeGroups_detectIncludeGroupsByIncludeType();
#endif
private:
......
......@@ -492,3 +492,171 @@ bool IncludeGroup::hasCommonIncludeDir() const
}
return true;
}
#ifdef WITH_TESTS
#include "cppmodelmanager.h"
#include "cppsourceprocessertesthelper.h"
#include "cppsourceprocessor.h"
#include "cpptoolsplugin.h"
#include "cpptoolstestcase.h"
#include <QtTest>
using namespace Tests;
using CppTools::Internal::CppToolsPlugin;
static QList<Include> includesForSource(const QByteArray &source)
{
const QString fileName = TestIncludePaths::testFilePath();
CppTools::Tests::TestCase::writeFile(fileName, source);
using namespace CppTools::Internal;
CppModelManager *cmm = CppModelManager::instance();
cmm->GC();
CppSourceProcessor sourceProcessor((QPointer<CppModelManager>(cmm)));
sourceProcessor.setIncludePaths(QStringList(TestIncludePaths::globalIncludePath()));
sourceProcessor.run(fileName);
Document::Ptr document = cmm->document(fileName);
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"
;
QList<Include> includes = includesForSource(source);
QCOMPARE(includes.size(), 17);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByNewLines(includes);
QCOMPARE(includeGroups.size(), 8);
QCOMPARE(includeGroups.at(0).size(), 1);
QVERIFY(includeGroups.at(0).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(0).hasOnlyIncludesOfType(Client::IncludeLocal));
QVERIFY(includeGroups.at(0).isSorted());
QCOMPARE(includeGroups.at(1).size(), 2);
QVERIFY(!includeGroups.at(1).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(1).hasOnlyIncludesOfType(Client::IncludeLocal));
QVERIFY(includeGroups.at(1).isSorted());
QCOMPARE(includeGroups.at(2).size(), 2);
QVERIFY(!includeGroups.at(2).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(2).hasOnlyIncludesOfType(Client::IncludeGlobal));
QVERIFY(!includeGroups.at(2).isSorted());
QCOMPARE(includeGroups.at(6).size(), 3);
QVERIFY(includeGroups.at(6).commonPrefix().isEmpty());
QVERIFY(includeGroups.at(6).hasOnlyIncludesOfType(Client::IncludeGlobal));
QVERIFY(!includeGroups.at(6).isSorted());
QCOMPARE(includeGroups.at(7).size(), 3);
QVERIFY(includeGroups.at(7).commonPrefix().isEmpty());
QVERIFY(!includeGroups.at(7).hasOnlyIncludesOfType(Client::IncludeLocal));
QVERIFY(!includeGroups.at(7).hasOnlyIncludesOfType(Client::IncludeGlobal));
QVERIFY(!includeGroups.at(7).isSorted());
QCOMPARE(IncludeGroup::filterIncludeGroups(includeGroups, Client::IncludeLocal).size(), 4);
QCOMPARE(IncludeGroup::filterIncludeGroups(includeGroups, Client::IncludeGlobal).size(), 3);
QCOMPARE(IncludeGroup::filterMixedIncludeGroups(includeGroups).size(), 1);
}
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"
;
QList<Include> includes = includesForSource(source);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
QCOMPARE(includeGroups.size(), 4);
QCOMPARE(includeGroups.at(0).size(), 2);
QVERIFY(includeGroups.at(0).commonIncludeDir().isEmpty());
QCOMPARE(includeGroups.at(1).size(), 2);
QCOMPARE(includeGroups.at(1).commonIncludeDir(), QLatin1String("lib/"));
QCOMPARE(includeGroups.at(2).size(), 2);
QCOMPARE(includeGroups.at(2).commonIncludeDir(), QLatin1String("otherlib/"));
QCOMPARE(includeGroups.at(3).size(), 3);
QCOMPARE(includeGroups.at(3).commonIncludeDir(), QLatin1String(""));
}
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"
;
QList<Include> includes = includesForSource(source);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
QCOMPARE(includeGroups.size(), 4);
QCOMPARE(includeGroups.at(0).size(), 2);
QVERIFY(includeGroups.at(0).hasOnlyIncludesOfType(Client::IncludeLocal));
QCOMPARE(includeGroups.at(1).size(), 2);
QVERIFY(includeGroups.at(1).hasOnlyIncludesOfType(Client::IncludeGlobal));
QCOMPARE(includeGroups.at(2).size(), 2);
QVERIFY(includeGroups.at(2).hasOnlyIncludesOfType(Client::IncludeLocal));
QCOMPARE(includeGroups.at(3).size(), 3);
QVERIFY(includeGroups.at(3).hasOnlyIncludesOfType(Client::IncludeGlobal));
}
#endif // WITH_TESTS
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment