File: test_testcontroller.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (241 lines) | stat: -rw-r--r-- 7,965 bytes parent folder | download
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
    Unit tests for TestController.
    Copyright 2012 Miha Čančula <miha@noughmad.eu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "test_testcontroller.h"
#include <testcontroller.h>
#include <QTest>
#include <QSignalSpy>

#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <tests/testproject.h>
#include <itestsuite.h>
#include <iproject.h>
#include <language/duchain/indexeddeclaration.h>

using namespace KDevelop;

const char* TestSuiteName = "TestTestSuite";
const char* TestSuiteNameTwo = "TestTestSuiteTwo";
const char* TestCaseNameOne = "TestTestCaseOne";
const char* TestCaseNameTwo = "TestTestCaseTwo";

Q_DECLARE_METATYPE(KDevelop::TestResult)

class FakeTestSuite : public KDevelop::ITestSuite
{
public:
    FakeTestSuite(const QString& name, IProject* project, const QStringList& cases = QStringList()) : m_name(name), m_project(project), m_cases(cases) {}
    ~FakeTestSuite() override {}

    IProject* project() const override {return m_project;}
    QString name() const override {return m_name;}
    QStringList cases() const override {return m_cases;}

    IndexedDeclaration declaration() const override;
    IndexedDeclaration caseDeclaration(const QString& testCase) const override;

    KJob* launchAllCases(TestJobVerbosity verbosity) override;
    KJob* launchCase(const QString& testCase, TestJobVerbosity verbosity) override;
    KJob* launchCases(const QStringList& testCases, TestJobVerbosity verbosity) override;

private:
    QString m_name;
    IProject* m_project;
    QStringList m_cases;
};

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

IndexedDeclaration FakeTestSuite::caseDeclaration(const QString& testCase) const
{
    Q_UNUSED(testCase);
    return IndexedDeclaration();
}

KJob* FakeTestSuite::launchAllCases(ITestSuite::TestJobVerbosity verbosity)
{
    Q_UNUSED(verbosity);
    return nullptr;
}

KJob* FakeTestSuite::launchCase(const QString& testCase, ITestSuite::TestJobVerbosity verbosity)
{
    Q_UNUSED(testCase);
    Q_UNUSED(verbosity);
    return nullptr;
}

KJob* FakeTestSuite::launchCases(const QStringList& testCases, ITestSuite::TestJobVerbosity verbosity)
{
    Q_UNUSED(testCases);
    Q_UNUSED(verbosity);
    return nullptr;
}

void TestTestController::emitTestResult(ITestSuite* suite, TestResult::TestCaseResult caseResult)
{
    TestResult result;
    result.suiteResult = caseResult;
    const auto testCases = suite->cases();
    for (const QString& testCase : testCases) {
        result.testCaseResults.insert(testCase, caseResult);
    }

    m_testController->notifyTestRunFinished(suite, result);
}

void TestTestController::initTestCase()
{
    AutoTestShell::init();
    TestCore::initialize( Core::NoUi );

    m_testController = Core::self()->testControllerInternal();
    m_project = new TestProject(Path(), this);

    qRegisterMetaType<KDevelop::ITestSuite*>("KDevelop::ITestSuite*");
    qRegisterMetaType<KDevelop::TestResult>("KDevelop::TestResult");
}

void TestTestController::cleanupTestCase()
{
    delete m_project;
    TestCore::shutdown();
}

void TestTestController::addSuite()
{
    FakeTestSuite suite(TestSuiteName, m_project);
    m_testController->addTestSuite(&suite);

    ITestSuite* found = m_testController->findTestSuite(m_project, TestSuiteName);

    QVERIFY(found);
    QCOMPARE(found->name(), QString(TestSuiteName));
    QCOMPARE(found->project(), m_project);

    m_testController->removeTestSuite(&suite);
}

void TestTestController::removeSuite()
{
    FakeTestSuite suite(TestSuiteName, m_project);
    m_testController->addTestSuite(&suite);

    QVERIFY(m_testController->findTestSuite(m_project, TestSuiteName));
    m_testController->removeTestSuite(&suite);

    QCOMPARE(m_testController->findTestSuite(m_project, TestSuiteName), (ITestSuite*)nullptr);
    QVERIFY(m_testController->testSuites().isEmpty());
}

