File indexing completed on 2024-04-28 03:53:13

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2007 Thomas Braxton <kde.braxton@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kentrymaptest.h"
0008 
0009 #include <QTest>
0010 
0011 // clazy:excludeall=non-pod-global-static
0012 
0013 static const QString group1{QStringLiteral("A Group")};
0014 static const QByteArray key1{"A Key"};
0015 static const QByteArray key2{"Another Key"};
0016 static const QByteArray value1{"A value"};
0017 static const QByteArray value2{"A different value"};
0018 
0019 QTEST_MAIN(KEntryMapTest)
0020 
0021 template<typename KeyTypeA, typename KeyTypeB>
0022 void KEntryMapTest::testKeyOrder()
0023 {
0024     const KeyTypeA groupMarkerA(group1, {});
0025     const KeyTypeA entryA(group1, key1);
0026     const KeyTypeA localizedA(group1, key1, true, false);
0027     const KeyTypeA localizedDefaultA(group1, key1, true, true);
0028 
0029     const KeyTypeB entryB(group1, key1);
0030     const KeyTypeB localizedB(group1, key1, true, false);
0031     const KeyTypeB localizedDefaultB(group1, key1, true, true);
0032     const KeyTypeB defaultEntryB(group1, key1, false, true);
0033 
0034     // group marker should come before all entries
0035     QVERIFY(groupMarkerA < entryB);
0036     QVERIFY(groupMarkerA < defaultEntryB);
0037     QVERIFY(groupMarkerA < localizedB);
0038     QVERIFY(groupMarkerA < localizedDefaultB);
0039 
0040     // localized should come before entry
0041     QVERIFY(localizedA < entryB);
0042 
0043     // localized-default should come after localized entry
0044     QVERIFY(localizedA < localizedDefaultB);
0045 
0046     // localized-default should come before non-localized entry
0047     QVERIFY(localizedDefaultA < entryB);
0048 
0049     // default should come after entry
0050     QVERIFY(entryA < defaultEntryB);
0051 }
0052 
0053 void KEntryMapTest::testKeyAndKeyOrder()
0054 {
0055     testKeyOrder<KEntryKey, KEntryKey>();
0056 }
0057 
0058 void KEntryMapTest::testKeyAndKeyViewOrder()
0059 {
0060     testKeyOrder<KEntryKey, KEntryKeyView>();
0061 }
0062 
0063 void KEntryMapTest::testKeyViewAndKeyOrder()
0064 {
0065     testKeyOrder<KEntryKeyView, KEntryKey>();
0066 }
0067 
0068 void KEntryMapTest::testSimple()
0069 {
0070     KEntryMap map;
0071 
0072     map.setEntry(group1, key1, value1, EntryOptions());
0073     QCOMPARE(map.size(), 2); // the group marker & 1 key
0074     map.setEntry(group1, key2, value2, EntryOptions());
0075     QCOMPARE(map.size(), 3); // the group marker & 2 keys
0076 
0077     QVERIFY(map.constFindEntry(group1) != map.cend());
0078     QCOMPARE(map.constFindEntry(group1.toLower()), map.cend());
0079 
0080     QVERIFY(map.constFindEntry(group1, key1) != map.cend());
0081     QCOMPARE(map.constFindEntry(group1, key1.toLower()), map.cend());
0082     QVERIFY(map.constFindEntry(group1, key2) != map.cend());
0083     QCOMPARE(map.constFindEntry(group1, key2.toUpper()), map.cend());
0084 
0085     QByteArray found = map.constFindEntry(group1, key1)->second.mValue;
0086     QCOMPARE(found, value1);
0087     QVERIFY(found != value2);
0088 
0089     found = map.constFindEntry(group1, key2)->second.mValue;
0090     QVERIFY(found != value1);
0091     QCOMPARE(found, value2);
0092 }
0093 
0094 void KEntryMapTest::testDirty()
0095 {
0096     KEntryMap map;
0097     bool ret = map.setEntry(group1, key1, value1, EntryDefault);
0098     QCOMPARE(ret, true);
0099     ret = map.setEntry(group1, key1, value1, EntryDefault);
0100     QCOMPARE(ret, false);
0101     ret = map.setEntry(group1, key2, value2, EntryOptions());
0102     QCOMPARE(ret, true);
0103     ret = map.setEntry(group1, key2, value2, EntryOptions());
0104     QCOMPARE(ret, false);
0105 }
0106 
0107 void KEntryMapTest::testDefault()
0108 {
0109     KEntryMap map;
0110 
0111     map.setEntry(group1, key1, value1, EntryDefault);
0112     QCOMPARE(map.size(), 3); // group marker, default, entry
0113     map.setEntry(group1, key2, value2, EntryOptions());
0114     QCOMPARE(map.size(), 4); // group marker, default1, entry1, entry2
0115 
0116     const auto defaultEntry(map.constFindEntry(group1, key1, SearchDefaults));
0117     const auto entry1(map.constFindEntry(group1, key1));
0118     const auto entry2(map.constFindEntry(group1, key2));
0119 
0120     // default set for entry1
0121     QVERIFY(defaultEntry != map.cend());
0122     QCOMPARE(defaultEntry->second.mValue, entry1->second.mValue);
0123 
0124     // no default set for entry2
0125     QCOMPARE(map.constFindEntry(group1, key2, SearchDefaults), map.cend());
0126 
0127     // change from default
0128     map.setEntry(group1, key1, value2, EntryOptions());
0129     QVERIFY(defaultEntry->second.mValue != entry1->second.mValue);
0130     QVERIFY(entry1 != entry2);
0131     QCOMPARE(entry1->second.mValue, entry2->second.mValue);
0132 
0133     // revert entry1
0134     map.revertEntry(group1, key1, EntryOptions());
0135     QCOMPARE(defaultEntry->second.mValue, entry1->second.mValue);
0136 
0137     // revert entry2, no default --> should be marked as deleted
0138     map.revertEntry(group1, key2, EntryOptions());
0139     QCOMPARE(entry2->second.mValue, QByteArray());
0140     QVERIFY(entry2->second.bDirty);
0141     QVERIFY(entry2->second.bReverted);
0142 }
0143 
0144 void KEntryMapTest::testDelete()
0145 {
0146     KEntryMap map;
0147 
0148     map.setEntry(group1, key1, value1, EntryDefault);
0149     map.setEntry(group1, key2, value2, EntryDefault);
0150     QCOMPARE(map.size(), 5);
0151 
0152     map.setEntry(group1, key2, QByteArray(), EntryDeleted | EntryDirty);
0153     QCOMPARE(map.size(), 5); // entry should still be in map, so it can override merged entries later
0154     QCOMPARE(map.constFindEntry(group1, key2)->second.mValue, QByteArray());
0155 }
0156 
0157 void KEntryMapTest::testGlobal()
0158 {
0159     KEntryMap map;
0160 
0161     map.setEntry(group1, key1, value1, EntryGlobal);
0162     QCOMPARE(map.constFindEntry(group1, key1)->second.bGlobal, true);
0163 
0164     // this should create a new key that is not "global"
0165     map.setEntry(group1, key1, value2, EntryOptions());
0166     QCOMPARE(map.constFindEntry(group1, key1)->second.bGlobal, false);
0167 }
0168 
0169 void KEntryMapTest::testImmutable()
0170 {
0171     KEntryMap map;
0172 
0173     map.setEntry(group1, key1, value1, EntryImmutable);
0174     QCOMPARE(map.constFindEntry(group1, key1)->second.bImmutable, true); // verify the immutable bit was set
0175 
0176     map.setEntry(group1, key1, value2, EntryOptions());
0177     QCOMPARE(map.constFindEntry(group1, key1)->second.mValue, value1); // verify the value didn't change
0178 
0179     map.clear();
0180 
0181     map.setEntry(group1, QByteArray(), QByteArray(), EntryImmutable);
0182     QCOMPARE(map.constFindEntry(group1)->second.bImmutable, true); // verify the group is immutable
0183 
0184     map.setEntry(group1, key1, value1, EntryOptions()); // should be ignored since the group is immutable
0185     QCOMPARE(map.constFindEntry(group1, key1), map.cend());
0186 }
0187 
0188 void KEntryMapTest::testLocale()
0189 {
0190     const QByteArray translatedDefault("hola");
0191     const QByteArray translated("bonjour");
0192     const QByteArray untranslated("hello");
0193     KEntryMap map;
0194 
0195     map.setEntry(group1, key1, untranslated, EntryDefault);
0196     QCOMPARE(map.constFindEntry(group1, key1)->second.mValue, untranslated);
0197     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->second.mValue, untranslated); // no localized value yet
0198 
0199     map.setEntry(group1, key1, translated, EntryLocalized);
0200 
0201     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->second.mValue, translated); // has localized value now
0202     QVERIFY(map.constFindEntry(group1, key1, SearchLocalized)->second.mValue != map.constFindEntry(group1, key1)->second.mValue);
0203     QCOMPARE(map.constFindEntry(group1, key1, SearchDefaults | SearchLocalized)->second.mValue, untranslated); // default should still be untranslated
0204 
0205     map.setEntry(group1, key1, translatedDefault, EntryDefault | EntryLocalized);
0206     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->second.mValue, translatedDefault);
0207     map.setEntry(group1, key1, translated, EntryLocalized); // set the translated entry to a different locale
0208     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->second.mValue, translated);
0209 }
0210 
0211 #include "moc_kentrymaptest.cpp"