File indexing completed on 2024-05-12 04:38:19

0001 /*
0002     SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 #include <shell/problem.h>
0009 #include <KLocalizedString>
0010 
0011 #include <serialization/itemrepositoryregistry.h>
0012 #include <interfaces/icore.h>
0013 #include <language/editor/documentrange.h>
0014 #include <tests/testcore.h>
0015 #include <tests/autotestshell.h>
0016 
0017 using namespace KDevelop;
0018 
0019 class TestDetectedProblem : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void initTestCase();
0024     void cleanupTestCase();
0025 
0026     void testSource();
0027     void testSeverity();
0028     void testDescription();
0029     void testExplanation();
0030     void testFinalLocation();
0031     void testDiagnostics();
0032     void testPluginName();
0033 
0034 private:
0035     IProblem::Ptr m_problem;
0036 };
0037 
0038 void TestDetectedProblem::initTestCase()
0039 {
0040     AutoTestShell::init();
0041     TestCore::initialize(Core::NoUi);
0042 
0043     m_problem = new DetectedProblem();
0044 }
0045 
0046 void TestDetectedProblem::cleanupTestCase()
0047 {
0048     TestCore::shutdown();
0049 }
0050 
0051 struct Source
0052 {
0053     IProblem::Source source;
0054     QString sourceString;
0055 };
0056 
0057 void TestDetectedProblem::testSource()
0058 {
0059     static const Source sources[] =
0060     {
0061         { IProblem::Unknown, i18n("Unknown") },
0062         { IProblem::Disk, i18n("Disk") },
0063         { IProblem::Preprocessor, i18n("Preprocessor") },
0064         { IProblem::Lexer, i18n("Lexer") },
0065         { IProblem::Parser, i18n("Parser") },
0066         { IProblem::DUChainBuilder, i18n("DuchainBuilder") },
0067         { IProblem::SemanticAnalysis, i18n("Semantic analysis") },
0068         { IProblem::ToDo, i18n("Todo") },
0069         { IProblem::Plugin, i18n("Plugin") }
0070     };
0071 
0072     for (auto& source : sources) {
0073         m_problem->setSource(source.source);
0074 
0075         QCOMPARE(source.source, m_problem->source());
0076         QCOMPARE(source.sourceString, m_problem->sourceString());
0077     }
0078 
0079 }
0080 
0081 struct Severity
0082 {
0083     IProblem::Severity severity;
0084     QString severityString;
0085 };
0086 
0087 void TestDetectedProblem::testSeverity()
0088 {
0089     static const Severity severities[] =
0090     {
0091         { IProblem::Error, i18n("Error") },
0092         { IProblem::Warning, i18n("Warning") },
0093         { IProblem::Hint, i18n("Hint") },
0094     };
0095 
0096     for (auto& severity : severities) {
0097         m_problem->setSeverity(severity.severity);
0098 
0099         QCOMPARE(severity.severity, m_problem->severity());
0100         QCOMPARE(severity.severityString, m_problem->severityString());
0101     }
0102 }
0103 
0104 void TestDetectedProblem::testDescription()
0105 {
0106     QString TESTDESCRIPTION = QStringLiteral("Just a test description");
0107 
0108     m_problem->setDescription(TESTDESCRIPTION);
0109     QCOMPARE(TESTDESCRIPTION, m_problem->description());
0110 }
0111 
0112 void TestDetectedProblem::testExplanation()
0113 {
0114     QString TESTEXPLANATION = QStringLiteral("Just a test explanation");
0115 
0116     m_problem->setExplanation(TESTEXPLANATION);
0117     QCOMPARE(TESTEXPLANATION, m_problem->explanation());
0118 }
0119 
0120 void TestDetectedProblem::testFinalLocation()
0121 {
0122     QString TESTPATH = QStringLiteral("/just/a/bogus/path/to/a/fake/document");
0123     int TESTLINE     = 9001;
0124     int TESTCOLUMN   = 1337;
0125 
0126     DocumentRange range;
0127     range.document = IndexedString(TESTPATH);
0128     range.setBothLines(TESTLINE);
0129     range.setBothColumns(TESTCOLUMN);
0130 
0131     m_problem->setFinalLocation(range);
0132 
0133     QCOMPARE(TESTPATH, m_problem->finalLocation().document.str());
0134     QCOMPARE(TESTLINE, m_problem->finalLocation().start().line());
0135     QCOMPARE(TESTLINE, m_problem->finalLocation().end().line());
0136     QCOMPARE(TESTCOLUMN, m_problem->finalLocation().start().column());
0137     QCOMPARE(TESTCOLUMN, m_problem->finalLocation().end().column());
0138 }
0139 
0140 void TestDetectedProblem::testDiagnostics()
0141 {
0142     QString one   = QStringLiteral("One");
0143     QString two   = QStringLiteral("Two");
0144     QString three = QStringLiteral("Three");
0145 
0146     IProblem::Ptr p1(new DetectedProblem());
0147     IProblem::Ptr p2(new DetectedProblem());
0148     IProblem::Ptr p3(new DetectedProblem());
0149 
0150     p1->setDescription(one);
0151     p2->setDescription(two);
0152     p3->setDescription(three);
0153 
0154     QCOMPARE(m_problem->diagnostics().size(), 0);
0155 
0156     m_problem->addDiagnostic(p1);
0157     m_problem->addDiagnostic(p2);
0158     m_problem->addDiagnostic(p3);
0159 
0160     QCOMPARE(m_problem->diagnostics().size(), 3);
0161     QCOMPARE(m_problem->diagnostics().at(0)->description(), one);
0162     QCOMPARE(m_problem->diagnostics().at(1)->description(), two);
0163     QCOMPARE(m_problem->diagnostics().at(2)->description(), three);
0164 
0165     m_problem->clearDiagnostics();
0166     QCOMPARE(0, m_problem->diagnostics().size());
0167 
0168     QVector<IProblem::Ptr> diags;
0169     diags.push_back(p3);
0170     diags.push_back(p2);
0171     diags.push_back(p1);
0172     m_problem->setDiagnostics(diags);
0173 
0174     QCOMPARE(m_problem->diagnostics().size(), 3);
0175     QCOMPARE(m_problem->diagnostics().at(2)->description(), one);
0176     QCOMPARE(m_problem->diagnostics().at(1)->description(), two);
0177     QCOMPARE(m_problem->diagnostics().at(0)->description(), three);
0178 
0179     m_problem->clearDiagnostics();
0180     QCOMPARE(m_problem->diagnostics().size(), 0);
0181 }
0182 
0183 void TestDetectedProblem::testPluginName()
0184 {
0185     DetectedProblem p1(QStringLiteral("Plugin1"));
0186     DetectedProblem p2(QStringLiteral("Plugin2"));
0187     DetectedProblem p3(QStringLiteral(""));
0188     DetectedProblem p4;
0189 
0190     QCOMPARE(p1.source(), IProblem::Plugin);
0191     QCOMPARE(p2.source(), IProblem::Plugin);
0192     QCOMPARE(p3.source(), IProblem::Plugin);
0193     QCOMPARE(p4.source(), IProblem::Unknown);
0194 
0195     QCOMPARE(p1.sourceString(), QStringLiteral("Plugin1"));
0196     QCOMPARE(p2.sourceString(), QStringLiteral("Plugin2"));
0197     QCOMPARE(p3.sourceString(), QStringLiteral(""));
0198     QCOMPARE(p4.sourceString(), i18n("Unknown"));
0199 
0200     p4.setSource(IProblem::Plugin);
0201     QCOMPARE(p4.source(), IProblem::Plugin);
0202     QCOMPARE(p4.sourceString(), i18n("Plugin"));
0203 }
0204 
0205 QTEST_MAIN(TestDetectedProblem)
0206 
0207 #include "test_detectedproblem.moc"