]>
git.saurik.com Git - wxWidgets.git/blob - tests/config/config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/config/config.cpp
3 // Purpose: wxConfig unit test
4 // Author: Marcin Wojdyr
6 // Copyright: (c) 2007 Marcin Wojdyr
7 ///////////////////////////////////////////////////////////////////////////////
9 // NOTE: this test is compiled in test_gui because it uses wxColour for
10 // its testing purpose.
11 // See also tests/config/fileconf.cpp for wxFileConfig specific tests and
12 // tests/config/regconf.cpp for wxRegConfig specific tests.
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
30 #include "wx/config.h"
31 #include "wx/colour.h"
33 // --------------------------------------------------------------------------
35 // --------------------------------------------------------------------------
37 class ConfigTestCase
: public CppUnit::TestCase
43 CPPUNIT_TEST_SUITE( ConfigTestCase
);
44 CPPUNIT_TEST( ReadWriteLocalTest
);
45 CPPUNIT_TEST( RecordingDefaultsTest
);
46 CPPUNIT_TEST_SUITE_END();
48 void ReadWriteLocalTest();
49 void RecordingDefaultsTest();
51 // return the number of values we (attempted to) read
52 int ReadValues(wxConfig
*config
, bool has_values
);
54 DECLARE_NO_COPY_CLASS(ConfigTestCase
)
57 // register in the unnamed registry so that these tests are run by default
58 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase
);
60 // also include in its own registry so that these tests can be run alone
61 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase
, "ConfigTestCase" );
63 void ConfigTestCase::ReadWriteLocalTest()
65 wxString app
= wxT("wxConfigTestCase");
66 wxString vendor
= wxT("wxWidgets");
67 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
68 wxCONFIG_USE_LOCAL_FILE
);
70 config
->Write(wxT("string1"), wxT("abc"));
71 config
->Write(wxT("string2"), wxString(wxT("def")));
72 config
->Write(wxT("int1"), 123);
73 config
->Write(wxString(wxT("long1")), 234L);
74 config
->Write(wxT("double1"), 345.67);
75 config
->Write(wxT("bool1"), true);
76 #ifdef wxHAS_CONFIG_TEMPLATE_RW
77 config
->Write(wxT("color1"), wxColour(11,22,33,44));
78 #endif // wxHAS_CONFIG_TEMPLATE_RW
82 config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
83 wxCONFIG_USE_LOCAL_FILE
);
84 wxString string1
= config
->Read(wxT("string1"));
85 CPPUNIT_ASSERT_EQUAL( "abc", string1
);
86 string1
= config
->Read(wxT("string1"), wxT("defaultvalue"));
87 CPPUNIT_ASSERT_EQUAL( "abc", string1
);
90 bool r
= config
->Read(wxT("string2"), &string2
);
92 CPPUNIT_ASSERT_EQUAL( "def", string2
);
94 r
= config
->Read(wxT("string2"), &string2
, wxT("defaultvalue"));
96 CPPUNIT_ASSERT_EQUAL( "def", string2
);
98 int int1
= config
->Read(wxT("int1"), 5);
99 CPPUNIT_ASSERT_EQUAL( 123, int1
);
102 r
= config
->Read(wxT("long1"), &long1
);
104 CPPUNIT_ASSERT_EQUAL( 234L, long1
);
106 CPPUNIT_ASSERT( config
->ReadLong(wxT("long1"), 0) == 234 );
109 r
= config
->Read(wxT("double1"), &double1
);
111 CPPUNIT_ASSERT_EQUAL( 345.67, double1
);
113 CPPUNIT_ASSERT( config
->ReadDouble(wxT("double1"), 0) == double1
);
116 r
= config
->Read(wxT("foo"), &bool1
); // there is no "foo" key
117 CPPUNIT_ASSERT( !r
);
119 r
= config
->Read(wxT("bool1"), &bool1
);
121 CPPUNIT_ASSERT_EQUAL( true, bool1
);
123 CPPUNIT_ASSERT( config
->ReadBool(wxT("bool1"), false) == bool1
);
125 #ifdef wxHAS_CONFIG_TEMPLATE_RW
127 r
= config
->Read(wxT("color1"), &color1
);
129 CPPUNIT_ASSERT( color1
== wxColour(11,22,33,44) );
131 CPPUNIT_ASSERT( config
->ReadObject(wxT("color1"), wxNullColour
) == color1
);
132 #endif // wxHAS_CONFIG_TEMPLATE_RW
138 int ConfigTestCase::ReadValues(wxConfig
*config
, bool has_values
)
143 wxString string1
= config
->Read(wxT("string1"), wxT("abc"));
146 wxString string2
= config
->Read(wxT("string2"), wxString(wxT("def")));
150 r
= config
->Read(wxT("string3"), &string3
, wxT("abc"));
151 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
155 r
= config
->Read(wxT("string4"), &string4
, wxString(wxT("def")));
156 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
160 r
= config
->Read(wxT("int1"), &int1
, 123);
161 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
164 int int2
= config
->Read(wxT("int2"), 1234);
165 CPPUNIT_ASSERT_EQUAL( int2
, 1234 );
169 r
= config
->Read(wxString(wxT("long1")), &long1
, 234L);
170 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
174 r
= config
->Read(wxT("double1"), &double1
, 345.67);
175 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
179 r
= config
->Read(wxT("bool1"), &bool1
, true);
180 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
183 #ifdef wxHAS_CONFIG_TEMPLATE_RW
185 r
= config
->Read(wxT("color1"), &color1
, wxColour(11,22,33,44));
186 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
188 #endif // wxHAS_CONFIG_TEMPLATE_RW
194 void ConfigTestCase::RecordingDefaultsTest()
196 wxString app
= wxT("wxConfigTestCaseRD");
197 wxString vendor
= wxT("wxWidgets");
198 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
199 wxCONFIG_USE_LOCAL_FILE
);
201 config
->SetRecordDefaults(false); // by default it is false
202 ReadValues(config
, false);
203 CPPUNIT_ASSERT_EQUAL( 0, config
->GetNumberOfEntries() );
204 config
->SetRecordDefaults(true);
205 int read
= ReadValues(config
, false);
206 CPPUNIT_ASSERT_EQUAL( read
, config
->GetNumberOfEntries() );
207 ReadValues(config
, true);
212 #endif //wxUSE_CONFIG