void TestTestController::replaceSuite()
{
    auto* suiteOne = new FakeTestSuite(TestSuiteName, m_project, QStringList() << TestCaseNameOne);
    m_testController->addTestSuite(suiteOne);

    QCOMPARE(m_testController->findTestSuite(m_project, TestSuiteName)->name(), QString(TestSuiteName));
    QCOMPARE(m_testController->findTestSuite(m_project, TestSuiteName)->cases().size(), 1);

    auto* suiteTwo = new FakeTestSuite(TestSuiteName, m_project, QStringList() << TestCaseNameOne << TestCaseNameTwo);
    m_testController->addTestSuite(suiteTwo);

    QCOMPARE(m_testController->testSuites().size(), 1);
    QCOMPARE(m_testController->findTestSuite(m_project, TestSuiteName)->name(), QString(TestSuiteName));
    QCOMPARE(m_testController->findTestSuite(m_project, TestSuiteName)->cases().size(), 2);

    // TestController deletes the old suite when replacing it, so make sure we don't delete suiteOne manually

    m_testController->removeTestSuite(suiteTwo);
    delete suiteTwo;
}

void TestTestController::findByProject()
{
    IProject* otherProject = new TestProject(Path(), this);

    ITestSuite* suiteOne = new FakeTestSuite(TestSuiteName, m_project);
    ITestSuite* suiteTwo = new FakeTestSuite(TestSuiteName, otherProject);
    m_testController->addTestSuite(suiteOne);
    m_testController->addTestSuite(suiteTwo);

    QCOMPARE(m_testController->testSuites().size(), 2);
    QCOMPARE(m_testController->testSuitesForProject(m_project).size(), 1);

    QCOMPARE(m_testController->testSuitesForProject(m_project).at(0), suiteOne);

    m_testController->removeTestSuite(suiteOne);
    m_testController->removeTestSuite(suiteTwo);
    delete suiteOne;
    delete suiteTwo;

    delete otherProject;
}

void TestTestController::testResults()
{
    ITestSuite* suite = new FakeTestSuite(TestSuiteName, m_project);
    m_testController->addTestSuite(suite);

    QSignalSpy spy(m_testController, SIGNAL(testRunFinished(KDevelop::ITestSuite*,KDevelop::TestResult)));
    QVERIFY(spy.isValid());

    QList<TestResult::TestCaseResult> results;
    results << TestResult::Passed << TestResult::Failed << TestResult::Error << TestResult::Skipped << TestResult::NotRun;

    for (const TestResult::TestCaseResult result : qAsConst(results)) {
        emitTestResult(suite, result);
        QCOMPARE(spy.size(), 1);

        QVariantList arguments = spy.takeFirst();
        QCOMPARE(arguments.size(), 2);

        QVERIFY(arguments.first().canConvert<ITestSuite*>());
        QCOMPARE(arguments.first().value<ITestSuite*>(), suite);

        QVERIFY(arguments.at(1).canConvert<TestResult>());
        QCOMPARE(arguments.at(1).value<TestResult>().suiteResult, result);

        const auto testCases = suite->cases();
        for (const QString& testCase : testCases) {
            QCOMPARE(arguments.at(1).value<TestResult>().testCaseResults[testCase], result);
        }
    }

    QCOMPARE(spy.size(), 0);

    ITestSuite* suiteTwo = new FakeTestSuite(TestSuiteNameTwo, m_project);
    m_testController->addTestSuite(suiteTwo);

    // Verify that only one signal gets emitted even with more suites present
    emitTestResult(suiteTwo, TestResult::Passed);

    QCOMPARE(spy.size(), 1);

    m_testController->removeTestSuite(suite);
    m_testController->removeTestSuite(suiteTwo);
    delete suite;
    delete suiteTwo;
}


QTEST_GUILESS_MAIN(TestTestController)