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