File indexing completed on 2024-09-01 14:31:26
0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0002 // SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk> 0003 0004 #include <QDebug> 0005 #include <QMetaEnum> 0006 #include <QTest> 0007 #include <utility> 0008 0009 #include "flatpakcommon.h" 0010 #include "flatpakpermission.h" 0011 0012 using namespace FlatpakStrings; 0013 0014 class FlatpakSimpleEntryTest : public QObject 0015 { 0016 Q_OBJECT 0017 0018 private Q_SLOTS: 0019 0020 // This test intentionally does not verify contents of the parsing result. 0021 // In case if we ever switch from raw strings to enum values, this test 0022 // would remain constant. 0023 void testValidAndRoundtrip_data() 0024 { 0025 QTest::addColumn<QString>("rawInput"); 0026 0027 const std::array groups = { 0028 std::make_pair(QLatin1String(FLATPAK_METADATA_KEY_SHARED), flatpak_context_shares), 0029 std::make_pair(QLatin1String(FLATPAK_METADATA_KEY_SOCKETS), flatpak_context_sockets), 0030 std::make_pair(QLatin1String(FLATPAK_METADATA_KEY_DEVICES), flatpak_context_devices), 0031 std::make_pair(QLatin1String(FLATPAK_METADATA_KEY_FEATURES), flatpak_context_features), 0032 }; 0033 0034 for (const auto &[group, entries] : groups) { 0035 for (const char **entryPtr = entries; *entryPtr != nullptr; entryPtr++) { 0036 const auto entry = QString(*entryPtr); 0037 const auto disabledEntry = QLatin1String("!%1").arg(entry); 0038 const auto testRowName = QLatin1String("%1/%2").arg(group, entry); 0039 QTest::newRow(qUtf8Printable(testRowName)) << entry; 0040 const auto disabledTestRowName = QLatin1String("%1/!%2").arg(group, entry); 0041 QTest::newRow(qUtf8Printable(disabledTestRowName)) << disabledEntry; 0042 } 0043 } 0044 } 0045 0046 void testValidAndRoundtrip() 0047 { 0048 QFETCH(QString, rawInput); 0049 const auto entry = FlatpakSimpleEntry::parse(rawInput); 0050 QVERIFY(entry.has_value()); 0051 const auto actual = entry.value().format(); 0052 QCOMPARE(actual, rawInput); 0053 } 0054 0055 void testEnabled() 0056 { 0057 const auto entry = FlatpakSimpleEntry::parse(QStringLiteral("devel")); 0058 QVERIFY(entry.has_value()); 0059 const auto &value = entry.value(); 0060 QVERIFY(value.isEnabled()); 0061 QCOMPARE(value.name(), QLatin1String("devel")); 0062 } 0063 0064 void testDisabled() 0065 { 0066 const auto entry = FlatpakSimpleEntry::parse(QStringLiteral("!multiarch")); 0067 QVERIFY(entry.has_value()); 0068 const auto &value = entry.value(); 0069 QVERIFY(!value.isEnabled()); 0070 QCOMPARE(value.name(), QLatin1String("multiarch")); 0071 } 0072 0073 void testEquality() 0074 { 0075 const auto entryOn = FlatpakSimpleEntry(QStringLiteral("bluetooth")); 0076 const auto entryOff = FlatpakSimpleEntry(QStringLiteral("!bluetooth")); 0077 0078 QVERIFY(entryOn.isEnabled()); 0079 QVERIFY(entryOff.isEnabled()); 0080 QCOMPARE(entryOn, entryOn); 0081 QCOMPARE(entryOff, entryOff); 0082 QVERIFY(entryOn != entryOff); 0083 0084 const auto entryAll1 = FlatpakSimpleEntry(QStringLiteral("all")); 0085 const auto entryAll2 = FlatpakSimpleEntry(QStringLiteral("all")); 0086 0087 QCOMPARE(entryAll1, entryAll2); 0088 QVERIFY(entryAll1 != entryOn); 0089 QVERIFY(entryAll1 != entryOff); 0090 } 0091 }; 0092 0093 QTEST_MAIN(FlatpakSimpleEntryTest) 0094 0095 #include "FlatpakSimpleEntryTest.moc"