KDE/kdelibs/kdecore/tests

David Faure faure at kde.org
Thu Oct 4 18:29:51 BST 2007


SVN commit 721135 by dfaure:

Today's quizz: KConfigGroup(c,"").group() == ?

Answered by this unit test: it's "<default>", i.e. the toplevel entries in the config files.
I thought we had support for the empty group ("[]" in the config file) but obviously not.
Good I guess, one less thing to support. But then we could get rid of the "<default>" string altogether,
and use the empty string instead, no?
CCMAIL: kde-core-devel at kde.org


 M  +58 -1     kconfigtest.cpp  
 M  +3 -0      kconfigtest.h  


--- trunk/KDE/kdelibs/kdecore/tests/kconfigtest.cpp #721134:721135
@@ -19,6 +19,7 @@
 
 #include <qtest_kde.h>
 #include "kconfigtest.h"
+#include <kstandarddirs.h>
 #include "kconfigtest.moc"
 
 #include <kconfig.h>
@@ -287,7 +288,7 @@
   QVERIFY( sc3.readEntry( "flags-bit0", KConfigTest::Flags() ) == KConfigTest::bit0 );
 
   int eid = staticMetaObject.indexOfEnumerator( "Flags" );
-  Q_ASSERT( eid != -1 );
+  QVERIFY( eid != -1 );
   QMetaEnum me = staticMetaObject.enumerator( eid );
   KConfigTest::Flags bitfield = KConfigTest::bit0|KConfigTest::bit1;
 
@@ -398,6 +399,46 @@
   QVERIFY( sc.entryMap("FooBar").isEmpty() ); //inexistant group
 }
 
+void KConfigTest::testDefaultGroup()
+{
+    KConfig sc( "kconfigtest" );
+    KConfigGroup defaultGroup(&sc, "<default>");
+    QCOMPARE(defaultGroup.group(), QString("<default>"));
+    QVERIFY(!defaultGroup.exists());
+    defaultGroup.writeEntry("TestKey", "defaultGroup");
+    QVERIFY(defaultGroup.exists());
+    QCOMPARE(defaultGroup.readEntry("TestKey", QString()), QString("defaultGroup"));
+    sc.sync();
+
+#ifdef Q_OS_UNIX
+    QList<QByteArray> lines = readLines();
+    QVERIFY(!lines.contains("[]"));
+    QCOMPARE(lines.first(), QByteArray("TestKey=defaultGroup\n"));
+#endif
+
+    defaultGroup.deleteGroup();
+}
+
+void KConfigTest::testEmptyGroup()
+{
+    KConfig sc( "kconfigtest" );
+    KConfigGroup emptyGroup(&sc, "");
+    QCOMPARE(emptyGroup.group(), QString("<default>")); // confusing, heh?
+    QVERIFY(!emptyGroup.exists());
+    emptyGroup.writeEntry("TestKey", "emptyGroup");
+    QVERIFY(emptyGroup.exists());
+    QCOMPARE(emptyGroup.readEntry("TestKey", QString()), QString("emptyGroup"));
+    sc.sync();
+
+#ifdef Q_OS_UNIX
+    QList<QByteArray> lines = readLines();
+    QVERIFY(!lines.contains("[]")); // there's no support for the [] group, in fact.
+#endif
+
+    emptyGroup.deleteGroup();
+}
+
+
 void KConfigTest::testKAboutDataOrganizationDomain()
 {
     KAboutData data( "app", 0, ki18n("program"), "version",
@@ -412,3 +453,19 @@
                       "http://edu.kde.org/kig" );
     QCOMPARE( data2.organizationDomain(), QString::fromLatin1( "kde.org" ) );
 }
+
+QList<QByteArray> KConfigTest::readLines()
+{
+    const QString path = KStandardDirs::locateLocal("config", "kconfigtest");
+    Q_ASSERT(!path.isEmpty());
+    QFile file(path);
+    Q_ASSERT(file.open(QIODevice::ReadOnly));
+    QList<QByteArray> lines;
+    QByteArray line;
+    do {
+        line = file.readLine();
+        if (!line.isEmpty())
+            lines.append(line);
+    } while(!line.isEmpty());
+    return lines;
+}
--- trunk/KDE/kdelibs/kdecore/tests/kconfigtest.h #721134:721135
@@ -41,6 +41,8 @@
     void testEnums();
     void testInvalid();
     void testDelete();
+    void testDefaultGroup();
+    void testEmptyGroup();
     void testRevertAllEntries();
     void testChangeGroup();
 
@@ -48,6 +50,7 @@
     void testKAboutDataOrganizationDomain();
 private:
     void revertEntries();
+    QList<QByteArray> readLines();
 };
 Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigTest::Flags)
 




More information about the kde-core-devel mailing list