]>
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
7 // Copyright: (c) 2007 Marcin Wojdyr
8 ///////////////////////////////////////////////////////////////////////////////
10 // see also tests/fileconf/fileconftest.cpp for wxFileConfig specific tests
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
26 #include "wx/config.h"
27 #include "wx/colour.h"
29 // --------------------------------------------------------------------------
31 // --------------------------------------------------------------------------
33 class ConfigTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( ConfigTestCase
);
40 CPPUNIT_TEST( ReadWriteLocalTest
);
41 CPPUNIT_TEST( RecordingDefaultsTest
);
42 CPPUNIT_TEST_SUITE_END();
44 void ReadWriteLocalTest();
45 void RecordingDefaultsTest();
47 // return the number of values we (attempted to) read
48 int ReadValues(wxConfig
*config
, bool has_values
);
50 DECLARE_NO_COPY_CLASS(ConfigTestCase
)
53 // register in the unnamed registry so that these tests are run by default
54 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase
);
56 // also include in it's own registry so that these tests can be run alone
57 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase
, "ConfigTestCase" );
59 void ConfigTestCase::ReadWriteLocalTest()
61 wxString app
= wxT("wxConfigTestCase");
62 wxString vendor
= wxT("wxWidgets");
63 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
64 wxCONFIG_USE_LOCAL_FILE
);
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
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
);
86 bool r
= config
->Read(wxT("string2"), &string2
);
88 CPPUNIT_ASSERT_EQUAL( "def", string2
);
90 r
= config
->Read(wxT("string2"), &string2
, wxT("defaultvalue"));
92 CPPUNIT_ASSERT_EQUAL( "def", string2
);
94 int int1
= config
->Read(wxT("int1"), 5);
95 CPPUNIT_ASSERT_EQUAL( 123, int1
);
98 r
= config
->Read(wxT("long1"), &long1
);
100 CPPUNIT_ASSERT_EQUAL( 234L, long1
);
102 CPPUNIT_ASSERT( config
->ReadLong(wxT("long1"), 0) == 234 );
105 r
= config
->Read(wxT("double1"), &double1
);
107 CPPUNIT_ASSERT_EQUAL( 345.67, double1
);
109 CPPUNIT_ASSERT( config
->ReadDouble(wxT("double1"), 0) == double1
);
112 r
= config
->Read(wxT("foo"), &bool1
); // there is no "foo" key
113 CPPUNIT_ASSERT( !r
);
115 r
= config
->Read(wxT("bool1"), &bool1
);
117 CPPUNIT_ASSERT_EQUAL( true, bool1
);
119 CPPUNIT_ASSERT( config
->ReadBool(wxT("bool1"), false) == bool1
);
121 #ifdef wxHAS_CONFIG_TEMPLATE_RW
123 r
= config
->Read(wxT("color1"), &color1
);
125 CPPUNIT_ASSERT( color1
== wxColour(11,22,33,44) );
127 CPPUNIT_ASSERT( config
->ReadObject(wxT("color1"), wxNullColour
) == color1
);
128 #endif // wxHAS_CONFIG_TEMPLATE_RW
134 int ConfigTestCase::ReadValues(wxConfig
*config
, bool has_values
)
139 wxString string1
= config
->Read(wxT("string1"), wxT("abc"));
142 wxString string2
= config
->Read(wxT("string2"), wxString(wxT("def")));
146 r
= config
->Read(wxT("string3"), &string3
, wxT("abc"));
147 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
151 r
= config
->Read(wxT("string4"), &string4
, wxString(wxT("def")));
152 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
156 r
= config
->Read(wxT("int1"), &int1
, 123);
157 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
160 int int2
= config
->Read(wxT("int2"), 1234);
161 CPPUNIT_ASSERT_EQUAL( int2
, 1234 );
165 r
= config
->Read(wxString(wxT("long1")), &long1
, 234L);
166 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
170 r
= config
->Read(wxT("double1"), &double1
, 345.67);
171 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
175 r
= config
->Read(wxT("bool1"), &bool1
, true);
176 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
179 #ifdef wxHAS_CONFIG_TEMPLATE_RW
181 r
= config
->Read(wxT("color1"), &color1
, wxColour(11,22,33,44));
182 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
184 #endif // wxHAS_CONFIG_TEMPLATE_RW
190 void ConfigTestCase::RecordingDefaultsTest()
192 wxString app
= wxT("wxConfigTestCaseRD");
193 wxString vendor
= wxT("wxWidgets");
194 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
195 wxCONFIG_USE_LOCAL_FILE
);
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);