1 /*
2 SPDX-FileCopyrightText: 2018 Anton Anikin <anton@anikin.xyz>
3 SPDX-FileCopyrightText: 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "test_compileanalyzejob.h"
9
10 #include "compileanalyzejob.h"
11
12 #include <tests/autotestshell.h>
13 #include <tests/testcore.h>
14
15 #include <QTest>
16
17 using namespace KDevelop;
18
19 class JobTester : public CompileAnalyzeJob
20 {
21 Q_OBJECT
22
23 public:
JobTester()24 JobTester()
25 {
26 setToolDisplayName("TestAnalyzer");
27 connect(this, &JobTester::infoMessage, this, &JobTester::collectStarted);
28 }
29
30 public:
31 using CompileAnalyzeJob::parseProgress;
32
started() const33 const QVector<QString>& started() const
34 {
35 return m_started;
36 }
37
38 // implementation detail not accessible
39 #if 0
40 void setTotalCount(int totalCount)
41 {
42 m_totalCount = totalCount;
43 }
44
45 int finishedCount() const
46 {
47 return m_finishedCount;
48 }
49 #endif
50
51 private Q_SLOTS:
collectStarted(KJob *,const QString & name)52 void collectStarted(KJob*, const QString& name)
53 {
54 m_started += name;
55 }
56
57 private:
58 QVector<QString> m_started;
59 };
60
initTestCase()61 void TestCompileAnalyzeJob::initTestCase()
62 {
63 AutoTestShell::init();
64 TestCore::initialize(Core::NoUi);
65 }
66
cleanupTestCase()67 void TestCompileAnalyzeJob::cleanupTestCase()
68 {
69 TestCore::shutdown();
70 }
71
testJob()72 void TestCompileAnalyzeJob::testJob()
73 {
74 JobTester jobTester;
75
76 // test progress parsing =======================================================================
77
78 static const QStringList stdoutOutput1 = {
79 QStringLiteral("TestAnalyzer check started for source2.cpp"),
80 QStringLiteral("TestAnalyzer check started for source1.cpp")
81 };
82
83 static const QStringList stdoutOutput2 = {
84 QStringLiteral("TestAnalyzer check finished for source2.cpp"),
85 QStringLiteral("TestAnalyzer check started for source3.cpp"),
86 QStringLiteral("TestAnalyzer check started for source4.cpp")
87 };
88
89 static const QStringList stdoutOutput3 = {
90 QStringLiteral("TestAnalyzer check finished for source1.cpp"),
91 QStringLiteral("TestAnalyzer check finished for source4.cpp")
92 };
93
94 static const QStringList stdoutOutput4 = {
95 QStringLiteral("TestAnalyzer check finished for source3.cpp"),
96 };
97
98 // jobTester.setTotalCount(4);
99
100 jobTester.parseProgress(stdoutOutput1);
101 QCOMPARE(jobTester.started().size(), 2);
102 QCOMPARE(jobTester.started().at(0), QStringLiteral("source2.cpp"));
103 QCOMPARE(jobTester.started().at(1), QStringLiteral("source1.cpp"));
104 // QCOMPARE(jobTester.finishedCount(), 0);
105 QCOMPARE(jobTester.percent(), (unsigned long)0);
106
107 jobTester.parseProgress(stdoutOutput2);
108 QCOMPARE(jobTester.started().size(), 4);
109 QCOMPARE(jobTester.started().at(2), QStringLiteral("source3.cpp"));
110 QCOMPARE(jobTester.started().at(3), QStringLiteral("source4.cpp"));
111 // QCOMPARE(jobTester.finishedCount(), 1);
112 // QCOMPARE(jobTester.percent(), (unsigned long)25);
113
114 jobTester.parseProgress(stdoutOutput3);
115 QCOMPARE(jobTester.started().size(), 4);
116 // QCOMPARE(jobTester.finishedCount(), 3);
117 // QCOMPARE(jobTester.percent(), (unsigned long)75);
118
119 jobTester.parseProgress(stdoutOutput4);
120 QCOMPARE(jobTester.started().size(), 4);
121 // QCOMPARE(jobTester.finishedCount(), 4);
122 // QCOMPARE(jobTester.percent(), (unsigned long)100);
123
124 QCOMPARE(jobTester.started().at(0), QStringLiteral("source2.cpp"));
125 QCOMPARE(jobTester.started().at(1), QStringLiteral("source1.cpp"));
126 QCOMPARE(jobTester.started().at(2), QStringLiteral("source3.cpp"));
127 QCOMPARE(jobTester.started().at(3), QStringLiteral("source4.cpp"));
128 }
129
130 QTEST_GUILESS_MAIN(TestCompileAnalyzeJob)
131
132 #include "test_compileanalyzejob.moc"
133