Warning, /frameworks/syntax-highlighting/autotests/test-config.h.in is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef TEST_CONFIG_H
0008 #define TEST_CONFIG_H
0009 
0010 #include <QFile>
0011 #include <QProcess>
0012 #include <QStandardPaths>
0013 #include <QString>
0014 #include <QTextStream>
0015 #include <QTest>
0016 
0017 #include <KSyntaxHighlighting/Repository>
0018 
0019 #define TESTSRCDIR "@CMAKE_CURRENT_SOURCE_DIR@"
0020 #define TESTBUILDDIR "@CMAKE_CURRENT_BINARY_DIR@"
0021 
0022 /**
0023  * init repository with right search paths, is needed if the files are not compiled in
0024  * as resources
0025  *
0026  * @param repository repository to init search paths
0027  */
0028 inline void initRepositorySearchPaths(KSyntaxHighlighting::Repository &repository)
0029 {
0030     // add extra search path for e.g. broken highlighting test
0031     repository.addCustomSearchPath(QStringLiteral(TESTSRCDIR "/input"));
0032 
0033     // handle no-resources case
0034 #ifndef HAS_SYNTAX_RESOURCE
0035     repository.addCustomSearchPath(QStringLiteral("@CMAKE_SOURCE_DIR@/data"));
0036     repository.addCustomSearchPath(QStringLiteral("@CMAKE_BINARY_DIR@/data/generated"));
0037 #endif
0038 }
0039 
0040 /**
0041  * helper to compare files
0042  * @param refFile reference file
0043  * @param outFile output file
0044  */
0045 inline void compareFiles(const QString &refFile, const QString &outFile)
0046 {
0047     /**
0048      * quick compare, all fine, if no diffs!
0049      * use text mode + text streams to avoid unix/windows mismatches
0050      */
0051     QFile ref(refFile);
0052     QFile out(outFile);
0053     QVERIFY(ref.open(QIODevice::ReadOnly | QIODevice::Text));
0054     QVERIFY(out.open(QIODevice::ReadOnly | QIODevice::Text));
0055     QTextStream refIn(&ref);
0056     QTextStream outIn(&out);
0057     const QString refContent = refIn.readAll();
0058     const QString outContent = outIn.readAll();
0059     const bool equalResults = refContent == outContent;
0060     if (equalResults) {
0061         return;
0062     }
0063 
0064     /**
0065      * elaborate diff output, if possible
0066      */
0067     const QString diffExecutable = QStandardPaths::findExecutable(QStringLiteral("diff"));
0068     if (!diffExecutable.isEmpty()) {
0069         QProcess proc;
0070         proc.setProcessChannelMode(QProcess::ForwardedChannels);
0071         proc.start(diffExecutable, {QStringLiteral("-u"), refFile, outFile});
0072         QVERIFY(proc.waitForFinished());
0073         QCOMPARE(proc.exitCode(), 0);
0074     }
0075 
0076     /**
0077      * else: trivial output of mismatching characters, e.g. for windows testing without diff
0078      */
0079     else {
0080         qDebug() << "Trivial differences output as the 'diff' executable is not in the PATH";
0081         for (int i = 0; i < refContent.size() && i < outContent.size(); ++i) {
0082             QCOMPARE(refContent[i], outContent[i]);
0083         }
0084         QCOMPARE(refContent.size(), outContent.size());
0085     }
0086 
0087     /**
0088      * if we arrive here, all lost!
0089      */
0090     QVERIFY(equalResults);
0091 }
0092 
0093 #endif // TEST_CONFIG_H