]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/config/config.cpp
applying editor part of patch, see #15003
[wxWidgets.git] / tests / config / config.cpp
index 1fc9a47dc5f7159ebd932bb0cc953b0846acb6d0..0abc18fb3c0622f6a7b577ee9b7c5ace16d0ae88 100644 (file)
@@ -7,7 +7,10 @@
 // Copyright:   (c) 2007 Marcin Wojdyr
 ///////////////////////////////////////////////////////////////////////////////
 
 // Copyright:   (c) 2007 Marcin Wojdyr
 ///////////////////////////////////////////////////////////////////////////////
 
-// see also tests/fileconf/fileconftest.cpp for wxFileConfig specific tests
+// NOTE: this test is compiled in test_gui because it uses wxColour for
+//       its testing purpose.
+// See also tests/config/fileconf.cpp for wxFileConfig specific tests and
+// tests/config/regconf.cpp for wxRegConfig specific tests.
 
 // ----------------------------------------------------------------------------
 // headers
 
 // ----------------------------------------------------------------------------
 // headers
@@ -15,6 +18,8 @@
 
 #include "testprec.h"
 
 
 #include "testprec.h"
 
+#if wxUSE_CONFIG
+
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif
@@ -44,13 +49,16 @@ private:
     void ReadWriteLocalTest();
     void RecordingDefaultsTest();
 
     void ReadWriteLocalTest();
     void RecordingDefaultsTest();
 
+    // return the number of values we (attempted to) read
+    int ReadValues(wxConfig *config, bool has_values);
+
     DECLARE_NO_COPY_CLASS(ConfigTestCase)
 };
 
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase );
 
     DECLARE_NO_COPY_CLASS(ConfigTestCase)
 };
 
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase );
 
-// also include in it's own registry so that these tests can be run alone
+// also include in its own registry so that these tests can be run alone
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" );
 
 void ConfigTestCase::ReadWriteLocalTest()
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" );
 
 void ConfigTestCase::ReadWriteLocalTest()
@@ -66,110 +74,140 @@ void ConfigTestCase::ReadWriteLocalTest()
     config->Write(wxString(wxT("long1")), 234L);
     config->Write(wxT("double1"), 345.67);
     config->Write(wxT("bool1"), true);
     config->Write(wxString(wxT("long1")), 234L);
     config->Write(wxT("double1"), 345.67);
     config->Write(wxT("bool1"), true);
+#ifdef wxHAS_CONFIG_TEMPLATE_RW
     config->Write(wxT("color1"), wxColour(11,22,33,44));
     config->Write(wxT("color1"), wxColour(11,22,33,44));
+#endif // wxHAS_CONFIG_TEMPLATE_RW
     config->Flush();
     delete config;
 
     config = new wxConfig(app, vendor, wxT(""), wxT(""),
                                     wxCONFIG_USE_LOCAL_FILE);
     wxString string1 = config->Read(wxT("string1"));
     config->Flush();
     delete config;
 
     config = new wxConfig(app, vendor, wxT(""), wxT(""),
                                     wxCONFIG_USE_LOCAL_FILE);
     wxString string1 = config->Read(wxT("string1"));
-    CPPUNIT_ASSERT( string1 == wxT("abc") );
+    CPPUNIT_ASSERT_EQUAL( "abc", string1 );
     string1 = config->Read(wxT("string1"), wxT("defaultvalue"));
     string1 = config->Read(wxT("string1"), wxT("defaultvalue"));
-    CPPUNIT_ASSERT( string1 == wxT("abc") );
+    CPPUNIT_ASSERT_EQUAL( "abc", string1 );
+
     wxString string2;
     bool r = config->Read(wxT("string2"), &string2);
     wxString string2;
     bool r = config->Read(wxT("string2"), &string2);
-    CPPUNIT_ASSERT( r == true );
-    CPPUNIT_ASSERT( string2 == wxT("def") );
+    CPPUNIT_ASSERT( r );
+    CPPUNIT_ASSERT_EQUAL( "def", string2 );
+
     r = config->Read(wxT("string2"), &string2, wxT("defaultvalue"));
     r = config->Read(wxT("string2"), &string2, wxT("defaultvalue"));
