]>
git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/fileconf/fileconf.cpp
3 // Purpose: wxFileConf unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
29 static const wxChar
*testconfig
=
33 _T("[root/group1/subgroup]\n")
34 _T("subentry=subvalue\n")
35 _T("subentry2=subvalue2\n")
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 static wxString
Dump(wxFileConfig
& fc
)
45 wxStringOutputStream sos
;
47 return wxTextFile::Translate(sos
.GetString(), wxTextFileType_Unix
);
50 // helper macro to test wxFileConfig contents
51 #define wxVERIFY_FILECONFIG(t, fc) CPPUNIT_ASSERT_EQUAL(wxString(t), Dump(fc))
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class FileConfigTestCase
: public CppUnit::TestCase
60 FileConfigTestCase() { }
63 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
65 CPPUNIT_TEST( AddEntries
);
66 CPPUNIT_TEST( GetEntries
);
67 CPPUNIT_TEST( GetGroups
);
68 CPPUNIT_TEST( HasEntry
);
69 CPPUNIT_TEST( HasGroup
);
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();
94 void CreateEntriesAndSubgroup();
95 void CreateSubgroupAndEntries();
96 void DeleteLastGroup();
97 void DeleteAndRecreateGroup();
99 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
106 void CheckGroupEntries(const wxFileConfig
& fc
,
110 void CheckGroupSubgroups(const wxFileConfig
& fc
,
115 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
118 // register in the unnamed registry so that these tests are run by default
119 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
121 // also include in it's own registry so that these tests can be run alone
122 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
124 void FileConfigTestCase::Path()
126 wxStringInputStream
sis(testconfig
);
127 wxFileConfig
fc(sis
);
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") );
137 void FileConfigTestCase::AddEntries()
141 wxVERIFY_FILECONFIG( _T(""), fc
);
143 fc
.Write(_T("/Foo"), _T("foo"));
144 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc
);
146 fc
.Write(_T("/Bar/Baz"), _T("baz"));
147 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
150 wxVERIFY_FILECONFIG( _T(""), fc
);
152 fc
.Write(_T("/Bar/Baz"), _T("baz"));
153 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc
);
155 fc
.Write(_T("/Foo"), _T("foo"));
156 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
160 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
165 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
167 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
170 va_start(ap
, nEntries
);
174 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
176 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
178 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
181 CPPUNIT_ASSERT( nEntries
== 0 );
187 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
192 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
194 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
197 va_start(ap
, nGroups
);
201 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
203 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
205 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
208 CPPUNIT_ASSERT( nGroups
== 0 );
213 void FileConfigTestCase::GetEntries()
215 wxStringInputStream
sis(testconfig
);
216 wxFileConfig
fc(sis
);
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"));
225 void FileConfigTestCase::GetGroups()
227 wxStringInputStream
sis(testconfig
);
228 wxFileConfig
fc(sis
);
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);
236 void FileConfigTestCase::HasEntry()
238 wxStringInputStream
sis(testconfig
);
239 wxFileConfig
fc(sis
);
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")) );
252 void FileConfigTestCase::HasGroup()
254 wxStringInputStream
sis(testconfig
);
255 wxFileConfig
fc(sis
);
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")) );
268 void FileConfigTestCase::Save()
270 wxStringInputStream
sis(testconfig
);
271 wxFileConfig
fc(sis
);
272 wxVERIFY_FILECONFIG( testconfig
, fc
);
275 void FileConfigTestCase::DeleteEntry()
277 wxStringInputStream
sis(testconfig
);
278 wxFileConfig
fc(sis
);
280 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
282 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
283 wxVERIFY_FILECONFIG( _T("[root]\n")
285 _T("[root/group1]\n")
286 _T("[root/group1/subgroup]\n")
287 _T("subentry2=subvalue2\n")
288 _T("[root/group2]\n"),
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")
296 _T("[root/group1]\n")
297 _T("[root/group2]\n"),
301 void FileConfigTestCase::DeleteGroup()
303 wxStringInputStream
sis(testconfig
);
304 wxFileConfig
fc(sis
);
306 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
308 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
309 wxVERIFY_FILECONFIG( _T("[root]\n")
311 _T("[root/group2]\n"),
314 // notice trailing slash: it should be ignored
315 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2/")) );
316 wxVERIFY_FILECONFIG( _T("[root]\n")
320 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
321 CPPUNIT_ASSERT( Dump(fc
).empty() );
324 void FileConfigTestCase::DeleteAll()
326 wxStringInputStream
sis(testconfig
);
327 wxFileConfig
fc(sis
);
329 CPPUNIT_ASSERT( fc
.DeleteAll() );
330 CPPUNIT_ASSERT( Dump(fc
).empty() );
333 void FileConfigTestCase::RenameEntry()
335 wxStringInputStream
sis(testconfig
);
336 wxFileConfig
fc(sis
);
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"),
349 fc
.SetPath(_T("group1/subgroup"));
350 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
351 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
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"),
364 void FileConfigTestCase::RenameGroup()
366 wxStringInputStream
sis(testconfig
);
367 wxFileConfig
fc(sis
);
369 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
370 wxVERIFY_FILECONFIG( _T("[foot]\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"),
379 // renaming a path doesn't work, it must be the immediate group
380 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("foot/group1"), _T("group2")) );
383 fc
.SetPath(_T("foot"));
385 // renaming to a name of existing group doesn't work
386 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("group1"), _T("group2")) );
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")
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"),
399 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
400 wxVERIFY_FILECONFIG( _T("[foot]\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"),
409 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
410 wxVERIFY_FILECONFIG( _T("[foot]\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"),
419 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
420 wxVERIFY_FILECONFIG( _T("[foot]\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"),
429 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
430 wxVERIFY_FILECONFIG( _T("[foot]\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"),
439 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
440 wxVERIFY_FILECONFIG( _T("[foot]\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"),
450 void FileConfigTestCase::CreateSubgroupAndEntries()
453 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
454 fc
.Write(_T("first"), _T("one"));
456 wxVERIFY_FILECONFIG( _T("first=one\n")
458 _T("sub_first=sub_one\n"),
462 void FileConfigTestCase::CreateEntriesAndSubgroup()
465 fc
.Write(_T("first"), _T("one"));
466 fc
.Write(_T("second"), _T("two"));
467 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
469 wxVERIFY_FILECONFIG( _T("first=one\n")
472 _T("sub_first=sub_one\n"),
476 static void EmptyConfigAndWriteKey()
478 wxFileConfig
fc(_T("deleteconftest"));
480 const wxString groupPath
= _T("/root");
482 if ( fc
.Exists(groupPath
) )
484 // using DeleteGroup exposes the problem, using DeleteAll doesn't
485 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
488 // the config must be empty for the problem to arise
489 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
490 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
493 // this crashes on second call of this function
494 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
497 void FileConfigTestCase::DeleteLastGroup()
500 We make 2 of the same calls, first to create a file config with a single
503 ::EmptyConfigAndWriteKey();
506 ... then the same but this time the key's group is deleted before the
507 key is written again. This causes a crash.
509 ::EmptyConfigAndWriteKey();
514 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
517 void FileConfigTestCase::DeleteAndRecreateGroup()
519 static const wxChar
*confInitial
=
525 wxStringInputStream
sis(confInitial
);
526 wxFileConfig
fc(sis
);
528 fc
.DeleteGroup(_T("Second"));
529 wxVERIFY_FILECONFIG( _T("[First]\n")
533 fc
.Write(_T("Second/Value2"), _T("New"));
534 wxVERIFY_FILECONFIG( _T("[First]\n")
541 #endif // wxUSE_FILECONFIG