File indexing completed on 2024-05-12 15:34:03

0001 /*
0002     Tests for KConfig Compiler
0003 
0004     SPDX-FileCopyrightText: 2005 Duncan Mac-Vicar <duncan@kde.org>
0005     SPDX-FileCopyrightText: 2009 Pino Toscano <pino@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "kconfigcompiler_test.h"
0011 #include <QDir>
0012 #include <QProcess>
0013 #include <QString>
0014 #include <QTest>
0015 #include <qstandardpaths.h>
0016 
0017 // QT5 TODO QTEST_GUILESS_MAIN(KConfigCompiler_Test)
0018 QTEST_MAIN(KConfigCompiler_Test)
0019 
0020 typedef const char *CompilerTestSet[];
0021 
0022 static CompilerTestSet testCases = {"test1.cpp",
0023                                     "test1.h",
0024                                     "test2.cpp",
0025                                     "test2.h",
0026                                     "test3.cpp",
0027                                     "test3.h",
0028                                     "test3a.cpp",
0029                                     "test3a.h",
0030                                     "test4.cpp",
0031                                     "test4.h",
0032                                     "test5.cpp",
0033                                     "test5.h",
0034                                     "test6.cpp",
0035                                     "test6.h",
0036                                     "test7.cpp",
0037                                     "test7.h",
0038                                     "test8a.cpp",
0039                                     "test8a.h",
0040                                     "test8b.cpp",
0041                                     "test8b.h",
0042                                     "test8c.cpp",
0043                                     "test8c.h",
0044                                     "test9.h",
0045                                     "test9.cpp",
0046                                     "test10.h",
0047                                     "test10.cpp",
0048                                     "test11.h",
0049                                     "test11.cpp",
0050                                     "test11a.h",
0051                                     "test11a.cpp",
0052                                     "test12.h",
0053                                     "test12.cpp",
0054                                     "test13.h",
0055                                     "test13.cpp",
0056                                     "test_dpointer.cpp",
0057                                     "test_dpointer.h",
0058                                     "test_qdebugcategory.cpp",
0059                                     "test_qdebugcategory.h",
0060                                     "test_signal.cpp",
0061                                     "test_signal.h",
0062                                     "test_notifiers.cpp",
0063                                     "test_notifiers.h",
0064                                     "test_translation_kde.cpp",
0065                                     "test_translation_kde.h",
0066                                     "test_translation_kde_domain.cpp",
0067                                     "test_translation_kde_domain.h",
0068                                     "test_translation_qt.cpp",
0069                                     "test_translation_qt.h",
0070                                     "test_emptyentries.cpp",
0071                                     "test_emptyentries.h",
0072                                     "test_properties_minmax.cpp",
0073                                     "test_properties_minmax.h",
0074                                     "test_param_minmax.cpp",
0075                                     "test_param_minmax.h",
0076                                     "test_subgroups.cpp",
0077                                     "test_subgroups.h",
0078                                     nullptr};
0079 
0080 static CompilerTestSet testCasesToRun = {"test1",
0081                                          "test2",
0082                                          "test3",
0083                                          "test3a",
0084                                          "test4",
0085                                          "test5",
0086                                          "test6",
0087                                          "test7",
0088                                          "test8",
0089                                          "test9",
0090                                          "test10",
0091                                          "test11",
0092                                          "test12",
0093                                          "test13",
0094                                          "test_enums_and_properties",
0095                                          "test_dpointer",
0096                                          "test_qdebugcategory",
0097                                          "test_signal",
0098                                          "test_translation_kde",
0099                                          "test_translation_kde_domain",
0100                                          "test_translation_qt",
0101                                          "test_emptyentries",
0102                                          "test_properties_minmax",
0103                                          "test_param_minmax",
0104                                          "test_subgroups",
0105                                          nullptr};
0106 
0107 #if 0
0108 static CompilerTestSet willFailCases = {
0109     // where is that QDir coming from?
0110     //"test9.cpp", NULL
0111     NULL
0112 };
0113 #endif
0114 
0115 void KConfigCompiler_Test::initTestCase()
0116 {
0117     m_diffExe = QStandardPaths::findExecutable(QStringLiteral("diff"));
0118     if (m_diffExe.isEmpty()) {
0119         qDebug() << "diff command not found, detailed info on comparison failure will not be available.";
0120     }
0121 }
0122 
0123 void KConfigCompiler_Test::testBaselineComparison_data()
0124 {
0125     QTest::addColumn<QString>("testName");
0126 
0127     for (const char **it = testCases; *it; ++it) {
0128         QTest::newRow(*it) << QString::fromLatin1(*it);
0129     }
0130 }
0131 
0132 void KConfigCompiler_Test::testBaselineComparison()
0133 {
0134     QFETCH(QString, testName);
0135 
0136     QFile file(QFINDTESTDATA(testName));
0137     QFile fileRef(QFINDTESTDATA(testName + QLatin1String(".ref")));
0138 
0139     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0140         qWarning() << "Failed to open" << file.fileName() << "(" << testName << ")";
0141         QFAIL("Can't open file for comparison");
0142     }
0143     if (!fileRef.open(QIODevice::ReadOnly | QIODevice::Text)) {
0144         qWarning() << "Failed to open" << fileRef.fileName() << "(" << testName << ".ref )";
0145         QFAIL("Can't open file for comparison");
0146     }
0147     const QByteArray content = file.readAll();
0148     const QByteArray contentRef = fileRef.readAll();
0149 
0150     if (content != contentRef) {
0151         appendFileDiff(fileRef.fileName(), file.fileName());
0152     }
0153 
0154     QVERIFY(content == contentRef);
0155 }
0156 
0157 void KConfigCompiler_Test::testRunning_data()
0158 {
0159     QTest::addColumn<QString>("testName");
0160 
0161     for (const char **it = testCasesToRun; *it; ++it) {
0162         QTest::newRow(*it) << QString::fromLatin1(*it);
0163     }
0164 }
0165 
0166 void KConfigCompiler_Test::testRunning()
0167 {
0168     QFETCH(QString, testName);
0169 
0170 #ifdef Q_OS_WIN
0171     testName += QLatin1String{".exe"};
0172 #endif
0173 
0174     QString program = QFINDTESTDATA(testName);
0175     QVERIFY2(!program.isEmpty(), qPrintable(testName + QLatin1String(" must exist!")));
0176     QVERIFY2(QFile::exists(program), qPrintable(program + QLatin1String(" must exist!")));
0177     QProcess process;
0178     process.start(program, QStringList(), QIODevice::ReadOnly);
0179     if (process.waitForStarted()) {
0180         QVERIFY(process.waitForFinished());
0181     }
0182     QCOMPARE((int)process.error(), (int)QProcess::UnknownError);
0183     QCOMPARE(process.exitCode(), 0);
0184 }
0185 
0186 void KConfigCompiler_Test::appendFileDiff(const QString &oldFile, const QString &newFile)
0187 {
0188     if (m_diffExe.isEmpty()) {
0189         return;
0190     }
0191 
0192     QStringList args({QStringLiteral("-u"), QFileInfo(oldFile).absoluteFilePath(), QFileInfo(newFile).absoluteFilePath()});
0193 
0194     QProcess process;
0195     process.start(m_diffExe, args, QIODevice::ReadOnly);
0196     process.waitForStarted();
0197     process.waitForFinished();
0198 
0199     if (process.exitCode() == 1) {
0200         const QString diffFileName = oldFile + QStringLiteral(".diff");
0201         QFile diffFile(diffFileName);
0202         QVERIFY2(diffFile.open(QIODevice::WriteOnly), qPrintable(QLatin1String("Could not save diff file for ") + oldFile));
0203         diffFile.write(process.readAllStandardOutput());
0204 
0205         // force a failure to print the flename where we stored the diff.
0206         QVERIFY2(false, qPrintable(QLatin1String("This test failed, look at the following file for details: ") + diffFileName));
0207     }
0208 }
0209 
0210 #include "moc_kconfigcompiler_test.cpp"