File: mesontests.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (227 lines) | stat: -rw-r--r-- 5,347 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
    SPDX-FileCopyrightText: 2019 Daniel Mensinger <daniel@mensinger-ka.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "mesontests.h"
#include <QJsonArray>
#include <QJsonObject>
#include <algorithm>
#include <debug.h>
#include <interfaces/iproject.h>
#include <kjob.h>
#include <outputview/outputexecutejob.h>
#include <util/executecompositejob.h>

using namespace std;
using namespace KDevelop;

// Class MesonTest

MesonTest::MesonTest(const QJsonObject& json, IProject* project)
    : m_project(project)
{
    fromJson(json);
}

MesonTest::~MesonTest() {}

QString MesonTest::name() const
{
    return m_name;
}

QStringList MesonTest::suites() const
{
    return m_suites;
}

IProject* MesonTest::project() const
{
    return m_project;
}

KJob* MesonTest::job(ITestSuite::TestJobVerbosity verbosity)
{
    auto convVerbosity = [verbosity]() {
        switch (verbosity) {
        case KDevelop::ITestSuite::Verbose:
            return OutputJob::Verbose;
        case KDevelop::ITestSuite::Silent:
            return OutputJob::Silent;
        }
        Q_UNREACHABLE();
    }();

    auto* job = new OutputExecuteJob(m_project, convVerbosity);
    *job << m_command;
    if (!m_workDir.isEmpty()) {
        job->setWorkingDirectory(m_workDir.toUrl());
    }
    job->setJobName(m_name);
    for (auto i = begin(m_env); i != end(m_env); ++i) {
        job->addEnvironmentOverride(i.key(), i.value());
    }
    return job;
}

void MesonTest::fromJson(const QJsonObject& json)
{
    m_name = json[QStringLiteral("name")].toString();
    m_workDir = Path(json[QStringLiteral("workdir")].toString());

    QJsonArray cmd = json[QStringLiteral("cmd")].toArray();
    QJsonArray suites = json[QStringLiteral("suite")].toArray();
    QJsonObject env = json[QStringLiteral("env")].toObject();

    transform(begin(cmd), end(cmd), back_inserter(m_command), [](const auto& x) { return x.toString(); });
    transform(begin(suites), end(suites), back_inserter(m_suites), [](const auto& x) { return x.toString(); });

    for (auto i = begin(env); i != end(env); ++i) {
        m_env[i.key()] = i.value().toString();
    }

    qCDebug(KDEV_Meson) << "MINTRO:   - Loaded test" << m_name << "suites:" << m_suites;
}

// Class MesonTestSuite

MesonTestSuite::MesonTestSuite(QString name, IProject* project)
    : m_name(name)
    , m_project(project)
{
    qCDebug(KDEV_Meson) << "MINTRO:   - New test suite" << m_name;
}

MesonTestSuite::~MesonTestSuite() {}

QString MesonTestSuite::name() const
{
    return m_name;
}

QStringList MesonTestSuite::cases() const
{
    QStringList result;
    for (auto i : m_tests) {
        result << i->name();
    }
    return result;
}

IProject* MesonTestSuite::project() const
{
    return m_project;
}

KJob* MesonTestSuite::launchCase(const QString& testCase, TestJobVerbosity verbosity)
{
    auto iter = m_tests.find(testCase);
    if (iter == end(m_tests)) {
        return nullptr;
    }

    return (*iter)->job(verbosity);
}

KJob* MesonTestSuite::launchCases(const QStringList& testCases, TestJobVerbosity verbosity)
{
    QList<KJob*> jobs;
    for (const auto& i : testCases) {
        auto iter = m_tests.find(i);
        if (iter == end(m_tests)) {
            continue;
        }

        jobs << (*iter)->job(verbosity);
    }

    return new ExecuteCompositeJob(m_project, jobs);
}

KJob* MesonTestSuite::launchAllCases(TestJobVerbosity verbosity)
{
    QList<KJob*> jobs;
    for (auto& i : m_tests) {
        jobs << i->job(verbosity);
    }

    return new ExecuteCompositeJob(m_project, jobs);
}

IndexedDeclaration MesonTestSuite::declaration() const
{
    return IndexedDeclaration();
}

IndexedDeclaration MesonTestSuite::caseDeclaration(const QString&) const
{
    return IndexedDeclaration();
}

void MesonTestSuite::addTestCase(MesonTestPtr test)
{
    if (!test) {
        qCDebug(KDEV_Meson) << "TESTS: Tried to add a nullptr test";
        return;
    }

    m_tests[test->name()] = test;
}

QHash<QString, MesonTestPtr> MesonTestSuite::tests()
{
    return m_tests;
}

// Class MesonTestSuites

MesonTestSuites::MesonTestSuites(const QJsonArray& json, IProject* project)
    : m_project(project)
{
    fromJSON(json);
}

MesonTestSuites::~MesonTestSuites() {}

QHash<QString, MesonTestSuitePtr> MesonTestSuites::testSuites()
{
    return m_suites;
}

MesonTestSuitePtr MesonTestSuites::testSuite(QString name)
{
    auto iter = m_suites.find(name);
    if (iter == end(m_suites)) {
        return nullptr;
    }
    return *iter;
}

MesonTestSuitePtr MesonTestSuites::operator[](QString name)
{
    return testSuite(name);
}

void MesonTestSuites::fromJSON(const QJsonArray& json)
{
    QVector<MesonTestPtr> tests;
    qCDebug(KDEV_Meson) << "MINTRO: -- Loading tests from JSON...";
    for (const auto& i : json) {
        tests << make_shared<MesonTest>(i.toObject(), m_project);
    }

    qCDebug(KDEV_Meson) << "MINTRO: -- Adding tests to suites";
    for (auto& i : tests) {
        for (QString j : i->suites()) {
            auto suite = testSuite(j);
            if (!suite) {
                suite = make_shared<MesonTestSuite>(j, m_project);
                m_suites[j] = suite;
            }
            suite->addTestCase(i);
            qCDebug(KDEV_Meson) << "MINTRO:   - Added test" << i->name() << "to suite" << suite->name();
        }
    }
}