-    CPPUNIT_ASSERT( r == true );
-    CPPUNIT_ASSERT( string2 == wxT("def") );
+    CPPUNIT_ASSERT( r );
+    CPPUNIT_ASSERT_EQUAL( "def", string2 );
+
     int int1 = config->Read(wxT("int1"), 5);
     int int1 = config->Read(wxT("int1"), 5);
-    CPPUNIT_ASSERT( int1 == 123 );
+    CPPUNIT_ASSERT_EQUAL( 123, int1 );
+
     long long1;
     r = config->Read(wxT("long1"), &long1);
     long long1;
     r = config->Read(wxT("long1"), &long1);
-    CPPUNIT_ASSERT( r == true );
-    CPPUNIT_ASSERT( long1 == 234 );
+    CPPUNIT_ASSERT( r );
+    CPPUNIT_ASSERT_EQUAL( 234L, long1 );
+
+    CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 );
+
+    double double1;
+    r = config->Read(wxT("double1"), &double1);
+    CPPUNIT_ASSERT( r );
+    CPPUNIT_ASSERT_EQUAL( 345.67, double1 );
+
+    CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 );
+
     bool bool1;
     bool bool1;
-    r = config->Read(wxT("foo"), &bool1);
-    CPPUNIT_ASSERT( r == false );
+    r = config->Read(wxT("foo"), &bool1); // there is no "foo" key
+    CPPUNIT_ASSERT( !r );
+
     r = config->Read(wxT("bool1"), &bool1);
     r = config->Read(wxT("bool1"), &bool1);
-    CPPUNIT_ASSERT( r == true );
-    CPPUNIT_ASSERT( bool1 == true );
+    CPPUNIT_ASSERT( r );
+    CPPUNIT_ASSERT_EQUAL( true, bool1 );
+
+    CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 );
+
+#ifdef wxHAS_CONFIG_TEMPLATE_RW
     wxColour color1;
     r = config->Read(wxT("color1"), &color1);
     wxColour color1;
     r = config->Read(wxT("color1"), &color1);
-    CPPUNIT_ASSERT( r == true );
+    CPPUNIT_ASSERT( r );
     CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) );
     CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) );
+
+    CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 );
+#endif // wxHAS_CONFIG_TEMPLATE_RW
+
     config->DeleteAll();
     delete config;
 }
 
     config->DeleteAll();
     delete config;
 }
 
