File indexing completed on 2024-05-19 05:17:52

0001 /*
0002     SPDX-FileCopyrightText: 2019-2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #ifndef TESTHELPERS_H
0007 #define TESTHELPERS_H
0008 
0009 #include <QFile>
0010 #include <QJsonDocument>
0011 #include <QProcess>
0012 #include <QTest>
0013 
0014 namespace Test {
0015     template <typename T>
0016     inline bool compareJson(const QString &refFile, const T &output, const T &ref)
0017     {
0018         if (output != ref) {
0019 #ifndef Q_OS_WIN
0020           QFile failFile(refFile + QLatin1StringView(".fail"));
0021           failFile.open(QFile::WriteOnly);
0022           failFile.write(QJsonDocument(output).toJson());
0023           failFile.close();
0024 
0025           QProcess proc;
0026           proc.setProcessChannelMode(QProcess::ForwardedChannels);
0027           proc.start(QStringLiteral("diff"),
0028                      {QStringLiteral("-u"), refFile, failFile.fileName()});
0029           proc.waitForFinished();
0030 #else
0031             qDebug().noquote() << "Got:" << QJsonDocument(output).toJson();
0032             qDebug().noquote() << "Expected:" << QJsonDocument(ref).toJson();
0033 #endif
0034             return false;
0035         }
0036         return true;
0037     }
0038 
0039     inline QByteArray readFile(const QString &fn)
0040     {
0041         QFile f(fn);
0042         f.open(QFile::ReadOnly);
0043         return f.readAll();
0044     }
0045 }
0046 
0047 #endif