File indexing completed on 2024-05-12 05:17:32

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "testhelpers.h"
0007 
0008 #include <../src/lib/plist/plistreader.cpp>
0009 
0010 #include <QDebug>
0011 #include <QFile>
0012 #include <QJsonArray>
0013 #include <QJsonDocument>
0014 #include <QObject>
0015 #include <QTest>
0016 
0017 using namespace KItinerary;
0018 
0019 class PListReaderTest : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void testPListReader()
0024     {
0025         const auto data = Test::readFile(QStringLiteral(SOURCE_DIR "/plist/apple-structured-data-train.plist"));
0026         PListReader reader(data);
0027         QVERIFY(reader.isValid());
0028         QCOMPARE(reader.objectCount(), 228);
0029         QCOMPARE(reader.rootObjectIndex(), 0);
0030 
0031         QCOMPARE(reader.objectType(0), PListObjectType::Dict);
0032         QCOMPARE(reader.objectType(1), PListObjectType::String);
0033         QCOMPARE(reader.object(1).toString(), QLatin1StringView("$version"));
0034         QCOMPARE(reader.objectType(5), PListObjectType::Int);
0035         QCOMPARE(reader.object(5).toInt(), 100000);
0036         QCOMPARE(reader.objectType(6), PListObjectType::String);
0037         QCOMPARE(reader.object(6).toString(),
0038                  QLatin1StringView("NSKeyedArchiver"));
0039     }
0040 
0041     void testUnpackKeyedArchive_data()
0042     {
0043         QTest::addColumn<QString>("inFile");
0044         QTest::addColumn<QString>("refFile");
0045 
0046         QDir dir(QStringLiteral(SOURCE_DIR "/plist"));
0047         const auto lst = dir.entryList(QStringList(QStringLiteral("*.plist")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0048         for (const auto &file : lst) {
0049             const QString refFile = dir.path() + QLatin1Char('/') + file.left(file.size() - 6) + QStringLiteral(".json");
0050             if (!QFile::exists(refFile)) {
0051                 qDebug() << "reference file" << refFile << "does not exist, skipping test file" << file;
0052                 continue;
0053             }
0054             QTest::newRow(file.toUtf8().constData()) << QString(dir.path() + QLatin1Char('/') +  file) << refFile;
0055         }
0056     }
0057 
0058     void testUnpackKeyedArchive()
0059     {
0060         QFETCH(QString, inFile);
0061         QFETCH(QString, refFile);
0062 
0063         const auto data = Test::readFile(inFile);
0064         QVERIFY(PListReader::maybePList(data));
0065 
0066         PListReader reader(data);
0067         QVERIFY(reader.isValid());
0068         const auto unpacked = reader.unpackKeyedArchive().toObject();
0069 
0070         const auto expected = QJsonDocument::fromJson(Test::readFile(refFile)).object();
0071         if (unpacked != expected) {
0072             Test::compareJson(refFile, unpacked, expected);
0073         }
0074         QCOMPARE(unpacked, expected);
0075     }
0076 };
0077 
0078 QTEST_APPLESS_MAIN(PListReaderTest)
0079 
0080 #include "plistreadertest.moc"