diff options
author | Alexander Lohnau <alexander.lohnau@gmx.de> | 2021-09-19 20:02:50 +0200 |
---|---|---|
committer | Alexander Lohnau <alexander.lohnau@gmx.de> | 2021-10-11 18:11:27 +0200 |
commit | f7754f2bb3db666e4bfb2b82af079828e0086b84 (patch) | |
tree | db1120cab3fb234b0c4d9f55f626a93968ac3fa1 /autotests/kconfigtest.cpp | |
parent | 13b79463dd80c84c7cf1c817c363e7747e4da034 (diff) | |
download | kconfig-f7754f2bb3db666e4bfb2b82af079828e0086b84.tar.gz kconfig-f7754f2bb3db666e4bfb2b82af079828e0086b84.tar.bz2 |
Create utility method for moving entries from one group to another
This will become especially useful when moving state data from the config file
to a dedicated state data file.
Task: https://phabricator.kde.org/T12549
Diffstat (limited to 'autotests/kconfigtest.cpp')
-rw-r--r-- | autotests/kconfigtest.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/autotests/kconfigtest.cpp b/autotests/kconfigtest.cpp index c3a68e42..00e7bbf0 100644 --- a/autotests/kconfigtest.cpp +++ b/autotests/kconfigtest.cpp @@ -1898,6 +1898,47 @@ void KConfigTest::testNewlines() #endif } +void KConfigTest::testMoveValuesTo() +{ + QTemporaryFile file; + QVERIFY(file.open()); + // Prepare kdeglobals + { + KConfig glob(QStringLiteral("kdeglobals")); + KConfigGroup general(&glob, "TestGroup"); + general.writeEntry("GlobalKey", "PlsDeleteMe"); + QVERIFY(glob.sync()); + } + + KConfigGroup grp = KSharedConfig::openConfig(file.fileName())->group("TestGroup"); + + grp.writeEntry("test1", "first_value"); + grp.writeEntry("test_empty", ""); + grp.writeEntry("other", "other_value"); + grp.writePathEntry("my_path", QStringLiteral("~/somepath")); + // because this key is from the global file it should be explicitly deleted + grp.deleteEntry("GlobalKey"); + + QTemporaryFile targetFile; + QVERIFY(targetFile.open()); + targetFile.close(); + KConfigGroup targetGroup = KSharedConfig::openConfig(targetFile.fileName(), KConfig::SimpleConfig)->group("MoveToGroup"); + + grp.moveValuesTo({"test1", "test_empty", "does_not_exist", "my_path", "GlobalKey"}, targetGroup); + QVERIFY(grp.config()->isDirty()); + QVERIFY(targetGroup.config()->isDirty()); + + QCOMPARE(grp.keyList(), QStringList{QStringLiteral("other")}); + QStringList expectedKeyList{QStringLiteral("my_path"), QStringLiteral("test1"), QStringLiteral("test_empty")}; + QCOMPARE(targetGroup.keyList(), expectedKeyList); + QCOMPARE(targetGroup.readEntry("test1"), QStringLiteral("first_value")); + + targetGroup.sync(); + QFile targetReadFile(targetFile.fileName()); + targetReadFile.open(QFile::ReadOnly); + QVERIFY(targetReadFile.readAll().contains(QByteArray("my_path[$e]=~/somepath"))); +} + void KConfigTest::testXdgListEntry() { QTemporaryFile file; |