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