]>
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 // NOTE: this test is compiled in test_gui because it uses wxColour for
11 // its testing purpose.
12 // See also tests/config/fileconf.cpp for wxFileConfig specific tests and
13 // tests/config/regconf.cpp for wxRegConfig specific tests.
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
29 #include "wx/config.h"
30 #include "wx/colour.h"
32 // --------------------------------------------------------------------------
34 // --------------------------------------------------------------------------
36 class ConfigTestCase
: public CppUnit::TestCase
42 CPPUNIT_TEST_SUITE( ConfigTestCase
);
43 CPPUNIT_TEST( ReadWriteLocalTest
);
44 CPPUNIT_TEST( RecordingDefaultsTest
);
45 CPPUNIT_TEST_SUITE_END();
47 void ReadWriteLocalTest();
48 void RecordingDefaultsTest();
50 // return the number of values we (attempted to) read
51 int ReadValues(wxConfig
*config
, bool has_values
);
53 DECLARE_NO_COPY_CLASS(ConfigTestCase
)
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase
);
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase
, "ConfigTestCase" );
62 void ConfigTestCase::ReadWriteLocalTest()
64 wxString app
= wxT("wxConfigTestCase");
65 wxString vendor
= wxT("wxWidgets");
66 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
67 wxCONFIG_USE_LOCAL_FILE
);
69 config
->Write(wxT("string1"), wxT("abc"));
70 config
->Write(wxT("string2"), wxString(wxT("def")));
71 config
->Write(wxT("int1"), 123);
72 config
->Write(wxString(wxT("long1")), 234L);
73 config
->Write(wxT("double1"), 345.67);
74 config
->Write(wxT("bool1"), true);
75 #ifdef wxHAS_CONFIG_TEMPLATE_RW
76 config
->Write(wxT("color1"), wxColour(11,22,33,44));
77 #endif // wxHAS_CONFIG_TEMPLATE_RW
81 config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
82 wxCONFIG_USE_LOCAL_FILE
);
83 wxString string1
= config
->Read(wxT("string1"));
84 CPPUNIT_ASSERT_EQUAL( "abc", string1
);
85 string1
= config
->Read(wxT("string1"), wxT("defaultvalue"));
86 CPPUNIT_ASSERT_EQUAL( "abc", string1
);
89 bool r
= config
->Read(wxT("string2"), &string2
);
91 CPPUNIT_ASSERT_EQUAL( "def", string2
);
93 r
= config
->Read(wxT("string2"), &string2
, wxT("defaultvalue"));
95 CPPUNIT_ASSERT_EQUAL( "def", string2
);
97 int int1
= config
->Read(wxT("int1"), 5);
98 CPPUNIT_ASSERT_EQUAL( 123, int1
);
101 r
= config
->Read(wxT("long1"), &long1
);
103 CPPUNIT_ASSERT_EQUAL( 234L, long1
);
105 CPPUNIT_ASSERT( config
->ReadLong(wxT("long1"), 0) == 234 );
108 r
= config
->Read(wxT("double1"), &double1
);
110 CPPUNIT_ASSERT_EQUAL( 345.67, double1
);
112 CPPUNIT_ASSERT( config
->ReadDouble(wxT("double1"), 0) == double1
);
115 r
= config
->Read(wxT("foo"), &bool1
); // there is no "foo" key
116 CPPUNIT_ASSERT( !r
);
118 r
= config
->Read(wxT("bool1"), &bool1
);
120 CPPUNIT_ASSERT_EQUAL( true, bool1
);
122 CPPUNIT_ASSERT( config
->ReadBool(wxT("bool1"), false) == bool1
);
124 #ifdef wxHAS_CONFIG_TEMPLATE_RW
126 r
= config
->Read(wxT("color1"), &color1
);
128 CPPUNIT_ASSERT( color1
== wxColour(11,22,33,44) );
130 CPPUNIT_ASSERT( config
->ReadObject(wxT("color1"), wxNullColour
) == color1
);
131 #endif // wxHAS_CONFIG_TEMPLATE_RW
137 int ConfigTestCase::ReadValues(wxConfig
*config
, bool has_values
)
142 wxString string1
= config
->Read(wxT("string1"), wxT("abc"));
145 wxString string2
= config
->Read(wxT("string2"), wxString(wxT("def")));
149 r
= config
->Read(wxT("string3"), &string3
, wxT("abc"));
150 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
154 r
= config
->Read(wxT("string4"), &string4
, wxString(wxT("def")));
155 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
159 r
= config
->Read(wxT("int1"), &int1
, 123);
160 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
163 int int2
= config
->Read(wxT("int2"), 1234);
164 CPPUNIT_ASSERT_EQUAL( int2
, 1234 );
168 r
= config
->Read(wxString(wxT("long1")), &long1
, 234L);
169 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
173 r
= config
->Read(wxT("double1"), &double1
, 345.67);
174 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
178 r
= config
->Read(wxT("bool1"), &bool1
, true);
179 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
182 #ifdef wxHAS_CONFIG_TEMPLATE_RW
184 r
= config
->Read(wxT("color1"), &color1
, wxColour(11,22,33,44));
185 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
187 #endif // wxHAS_CONFIG_TEMPLATE_RW
193 void ConfigTestCase::RecordingDefaultsTest()
195 wxString app
= wxT("wxConfigTestCaseRD");
196 wxString vendor
= wxT("wxWidgets");
197 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
198 wxCONFIG_USE_LOCAL_FILE
);
200 config
->SetRecordDefaults(false); // by default it is false
201 ReadValues(config
, false);
202 CPPUNIT_ASSERT_EQUAL( 0, config
->GetNumberOfEntries() );
203 config
->SetRecordDefaults(true);
204 int read
= ReadValues(config
, false);
205 CPPUNIT_ASSERT_EQUAL( read
, config
->GetNumberOfEntries() );
206 ReadValues(config
, true);