-void ConfigTestCase::RecordingDefaultsTest()
+int ConfigTestCase::ReadValues(wxConfig *config, bool has_values)
 {
 {
-    wxString app = wxT("wxConfigTestCaseRD");
-    wxString vendor = wxT("wxWidgets");
-    wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
-                                    wxCONFIG_USE_LOCAL_FILE);
-    config->DeleteAll();
-    config->SetRecordDefaults(false); // by default it is false
-    wxString string1, string2, string3, string4;
-    string1 = config->Read(wxT("string1"), wxT("abc"));
-    string2 = config->Read(wxT("string2"), wxString(wxT("def")));
-    config->Read(wxT("string3"), &string3, wxT("abc"));
-    config->Read(wxT("string4"), &string4, wxString(wxT("def")));
-    int int1, int2;
-    config->Read(wxT("int1"), &int1, 123);
-    int2 = config->Read(wxT("int2"), 1234);
-    long long1;
-    config->Read(wxString(wxT("long1")), &long1, 234L);
-    double double1;
-    config->Read(wxT("double1"), &double1, 345.67);
-    bool bool1;
-    config->Read(wxT("bool1"), &bool1, true);
-    wxColour color1;
-    config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
+    int read = 0;
+    bool r;
 
 
-    CPPUNIT_ASSERT ( config->GetNumberOfEntries() == 0 );
+    wxString string1 = config->Read(wxT("string1"), wxT("abc"));
+    read++;
 
 
-    config->SetRecordDefaults(true);
+    wxString string2 = config->Read(wxT("string2"), wxString(wxT("def")));
+    read++;
 
 
-    bool r;
-    string1 = config->Read(wxT("string1"), wxT("abc"));
-    string2 = config->Read(wxT("string2"), wxString(wxT("def")));
+    wxString string3;
     r = config->Read(wxT("string3"), &string3, wxT("abc"));
     r = config->Read(wxT("string3"), &string3, wxT("abc"));
-    CPPUNIT_ASSERT( r == false );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
+
+    wxString string4;
     r = config->Read(wxT("string4"), &string4, wxString(wxT("def")));
     r = config->Read(wxT("string4"), &string4, wxString(wxT("def")));
-    CPPUNIT_ASSERT( r == false );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
+
+    int int1;
     r = config->Read(wxT("int1"), &int1, 123);
     r = config->Read(wxT("int1"), &int1, 123);
-    CPPUNIT_ASSERT( r == false );
-    int2 = config->Read(wxT("int2"), 1234);
-    r = config->Read(wxString(wxT("long1")), &long1, 234L);
-    CPPUNIT_ASSERT( r == false );
-    r = config->Read(wxT("double1"), &double1, 345.67);
-    CPPUNIT_ASSERT( r == false );
-    r = config->Read(wxT("bool1"), &bool1, true);
-    CPPUNIT_ASSERT( r == false );
-    r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
-    CPPUNIT_ASSERT( r == false );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
 
 
-    CPPUNIT_ASSERT ( config->GetNumberOfEntries() == 10 );
+    int int2 = config->Read(wxT("int2"), 1234);
+    CPPUNIT_ASSERT_EQUAL( int2, 1234 );
+    read++;
 
 
-    r = config->Read(wxT("string3"), &string3, wxT("abc"));
-    CPPUNIT_ASSERT( r == true );
-    r = config->Read(wxT("string4"), &string4, wxString(wxT("def")));
-    CPPUNIT_ASSERT( r == true );
-    r = config->Read(wxT("int1"), &int1, 123);
-    CPPUNIT_ASSERT( r == true );
+    long long1;
     r = config->Read(wxString(wxT("long1")), &long1, 234L);
     r = config->Read(wxString(wxT("long1")), &long1, 234L);
-    CPPUNIT_ASSERT( r == true );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
+
+    double double1;
     r = config->Read(wxT("double1"), &double1, 345.67);
     r = config->Read(wxT("double1"), &double1, 345.67);
-    CPPUNIT_ASSERT( r == true );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
+
+    bool bool1;
     r = config->Read(wxT("bool1"), &bool1, true);
     r = config->Read(wxT("bool1"), &bool1, true);
-    CPPUNIT_ASSERT( r == true );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
+
+#ifdef wxHAS_CONFIG_TEMPLATE_RW
+    wxColour color1;
     r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
     r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
-    CPPUNIT_ASSERT( r == true );
+    CPPUNIT_ASSERT_EQUAL( has_values, r );
+    read++;
+#endif // wxHAS_CONFIG_TEMPLATE_RW
+
+    return read;
+}
+
 
 
+void ConfigTestCase::RecordingDefaultsTest()
+{
+    wxString app = wxT("wxConfigTestCaseRD");
+    wxString vendor = wxT("wxWidgets");
+    wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
+                                    wxCONFIG_USE_LOCAL_FILE);
+    config->DeleteAll();
+    config->SetRecordDefaults(false); // by default it is false
+    ReadValues(config, false);
+    CPPUNIT_ASSERT_EQUAL( 0, config->GetNumberOfEntries() );
+    config->SetRecordDefaults(true);
+    int read = ReadValues(config, false);
+    CPPUNIT_ASSERT_EQUAL( read, config->GetNumberOfEntries() );
+    ReadValues(config, true);
     config->DeleteAll();
     delete config;
 }
 
     config->DeleteAll();
     delete config;
 }
 
-
+#endif //wxUSE_CONFIG