File indexing completed on 2024-04-28 04:37:27

0001 /*
0002     SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "testcontroller.h"
0008 #include "interfaces/itestsuite.h"
0009 #include "debug.h"
0010 #include <interfaces/icore.h>
0011 
0012 #include <KLocalizedString>
0013 
0014 using namespace KDevelop;
0015 
0016 class KDevelop::TestControllerPrivate
0017 {
0018 public:
0019     QList<ITestSuite*> suites;
0020 };
0021 
0022 TestController::TestController(QObject *parent)
0023 : ITestController(parent)
0024 , d_ptr(new TestControllerPrivate)
0025 {
0026 }
0027 
0028 TestController::~TestController() = default;
0029 
0030 void TestController::initialize()
0031 {
0032 
0033 }
0034 
0035 void TestController::cleanup()
0036 {
0037     Q_D(TestController);
0038 
0039     d->suites.clear();
0040 }
0041 
0042 QList<ITestSuite*> TestController::testSuites() const
0043 {
0044     Q_D(const TestController);
0045 
0046     return d->suites;
0047 }
0048 
0049 void TestController::removeTestSuite(ITestSuite* suite)
0050 {
0051     Q_D(TestController);
0052 
0053     if (d->suites.removeAll(suite)) {
0054         emit testSuiteRemoved(suite);
0055     }
0056 }
0057 
0058 void TestController::addTestSuite(ITestSuite* suite)
0059 {
0060     Q_D(TestController);
0061 
0062     if (ITestSuite* existingSuite = findTestSuite(suite->project(), suite->name()))
0063     {
0064         if (existingSuite == suite) {
0065             return;
0066         }
0067         removeTestSuite(existingSuite);
0068         delete existingSuite;
0069     }
0070     d->suites.append(suite);
0071     if(!ICore::self()->shuttingDown())
0072         emit testSuiteAdded(suite);
0073 }
0074 
0075 ITestSuite* TestController::findTestSuite(IProject* project, const QString& name) const
0076 {
0077     const auto suites = testSuitesForProject(project);
0078     auto it = std::find_if(suites.begin(), suites.end(), [&](ITestSuite* suite) {
0079         return (suite->name() == name);
0080     });
0081 
0082     return (it != suites.end()) ? *it : nullptr;
0083 }
0084 
0085 
0086 QList< ITestSuite* > TestController::testSuitesForProject(IProject* project) const
0087 {
0088     Q_D(const TestController);
0089 
0090     QList<ITestSuite*> suites;
0091     for (ITestSuite* suite : qAsConst(d->suites)) {
0092         if (suite->project() == project)
0093         {
0094             suites << suite;
0095         }
0096     }
0097     return suites;
0098 }
0099 
0100 void TestController::notifyTestRunFinished(ITestSuite* suite, const TestResult& result)
0101 {
0102     qCDebug(SHELL) << "Test run finished for suite" << suite->name();
0103     emit testRunFinished(suite, result);
0104 }
0105 
0106 void TestController::notifyTestRunStarted(ITestSuite* suite, const QStringList& test_cases)
0107 {
0108     qCDebug(SHELL) << "Test run started for suite" << suite->name();
0109     emit testRunStarted(suite, test_cases);
0110 }
0111 
0112 #include "moc_testcontroller.cpp"