Commit 2b3a9ce7 authored by Volker Krause's avatar Volker Krause
Browse files

Factor out JSON diff test helper and make it show something on Windows

parent 3410116a
Pipeline #236346 passed with stage
in 4 minutes and 58 seconds
......@@ -5,6 +5,7 @@
*/
#include "config-kitinerary.h"
#include "testhelpers.h"
#include <KItinerary/ExtractorDocumentNode>
#include <KItinerary/ExtractorDocumentNodeFactory>
......@@ -16,7 +17,6 @@
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QProcess>
#include <QTest>
using namespace KItinerary;
......@@ -87,19 +87,7 @@ private Q_SLOTS:
QFile ref(refFile);
QVERIFY(ref.open(QFile::ReadOnly));
const auto refResult = QJsonDocument::fromJson(ref.readAll()).array();
if (result != refResult) {
QFile failFile(refFile + QLatin1String(".fail"));
QVERIFY(failFile.open(QFile::WriteOnly));
failFile.write(QJsonDocument(result).toJson());
failFile.close();
QProcess proc;
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), refFile, failFile.fileName()});
QVERIFY(proc.waitForFinished());
}
QCOMPARE(result, refResult);
QVERIFY(Test::compareJson(refFile, result, refResult));
}
void testInfiniteLoop()
......
......@@ -6,6 +6,8 @@
#include <config-kitinerary.h>
#include "testhelpers.h"
#include <KItinerary/ExtractorEngine>
#include <KItinerary/ExtractorPostprocessor>
#include <KItinerary/ExtractorValidator>
......@@ -20,7 +22,6 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QObject>
#include <QProcess>
#include <QTest>
void initLocale()
......@@ -154,19 +155,7 @@ private Q_SLOTS:
QFile f(refFile);
QVERIFY(f.open(QFile::ReadOnly));
const auto refDoc = QJsonDocument::fromJson(f.readAll());
if (refDoc.array() != encodedResult) {
QFile failFile(refFile + QLatin1String(".fail"));
QVERIFY(failFile.open(QFile::WriteOnly));
failFile.write(QJsonDocument(encodedResult).toJson());
failFile.close();
QProcess proc;
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), refFile, failFile.fileName()});
QVERIFY(proc.waitForFinished());
}
QCOMPARE(refDoc.array(), encodedResult);
QVERIFY(Test::compareJson(refFile, encodedResult, refDoc.array()));
}
void testNegative()
......
/*
SPDX-FileCopyrightText: 2019-2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef TESTHELPERS_H
#define TESTHELPERS_H
#include <QFile>
#include <QJsonDocument>
#include <QProcess>
#include <QTest>
namespace Test {
template <typename T>
inline bool compareJson(const QString &refFile, const T &output, const T &ref)
{
if (output != ref) {
#ifndef Q_OS_WIN
QFile failFile(refFile + QLatin1String(".fail"));
failFile.open(QFile::WriteOnly);
failFile.write(QJsonDocument(output).toJson());
failFile.close();
QProcess proc;
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), refFile, failFile.fileName()});
proc.waitForFinished();
#else
qDebug().noquote() << "Got:" << QJsonDocument(output).toJson();
qDebug().noquote() << "Expected:" << QJsonDocument(ref).toJson();
#endif
return false;
}
return true;
}
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment