File indexing completed on 2024-04-28 15:19:33

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 QByteArray group1{"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 void KEntryMapTest::testKeyOrder()
0022 {
0023     const KEntryKey groupMarker(group1);
0024     const KEntryKey entry(group1, key1);
0025     const KEntryKey localized(group1, key1, true, false);
0026     const KEntryKey localizedDefault(group1, key1, true, true);
0027     const KEntryKey defaultEntry(group1, key1, false, true);
0028 
0029     // group marker should come before all entries
0030     QVERIFY(groupMarker < entry);
0031     QVERIFY(groupMarker < defaultEntry);
0032     QVERIFY(groupMarker < localized);
0033     QVERIFY(groupMarker < localizedDefault);
0034 
0035     // localized should come before entry
0036     QVERIFY(localized < entry);
0037 
0038     // localized-default should come after localized entry
0039     QVERIFY(localized < localizedDefault);
0040 
0041     // localized-default should come before non-localized entry
0042     QVERIFY(localizedDefault < entry);
0043 
0044     // default should come after entry
0045     QVERIFY(entry < defaultEntry);
0046 }
0047 
0048 void KEntryMapTest::testSimple()
0049 {
0050     KEntryMap map;
0051 
0052     map.setEntry(group1, key1, value1, EntryOptions());
0053     QCOMPARE(map.size(), 2); // the group marker & 1 key
0054     map.setEntry(group1, key2, value2, EntryOptions());
0055     QCOMPARE(map.size(), 3); // the group marker & 2 keys
0056 
0057     QVERIFY(map.constFindEntry(group1) != map.cend());
0058     QCOMPARE(map.constFindEntry(group1.toLower()), map.cend());
0059 
0060     QVERIFY(map.constFindEntry(group1, key1) != map.cend());
0061     QCOMPARE(map.constFindEntry(group1, key1.toLower()), map.cend());
0062     QVERIFY(map.constFindEntry(group1, key2) != map.cend());
0063     QCOMPARE(map.constFindEntry(group1, key2.toUpper()), map.cend());
0064 
0065     QByteArray found = map.constFindEntry(group1, key1)->mValue;
0066     QCOMPARE(found, value1);
0067     QVERIFY(found != value2);
0068 
0069     found = map.constFindEntry(group1, key2)->mValue;
0070     QVERIFY(found != value1);
0071     QCOMPARE(found, value2);
0072 }
0073 
0074 void KEntryMapTest::testDirty()
0075 {
0076     KEntryMap map;
0077     bool ret = map.setEntry(group1, key1, value1, EntryDefault);
0078     QCOMPARE(ret, true);
0079     ret = map.setEntry(group1, key1, value1, EntryDefault);
0080     QCOMPARE(ret, false);
0081     ret = map.setEntry(group1, key2, value2, EntryOptions());
0082     QCOMPARE(ret, true);
0083     ret = map.setEntry(group1, key2, value2, EntryOptions());
0084     QCOMPARE(ret, false);
0085 }
0086 
0087 void KEntryMapTest::testDefault()
0088 {
0089     KEntryMap map;
0090 
0091     map.setEntry(group1, key1, value1, EntryDefault);
0092     QCOMPARE(map.size(), 3); // group marker, default, entry
0093     map.setEntry(group1, key2, value2, EntryOptions());
0094     QCOMPARE(map.size(), 4); // group marker, default1, entry1, entry2
0095 
0096     const auto defaultEntry(map.constFindEntry(group1, key1, SearchDefaults));
0097     const auto entry1(map.constFindEntry(group1, key1));
0098     const auto entry2(map.constFindEntry(group1, key2));
0099 
0100     // default set for entry1
0101     QVERIFY(defaultEntry != map.constEnd());
0102     QCOMPARE(defaultEntry->mValue, entry1->mValue);
0103 
0104     // no default set for entry2
0105     QCOMPARE(map.constFindEntry(group1, key2, SearchDefaults), map.cend());
0106 
0107     // change from default
0108     map.setEntry(group1, key1, value2, EntryOptions());
0109     QVERIFY(defaultEntry->mValue != entry1->mValue);
0110     QVERIFY(entry1 != entry2);
0111     QCOMPARE(entry1->mValue, entry2->mValue);
0112 
0113     // revert entry1
0114     map.revertEntry(group1, key1, EntryOptions());
0115     QCOMPARE(defaultEntry->mValue, entry1->mValue);
0116 
0117     // revert entry2, no default --> should be marked as deleted
0118     map.revertEntry(group1, key2, EntryOptions());
0119     QCOMPARE(entry2->mValue, QByteArray());
0120     QVERIFY(entry2->bDirty);
0121     QVERIFY(entry2->bReverted);
0122 }
0123 
0124 void KEntryMapTest::testDelete()
0125 {
0126     KEntryMap map;
0127 
0128     map.setEntry(group1, key1, value1, EntryDefault);
0129     map.setEntry(group1, key2, value2, EntryDefault);
0130     QCOMPARE(map.size(), 5);
0131 
0132     map.setEntry(group1, key2, QByteArray(), EntryDeleted | EntryDirty);
0133     QCOMPARE(map.size(), 5); // entry should still be in map, so it can override merged entries later
0134     QCOMPARE(map.constFindEntry(group1, key2)->mValue, QByteArray());
0135 }
0136 
0137 void KEntryMapTest::testGlobal()
0138 {
0139     KEntryMap map;
0140 
0141     map.setEntry(group1, key1, value1, EntryGlobal);
0142     QCOMPARE(map.constFindEntry(group1, key1)->bGlobal, true);
0143 
0144     // this should create a new key that is not "global"
0145     map.setEntry(group1, key1, value2, EntryOptions());
0146     QCOMPARE(map.constFindEntry(group1, key1)->bGlobal, false);
0147 }
0148 
0149 void KEntryMapTest::testImmutable()
0150 {
0151     KEntryMap map;
0152 
0153     map.setEntry(group1, key1, value1, EntryImmutable);
0154     QCOMPARE(map.constFindEntry(group1, key1)->bImmutable, true); // verify the immutable bit was set
0155 
0156     map.setEntry(group1, key1, value2, EntryOptions());
0157     QCOMPARE(map.constFindEntry(group1, key1)->mValue, value1); // verify the value didn't change
0158 
0159     map.clear();
0160 
0161     map.setEntry(group1, QByteArray(), QByteArray(), EntryImmutable);
0162     QCOMPARE(map.constFindEntry(group1)->bImmutable, true); // verify the group is immutable
0163 
0164     map.setEntry(group1, key1, value1, EntryOptions()); // should be ignored since the group is immutable
0165     QCOMPARE(map.constFindEntry(group1, key1), map.cend());
0166 }
0167 
0168 void KEntryMapTest::testLocale()
0169 {
0170     const QByteArray translatedDefault("hola");
0171     const QByteArray translated("bonjour");
0172     const QByteArray untranslated("hello");
0173     KEntryMap map;
0174 
0175     map.setEntry(group1, key1, untranslated, EntryDefault);
0176     QCOMPARE(map.constFindEntry(group1, key1)->mValue, untranslated);
0177     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->mValue, untranslated); // no localized value yet
0178 
0179     map.setEntry(group1, key1, translated, EntryLocalized);
0180 
0181     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->mValue, translated); // has localized value now
0182     QVERIFY(map.constFindEntry(group1, key1, SearchLocalized)->mValue != map.constFindEntry(group1, key1)->mValue);
0183     QCOMPARE(map.constFindEntry(group1, key1, SearchDefaults | SearchLocalized)->mValue, untranslated); // default should still be untranslated
0184 
0185     map.setEntry(group1, key1, translatedDefault, EntryDefault | EntryLocalized);
0186     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->mValue, translatedDefault);
0187     map.setEntry(group1, key1, translated, EntryLocalized); // set the translated entry to a different locale
0188     QCOMPARE(map.constFindEntry(group1, key1, SearchLocalized)->mValue, translated);
0189 }
0190 
0191 #include "moc_kentrymaptest.cpp"