1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
SPDX-FileCopyrightText: 2017 Kevin Funk <kfunk@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "test_ctestfindsuites.h"
#include "testhelpers.h"
#include "cmakeutils.h"
#include "cmake-test-paths.h"
#include <language/duchain/declaration.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/duchain/topducontext.h>
#include <interfaces/itestcontroller.h>
#include <interfaces/itestsuite.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <interfaces/ibuildsystemmanager.h>
#include <interfaces/iprojectbuilder.h>
#include <testing/ctestsuite.h>
#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <project/projectmodel.h>
#include <QDir>
#include <QTest>
#include <KJob>
using namespace KDevelop;
void waitForSuites(IProject* project, int count, int secondsTimeout)
{
auto testController = ICore::self()->testController();
QTRY_COMPARE_WITH_TIMEOUT(testController->testSuitesForProject(project).size(), count, secondsTimeout * 1000);
}
void TestCTestFindSuites::initTestCase()
{
AutoTestShell::init({"KDevCMakeManager", "KDevCMakeBuilder", "KDevMakeBuilder", "KDevStandardOutputView"});
TestCore::initialize();
qRegisterMetaType<ITestSuite*>();
cleanup();
}
void TestCTestFindSuites::cleanup()
{
const auto projects = ICore::self()->projectController()->projects() ;
for (IProject* p : projects) {
if (p && p->buildSystemManager() && p->buildSystemManager()->builder())
{
p->buildSystemManager()->builder()->prune(p)->exec();
}
ICore::self()->projectController()->closeProject(p);
}
QVERIFY(ICore::self()->projectController()->projects().isEmpty());
}
void TestCTestFindSuites::cleanupTestCase()
{
TestCore::shutdown();
}
void TestCTestFindSuites::testCTestSuite()
{
IProject* project = loadProject( "unit_tests" );
QVERIFY2(project, "Project was not opened");
waitForSuites(project, 5, 25);
const QList<ITestSuite*> suites = ICore::self()->testController()->testSuitesForProject(project);
QCOMPARE(suites.size(), 5);
DUChainReadLocker locker(DUChain::lock());
for (auto suite : suites) {
QCOMPARE(suite->cases(), QStringList());
auto* ctestSuite = static_cast<CTestSuite*>(suite);
QVERIFY(suite->declaration().isValid());
const auto buildDir = Path(CMake::allBuildDirs(project).at(0));
QString exeSubdir = buildDir.relativePath(ctestSuite->executable().parent());
QCOMPARE(exeSubdir, ctestSuite->name() == "fail" ? QStringLiteral("bin") : QString() );
QString willFail;
const QString workingDirectory = ctestSuite->properties().value(QLatin1String("WORKING_DIRECTORY"), QString());
auto cursor = ctestSuite->declaration().data()->range().start;
if (ctestSuite->name() == QLatin1String("fail")) {
willFail = QLatin1String("TRUE");
QCOMPARE(workingDirectory, QLatin1String("/bar/baz"));
QCOMPARE(ctestSuite->properties().value(QLatin1String("FOO"), QString()), QLatin1String("foo"));
QCOMPARE(ctestSuite->properties().value(QLatin1String("BAR"), QString()), QLatin1String("TRUE"));
QCOMPARE(ctestSuite->properties().value(QLatin1String("MULTILINE"), QString()), QLatin1String("this is \na multi\nline property"));
QCOMPARE(ctestSuite->properties().value(QLatin1String("QUOTES"), QString()), QLatin1String("\"\\\\\"\\\\\\"));
QCOMPARE(cursor.line, 0);
} else if (ctestSuite->name() == QLatin1String("test_three")) {
QCOMPARE(workingDirectory, QLatin1String("/foo"));
QCOMPARE(ctestSuite->arguments(), QStringList("3"));
QCOMPARE(cursor.line, 2);
} else if (ctestSuite->name() == QLatin1String("test_four")) {
QCOMPARE(ctestSuite->arguments(), QStringList("4"));
QCOMPARE(cursor.line, 2);
} else if (ctestSuite->name() == QLatin1String("test_five")) {
QCOMPARE(workingDirectory, QString(buildDir.path() + QLatin1String("/bin")));
QCOMPARE(cursor.line, 2);
} else {
QCOMPARE(workingDirectory, QString());
QCOMPARE(cursor.line, 0);
}
QCOMPARE(ctestSuite->properties().value(QLatin1String("WILL_FAIL")), willFail);
}
}
void TestCTestFindSuites::testQtTestCases()
{
IProject* project = loadProject( "unit_tests_kde" );
QVERIFY2(project, "Project was not opened");
waitForSuites(project, 2, 25);
const QList<ITestSuite*> suites = ICore::self()->testController()->testSuitesForProject(project);
QCOMPARE(suites.size(), 2);
QMap<QString, int> caseLines = {
{QStringLiteral("passingTestCase"), 9},
{QStringLiteral("failingTestCase"), 14},
{QStringLiteral("expectedFailTestCase"), 19},
{QStringLiteral("unexpectedPassTestCase"), 25},
{QStringLiteral("skippedTestCase"), 31}
};
DUChainReadLocker locker(DUChain::lock());
for (auto suite : suites) {
const auto caseNames = suite->cases();
QCOMPARE(caseNames.count(), caseLines.count());
QVERIFY(suite->declaration().isValid());
for (const auto& caseName : caseNames) {
auto decl = suite->caseDeclaration(caseName);
QVERIFY(decl.isValid());
auto it = caseLines.find(caseName);
QVERIFY(it != caseLines.end());
QCOMPARE(decl.data()->range().start.line, it.value());
}
}
}
QTEST_MAIN(TestCTestFindSuites)
#include "moc_test_ctestfindsuites.cpp"
|