]> git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.cpp
Temporary ugly trick to make release for OpenWatcom possible.
[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 #include "wx/log.h"
28
29 static const wxChar *testconfig =
30 _T("[root]\n")
31 _T("entry=value\n")
32 _T("[root/group1]\n")
33 _T("[root/group1/subgroup]\n")
34 _T("subentry=subvalue\n")
35 _T("subentry2=subvalue2\n")
36 _T("[root/group2]\n")
37 ;
38
39 // ----------------------------------------------------------------------------
40 // test class
41 // ----------------------------------------------------------------------------
42
43 class FileConfigTestCase : public CppUnit::TestCase
44 {
45 public:
46 FileConfigTestCase() { }
47
48 private:
49 CPPUNIT_TEST_SUITE( FileConfigTestCase );
50 CPPUNIT_TEST( Path );
51 CPPUNIT_TEST( AddEntries );
52 CPPUNIT_TEST( GetEntries );
53 CPPUNIT_TEST( GetGroups );
54 CPPUNIT_TEST( HasEntry );
55 CPPUNIT_TEST( HasGroup );
56 CPPUNIT_TEST( Save );
57 CPPUNIT_TEST( DeleteEntry );
58 CPPUNIT_TEST( DeleteGroup );
59 CPPUNIT_TEST( DeleteAll );
60 CPPUNIT_TEST( RenameEntry );
61 CPPUNIT_TEST( RenameGroup );
62 CPPUNIT_TEST( CreateEntriesAndSubgroup );
63 CPPUNIT_TEST( CreateSubgroupAndEntries );
64 CPPUNIT_TEST( DeleteLastGroup );
65 CPPUNIT_TEST_SUITE_END();
66
67 void Path();
68 void AddEntries();
69 void GetEntries();
70 void GetGroups();
71 void HasEntry();
72 void HasGroup();
73 void Save();
74 void DeleteEntry();
75 void DeleteGroup();
76 void DeleteAll();
77 void RenameEntry();
78 void RenameGroup();
79 void CreateEntriesAndSubgroup();
80 void CreateSubgroupAndEntries();
81 void DeleteLastGroup();
82
83 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
84 {
85 fc.SetPath(path);
86
87 return fc.GetPath();
88 }
89
90 static wxString Dump(wxFileConfig& fc)
91 {
92 wxStringOutputStream sos;
93 fc.Save(sos);
94 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
95 }
96
97 void CheckGroupEntries(const wxFileConfig& fc,
98 const wxChar *path,
99 size_t nEntries,
100 ...);
101 void CheckGroupSubgroups(const wxFileConfig& fc,
102 const wxChar *path,
103 size_t nGroups,
104 ...);
105
106 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
107 };
108
109 // register in the unnamed registry so that these tests are run by default
110 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
111
112 // also include in it's own registry so that these tests can be run alone
113 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
114
115 void FileConfigTestCase::Path()
116 {
117 wxStringInputStream sis(testconfig);
118 wxFileConfig fc(sis);
119
120 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
121 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
122 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
123 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
124 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
125 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
126 }
127
128 void FileConfigTestCase::AddEntries()
129 {
130 wxFileConfig fc;
131
132 CPPUNIT_ASSERT( Dump(fc) == _T("") );
133
134 fc.Write(_T("/Foo"), _T("foo"));
135 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n") );
136
137 fc.Write(_T("/Bar/Baz"), _T("baz"));
138 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
139
140 fc.DeleteAll();
141 CPPUNIT_ASSERT( Dump(fc) == _T("") );
142
143 fc.Write(_T("/Bar/Baz"), _T("baz"));
144 CPPUNIT_ASSERT( Dump(fc) == _T("[Bar]\nBaz=baz\n") );
145
146 fc.Write(_T("/Foo"), _T("foo"));
147 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
148 }
149
150 void
151 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
152 const wxChar *path,
153 size_t nEntries,
154 ...)
155 {
156 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
157
158 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
159
160 va_list ap;
161 va_start(ap, nEntries);
162
163 long cookie;
164 wxString name;
165 for ( bool cont = fc.GetFirstEntry(name, cookie);
166 cont;
167 cont = fc.GetNextEntry(name, cookie), nEntries-- )
168 {
169 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
170 }
171
172 CPPUNIT_ASSERT( nEntries == 0 );
173
174 va_end(ap);
175 }
176
177 void
178 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
179 const wxChar *path,
180 size_t nGroups,
181 ...)
182 {
183 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
184
185 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
186
187 va_list ap;
188 va_start(ap, nGroups);
189
190 long cookie;
191 wxString name;
192 for ( bool cont = fc.GetFirstGroup(name, cookie);
193 cont;
194 cont = fc.GetNextGroup(name, cookie), nGroups-- )
195 {
196 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
197 }
198
199 CPPUNIT_ASSERT( nGroups == 0 );
200
201 va_end(ap);
202 }
203
204 void FileConfigTestCase::GetEntries()
205 {
206 wxStringInputStream sis(testconfig);
207 wxFileConfig fc(sis);
208
209 CheckGroupEntries(fc, _T(""), 0);
210 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
211 CheckGroupEntries(fc, _T("/root/group1"), 0);
212 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
213 2, _T("subentry"), _T("subentry2"));
214 }
215
216 void FileConfigTestCase::GetGroups()
217 {
218 wxStringInputStream sis(testconfig);
219 wxFileConfig fc(sis);
220
221 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
222 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
223 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
224 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
225 }
226
227 void FileConfigTestCase::HasEntry()
228 {
229 wxStringInputStream sis(testconfig);
230 wxFileConfig fc(sis);
231
232 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
233 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
234 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
235 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
236 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
237 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
238 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
239 CPPUNIT_ASSERT( !fc.HasEntry(_T("/root/no_such_group/entry")) );
240 CPPUNIT_ASSERT( !fc.HasGroup(_T("/root/no_such_group")) );
241 }
242
243 void FileConfigTestCase::HasGroup()
244 {
245 wxStringInputStream sis(testconfig);
246 wxFileConfig fc(sis);
247
248 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
249 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
250 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
251 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
252 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
253 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
254 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
255 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot/subgroup")) );
256 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
257 }
258
259 void FileConfigTestCase::Save()
260 {
261 wxStringInputStream sis(testconfig);
262 wxFileConfig fc(sis);
263 CPPUNIT_ASSERT( Dump(fc) == testconfig );
264 }
265
266 void FileConfigTestCase::DeleteEntry()
267 {
268 wxStringInputStream sis(testconfig);
269 wxFileConfig fc(sis);
270
271 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
272
273 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
274 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
275 _T("entry=value\n")
276 _T("[root/group1]\n")
277 _T("[root/group1/subgroup]\n")
278 _T("subentry2=subvalue2\n")
279 _T("[root/group2]\n") );
280
281 // group should be deleted now as well as it became empty
282 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
283 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
284 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
285 _T("entry=value\n")
286 _T("[root/group1]\n")
287 _T("[root/group2]\n") );
288 }
289
290 void FileConfigTestCase::DeleteGroup()
291 {
292 wxStringInputStream sis(testconfig);
293 wxFileConfig fc(sis);
294
295 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
296
297 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
298 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
299 _T("entry=value\n")
300 _T("[root/group2]\n") );
301
302 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2")) );
303 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
304 _T("entry=value\n") );
305
306 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
307 CPPUNIT_ASSERT( Dump(fc).empty() );
308 }
309
310 void FileConfigTestCase::DeleteAll()
311 {
312 wxStringInputStream sis(testconfig);
313 wxFileConfig fc(sis);
314
315 CPPUNIT_ASSERT( fc.DeleteAll() );
316 CPPUNIT_ASSERT( Dump(fc).empty() );
317 }
318
319 void FileConfigTestCase::RenameEntry()
320 {
321 wxStringInputStream sis(testconfig);
322 wxFileConfig fc(sis);
323
324 fc.SetPath(_T("root"));
325 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
326 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
327 _T("newname=value\n")
328 _T("[root/group1]\n")
329 _T("[root/group1/subgroup]\n")
330 _T("subentry=subvalue\n")
331 _T("subentry2=subvalue2\n")
332 _T("[root/group2]\n") );
333
334 fc.SetPath(_T("group1/subgroup"));
335 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
336 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
337
338 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
339 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
340 _T("newname=value\n")
341 _T("[root/group1]\n")
342 _T("[root/group1/subgroup]\n")
343 _T("subentry2=subvalue2\n")
344 _T("subentry1=subvalue\n")
345 _T("[root/group2]\n") );
346 }
347
348 void FileConfigTestCase::RenameGroup()
349 {
350 wxStringInputStream sis(testconfig);
351 wxFileConfig fc(sis);
352
353 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
354 CPPUNIT_ASSERT( Dump(fc) == _T("[foot]\n")
355 _T("entry=value\n")
356 _T("[foot/group1]\n")
357 _T("[foot/group1/subgroup]\n")
358 _T("subentry=subvalue\n")
359 _T("subentry2=subvalue2\n")
360 _T("[foot/group2]\n") );
361 }
362
363 void FileConfigTestCase::CreateSubgroupAndEntries()
364 {
365 wxFileConfig fc;
366 fc.Write(_T("sub/sub_first"), _T("sub_one"));
367 fc.Write(_T("first"), _T("one"));
368
369 CPPUNIT_ASSERT( Dump(fc) == _T("first=one\n")
370 _T("[sub]\n")
371 _T("sub_first=sub_one\n"));
372 }
373
374 void FileConfigTestCase::CreateEntriesAndSubgroup()
375 {
376 wxFileConfig fc;
377 fc.Write(_T("first"), _T("one"));
378 fc.Write(_T("second"), _T("two"));
379 fc.Write(_T("sub/sub_first"), _T("sub_one"));
380
381 CPPUNIT_ASSERT( Dump(fc) == _T("first=one\n")
382 _T("second=two\n")
383 _T("[sub]\n")
384 _T("sub_first=sub_one\n"));
385 }
386
387 static void EmptyConfigAndWriteKey()
388 {
389 wxFileConfig fc(_T("deleteconftest"));
390
391 const wxString groupPath = _T("/root");
392
393 if ( fc.Exists(groupPath) )
394 {
395 // using DeleteGroup exposes the problem, using DeleteAll doesn't
396 CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
397 }
398
399 // the config must be empty for the problem to arise
400 CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
401 CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
402
403
404 // this crashes on second call of this function
405 CPPUNIT_ASSERT( fc.Write(groupPath + _T("/entry"), _T("value")) );
406 }
407
408 void FileConfigTestCase::DeleteLastGroup()
409 {
410 /*
411 We make 2 of the same calls, first to create a file config with a single
412 group and key...
413 */
414 ::EmptyConfigAndWriteKey();
415
416 /*
417 ... then the same but this time the key's group is deleted before the
418 key is written again. This causes a crash.
419 */
420 ::EmptyConfigAndWriteKey();
421
422
423 // clean up
424 wxLogNull noLogging;
425 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
426 }
427
428 #endif // wxUSE_FILECONFIG
429