File indexing completed on 2022-09-20 14:06:27

0001 /* This file is part of KDevelop
0002 
0003    Copyright 2016 Anton Anikin <anton.anikin@htower.ru>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013    General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "test_verappjob.h"
0022 
0023 #include "job.h"
0024 
0025 #include <tests/autotestshell.h>
0026 #include <tests/testcore.h>
0027 #include <language/editor/documentrange.h>
0028 
0029 #include <QtTest/QTest>
0030 
0031 using namespace KDevelop;
0032 using namespace verapp;
0033 
0034 class JobTester : public Job
0035 {
0036     Q_OBJECT
0037 
0038 public:
0039     explicit JobTester(const Parameters& params) : Job(params)
0040     {
0041         connect(this, &JobTester::problemsDetected, this, &JobTester::problemsDetectedSlot);
0042     }
0043 
0044     using Job::postProcessStdout;
0045 
0046     QVector<KDevelop::IProblem::Ptr> problems()
0047     {
0048         return m_problems;
0049     }
0050 
0051 private:
0052     void problemsDetectedSlot(const QVector<KDevelop::IProblem::Ptr>& problems)
0053     {
0054         m_problems += problems;
0055     }
0056 
0057     QVector<KDevelop::IProblem::Ptr> m_problems;
0058 };
0059 
0060 void TestVerappJob::initTestCase()
0061 {
0062     AutoTestShell::init({"kdevverapp"});
0063     TestCore::initialize(Core::NoUi);
0064 }
0065 
0066 void TestVerappJob::cleanupTestCase()
0067 {
0068     TestCore::shutdown();
0069 }
0070 
0071 void TestVerappJob::testJob()
0072 {
0073     QStringList stdoutOutput1 = {
0074         "source1.cpp:35: L004: line is longer than 100 characters",
0075         "source1.cpp:37: T009: comma should not be preceded by whitespace",
0076         "source1.cpp:42: T011: closing curly bracket not in the same line or column",
0077     };
0078 
0079     QStringList stdoutOutput2 = {
0080         "source2.cpp:13: T012: negation operator used in its short form",
0081         "source2.cpp:30: T019: full block {} expected in the control structure"
0082     };
0083 
0084     Parameters jobParams;
0085     JobTester jobTester(jobParams);
0086 
0087     // test incremental parsing
0088     jobTester.postProcessStdout(stdoutOutput1);
0089     QCOMPARE(jobTester.problems().size(), 3);
0090 
0091     jobTester.postProcessStdout(stdoutOutput2);
0092     QCOMPARE(jobTester.problems().size(), 5);
0093 
0094     // test common values
0095     auto problems = jobTester.problems();
0096     foreach (auto problem, problems) {
0097         QCOMPARE(problem->source(), KDevelop::IProblem::Plugin);
0098         QCOMPARE(problem->severity(), KDevelop::IProblem::Warning);
0099     }
0100 
0101     // test description
0102     QCOMPARE(problems[0]->description(), QStringLiteral("L004: line is longer than 100 characters"));
0103     QCOMPARE(problems[1]->description(), QStringLiteral("T009: comma should not be preceded by whitespace"));
0104     QCOMPARE(problems[2]->description(), QStringLiteral("T011: closing curly bracket not in the same line or column"));
0105     QCOMPARE(problems[3]->description(), QStringLiteral("T012: negation operator used in its short form"));
0106     QCOMPARE(problems[4]->description(), QStringLiteral("T019: full block {} expected in the control structure"));
0107 
0108     // test problem location (file)
0109     QCOMPARE(problems[0]->finalLocation().document.str(), QStringLiteral("source1.cpp"));
0110     QCOMPARE(problems[1]->finalLocation().document.str(), QStringLiteral("source1.cpp"));
0111     QCOMPARE(problems[2]->finalLocation().document.str(), QStringLiteral("source1.cpp"));
0112 
0113     QCOMPARE(problems[3]->finalLocation().document.str(), QStringLiteral("source2.cpp"));
0114     QCOMPARE(problems[4]->finalLocation().document.str(), QStringLiteral("source2.cpp"));
0115 
0116     // test problem location (line)
0117     QCOMPARE(problems[0]->finalLocation().start().line(), 34);
0118     QCOMPARE(problems[1]->finalLocation().start().line(), 36);
0119     QCOMPARE(problems[2]->finalLocation().start().line(), 41);
0120 
0121     QCOMPARE(problems[3]->finalLocation().start().line(), 12);
0122     QCOMPARE(problems[4]->finalLocation().start().line(), 29);
0123 }
0124 
0125 QTEST_GUILESS_MAIN(TestVerappJob)
0126 
0127 #include "test_verappjob.moc"