]> git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.cpp
remove sole makefile.dmc in tree
[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 // local functions
41 // ----------------------------------------------------------------------------
42
43 static wxString Dump(wxFileConfig& fc)
44 {
45 wxStringOutputStream sos;
46 fc.Save(sos);
47 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
48 }
49
50 // helper macro to test wxFileConfig contents
51 #define wxVERIFY_FILECONFIG(t, fc) CPPUNIT_ASSERT_EQUAL(wxString(t), Dump(fc))
52
53 // ----------------------------------------------------------------------------
54 // test class
55 // ----------------------------------------------------------------------------
56
57 class FileConfigTestCase : public CppUnit::TestCase
58 {
59 public:
60 FileConfigTestCase() { }
61
62 private:
63 CPPUNIT_TEST_SUITE( FileConfigTestCase );
64 CPPUNIT_TEST( Path );
65 CPPUNIT_TEST( AddEntries );
66 CPPUNIT_TEST( GetEntries );
67 CPPUNIT_TEST( GetGroups );
68 CPPUNIT_TEST( HasEntry );
69 CPPUNIT_TEST( HasGroup );
70 CPPUNIT_TEST( Save );
71 CPPUNIT_TEST( DeleteEntry );
72 CPPUNIT_TEST( DeleteGroup );
73 CPPUNIT_TEST( DeleteAll );
74 CPPUNIT_TEST( RenameEntry );
75 CPPUNIT_TEST( RenameGroup );
76 CPPUNIT_TEST( CreateEntriesAndSubgroup );
77 CPPUNIT_TEST( CreateSubgroupAndEntries );
78 CPPUNIT_TEST( DeleteLastGroup );
79 CPPUNIT_TEST( DeleteAndRecreateGroup );
80 CPPUNIT_TEST_SUITE_END();
81
82 void Path();
83 void AddEntries();
84 void GetEntries();
85 void GetGroups();
86 void HasEntry();
87 void HasGroup();
88 void Save();
89 void DeleteEntry();
90 void DeleteGroup();
91 void DeleteAll();
92 void RenameEntry();
93 void RenameGroup();
94 void CreateEntriesAndSubgroup();
95 void CreateSubgroupAndEntries();
96 void DeleteLastGroup();
97 void DeleteAndRecreateGroup();
98
99 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
100 {
101 fc.SetPath(path);
102
103 return fc.GetPath();
104 }
105
106 void CheckGroupEntries(const wxFileConfig& fc,
107 const wxChar *path,
108 size_t nEntries,
109 ...);
110 void CheckGroupSubgroups(const wxFileConfig& fc,
111 const wxChar *path,
112 size_t nGroups,
113 ...);
114
115 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
116 };
117
118 // register in the unnamed registry so that these tests are run by default
119 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
120
121 // also include in it's own registry so that these tests can be run alone
122 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
123
124 void FileConfigTestCase::Path()
125 {
126 wxStringInputStream sis(testconfig);
127 wxFileConfig fc(sis);
128
129 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
130 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
131 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
132 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
133 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
134 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
135 }
136
137 void FileConfigTestCase::AddEntries()
138 {
139 wxFileConfig fc;
140
141 wxVERIFY_FILECONFIG( _T(""), fc );
142
143 fc.Write(_T("/Foo"), _T("foo"));
144 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc );
145
146 fc.Write(_T("/Bar/Baz"), _T("baz"));
147 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
148
149 fc.DeleteAll();
150 wxVERIFY_FILECONFIG( _T(""), fc );
151
152 fc.Write(_T("/Bar/Baz"), _T("baz"));
153 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc );
154
155 fc.Write(_T("/Foo"), _T("foo"));
156 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
157 }
158
159 void
160 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
161 const wxChar *path,
162 size_t nEntries,
163 ...)
164 {
165 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
166
167 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
168
169 va_list ap;
170 va_start(ap, nEntries);
171
172 long cookie;
173 wxString name;
174 for ( bool cont = fc.GetFirstEntry(name, cookie);
175 cont;
176 cont = fc.GetNextEntry(name, cookie), nEntries-- )
177 {
178 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
179 }
180
181 CPPUNIT_ASSERT( nEntries == 0 );
182
183 va_end(ap);
184 }
185
186 void
187 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
188 const wxChar *path,
189 size_t nGroups,
190 ...)
191 {
192 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
193
194 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
195
196 va_list ap;
197 va_start(ap, nGroups);
198
199 long cookie;
200 wxString name;
201 for ( bool cont = fc.GetFirstGroup(name, cookie);
202 cont;
203 cont = fc.GetNextGroup(name, cookie), nGroups-- )
204 {
205 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
206 }
207
208 CPPUNIT_ASSERT( nGroups == 0 );
209
210 va_end(ap);
211 }
212
213 void FileConfigTestCase::GetEntries()
214 {
215 wxStringInputStream sis(testconfig);
216 wxFileConfig fc(sis);
217
218 CheckGroupEntries(fc, _T(""), 0);
219 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
220 CheckGroupEntries(fc, _T("/root/group1"), 0);
221 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
222 2, _T("subentry"), _T("subentry2"));
223 }
224
225 void FileConfigTestCase::GetGroups()
226 {
227 wxStringInputStream sis(testconfig);
228 wxFileConfig fc(sis);
229
230 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
231 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
232 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
233 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
234 }
235
236 void FileConfigTestCase::HasEntry()
237 {
238 wxStringInputStream sis(testconfig);
239 wxFileConfig fc(sis);
240
241 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
242 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
243 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
244 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
245 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
246 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
247 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
248 CPPUNIT_ASSERT( !fc.HasEntry(_T("/root/no_such_group/entry")) );
249 CPPUNIT_ASSERT( !fc.HasGroup(_T("/root/no_such_group")) );
250 }
251
252 void FileConfigTestCase::HasGroup()
253 {
254 wxStringInputStream sis(testconfig);
255 wxFileConfig fc(sis);
256
257 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
258 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
259 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
260 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
261 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
262 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
263 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
264 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot/subgroup")) );
265 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
266 }
267
268 void FileConfigTestCase::Save()
269 {
270 wxStringInputStream sis(testconfig);
271 wxFileConfig fc(sis);
272 wxVERIFY_FILECONFIG( testconfig, fc );
273 }
274
275 void FileConfigTestCase::DeleteEntry()
276 {
277 wxStringInputStream sis(testconfig);
278 wxFileConfig fc(sis);
279
280 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
281
282 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
283 wxVERIFY_FILECONFIG( _T("[root]\n")
284 _T("entry=value\n")
285 _T("[root/group1]\n")
286 _T("[root/group1/subgroup]\n")
287 _T("subentry2=subvalue2\n")
288 _T("[root/group2]\n"),
289 fc );
290
291 // group should be deleted now as well as it became empty
292 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
293 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
294 wxVERIFY_FILECONFIG( _T("[root]\n")
295 _T("entry=value\n")
296 _T("[root/group1]\n")
297 _T("[root/group2]\n"),
298 fc );
299 }
300
301 void FileConfigTestCase::DeleteGroup()
302 {
303 wxStringInputStream sis(testconfig);
304 wxFileConfig fc(sis);
305
306 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
307
308 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
309 wxVERIFY_FILECONFIG( _T("[root]\n")
310 _T("entry=value\n")
311 _T("[root/group2]\n"),
312 fc );
313
314 // notice trailing slash: it should be ignored
315 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2/")) );
316 wxVERIFY_FILECONFIG( _T("[root]\n")
317 _T("entry=value\n"),
318 fc );
319
320 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
321 CPPUNIT_ASSERT( Dump(fc).empty() );
322 }
323
324 void FileConfigTestCase::DeleteAll()
325 {
326 wxStringInputStream sis(testconfig);
327 wxFileConfig fc(sis);
328
329 CPPUNIT_ASSERT( fc.DeleteAll() );
330 CPPUNIT_ASSERT( Dump(fc).empty() );
331 }
332
333 void FileConfigTestCase::RenameEntry()
334 {
335 wxStringInputStream sis(testconfig);
336 wxFileConfig fc(sis);
337
338 fc.SetPath(_T("root"));
339 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
340 wxVERIFY_FILECONFIG( _T("[root]\n")
341 _T("newname=value\n")
342 _T("[root/group1]\n")
343 _T("[root/group1/subgroup]\n")
344 _T("subentry=subvalue\n")
345 _T("subentry2=subvalue2\n")
346 _T("[root/group2]\n"),
347 fc );
348
349 fc.SetPath(_T("group1/subgroup"));
350 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
351 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
352
353 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
354 wxVERIFY_FILECONFIG( _T("[root]\n")
355 _T("newname=value\n")
356 _T("[root/group1]\n")
357 _T("[root/group1/subgroup]\n")
358 _T("subentry2=subvalue2\n")
359 _T("subentry1=subvalue\n")
360 _T("[root/group2]\n"),
361 fc );
362 }
363
364 void FileConfigTestCase::RenameGroup()
365 {
366 wxStringInputStream sis(testconfig);
367 wxFileConfig fc(sis);
368
369 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
370 wxVERIFY_FILECONFIG( _T("[foot]\n")
371 _T("entry=value\n")
372 _T("[foot/group1]\n")
373 _T("[foot/group1/subgroup]\n")
374 _T("subentry=subvalue\n")
375 _T("subentry2=subvalue2\n")
376 _T("[foot/group2]\n"),
377 fc );
378
379 // renaming a path doesn't work, it must be the immediate group
380 CPPUNIT_ASSERT( !fc.RenameGroup(_T("foot/group1"), _T("group2")) );
381
382
383 fc.SetPath(_T("foot"));
384
385 // renaming to a name of existing group doesn't work
386 CPPUNIT_ASSERT( !fc.RenameGroup(_T("group1"), _T("group2")) );
387
388 // try exchanging the groups names and then restore them back
389 CPPUNIT_ASSERT( fc.RenameGroup(_T("group1"), _T("groupTmp")) );
390 wxVERIFY_FILECONFIG( _T("[foot]\n")
391 _T("entry=value\n")
392 _T("[foot/groupTmp]\n")
393 _T("[foot/groupTmp/subgroup]\n")
394 _T("subentry=subvalue\n")
395 _T("subentry2=subvalue2\n")
396 _T("[foot/group2]\n"),
397 fc );
398
399 CPPUNIT_ASSERT( fc.RenameGroup(_T("group2"), _T("group1")) );
400 wxVERIFY_FILECONFIG( _T("[foot]\n")
401 _T("entry=value\n")
402 _T("[foot/groupTmp]\n")
403 _T("[foot/groupTmp/subgroup]\n")
404 _T("subentry=subvalue\n")
405 _T("subentry2=subvalue2\n")
406 _T("[foot/group1]\n"),
407 fc );
408
409 CPPUNIT_ASSERT( fc.RenameGroup(_T("groupTmp"), _T("group2")) );
410 wxVERIFY_FILECONFIG( _T("[foot]\n")
411 _T("entry=value\n")
412 _T("[foot/group2]\n")
413 _T("[foot/group2/subgroup]\n")
414 _T("subentry=subvalue\n")
415 _T("subentry2=subvalue2\n")
416 _T("[foot/group1]\n"),
417 fc );
418
419 CPPUNIT_ASSERT( fc.RenameGroup(_T("group1"), _T("groupTmp")) );
420 wxVERIFY_FILECONFIG( _T("[foot]\n")
421 _T("entry=value\n")
422 _T("[foot/group2]\n")
423 _T("[foot/group2/subgroup]\n")
424 _T("subentry=subvalue\n")
425 _T("subentry2=subvalue2\n")
426 _T("[foot/groupTmp]\n"),
427 fc );
428
429 CPPUNIT_ASSERT( fc.RenameGroup(_T("group2"), _T("group1")) );
430 wxVERIFY_FILECONFIG( _T("[foot]\n")
431 _T("entry=value\n")
432 _T("[foot/group1]\n")
433 _T("[foot/group1/subgroup]\n")
434 _T("subentry=subvalue\n")
435 _T("subentry2=subvalue2\n")
436 _T("[foot/groupTmp]\n"),
437 fc );
438
439 CPPUNIT_ASSERT( fc.RenameGroup(_T("groupTmp"), _T("group2")) );
440 wxVERIFY_FILECONFIG( _T("[foot]\n")
441 _T("entry=value\n")
442 _T("[foot/group1]\n")
443 _T("[foot/group1/subgroup]\n")
444 _T("subentry=subvalue\n")
445 _T("subentry2=subvalue2\n")
446 _T("[foot/group2]\n"),
447 fc );
448 }
449
450 void FileConfigTestCase::CreateSubgroupAndEntries()
451 {
452 wxFileConfig fc;
453 fc.Write(_T("sub/sub_first"), _T("sub_one"));
454 fc.Write(_T("first"), _T("one"));
455
456 wxVERIFY_FILECONFIG( _T("first=one\n")
457 _T("[sub]\n")
458 _T("sub_first=sub_one\n"),
459 fc );
460 }
461
462 void FileConfigTestCase::CreateEntriesAndSubgroup()
463 {
464 wxFileConfig fc;
465 fc.Write(_T("first"), _T("one"));
466 fc.Write(_T("second"), _T("two"));
467 fc.Write(_T("sub/sub_first"), _T("sub_one"));
468
469 wxVERIFY_FILECONFIG( _T("first=one\n")
470 _T("second=two\n")
471 _T("[sub]\n")
472 _T("sub_first=sub_one\n"),
473 fc );
474 }
475
476 static void EmptyConfigAndWriteKey()
477 {
478 wxFileConfig fc(_T("deleteconftest"));
479
480 const wxString groupPath = _T("/root");
481
482 if ( fc.Exists(groupPath) )
483 {
484 // using DeleteGroup exposes the problem, using DeleteAll doesn't
485 CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
486 }
487
488 // the config must be empty for the problem to arise
489 CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
490 CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
491
492
493 // this crashes on second call of this function
494 CPPUNIT_ASSERT( fc.Write(groupPath + _T("/entry"), _T("value")) );
495 }
496
497 void FileConfigTestCase::DeleteLastGroup()
498 {
499 /*
500 We make 2 of the same calls, first to create a file config with a single
501 group and key...
502 */
503 ::EmptyConfigAndWriteKey();
504
505 /*
506 ... then the same but this time the key's group is deleted before the
507 key is written again. This causes a crash.
508 */
509 ::EmptyConfigAndWriteKey();
510
511
512 // clean up
513 wxLogNull noLogging;
514 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
515 }
516
517 void FileConfigTestCase::DeleteAndRecreateGroup()
518 {
519 static const wxChar *confInitial =
520 _T("[First]\n")
521 _T("Value1=Foo\n")
522 _T("[Second]\n")
523 _T("Value2=Bar\n");
524
525 wxStringInputStream sis(confInitial);
526 wxFileConfig fc(sis);
527
528 fc.DeleteGroup(_T("Second"));
529 wxVERIFY_FILECONFIG( _T("[First]\n")
530 _T("Value1=Foo\n"),
531 fc );
532
533 fc.Write(_T("Second/Value2"), _T("New"));
534 wxVERIFY_FILECONFIG( _T("[First]\n")
535 _T("Value1=Foo\n")
536 _T("[Second]\n")
537 _T("Value2=New\n"),
538 fc );
539 }
540
541 #endif // wxUSE_FILECONFIG
542