File indexing completed on 2024-05-12 05:21:34
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "field.h" 0008 #include "pass.h" 0009 0010 #include <QJsonDocument> 0011 #include <QJsonObject> 0012 #include <QLocale> 0013 #include <QTest> 0014 #include <QTimeZone> 0015 0016 #include <cmath> 0017 0018 void initLocale() 0019 { 0020 qputenv("TZ", "UTC"); 0021 } 0022 0023 Q_CONSTRUCTOR_FUNCTION(initLocale) 0024 0025 namespace KPkPass 0026 { 0027 class FieldTest : public QObject 0028 { 0029 Q_OBJECT 0030 private Q_SLOTS: 0031 void initTestCase() 0032 { 0033 QLocale::setDefault(QLocale(QStringLiteral("fr_FR"))); 0034 } 0035 0036 void testBoardingPass() 0037 { 0038 std::unique_ptr<KPkPass::Pass> pass(KPkPass::Pass::fromFile(QStringLiteral(SOURCE_DIR "/data/boardingpass-v1.pkpass"))); 0039 QVERIFY(pass); 0040 0041 auto obj = 0042 QJsonDocument::fromJson(R"({"key":"valid-date","label":"Datum","dateStyle":"PKDateStyleShort","value":"2021-06-27T14:30:00+02:00"})").object(); 0043 KPkPass::Field f(obj, pass.get()); 0044 QCOMPARE(f.value().userType(), QMetaType::QDateTime); 0045 QCOMPARE(f.value(), QDateTime({2021, 6, 27}, {14, 30}, QTimeZone::fromSecondsAheadOfUtc(7200))); 0046 QCOMPARE(f.valueDisplayString(), QLatin1StringView("27/06/2021 14:30")); 0047 0048 obj = QJsonDocument::fromJson(R"({"key":"valid-date","label":"Datum","dateStyle":"PKDateStyleShort","value":"2021-06-27T00:00:00+02:00"})").object(); 0049 f = KPkPass::Field(obj, pass.get()); 0050 QCOMPARE(f.value().userType(), QMetaType::QDateTime); 0051 QCOMPARE(f.value().toDateTime().date(), QDate({2021, 6, 27})); 0052 QCOMPARE(f.valueDisplayString(), QLatin1StringView("27/06/2021")); 0053 0054 obj = QJsonDocument::fromJson(R"({"key":"valid-locations","label":"Ort","value":"Freibad Killesberg\n"})").object(); 0055 f = KPkPass::Field(obj, pass.get()); 0056 0057 QCOMPARE(f.value().userType(), QMetaType::QString); 0058 QCOMPARE(f.value(), QLatin1StringView("Freibad Killesberg\n")); 0059 QCOMPARE(f.valueDisplayString(), QLatin1StringView("Freibad Killesberg")); 0060 } 0061 }; 0062 } 0063 0064 QTEST_GUILESS_MAIN(KPkPass::FieldTest) 0065 0066 #include "fieldtest.moc"