fieldtest.cpp 1.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
    SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include <field.h>
#include <pass.h>

#include <QJsonDocument>
#include <QJsonObject>
#include <QLocale>
#include <QTest>

#include <cmath>

17
18
19
20
21
22
23
void initLocale()
{
    qputenv("TZ", "UTC");
}

Q_CONSTRUCTOR_FUNCTION(initLocale)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
namespace KPkPass
{
class FieldTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase()
    {
        QLocale::setDefault(QLocale(QStringLiteral("fr_FR")));
    }

    void testBoardingPass()
    {
        std::unique_ptr<KPkPass::Pass> pass(KPkPass::Pass::fromFile(QStringLiteral(SOURCE_DIR "/data/boardingpass-v1.pkpass")));
        QVERIFY(pass);

        auto obj =
            QJsonDocument::fromJson(R"({"key":"valid-date","label":"Datum","dateStyle":"PKDateStyleShort","value":"2021-06-27T14:30:00+02:00"})").object();
        KPkPass::Field f(obj, pass.get());
        QCOMPARE(f.value().type(), QVariant::DateTime);
44
        QCOMPARE(f.value(), QDateTime({2021, 6, 27}, {14, 30}, Qt::OffsetFromUTC, 7200));
45
        QCOMPARE(f.valueDisplayString(), QLatin1String("27/06/2021 14:30"));
Volker Krause's avatar
Volker Krause committed
46

47
48
49
50
51
52
        obj = QJsonDocument::fromJson(R"({"key":"valid-date","label":"Datum","dateStyle":"PKDateStyleShort","value":"2021-06-27T00:00:00+02:00"})").object();
        f = KPkPass::Field(obj, pass.get());
        QCOMPARE(f.value().type(), QVariant::DateTime);
        QCOMPARE(f.value().toDateTime().date(), QDate({2021, 6, 27}));
        QCOMPARE(f.valueDisplayString(), QLatin1String("27/06/2021"));

Volker Krause's avatar
Volker Krause committed
53
54
55
56
57
58
        obj = QJsonDocument::fromJson(R"({"key":"valid-locations","label":"Ort","value":"Freibad Killesberg\n"})").object();
        f = KPkPass::Field(obj, pass.get());

        QCOMPARE(f.value().type(), QVariant::String);
        QCOMPARE(f.value(), QLatin1String("Freibad Killesberg\n"));
        QCOMPARE(f.valueDisplayString(), QLatin1String("Freibad Killesberg"));
59
60
61
62
63
64
65
    }
};
}

QTEST_GUILESS_MAIN(KPkPass::FieldTest)

#include "fieldtest.moc"