1 /*
2     SPDX-FileCopyrightText: 2016 Carlos Nihelton <carlosnsoliveira@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "test_clangtidyjob.h"
8 
9 // SUT
10 #include "job.h"
11 // KDevPlatform
12 #include <language/editor/documentrange.h>
13 #include <tests/autotestshell.h>
14 #include <tests/testcore.h>
15 // Qt
16 #include <QTest>
17 #include <QSignalSpy>
18 
19 
20 using namespace KDevelop;
21 using namespace ClangTidy;
22 
23 class JobTester : public Job
24 {
25     Q_OBJECT
26 
27 public:
JobTester(Job::Parameters params)28     JobTester(Job::Parameters params)
29         : Job(params)
30     {
31     }
32 
33     using Job::processStdoutLines;
34     using Job::processStderrLines;
35     using Job::childProcessExited;
36 
standardOutput() const37     QString standardOutput() const { return m_standardOutput.join('\n'); }
38 };
39 
initTestCase()40 void TestClangTidyJob::initTestCase()
41 {
42     AutoTestShell::init({ "kdevclangtidy" });
43     TestCore::initialize(Core::NoUi);
44 }
45 
cleanupTestCase()46 void TestClangTidyJob::cleanupTestCase()
47 {
48     TestCore::shutdown();
49 }
50 
testJob()51 void TestClangTidyJob::testJob()
52 {
53     QFile output_example_file(QFINDTESTDATA("data/output_example"));
54     QVERIFY(output_example_file.open(QIODevice::ReadOnly));
55     QTextStream ios(&output_example_file);
56     QStringList stdoutOutput;
57     QString line;
58     while (ios.readLineInto(&line)) {
59         stdoutOutput << line;
60     }
61     QVERIFY(!stdoutOutput.isEmpty());
62 
63     Job::Parameters jobParams;
64     JobTester jobTester(jobParams);
65 
66     qRegisterMetaType<QVector<KDevelop::IProblem::Ptr>>();
67     QSignalSpy problemsSpy(&jobTester, &JobTester::problemsDetected);
68 
69     jobTester.processStdoutLines(stdoutOutput);
70     QCOMPARE(jobTester.standardOutput(), stdoutOutput.join('\n'));
71 
72     jobTester.childProcessExited(0, QProcess::NormalExit);
73 
74     QCOMPARE(problemsSpy.count(), 1);
75     const auto problems = qvariant_cast<QVector<KDevelop::IProblem::Ptr>>(problemsSpy.at(0).at(0));
76 
77     QVERIFY(problems[0]->finalLocation().document.str().contains(QStringLiteral("/kdev-clang-tidy/src/plugin.cpp")));
78     QVERIFY(
79         problems[0]->explanation().startsWith(QStringLiteral("[cppcoreguidelines-pro-bounds-array-to-pointer-decay]")));
80     QVERIFY(problems[1]->finalLocation().document.str().contains(QStringLiteral("/kdev-clang-tidy/src/plugin.cpp")));
81 
82     QVERIFY(
83         problems[1]->explanation().startsWith(QStringLiteral("[cppcoreguidelines-pro-bounds-array-to-pointer-decay]")));
84     QVERIFY(problems[2]->finalLocation().document.str().contains(QStringLiteral("/kdev-clang-tidy/src/plugin.cpp")));
85 
86     QVERIFY(
87         problems[2]->explanation().startsWith(QStringLiteral("[cppcoreguidelines-pro-bounds-array-to-pointer-decay]")));
88 }
89 
90 QTEST_GUILESS_MAIN(TestClangTidyJob)
91 
92 #include "test_clangtidyjob.moc"
93