File indexing completed on 2025-01-19 04:46:48

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "diffhighlightertest.h"
0008 #include "../highlighter/highlighter.h"
0009 
0010 #include <KSyntaxHighlighting/Definition>
0011 #include <KSyntaxHighlighting/Repository>
0012 #include <KSyntaxHighlighting/Theme>
0013 
0014 #include <QProcess>
0015 #include <QStandardPaths>
0016 #include <QTest>
0017 
0018 DiffHighlighterTest::DiffHighlighterTest(QObject *parent)
0019     : QObject(parent)
0020 {
0021     QStandardPaths::setTestModeEnabled(true);
0022 }
0023 
0024 QString readDiffFile(const QString &diffFile)
0025 {
0026     QFile file(diffFile);
0027     file.open(QIODevice::ReadOnly);
0028     Q_ASSERT(file.isOpen());
0029     const QString data = QString::fromUtf8(file.readAll());
0030     return data;
0031 }
0032 
0033 void DiffHighlighterTest::shouldGenerateDiff_data()
0034 {
0035     QTest::addColumn<QString>("input");
0036     QTest::newRow("test1") << QStringLiteral("test1");
0037     QTest::newRow("kcontact1") << QStringLiteral("kcontact1");
0038     QTest::newRow("diff-akonadiconsole-16.12-master") << QStringLiteral("diff-akonadiconsole-16.12-master");
0039 }
0040 
0041 void DiffHighlighterTest::shouldGenerateDiff()
0042 {
0043     QFETCH(QString, input);
0044 
0045     const QString originalFile = QStringLiteral(DIFF_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral(".diff");
0046     const QString refFile = QStringLiteral(DIFF_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral("-ref.diff");
0047     const QString generatedFile = QStringLiteral(DIFF_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral("-generated.diff");
0048     QString diff = readDiffFile(originalFile);
0049 
0050     // Create generated file
0051     QFile f(generatedFile);
0052     QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0053     QTextStream s(&f);
0054 
0055     KSyntaxHighlighting::Repository repo;
0056     Highlighter highLighter(&s);
0057     highLighter.setDefinition(repo.definitionForName(QStringLiteral("Diff")));
0058     highLighter.setTheme(repo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
0059     highLighter.highlight(diff);
0060 
0061     s.flush();
0062     f.close();
0063 
0064     // compare to reference file
0065     QStringList args = QStringList() << QStringLiteral("-u") << refFile << generatedFile;
0066     QProcess proc;
0067     proc.setProcessChannelMode(QProcess::ForwardedChannels);
0068     proc.start(QStringLiteral("diff"), args);
0069     QVERIFY(proc.waitForFinished());
0070 
0071     QCOMPARE(proc.exitCode(), 0);
0072 }
0073 
0074 QTEST_MAIN(DiffHighlighterTest)
0075 
0076 #include "moc_diffhighlightertest.cpp"