File indexing completed on 2024-05-05 04:39:19

0001 /*
0002     SPDX-FileCopyrightText: 2018 Anton Anikin <anton@anikin.xyz>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "test_clazyjob.h"
0008 
0009 #include "job.h"
0010 
0011 #include <tests/autotestshell.h>
0012 #include <tests/testcore.h>
0013 #include <language/editor/documentrange.h>
0014 
0015 #include <QTest>
0016 
0017 using namespace KDevelop;
0018 using namespace Clazy;
0019 
0020 class JobTester : public Job
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     JobTester()
0026     {
0027         connect(this, &JobTester::problemsDetected,
0028                 this, [this](const QVector<KDevelop::IProblem::Ptr>& problems) {
0029                     m_problems += problems;
0030                 });
0031     }
0032 
0033     ~JobTester() override = default;
0034 
0035     using Job::processStdoutLines;
0036     using Job::processStderrLines;
0037 
0038     const QVector<KDevelop::IProblem::Ptr>& problems() const
0039     {
0040         return m_problems;
0041     }
0042 
0043 // implementation detail not accessible
0044 #if 0
0045     void setTotalCount(int totalCount)
0046     {
0047         m_totalCount = totalCount;
0048     }
0049 
0050     int finishedCount() const
0051     {
0052         return m_finishedCount;
0053     }
0054 #endif
0055 
0056 private:
0057     QVector<KDevelop::IProblem::Ptr> m_problems;
0058 };
0059 
0060 void TestClazyJob::initTestCase()
0061 {
0062     AutoTestShell::init({"kdevclazy"});
0063     TestCore::initialize(Core::NoUi);
0064 }
0065 
0066 void TestClazyJob::cleanupTestCase()
0067 {
0068     TestCore::shutdown();
0069 }
0070 
0071 void TestClazyJob::testJob()
0072 {
0073     JobTester jobTester;
0074 
0075     // test errors parsing =========================================================================
0076 
0077     static const QStringList stderrOutput1 = {
0078         QStringLiteral("source2.cpp:13:10: warning: unused variable 'check' [-Wunused-variable]"),
0079         QStringLiteral("    auto check = db.checks()[\"returning-void-expression\"];")
0080     };
0081 
0082     static const QStringList stderrOutput2 = {
0083         QStringLiteral("source3.cpp:248:21: warning: Don't call QList::first() on temporary [-Wclazy-detaching-temporary]"),
0084         QStringLiteral("        auto item = pContext->items().first();"),
0085         QStringLiteral("                    ^"),
0086         QStringLiteral("1 warning generated.")
0087     };
0088 
0089     static const QStringList stderrOutput3 = {
0090         QStringLiteral("source4.cpp:47:9: warning: unused QString [-Wclazy-unused-non-trivial-variable]"),
0091         QStringLiteral("        auto test = QString(\"%1 : %2\").arg(\"a\").arg(\"b\");"),
0092         QStringLiteral("        ^"),
0093         QStringLiteral("source4.cpp:47:47: warning: Use multi-arg instead [-Wclazy-qstring-arg]"),
0094         QStringLiteral("        auto test = QString(\"%1 : %2\").arg(\"a\").arg(\"b\");"),
0095         QStringLiteral("                                              ^"),
0096         QStringLiteral("2 warnings generated.")
0097     };
0098 
0099     jobTester.processStderrLines(stderrOutput1);
0100     QCOMPARE(jobTester.problems().size(), 0);
0101 
0102     jobTester.processStderrLines(stderrOutput2);
0103     QCOMPARE(jobTester.problems().size(), 1);
0104 
0105     jobTester.processStderrLines(stderrOutput3);
0106     QCOMPARE(jobTester.problems().size(), 3);
0107 
0108     // test common values
0109     const auto problems = jobTester.problems();
0110     for (auto problem : problems) {
0111         QCOMPARE(problem->severity(), KDevelop::IProblem::Warning);
0112         QCOMPARE(problem->source(), KDevelop::IProblem::Plugin);
0113     }
0114 
0115     // test problem description
0116     QCOMPARE(problems[0]->description(), QStringLiteral("Don't call QList::first() on temporary"));
0117     QCOMPARE(problems[1]->description(), QStringLiteral("unused QString"));
0118     QCOMPARE(problems[2]->description(), QStringLiteral("Use multi-arg instead"));
0119 
0120     // test problem location (file)
0121     QCOMPARE(problems[0]->finalLocation().document.str(), QStringLiteral("source3.cpp"));
0122     QCOMPARE(problems[1]->finalLocation().document.str(), QStringLiteral("source4.cpp"));
0123     QCOMPARE(problems[2]->finalLocation().document.str(), QStringLiteral("source4.cpp"));
0124 
0125     // test problem location (line)
0126     QCOMPARE(problems[0]->finalLocation().start().line(), 247);
0127     QCOMPARE(problems[1]->finalLocation().start().line(), 46);
0128     QCOMPARE(problems[2]->finalLocation().start().line(), 46);
0129 
0130     // test problem location (column)
0131     QCOMPARE(problems[0]->finalLocation().start().column(), 20);
0132     QCOMPARE(problems[1]->finalLocation().start().column(), 8);
0133     QCOMPARE(problems[2]->finalLocation().start().column(), 46);
0134 }
0135 
0136 QTEST_GUILESS_MAIN(TestClazyJob)
0137 
0138 #include "test_clazyjob.moc"
0139 #include "moc_test_clazyjob.cpp"