]> git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.cpp
use MSWAlwaysDrawBg() to fix the problem with black background; rewrote/cleaned up...
[wxWidgets.git] / tests / fileconf / fileconftest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/fileconf/fileconf.cpp
3 // Purpose: wxFileConf unit test
4 // Author: Vadim Zeitlin
5 // Created: 2004-09-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_FILECONFIG
21
22 #ifndef WX_PRECOMP
23 #endif // WX_PRECOMP
24
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
27
28 static const wxChar *testconfig =
29 _T("[root]\n")
30 _T("entry=value\n")
31 _T("[root/group1]\n")
32 _T("[root/group1/subgroup]\n")
33 _T("subentry=subvalue\n")
34 _T("subentry2=subvalue2\n")
35 _T("[root/group2]\n")
36 ;
37
38 // ----------------------------------------------------------------------------
39 // test class
40 // ----------------------------------------------------------------------------
41
42 class FileConfigTestCase : public CppUnit::TestCase
43 {
44 public:
45 FileConfigTestCase() { }
46
47 private:
48 CPPUNIT_TEST_SUITE( FileConfigTestCase );
49 CPPUNIT_TEST( Path );
50 CPPUNIT_TEST( AddEntries );
51 CPPUNIT_TEST( GetEntries );
52 CPPUNIT_TEST( GetGroups );
53 CPPUNIT_TEST( HasEntry );
54 CPPUNIT_TEST( HasGroup );
55 CPPUNIT_TEST( Save );
56 CPPUNIT_TEST( DeleteEntry );
57 CPPUNIT_TEST( DeleteGroup );
58 CPPUNIT_TEST( DeleteAll );
59 CPPUNIT_TEST( RenameEntry );
60 CPPUNIT_TEST( RenameGroup );
61 CPPUNIT_TEST_SUITE_END();
62
63 void Path();
64 void AddEntries();
65 void GetEntries();
66 void GetGroups();
67 void HasEntry();
68 void HasGroup();
69 void Save();
70 void DeleteEntry();
71 void DeleteGroup();
72 void DeleteAll();
73 void RenameEntry();
74 void RenameGroup();
75
76 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
77 {
78 fc.SetPath(path);
79
80 return fc.GetPath();
81 }
82
83 static wxString Dump(wxFileConfig& fc)
84 {
85 wxStringOutputStream sos;
86 fc.Save(sos);
87 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
88 }
89
90 void CheckGroupEntries(const wxFileConfig& fc,
91 const wxChar *path,
92 size_t nEntries,
93 ...);
94 void CheckGroupSubgroups(const wxFileConfig& fc,
95 const wxChar *path,
96 size_t nGroups,
97 ...);
98
99 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
100 };
101
102 // register in the unnamed registry so that these tests are run by default
103 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
104
105 // also include in it's own registry so that these tests can be run alone
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
107
108 void FileConfigTestCase::Path()
109 {
110 wxStringInputStream sis(testconfig);
111 wxFileConfig fc(sis);
112
113 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
114 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
115 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
116 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
117 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
118 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
119 }
120
121 void FileConfigTestCase::AddEntries()
122 {
123 wxFileConfig fc;
124
125 CPPUNIT_ASSERT( Dump(fc) == _T("") );
126
127 fc.Write(_T("/Foo"), _T("foo"));
128 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n") );
129
130 fc.Write(_T("/Bar/Baz"), _T("baz"));
131 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
132
133 fc.DeleteAll();
134 CPPUNIT_ASSERT( Dump(fc) == _T("") );
135
136 fc.Write(_T("/Bar/Baz"), _T("baz"));
137 CPPUNIT_ASSERT( Dump(fc) == _T("[Bar]\nBaz=baz\n") );
138
139 fc.Write(_T("/Foo"), _T("foo"));
140 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
141 }
142
143 void
144 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
145 const wxChar *path,
146 size_t nEntries,
147 ...)
148 {
149 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
150
151 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
152
153 va_list ap;
154 va_start(ap, nEntries);
155
156 long cookie;
157 wxString name;
158 for ( bool cont = fc.GetFirstEntry(name, cookie);
159 cont;
160 cont = fc.GetNextEntry(name, cookie), nEntries-- )
161 {
162 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
163 }
164
165 CPPUNIT_ASSERT( nEntries == 0 );
166
167 va_end(ap);
168 }
169
170 void
171 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
172 const wxChar *path,
173 size_t nGroups,
174 ...)
175 {
176 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
177
178 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
179
180 va_list ap;
181 va_start(ap, nGroups);
182
183 long cookie;
184 wxString name;
185 for ( bool cont = fc.GetFirstGroup(name, cookie);
186 cont;
187 cont = fc.GetNextGroup(name, cookie), nGroups-- )
188 {
189 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
190 }
191
192 CPPUNIT_ASSERT( nGroups == 0 );
193
194 va_end(ap);
195 }
196
197 void FileConfigTestCase::GetEntries()
198 {
199 wxStringInputStream sis(testconfig);
200 wxFileConfig fc(sis);
201
202 CheckGroupEntries(fc, _T(""), 0);
203 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
204 CheckGroupEntries(fc, _T("/root/group1"), 0);
205 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
206 2, _T("subentry"), _T("subentry2"));
207 }
208
209 void FileConfigTestCase::GetGroups()
210 {
211 wxStringInputStream sis(testconfig);
212 wxFileConfig fc(sis);
213
214 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
215 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
216 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
217 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
218 }
219
220 void FileConfigTestCase::HasEntry()
221 {
222 wxStringInputStream sis(testconfig);
223 wxFileConfig fc(sis);
224
225 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
226 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
227 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
228 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
229 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
230 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
231 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
232 }
233
234 void FileConfigTestCase::HasGroup()
235 {
236 wxStringInputStream sis(testconfig);
237 wxFileConfig fc(sis);
238
239 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
240 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
241 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
242 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
243 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
244 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
245 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
246 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
247 }
248
249 void FileConfigTestCase::Save()
250 {
251 wxStringInputStream sis(testconfig);
252 wxFileConfig fc(sis);
253 CPPUNIT_ASSERT( Dump(fc) == testconfig );
254 }
255
256 void FileConfigTestCase::DeleteEntry()
257 {
258 wxStringInputStream sis(testconfig);
259 wxFileConfig fc(sis);
260
261 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
262
263 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
264 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
265 _T("entry=value\n")
266 _T("[root/group1]\n")
267 _T("[root/group1/subgroup]\n")
268 _T("subentry2=subvalue2\n")
269 _T("[root/group2]\n") );
270
271 // group should be deleted now as well as it became empty
272 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
273 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
274 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
275 _T("entry=value\n")
276 _T("[root/group1]\n")
277 _T("[root/group2]\n") );
278 }
279
280 void FileConfigTestCase::DeleteGroup()
281 {
282 wxStringInputStream sis(testconfig);
283 wxFileConfig fc(sis);
284
285 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
286
287 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
288 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
289 _T("entry=value\n")
290 _T("[root/group2]\n") );
291
292 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2")) );
293 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
294 _T("entry=value\n") );
295
296 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
297 CPPUNIT_ASSERT( Dump(fc).empty() );
298 }
299
300 void FileConfigTestCase::DeleteAll()
301 {
302 wxStringInputStream sis(testconfig);
303 wxFileConfig fc(sis);
304
305 CPPUNIT_ASSERT( fc.DeleteAll() );
306 CPPUNIT_ASSERT( Dump(fc).empty() );
307 }
308
309 void FileConfigTestCase::RenameEntry()
310 {
311 wxStringInputStream sis(testconfig);
312 wxFileConfig fc(sis);
313
314 fc.SetPath(_T("root"));
315 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
316 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
317 _T("newname=value\n")
318 _T("[root/group1]\n")
319 _T("[root/group1/subgroup]\n")
320 _T("subentry=subvalue\n")
321 _T("subentry2=subvalue2\n")
322 _T("[root/group2]\n") );
323
324 fc.SetPath(_T("group1/subgroup"));
325 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
326 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
327
328 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
329 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
330 _T("newname=value\n")
331 _T("[root/group1]\n")
332 _T("[root/group1/subgroup]\n")
333 _T("subentry2=subvalue2\n")
334 _T("subentry1=subvalue\n")
335 _T("[root/group2]\n") );
336 }
337
338 void FileConfigTestCase::RenameGroup()
339 {
340 wxStringInputStream sis(testconfig);
341 wxFileConfig fc(sis);
342
343 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
344 CPPUNIT_ASSERT( Dump(fc) == _T("[foot]\n")
345 _T("entry=value\n")
346 _T("[foot/group1]\n")
347 _T("[foot/group1/subgroup]\n")
348 _T("subentry=subvalue\n")
349 _T("subentry2=subvalue2\n")
350 _T("[foot/group2]\n") );
351 }
352
353 #endif // wxUSE_FILECONFIG
354