File indexing completed on 2024-04-28 04:01:09

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "test-config.h"
0008 
0009 #include <KSyntaxHighlighting/Definition>
0010 #include <KSyntaxHighlighting/Repository>
0011 #include <KSyntaxHighlighting/Theme>
0012 #include <htmlhighlighter.h>
0013 
0014 #include <QDir>
0015 #include <QObject>
0016 #include <QStandardPaths>
0017 #include <QTest>
0018 
0019 using namespace KSyntaxHighlighting;
0020 
0021 class HTMLHighlighterTest : public QObject
0022 {
0023     Q_OBJECT
0024 public:
0025     explicit HTMLHighlighterTest(QObject *parent = nullptr)
0026         : QObject(parent)
0027         , m_repo(nullptr)
0028     {
0029     }
0030 
0031 private:
0032     Repository *m_repo;
0033 
0034 private Q_SLOTS:
0035     void initTestCase()
0036     {
0037         QStandardPaths::setTestModeEnabled(true);
0038         m_repo = new Repository;
0039         initRepositorySearchPaths(*m_repo);
0040     }
0041 
0042     void cleanupTestCase()
0043     {
0044         delete m_repo;
0045         m_repo = nullptr;
0046     }
0047 
0048     void testHighlight_data()
0049     {
0050         QTest::addColumn<QString>("inFile");
0051         QTest::addColumn<QString>("outFile");
0052         QTest::addColumn<QString>("refFile");
0053         QTest::addColumn<QString>("outDarkFile");
0054         QTest::addColumn<QString>("refDarkFile");
0055         QTest::addColumn<QString>("syntax");
0056 
0057         const QDir dir(QStringLiteral(TESTSRCDIR "/input"));
0058         for (const auto &fileName : dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable | QDir::Hidden, QDir::Name)) {
0059             // skip .clang-format file we use to avoid formatting test files
0060             if (fileName == QLatin1String(".clang-format")) {
0061                 continue;
0062             }
0063 
0064             const auto inFile = dir.absoluteFilePath(fileName);
0065             if (inFile.endsWith(QLatin1String(".syntax"))) {
0066                 continue;
0067             }
0068 
0069             QString syntax;
0070             QFile syntaxOverride(inFile + QStringLiteral(".syntax"));
0071             if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) {
0072                 syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed();
0073             }
0074 
0075             QTest::newRow(fileName.toUtf8().constData()) << inFile << (QStringLiteral(TESTBUILDDIR "/html.output/") + fileName + QStringLiteral(".html"))
0076                                                          << (QStringLiteral(TESTSRCDIR "/html/") + fileName + QStringLiteral(".html"))
0077                                                          << (QStringLiteral(TESTBUILDDIR "/html.output/") + fileName + QStringLiteral(".dark.html"))
0078                                                          << (QStringLiteral(TESTSRCDIR "/html/") + fileName + QStringLiteral(".dark.html")) << syntax;
0079         }
0080 
0081         // cleanup before we test
0082         QDir(QStringLiteral(TESTBUILDDIR "/html.output/")).removeRecursively();
0083         QDir().mkpath(QStringLiteral(TESTBUILDDIR "/html.output/"));
0084     }
0085 
0086     void testHighlight()
0087     {
0088         QFETCH(QString, inFile);
0089         QFETCH(QString, outFile);
0090         QFETCH(QString, refFile);
0091         QFETCH(QString, outDarkFile);
0092         QFETCH(QString, refDarkFile);
0093         QFETCH(QString, syntax);
0094         QVERIFY(m_repo);
0095 
0096         // get the matching definitions
0097         const auto def = syntax.isEmpty() ? m_repo->definitionForFileName(inFile) : m_repo->definitionForName(syntax);
0098         QVERIFY(def.isValid());
0099 
0100         // we try both light and dark themes
0101         for (int i = 0; i < 2; ++i) {
0102             HtmlHighlighter highlighter;
0103             highlighter.setTheme(m_repo->defaultTheme((i == 0) ? Repository::LightTheme : Repository::DarkTheme));
0104             QVERIFY(highlighter.theme().isValid());
0105             highlighter.setDefinition(def);
0106             highlighter.setOutputFile((i == 0) ? outFile : outDarkFile);
0107             highlighter.highlightFile(inFile);
0108 
0109             // compare results
0110             compareFiles((i == 0) ? refFile : refDarkFile, (i == 0) ? outFile : outDarkFile);
0111         }
0112     }
0113 };
0114 
0115 QTEST_GUILESS_MAIN(HTMLHighlighterTest)
0116 
0117 #include "htmlhighlighter_test.moc"