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