]>
git.saurik.com Git - wxWidgets.git/blob - tests/config/fileconf.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
);
70 CPPUNIT_TEST( Binary
);
72 CPPUNIT_TEST( DeleteEntry
);
73 CPPUNIT_TEST( DeleteAndWriteEntry
);
74 CPPUNIT_TEST( DeleteGroup
);
75 CPPUNIT_TEST( DeleteAll
);
76 CPPUNIT_TEST( RenameEntry
);
77 CPPUNIT_TEST( RenameGroup
);
78 CPPUNIT_TEST( CreateEntriesAndSubgroup
);
79 CPPUNIT_TEST( CreateSubgroupAndEntries
);
80 CPPUNIT_TEST( DeleteLastGroup
);
81 CPPUNIT_TEST( DeleteAndRecreateGroup
);
82 CPPUNIT_TEST( AddToExistingRoot
);
83 CPPUNIT_TEST_SUITE_END();
94 void DeleteAndWriteEntry();
99 void CreateEntriesAndSubgroup();
100 void CreateSubgroupAndEntries();
101 void DeleteLastGroup();
102 void DeleteAndRecreateGroup();
103 void AddToExistingRoot();
106 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
113 void CheckGroupEntries(const wxFileConfig
& fc
,
117 void CheckGroupSubgroups(const wxFileConfig
& fc
,
122 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
125 // register in the unnamed registry so that these tests are run by default
126 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
128 // also include in it's own registry so that these tests can be run alone
129 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
131 void FileConfigTestCase::Path()
133 wxStringInputStream
sis(testconfig
);
134 wxFileConfig
fc(sis
);
136 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
137 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
138 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
139 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
140 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
141 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
144 void FileConfigTestCase::AddEntries()
148 wxVERIFY_FILECONFIG( _T(""), fc
);
150 fc
.Write(_T("/Foo"), _T("foo"));
151 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc
);
153 fc
.Write(_T("/Bar/Baz"), _T("baz"));
154 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
157 wxVERIFY_FILECONFIG( _T(""), fc
);
159 fc
.Write(_T("/Bar/Baz"), _T("baz"));
160 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc
);
162 fc
.Write(_T("/Foo"), _T("foo"));
163 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
167 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
172 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
174 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
177 va_start(ap
, nEntries
);
181 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
183 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
185 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
188 CPPUNIT_ASSERT( nEntries
== 0 );
194 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
199 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
201 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
204 va_start(ap
, nGroups
);
208 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
210 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
212 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
215 CPPUNIT_ASSERT( nGroups
== 0 );
220 void FileConfigTestCase::GetEntries()
222 wxStringInputStream
sis(testconfig
);
223 wxFileConfig
fc(sis
);
225 CheckGroupEntries(fc
, _T(""), 0);
226 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
227 CheckGroupEntries(fc
, _T("/root/group1"), 0);
228 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
229 2, _T("subentry"), _T("subentry2"));
232 void FileConfigTestCase::GetGroups()
234 wxStringInputStream
sis(testconfig
);
235 wxFileConfig
fc(sis
);
237 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
238 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
239 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
240 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
243 void FileConfigTestCase::HasEntry()
245 wxStringInputStream
sis(testconfig
);
246 wxFileConfig
fc(sis
);
248 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
249 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
250 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
251 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
252 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
253 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
254 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
255 CPPUNIT_ASSERT( !fc
.HasEntry(_T("/root/no_such_group/entry")) );
256 CPPUNIT_ASSERT( !fc
.HasGroup(_T("/root/no_such_group")) );
259 void FileConfigTestCase::HasGroup()
261 wxStringInputStream
sis(testconfig
);
262 wxFileConfig
fc(sis
);
264 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
265 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
266 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
267 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
268 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
269 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
270 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
271 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot/subgroup")) );
272 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
275 void FileConfigTestCase::Binary()
277 wxStringInputStream
sis(
281 wxFileConfig
fc(sis
);
284 fc
.Read("/root/binary", &buf
);
286 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
289 buf
.AppendData("\0\1\2", 3);
290 fc
.Write("/root/012", buf
);
299 void FileConfigTestCase::Save()
301 wxStringInputStream
sis(testconfig
);
302 wxFileConfig
fc(sis
);
303 wxVERIFY_FILECONFIG( testconfig
, fc
);
306 void FileConfigTestCase::DeleteEntry()
308 wxStringInputStream
sis(testconfig
);
309 wxFileConfig
fc(sis
);
311 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
313 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
314 wxVERIFY_FILECONFIG( _T("[root]\n")
316 _T("[root/group1]\n")
317 _T("[root/group1/subgroup]\n")
318 _T("subentry2=subvalue2\n")
319 _T("[root/group2]\n"),
322 // group should be deleted now as well as it became empty
323 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
324 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
325 wxVERIFY_FILECONFIG( _T("[root]\n")
327 _T("[root/group1]\n")
328 _T("[root/group2]\n"),
332 void FileConfigTestCase::DeleteAndWriteEntry()
334 wxStringInputStream
sis(
336 "subentry=subvalue\n"
337 "subentry2=subvalue2\n"
338 "subentry3=subvalue3\n"
341 wxFileConfig
fc(sis
);
343 fc
.DeleteEntry("/root/group1/subentry2");
344 fc
.Write("/root/group1/subentry2", "testvalue");
345 fc
.DeleteEntry("/root/group2/subentry2");
346 fc
.Write("/root/group2/subentry2", "testvalue2");
347 fc
.DeleteEntry("/root/group1/subentry2");
348 fc
.Write("/root/group1/subentry2", "testvalue");
349 fc
.DeleteEntry("/root/group2/subentry2");
350 fc
.Write("/root/group2/subentry2", "testvalue2");
352 wxVERIFY_FILECONFIG( "[root/group1]\n"
353 "subentry=subvalue\n"
354 "subentry3=subvalue3\n"
355 "subentry2=testvalue\n"
357 "subentry2=testvalue2\n",
360 fc
.DeleteEntry("/root/group2/subentry2");
361 wxVERIFY_FILECONFIG( "[root/group1]\n"
362 "subentry=subvalue\n"
363 "subentry3=subvalue3\n"
364 "subentry2=testvalue\n",
367 fc
.DeleteEntry("/root/group1/subentry2");
368 fc
.DeleteEntry("/root/group1/subentry");
369 fc
.DeleteEntry("/root/group1/subentry3");
370 wxVERIFY_FILECONFIG( "", fc
);
373 void FileConfigTestCase::DeleteGroup()
375 wxStringInputStream
sis(testconfig
);
376 wxFileConfig
fc(sis
);
378 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
380 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
381 wxVERIFY_FILECONFIG( _T("[root]\n")
383 _T("[root/group2]\n"),
386 // notice trailing slash: it should be ignored
387 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2/")) );
388 wxVERIFY_FILECONFIG( _T("[root]\n")
392 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
393 CPPUNIT_ASSERT( Dump(fc
).empty() );
396 void FileConfigTestCase::DeleteAll()
398 wxStringInputStream
sis(testconfig
);
399 wxFileConfig
fc(sis
);
401 CPPUNIT_ASSERT( fc
.DeleteAll() );
402 CPPUNIT_ASSERT( Dump(fc
).empty() );
405 void FileConfigTestCase::RenameEntry()
407 wxStringInputStream
sis(testconfig
);
408 wxFileConfig
fc(sis
);
410 fc
.SetPath(_T("root"));
411 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
412 wxVERIFY_FILECONFIG( _T("[root]\n")
413 _T("newname=value\n")
414 _T("[root/group1]\n")
415 _T("[root/group1/subgroup]\n")
416 _T("subentry=subvalue\n")
417 _T("subentry2=subvalue2\n")
418 _T("[root/group2]\n"),
421 fc
.SetPath(_T("group1/subgroup"));
422 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
423 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
425 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
426 wxVERIFY_FILECONFIG( _T("[root]\n")
427 _T("newname=value\n")
428 _T("[root/group1]\n")
429 _T("[root/group1/subgroup]\n")
430 _T("subentry2=subvalue2\n")
431 _T("subentry1=subvalue\n")
432 _T("[root/group2]\n"),
436 void FileConfigTestCase::RenameGroup()
438 wxStringInputStream
sis(testconfig
);
439 wxFileConfig
fc(sis
);
441 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
442 wxVERIFY_FILECONFIG( _T("[foot]\n")
444 _T("[foot/group1]\n")
445 _T("[foot/group1/subgroup]\n")
446 _T("subentry=subvalue\n")
447 _T("subentry2=subvalue2\n")
448 _T("[foot/group2]\n"),
451 // renaming a path doesn't work, it must be the immediate group
452 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("foot/group1"), _T("group2")) );
455 fc
.SetPath(_T("foot"));
457 // renaming to a name of existing group doesn't work
458 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("group1"), _T("group2")) );
460 // try exchanging the groups names and then restore them back
461 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
462 wxVERIFY_FILECONFIG( _T("[foot]\n")
464 _T("[foot/groupTmp]\n")
465 _T("[foot/groupTmp/subgroup]\n")
466 _T("subentry=subvalue\n")
467 _T("subentry2=subvalue2\n")
468 _T("[foot/group2]\n"),
471 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
472 wxVERIFY_FILECONFIG( _T("[foot]\n")
474 _T("[foot/groupTmp]\n")
475 _T("[foot/groupTmp/subgroup]\n")
476 _T("subentry=subvalue\n")
477 _T("subentry2=subvalue2\n")
478 _T("[foot/group1]\n"),
481 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
482 wxVERIFY_FILECONFIG( _T("[foot]\n")
484 _T("[foot/group2]\n")
485 _T("[foot/group2/subgroup]\n")
486 _T("subentry=subvalue\n")
487 _T("subentry2=subvalue2\n")
488 _T("[foot/group1]\n"),
491 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
492 wxVERIFY_FILECONFIG( _T("[foot]\n")
494 _T("[foot/group2]\n")
495 _T("[foot/group2/subgroup]\n")
496 _T("subentry=subvalue\n")
497 _T("subentry2=subvalue2\n")
498 _T("[foot/groupTmp]\n"),
501 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
502 wxVERIFY_FILECONFIG( _T("[foot]\n")
504 _T("[foot/group1]\n")
505 _T("[foot/group1/subgroup]\n")
506 _T("subentry=subvalue\n")
507 _T("subentry2=subvalue2\n")
508 _T("[foot/groupTmp]\n"),
511 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
512 wxVERIFY_FILECONFIG( _T("[foot]\n")
514 _T("[foot/group1]\n")
515 _T("[foot/group1/subgroup]\n")
516 _T("subentry=subvalue\n")
517 _T("subentry2=subvalue2\n")
518 _T("[foot/group2]\n"),
522 void FileConfigTestCase::CreateSubgroupAndEntries()
525 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
526 fc
.Write(_T("first"), _T("one"));
528 wxVERIFY_FILECONFIG( _T("first=one\n")
530 _T("sub_first=sub_one\n"),
534 void FileConfigTestCase::CreateEntriesAndSubgroup()
537 fc
.Write(_T("first"), _T("one"));
538 fc
.Write(_T("second"), _T("two"));
539 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
541 wxVERIFY_FILECONFIG( _T("first=one\n")
544 _T("sub_first=sub_one\n"),
548 static void EmptyConfigAndWriteKey()
550 wxFileConfig
fc(_T("deleteconftest"));
552 const wxString groupPath
= _T("/root");
554 if ( fc
.Exists(groupPath
) )
556 // using DeleteGroup exposes the problem, using DeleteAll doesn't
557 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
560 // the config must be empty for the problem to arise
561 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
562 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
565 // this crashes on second call of this function
566 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
569 void FileConfigTestCase::DeleteLastGroup()
572 We make 2 of the same calls, first to create a file config with a single
575 ::EmptyConfigAndWriteKey();
578 ... then the same but this time the key's group is deleted before the
579 key is written again. This causes a crash.
581 ::EmptyConfigAndWriteKey();
586 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
589 void FileConfigTestCase::DeleteAndRecreateGroup()
591 static const wxChar
*confInitial
=
597 wxStringInputStream
sis(confInitial
);
598 wxFileConfig
fc(sis
);
600 fc
.DeleteGroup(_T("Second"));
601 wxVERIFY_FILECONFIG( _T("[First]\n")
605 fc
.Write(_T("Second/Value2"), _T("New"));
606 wxVERIFY_FILECONFIG( _T("[First]\n")
613 void FileConfigTestCase::AddToExistingRoot()
615 static const wxChar
*confInitial
=
619 wxStringInputStream
sis(confInitial
);
620 wxFileConfig
fc(sis
);
622 fc
.Write(_T("/value1"), _T("bar"));
631 #endif // wxUSE_FILECONFIG