File indexing completed on 2022-11-29 19:02:33

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KOpeningHours/OpeningHours>
0008 
0009 #include <QFile>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QTest>
0013 
0014 using namespace KOpeningHours;
0015 
0016 class JsonLdTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void initTestCase()
0021     {
0022         QLocale::setDefault(QLocale::c());
0023     }
0024 
0025     void testJsonLd_data()
0026     {
0027         QTest::addColumn<QString>("inputFile");
0028         QTest::addColumn<QByteArray>("osmExpr");
0029 
0030 #define T(json, osm) QTest::newRow(json) << QStringLiteral(SOURCE_DIR "/jsonlddata/" json ".json") << QByteArray(osm)
0031         T("oh-simple", "Mo,Tu,We,Th 09:00-12:00");
0032         T("oh-array", "Mo-Fr 10:00-19:00; Sa 10:00-22:00; Su 10:00-21:00");
0033         T("ohs-example", "Su 09:00-17:00 open; Sa 09:00-16:00 open; Th 09:00-15:00 open; Tu 09:00-14:00 open; Fr 09:00-13:00 open; Mo 09:00-12:00 open; We 09:00-11:00 open");
0034         T("ohs-mixed", "Mo,Tu,We,Th,Fr,Sa,Su 09:00-14:00; 2013 Dec 24-25 09:00-11:00 open; 2014 Jan 01 12:00-14:00 open");
0035 #undef T
0036     }
0037 
0038     void testJsonLd()
0039     {
0040         QFETCH(QString, inputFile);
0041         QFETCH(QByteArray, osmExpr);
0042 
0043         QFile inFile(inputFile);
0044         QVERIFY(inFile.open(QFile::ReadOnly));
0045 
0046         const auto obj = QJsonDocument::fromJson(inFile.readAll()).object();
0047         QVERIFY(!obj.isEmpty());
0048 
0049         auto oh = OpeningHours::fromJsonLd(obj);
0050         QCOMPARE(oh.error(), OpeningHours::NoError);
0051         QCOMPARE(oh.normalizedExpression(), osmExpr);
0052     }
0053 };
0054 
0055 QTEST_GUILESS_MAIN(JsonLdTest)
0056 
0057 #include "jsonldtest.moc"