File indexing completed on 2024-12-22 04:44:36
0001 /* 0002 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include <QDebug> 0010 #include <QJsonArray> 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QProcess> 0014 #include <QString> 0015 #include <QTest> 0016 0017 namespace AutoTestHelper 0018 { 0019 void diffFile(const QString &refFile, const QString &generatedFile) 0020 { 0021 QProcess proc; 0022 #ifdef _WIN32 0023 QStringList args = QStringList() << QStringLiteral("Compare-Object") << QString(QStringLiteral("(Get-Content %1)")).arg(refFile) 0024 << QString(QStringLiteral("(Get-Content %1)")).arg(generatedFile); 0025 0026 proc.start(QStringLiteral("powershell"), args); 0027 QVERIFY(proc.waitForFinished()); 0028 0029 auto pStdOut = proc.readAllStandardOutput(); 0030 if (pStdOut.size()) { 0031 qDebug() << "Files are different, diff output message:\n" << pStdOut.toStdString().c_str(); 0032 } 0033 0034 QCOMPARE(pStdOut.size(), 0); 0035 #else 0036 // compare to reference file 0037 const QStringList args = QStringList() << QStringLiteral("-u") << refFile << generatedFile; 0038 0039 proc.setProcessChannelMode(QProcess::ForwardedChannels); 0040 proc.start(QStringLiteral("diff"), args); 0041 QVERIFY(proc.waitForFinished()); 0042 QCOMPARE(proc.exitCode(), 0); 0043 #endif 0044 } 0045 0046 QJsonDocument loadJsonDocument(const QString &originalJsonFile) 0047 { 0048 QFile f(originalJsonFile); 0049 [&]() { 0050 QVERIFY(f.open(QIODevice::ReadOnly)); 0051 }(); 0052 const auto content = f.readAll(); 0053 f.close(); 0054 0055 QJsonParseError error; 0056 const auto doc = QJsonDocument::fromJson(content, &error); 0057 [&]() { 0058 QVERIFY2(!error.error, qPrintable(error.errorString())); 0059 }(); 0060 return doc; 0061 } 0062 0063 QJsonObject loadJsonObject(const QString &originalJsonFile) 0064 { 0065 return loadJsonDocument(originalJsonFile).object(); 0066 } 0067 0068 QJsonArray loadJsonArrayObject(const QString &originalJsonFile) 0069 { 0070 return loadJsonDocument(originalJsonFile).array(); 0071 } 0072 }