Commit 13e4f8c9 authored by Volker Krause's avatar Volker Krause
Browse files

Factor out test helper functions for showing JSON diffs

parent 3b1bf179
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelpers.h"
#include <KPublicTransport/Journey>
#include <KPublicTransport/HafasMgateParser>
#include <KPublicTransport/Stopover>
......@@ -23,14 +25,6 @@ using namespace KPublicTransport;
class HafasMgateParserTest : public QObject
{
Q_OBJECT
private:
QByteArray readFile(const QString &fn)
{
QFile f(fn);
f.open(QFile::ReadOnly);
return f.readAll();
}
private Q_SLOTS:
void initTestCase()
{
......@@ -53,21 +47,21 @@ private Q_SLOTS:
{
HafasMgateParser p;
p.parseLocations(readFile(input));
p.parseLocations(Test::readFile(input));
QCOMPARE(p.error(), Reply::UnknownError);
QVERIFY(!p.errorMessage().isEmpty());
}
{
HafasMgateParser p;
p.parseDepartures(readFile(input));
p.parseDepartures(Test::readFile(input));
QCOMPARE(p.error(), Reply::UnknownError);
QVERIFY(!p.errorMessage().isEmpty());
}
{
HafasMgateParser p;
p.parseJourneys(readFile(input));
p.parseJourneys(Test::readFile(input));
QCOMPARE(p.error(), Reply::UnknownError);
QVERIFY(!p.errorMessage().isEmpty());
}
......@@ -76,7 +70,7 @@ private Q_SLOTS:
void testParseDepartureError()
{
HafasMgateParser p;
const auto res = p.parseDepartures(readFile(s(SOURCE_DIR "/data/hafas/stationboard-error-response.json")));
const auto res = p.parseDepartures(Test::readFile(s(SOURCE_DIR "/data/hafas/stationboard-error-response.json")));
QVERIFY(res.empty());
QCOMPARE(p.error(), Reply::NotFoundError);
QVERIFY(!p.errorMessage().isEmpty());
......@@ -122,10 +116,10 @@ private Q_SLOTS:
HafasMgateParser p;
p.setLocationIdentifierTypes(QStringLiteral("unit-test"));
const auto res = p.parseDepartures(readFile(inFileName));
const auto res = p.parseDepartures(Test::readFile(inFileName));
const auto jsonRes = Stopover::toJson(res);
const auto ref = QJsonDocument::fromJson(readFile(refFileName)).array();
const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
if (jsonRes != ref) {
qDebug().noquote() << QJsonDocument(jsonRes).toJson();
......@@ -154,16 +148,13 @@ private Q_SLOTS:
HafasMgateParser p;
p.setLocationIdentifierTypes(QStringLiteral("unit-test"));
const auto res = p.parseJourneys(readFile(inFileName));
const auto res = p.parseJourneys(Test::readFile(inFileName));
const auto jsonRes = Journey::toJson(res);
const auto ref = QJsonDocument::fromJson(readFile(refFileName)).array();
const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
if (jsonRes != ref) {
qDebug().noquote() << QJsonDocument(jsonRes).toJson();
}
QVERIFY(!jsonRes.empty());
QCOMPARE(jsonRes, ref);
QVERIFY(Test::compareJson(refFileName, jsonRes, ref));
}
};
......
......@@ -4,6 +4,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelpers.h"
#include "ifopt/ifoptutil.cpp"
#include "backends/ivvassparser.cpp"
......@@ -25,14 +26,6 @@ using namespace KPublicTransport;
class IvvAssParserTest : public QObject
{
Q_OBJECT
private:
QByteArray readFile(const QString &fn)
{
QFile f(fn);
f.open(QFile::ReadOnly);
return f.readAll();
}
private Q_SLOTS:
void testParseLocations_data()
{
......@@ -53,11 +46,11 @@ private Q_SLOTS:
QFETCH(QString, outFileName);
IvvAssParser p(QTimeZone("Europe/Berlin"), s("vrs"));
const auto result = p.parseLocations(readFile(inFileName));
const auto result = p.parseLocations(Test::readFile(inFileName));
QVERIFY(!result.empty());
QVERIFY(p.errorMessage.isEmpty());
const auto resultJson = Location::toJson(result);
const auto resultRef = QJsonDocument::fromJson(readFile(outFileName)).array();
const auto resultRef = QJsonDocument::fromJson(Test::readFile(outFileName)).array();
if (resultJson != resultRef) {
qDebug().noquote() << QJsonDocument(resultJson).toJson();
}
......@@ -81,11 +74,11 @@ private Q_SLOTS:
QFETCH(QString, outFileName);
IvvAssParser p(QTimeZone("Europe/Berlin"), s("vrs"));
const auto result = p.parseStopovers(readFile(inFileName));
const auto result = p.parseStopovers(Test::readFile(inFileName));
QVERIFY(!result.empty());
QVERIFY(p.errorMessage.isEmpty());
const auto resultJson = Stopover::toJson(result);
const auto resultRef = QJsonDocument::fromJson(readFile(outFileName)).array();
const auto resultRef = QJsonDocument::fromJson(Test::readFile(outFileName)).array();
if (resultJson != resultRef) {
qDebug().noquote() << QJsonDocument(resultJson).toJson();
}
......@@ -115,16 +108,13 @@ private Q_SLOTS:
QFETCH(QString, outFileName);
IvvAssParser p(QTimeZone("Europe/Berlin"), s("vrs"));
const auto result = p.parseJourneys(readFile(inFileName));
const auto result = p.parseJourneys(Test::readFile(inFileName));
QVERIFY(!result.empty());
QVERIFY(p.errorMessage.isEmpty());
const auto resultJson = Journey::toJson(result);
const auto resultRef = QJsonDocument::fromJson(readFile(outFileName)).array();
if (resultJson != resultRef) {
qDebug().noquote() << QJsonDocument(resultJson).toJson();
}
const auto resultRef = QJsonDocument::fromJson(Test::readFile(outFileName)).array();
QVERIFY(!resultJson.isEmpty());
QCOMPARE(resultJson, resultRef);
QVERIFY(Test::compareJson(outFileName, resultJson, resultRef));
}
void testParseError()
......
......@@ -4,6 +4,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelpers.h"
#include "backends/oebbvehiclelayoutparser.cpp"
#include "uic/uicrailwaycoach.cpp"
......@@ -11,7 +12,6 @@
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonValue>
#include <QProcess>
#include <QTest>
#include <QTimeZone>
......@@ -22,30 +22,6 @@ using namespace KPublicTransport;
class OebbParserTest : public QObject
{
Q_OBJECT
private:
QByteArray readFile(const QString &fn)
{
QFile f(fn);
f.open(QFile::ReadOnly);
return f.readAll();
}
template <typename T>
inline void diffJson(const QString &refFile, const T &output, const T &ref)
{
if (output != ref) {
QFile failFile(refFile + QLatin1String(".fail"));
QVERIFY(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()});
QVERIFY(proc.waitForFinished());
}
}
private Q_SLOTS:
void initTestCase()
{
......@@ -77,14 +53,11 @@ private Q_SLOTS:
KPublicTransport::OebbVehicleLayoutParser parser;
QVERIFY(parser.parse(readFile(inFileName)));
QVERIFY(parser.parse(Test::readFile(inFileName)));
const auto departureJson = Stopover::toJson(parser.stopover);
const auto departureRef = QJsonDocument::fromJson(readFile(departureFileName)).object();
if (departureJson != departureRef) {
diffJson(departureFileName, departureJson, departureRef);
}
const auto departureRef = QJsonDocument::fromJson(Test::readFile(departureFileName)).object();
QVERIFY(!departureJson.isEmpty());
QCOMPARE(departureJson, departureRef);
QVERIFY(Test::compareJson(departureFileName, departureJson, departureRef));
}
};
......
......@@ -4,6 +4,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelpers.h"
#include "backends/opentripplannerparser.h"
#include <KPublicTransport/Journey>
......@@ -16,7 +17,6 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QProcess>
#include <QTest>
#include <QTimeZone>
......@@ -29,33 +29,6 @@ using RentalVehicleNetworkMap = QHash<QString, RentalVehicleNetwork>;
class OtpParserTest : public QObject
{
Q_OBJECT
private:
QByteArray readFile(const QString &fn)
{
if (fn.isEmpty()) {
return {};
}
QFile f(fn);
f.open(QFile::ReadOnly);
return f.readAll();
}
template <typename T>
inline void diffJson(const QString &refFile, const T &output, const T &ref)
{
if (output != ref) {
QFile failFile(refFile + QLatin1String(".fail"));
QVERIFY(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()});
QVERIFY(proc.waitForFinished());
}
}
private Q_SLOTS:
void initTestCase()
{
......@@ -97,10 +70,10 @@ private Q_SLOTS:
OpenTripPlannerParser p(s("gtfs"));
p.setKnownRentalVehicleNetworks(networks);
const auto res = p.parseLocationsByCoordinate(QJsonDocument::fromJson(readFile(inFileName)).object());
const auto res = p.parseLocationsByCoordinate(QJsonDocument::fromJson(Test::readFile(inFileName)).object());
const auto jsonRes = Location::toJson(res);
const auto ref = QJsonDocument::fromJson(readFile(refFileName)).array();
const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
if (jsonRes != ref) {
qDebug().noquote() << QJsonDocument(jsonRes).toJson();
......@@ -128,10 +101,10 @@ private Q_SLOTS:
QFETCH(QString, refFileName);
OpenTripPlannerParser p(s("gtfs"));
const auto res = p.parseLocationsByName(QJsonDocument::fromJson(readFile(inFileName)).object());
const auto res = p.parseLocationsByName(QJsonDocument::fromJson(Test::readFile(inFileName)).object());
const auto jsonRes = Location::toJson(res);
const auto ref = QJsonDocument::fromJson(readFile(refFileName)).array();
const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
if (jsonRes != ref) {
qDebug().noquote() << QJsonDocument(jsonRes).toJson();
......@@ -156,10 +129,10 @@ private Q_SLOTS:
QFETCH(QString, refFileName);
OpenTripPlannerParser p(s("gtfs"));
const auto res = p.parseDepartures(QJsonDocument::fromJson(readFile(inFileName)).object());
const auto res = p.parseDepartures(QJsonDocument::fromJson(Test::readFile(inFileName)).object());
const auto jsonRes = Stopover::toJson(res);
const auto ref = QJsonDocument::fromJson(readFile(refFileName)).array();
const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
if (jsonRes != ref) {
qDebug().noquote() << QJsonDocument(jsonRes).toJson();
......@@ -206,7 +179,7 @@ private Q_SLOTS:
QFETCH(QString, refFileName);
QFETCH(QString, networkConfig);
const auto configObj = QJsonDocument::fromJson(readFile(networkConfig)).object();
const auto configObj = QJsonDocument::fromJson(Test::readFile(networkConfig)).object();
const auto networkConfigObj = configObj.value(QLatin1String("options")).toObject().value(QLatin1String("rentalVehicleNetworks")).toObject();
QHash <QString, RentalVehicleNetwork> rentalVehicleNetworks;
for (auto it = networkConfigObj.begin(); it != networkConfigObj.end(); ++it) {
......@@ -216,14 +189,13 @@ private Q_SLOTS:
OpenTripPlannerParser p(s("gtfs"), s("1"));
p.setKnownRentalVehicleNetworks(rentalVehicleNetworks);
const auto res = p.parseJourneys(QJsonDocument::fromJson(readFile(inFileName)).object());
const auto res = p.parseJourneys(QJsonDocument::fromJson(Test::readFile(inFileName)).object());
const auto jsonRes = Journey::toJson(res);
const auto ref = QJsonDocument::fromJson(readFile(refFileName)).array();
const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
diffJson(refFileName, jsonRes, ref);
QVERIFY(!jsonRes.empty());
QCOMPARE(jsonRes, ref);
QVERIFY(Test::compareJson(refFileName, jsonRes, ref));
}
};
......
/*
SPDX-FileCopyrightText: 2019-2021 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 {
QByteArray readFile(const QString &fn)
{
QFile f(fn);
f.open(QFile::ReadOnly);
return f.readAll();
}
template <typename T>
inline bool compareJson(const QString &refFile, const T &output, const T &ref)
{
if (output != ref) {
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();
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