]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/fileconf/fileconftest.cpp
Temporary ugly trick to make release for OpenWatcom possible.
[wxWidgets.git] / tests / fileconf / fileconftest.cpp
index 766e2accaf4ea098ec78f99a7fab249d1bc89043..82a983500b2c358074df54e4633c84d779f76152 100644 (file)
@@ -11,7 +11,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
-#include "wx/wxprec.h"
+#include "testprec.h"
 
 #ifdef __BORLANDC__
     #pragma hdrstop
@@ -24,9 +24,7 @@
 
 #include "wx/fileconf.h"
 #include "wx/sstream.h"
-#include "wx/wfstream.h"
-
-#include "wx/cppunit.h"
+#include "wx/log.h"
 
 static const wxChar *testconfig =
 _T("[root]\n")
@@ -45,9 +43,7 @@ _T("[root/group2]\n")
 class FileConfigTestCase : public CppUnit::TestCase
 {
 public:
-    FileConfigTestCase() 
-    {
-    }
+    FileConfigTestCase() { }
 
 private:
     CPPUNIT_TEST_SUITE( FileConfigTestCase );
@@ -63,6 +59,9 @@ private:
         CPPUNIT_TEST( DeleteAll );
         CPPUNIT_TEST( RenameEntry );
         CPPUNIT_TEST( RenameGroup );
+        CPPUNIT_TEST( CreateEntriesAndSubgroup );
+        CPPUNIT_TEST( CreateSubgroupAndEntries );
+        CPPUNIT_TEST( DeleteLastGroup );
     CPPUNIT_TEST_SUITE_END();
 
     void Path();
@@ -77,6 +76,9 @@ private:
     void DeleteAll();
     void RenameEntry();
     void RenameGroup();
+    void CreateEntriesAndSubgroup();
+    void CreateSubgroupAndEntries();
+    void DeleteLastGroup();
 
     static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
     {
@@ -87,23 +89,9 @@ private:
 
     static wxString Dump(wxFileConfig& fc)
     {
-        wxFileOutputStream* pOutFile = new wxFileOutputStream(_T("outconf.txt"));
-        fc.Save(*pOutFile);
-        delete pOutFile;
-
-        wxFileInputStream* pInFile = new wxFileInputStream(_T("outconf.txt"));
-        char* szOut = new char[pInFile->GetSize()];
-        pInFile->Read(szOut, pInFile->GetSize());
-
-        wxString realString = wxTextFile::Translate(
-                                     wxString(szOut, wxConvLocal, pInFile->GetSize()), 
-                                     wxTextFileType_Unix
-                                                   );
-
-        delete szOut;
-        delete pInFile;
-
-        return realString;
+        wxStringOutputStream sos;
+        fc.Save(sos);
+        return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
     }
 
     void CheckGroupEntries(const wxFileConfig& fc,
@@ -248,6 +236,8 @@ void FileConfigTestCase::HasEntry()
     CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
     CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
     CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
+    CPPUNIT_ASSERT( !fc.HasEntry(_T("/root/no_such_group/entry")) );
+    CPPUNIT_ASSERT( !fc.HasGroup(_T("/root/no_such_group")) );
 }
 
 void FileConfigTestCase::HasGroup()
@@ -259,10 +249,11 @@ void FileConfigTestCase::HasGroup()
     CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
     CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
     CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
-    CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
     CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
     CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
     CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
+    CPPUNIT_ASSERT( !fc.HasGroup(_T("foot/subgroup")) );
+    CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
 }
 
 void FileConfigTestCase::Save()
@@ -369,5 +360,70 @@ void FileConfigTestCase::RenameGroup()
                                 _T("[foot/group2]\n") );
 }
 
+void FileConfigTestCase::CreateSubgroupAndEntries()
+{
+    wxFileConfig fc;
+    fc.Write(_T("sub/sub_first"), _T("sub_one"));
+    fc.Write(_T("first"), _T("one"));
+
+    CPPUNIT_ASSERT( Dump(fc) == _T("first=one\n")
+                                _T("[sub]\n")
+                                _T("sub_first=sub_one\n"));
+}
+
+void FileConfigTestCase::CreateEntriesAndSubgroup()
+{
+    wxFileConfig fc;
+    fc.Write(_T("first"), _T("one"));
+    fc.Write(_T("second"), _T("two"));
+    fc.Write(_T("sub/sub_first"), _T("sub_one"));
+
+    CPPUNIT_ASSERT( Dump(fc) == _T("first=one\n")
+                                _T("second=two\n")
+                                _T("[sub]\n")
+                                _T("sub_first=sub_one\n"));
+}
+
+static void EmptyConfigAndWriteKey()
+{
+    wxFileConfig fc(_T("deleteconftest"));
+
+    const wxString groupPath = _T("/root");
+
+    if ( fc.Exists(groupPath) )
+    {
+        // using DeleteGroup exposes the problem, using DeleteAll doesn't
+        CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
+    }
+
+    // the config must be empty for the problem to arise
+    CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
+    CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
+
+
+    // this crashes on second call of this function
+    CPPUNIT_ASSERT( fc.Write(groupPath + _T("/entry"), _T("value")) );
+}
+
+void FileConfigTestCase::DeleteLastGroup()
+{
+    /*
+    We make 2 of the same calls, first to create a file config with a single
+    group and key...
+    */
+    ::EmptyConfigAndWriteKey();
+
+    /*
+    ... then the same but this time the key's group is deleted before the
+    key is written again. This causes a crash.
+    */
+    ::EmptyConfigAndWriteKey();
+
+
+    // clean up
+    wxLogNull noLogging;
+    (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
+}
+
 #endif // wxUSE_FILECONFIG