| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: tests/config/config.cpp |
| 3 | // Purpose: wxConfig unit test |
| 4 | // Author: Marcin Wojdyr |
| 5 | // Created: 2007-07-07 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2007 Marcin Wojdyr |
| 8 | /////////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // see also tests/fileconf/fileconftest.cpp for wxFileConfig specific tests |
| 11 | |
| 12 | // ---------------------------------------------------------------------------- |
| 13 | // headers |
| 14 | // ---------------------------------------------------------------------------- |
| 15 | |
| 16 | #include "testprec.h" |
| 17 | |
| 18 | #ifdef __BORLANDC__ |
| 19 | #pragma hdrstop |
| 20 | #endif |
| 21 | |
| 22 | #ifndef WX_PRECOMP |
| 23 | #include "wx/wx.h" |
| 24 | #endif // WX_PRECOMP |
| 25 | |
| 26 | #include "wx/config.h" |
| 27 | #include "wx/colour.h" |
| 28 | |
| 29 | // -------------------------------------------------------------------------- |
| 30 | // test class |
| 31 | // -------------------------------------------------------------------------- |
| 32 | |
| 33 | class ConfigTestCase : public CppUnit::TestCase |
| 34 | { |
| 35 | public: |
| 36 | ConfigTestCase() {} |
| 37 | |
| 38 | private: |
| 39 | CPPUNIT_TEST_SUITE( ConfigTestCase ); |
| 40 | CPPUNIT_TEST( ReadWriteLocalTest ); |
| 41 | CPPUNIT_TEST( RecordingDefaultsTest ); |
| 42 | CPPUNIT_TEST_SUITE_END(); |
| 43 | |
| 44 | void ReadWriteLocalTest(); |
| 45 | void RecordingDefaultsTest(); |
| 46 | |
| 47 | // return the number of values we (attempted to) read |
| 48 | int ReadValues(wxConfig *config, bool has_values); |
| 49 | |
| 50 | DECLARE_NO_COPY_CLASS(ConfigTestCase) |
| 51 | }; |
| 52 | |
| 53 | // register in the unnamed registry so that these tests are run by default |
| 54 | CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase ); |
| 55 | |
| 56 | // also include in it's own registry so that these tests can be run alone |
| 57 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" ); |
| 58 | |
| 59 | void ConfigTestCase::ReadWriteLocalTest() |
| 60 | { |
| 61 | wxString app = wxT("wxConfigTestCase"); |
| 62 | wxString vendor = wxT("wxWidgets"); |
| 63 | wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""), |
| 64 | wxCONFIG_USE_LOCAL_FILE); |
| 65 | config->DeleteAll(); |
| 66 | config->Write(wxT("string1"), wxT("abc")); |
| 67 | config->Write(wxT("string2"), wxString(wxT("def"))); |
| 68 | config->Write(wxT("int1"), 123); |
| 69 | config->Write(wxString(wxT("long1")), 234L); |
| 70 | config->Write(wxT("double1"), 345.67); |
| 71 | config->Write(wxT("bool1"), true); |
| 72 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
| 73 | config->Write(wxT("color1"), wxColour(11,22,33,44)); |
| 74 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
| 75 | config->Flush(); |
| 76 | delete config; |
| 77 | |
| 78 | config = new wxConfig(app, vendor, wxT(""), wxT(""), |
| 79 | wxCONFIG_USE_LOCAL_FILE); |
| 80 | wxString string1 = config->Read(wxT("string1")); |
| 81 | CPPUNIT_ASSERT_EQUAL( "abc", string1 ); |
| 82 | string1 = config->Read(wxT("string1"), wxT("defaultvalue")); |
| 83 | CPPUNIT_ASSERT_EQUAL( "abc", string1 ); |
| 84 | |
| 85 | wxString string2; |
| 86 | bool r = config->Read(wxT("string2"), &string2); |
| 87 | CPPUNIT_ASSERT( r ); |
| 88 | CPPUNIT_ASSERT_EQUAL( "def", string2 ); |
| 89 | |
| 90 | r = config->Read(wxT("string2"), &string2, wxT("defaultvalue")); |
| 91 | CPPUNIT_ASSERT( r ); |
| 92 | CPPUNIT_ASSERT_EQUAL( "def", string2 ); |
| 93 | |
| 94 | int int1 = config->Read(wxT("int1"), 5); |
| 95 | CPPUNIT_ASSERT_EQUAL( 123, int1 ); |
| 96 | |
| 97 | long long1; |
| 98 | r = config->Read(wxT("long1"), &long1); |
| 99 | CPPUNIT_ASSERT( r ); |
| 100 | CPPUNIT_ASSERT_EQUAL( 234L, long1 ); |
| 101 | |
| 102 | CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 ); |
| 103 | |
| 104 | double double1; |
| 105 | r = config->Read(wxT("double1"), &double1); |
| 106 | CPPUNIT_ASSERT( r ); |
| 107 | CPPUNIT_ASSERT_EQUAL( 345.67, double1 ); |
| 108 | |
| 109 | CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 ); |
| 110 | |
| 111 | bool bool1; |
| 112 | r = config->Read(wxT("foo"), &bool1); // there is no "foo" key |
| 113 | CPPUNIT_ASSERT( !r ); |
| 114 | |
| 115 | r = config->Read(wxT("bool1"), &bool1); |
| 116 | CPPUNIT_ASSERT( r ); |
| 117 | CPPUNIT_ASSERT_EQUAL( true, bool1 ); |
| 118 | |
| 119 | CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 ); |
| 120 | |
| 121 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
| 122 | wxColour color1; |
| 123 | r = config->Read(wxT("color1"), &color1); |
| 124 | CPPUNIT_ASSERT( r ); |
| 125 | CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) ); |
| 126 | |
| 127 | CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 ); |
| 128 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
| 129 | |
| 130 | config->DeleteAll(); |
| 131 | delete config; |
| 132 | } |
| 133 | |
| 134 | int ConfigTestCase::ReadValues(wxConfig *config, bool has_values) |
| 135 | { |
| 136 | int read = 0; |
| 137 | bool r; |
| 138 | |
| 139 | wxString string1 = config->Read(wxT("string1"), wxT("abc")); |
| 140 | read++; |
| 141 | |
| 142 | wxString string2 = config->Read(wxT("string2"), wxString(wxT("def"))); |
| 143 | read++; |
| 144 | |
| 145 | wxString string3; |
| 146 | r = config->Read(wxT("string3"), &string3, wxT("abc")); |
| 147 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 148 | read++; |
| 149 | |
| 150 | wxString string4; |
| 151 | r = config->Read(wxT("string4"), &string4, wxString(wxT("def"))); |
| 152 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 153 | read++; |
| 154 | |
| 155 | int int1; |
| 156 | r = config->Read(wxT("int1"), &int1, 123); |
| 157 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 158 | read++; |
| 159 | |
| 160 | int int2 = config->Read(wxT("int2"), 1234); |
| 161 | CPPUNIT_ASSERT_EQUAL( int2, 1234 ); |
| 162 | read++; |
| 163 | |
| 164 | long long1; |
| 165 | r = config->Read(wxString(wxT("long1")), &long1, 234L); |
| 166 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 167 | read++; |
| 168 | |
| 169 | double double1; |
| 170 | r = config->Read(wxT("double1"), &double1, 345.67); |
| 171 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 172 | read++; |
| 173 | |
| 174 | bool bool1; |
| 175 | r = config->Read(wxT("bool1"), &bool1, true); |
| 176 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 177 | read++; |
| 178 | |
| 179 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
| 180 | wxColour color1; |
| 181 | r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44)); |
| 182 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
| 183 | read++; |
| 184 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
| 185 | |
| 186 | return read; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void ConfigTestCase::RecordingDefaultsTest() |
| 191 | { |
| 192 | wxString app = wxT("wxConfigTestCaseRD"); |
| 193 | wxString vendor = wxT("wxWidgets"); |
| 194 | wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""), |
| 195 | wxCONFIG_USE_LOCAL_FILE); |
| 196 | config->DeleteAll(); |
| 197 | config->SetRecordDefaults(false); // by default it is false |
| 198 | ReadValues(config, false); |
| 199 | CPPUNIT_ASSERT_EQUAL( 0, config->GetNumberOfEntries() ); |
| 200 | config->SetRecordDefaults(true); |
| 201 | int read = ReadValues(config, false); |
| 202 | CPPUNIT_ASSERT_EQUAL( read, config->GetNumberOfEntries() ); |
| 203 | ReadValues(config, true); |
| 204 | config->DeleteAll(); |
| 205 | delete config; |
| 206 | } |
| 207 | |
| 208 | |