File indexing completed on 2024-05-19 04:40:44

0001 /*
0002     SPDX-FileCopyrightText: 2016 Peje Nilsson <peje66@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "test_cppcheckparser.h"
0008 
0009 #include <QTest>
0010 #include <tests/testcore.h>
0011 #include <tests/autotestshell.h>
0012 
0013 #include <language/editor/documentrange.h>
0014 #include <shell/problem.h>
0015 
0016 #include "parser.h"
0017 
0018 using namespace KDevelop;
0019 using namespace cppcheck;
0020 
0021 void TestCppcheckParser::initTestCase()
0022 {
0023     AutoTestShell::init({"kdevcppcheck"});
0024     TestCore::initialize(Core::NoUi);
0025 }
0026 
0027 void TestCppcheckParser::cleanupTestCase()
0028 {
0029     TestCore::shutdown();
0030 }
0031 
0032 void TestCppcheckParser::testParser()
0033 {
0034     const QString cppcheck_example_output = QStringLiteral(
0035         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
0036         "<results version=\"2\">\n"
0037         "    <cppcheck version=\"1.72\"/>"
0038         "    <errors>"
0039         "        <error id=\"memleak\" severity=\"error\" msg=\"Memory leak: ej\" verbose=\"Memory leak: ej\" cwe=\"401\">"
0040         "            <location file=\"/kdesrc/kdev-cppcheck/plugin.cpp\" line=\"169\"/>"
0041         "        </error>"
0042         "        <error id=\"redundantAssignment\" severity=\"performance\""
0043         "               msg=\"location_test_msg\" verbose=\"location_test_verbose\">"
0044         "            <location file=\"location_test.cpp\" line=\"120\"/>"
0045         "            <location file=\"location_test.cpp\" line=\"100\"/>"
0046         "        </error>"
0047         "        <error id=\"variableScope\" severity=\"style\" inconclusive=\"true\""
0048         "               msg=\"The scope of the variable...\""
0049         "               verbose=\"...Here is an example...:\\012void f(int x)\\012{\\012    int i = 0;\\012}\\012...\">"
0050         "            <location file=\"html_pre_test.cpp\" line=\"41\"/>"
0051         "        </error>"
0052         "    </errors>"
0053         "</results>");
0054 
0055     cppcheck::CppcheckParser parser;
0056     parser.addData(cppcheck_example_output);
0057 
0058     const auto problems = parser.parse();
0059     QVERIFY(!problems.empty());
0060 
0061     IProblem::Ptr p = problems[0];
0062     QCOMPARE(p->description(), QStringLiteral("Memory leak: ej"));
0063     QCOMPARE(p->explanation(), QStringLiteral("<html>Memory leak: ej</html>"));
0064     QCOMPARE(p->finalLocation().document.str(), QStringLiteral("/kdesrc/kdev-cppcheck/plugin.cpp"));
0065     QCOMPARE(p->finalLocation().start().line()+1, 169);
0066     QCOMPARE(p->severity(), IProblem::Error);
0067     QCOMPARE(p->source(), IProblem::Plugin);
0068 
0069     // test problem with 2 <location> elements
0070     p = problems[1];
0071     QCOMPARE(p->description(), QStringLiteral("(performance) location_test_msg"));
0072     QCOMPARE(p->explanation(), QStringLiteral("<html>location_test_verbose</html>"));
0073     QCOMPARE(p->finalLocation().document.str(), QStringLiteral("location_test.cpp"));
0074     QCOMPARE(p->finalLocation().start().line()+1, 120);
0075     QCOMPARE(p->severity(), IProblem::Hint);
0076     QCOMPARE(p->source(), IProblem::Plugin);
0077 
0078     // test problem with '\\012' tokens in verbose message
0079     p = problems[2];
0080     QCOMPARE(p->description(), QStringLiteral("(style, inconclusive) The scope of the variable..."));
0081     QCOMPARE(p->explanation(), QStringLiteral("<html>...Here is an example...:<pre>void f(int x)\n{\n    int i = 0;\n}</pre><br>...</html>"));
0082     QCOMPARE(p->finalLocation().document.str(), QStringLiteral("html_pre_test.cpp"));
0083     QCOMPARE(p->finalLocation().start().line()+1, 41);
0084     QCOMPARE(p->severity(), IProblem::Hint);
0085     QCOMPARE(p->source(), IProblem::Plugin);
0086 }
0087 
0088 QTEST_GUILESS_MAIN(TestCppcheckParser)
0089 
0090 #include "moc_test_cppcheckparser.cpp"