]>
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( ReadNonExistent
);
84 CPPUNIT_TEST_SUITE_END();
95 void DeleteAndWriteEntry();
100 void CreateEntriesAndSubgroup();
101 void CreateSubgroupAndEntries();
102 void DeleteLastGroup();
103 void DeleteAndRecreateGroup();
104 void AddToExistingRoot();
105 void ReadNonExistent();
108 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
115 void CheckGroupEntries(const wxFileConfig
& fc
,
119 void CheckGroupSubgroups(const wxFileConfig
& fc
,
124 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
127 // register in the unnamed registry so that these tests are run by default
128 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
130 // also include in it's own registry so that these tests can be run alone
131 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
133 void FileConfigTestCase::Path()
135 wxStringInputStream
sis(testconfig
);
136 wxFileConfig
fc(sis
);
138 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
139 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
140 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
141 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
142 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
143 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
146 void FileConfigTestCase::AddEntries()
150 wxVERIFY_FILECONFIG( _T(""), fc
);
152 fc
.Write(_T("/Foo"), _T("foo"));
153 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc
);
155 fc
.Write(_T("/Bar/Baz"), _T("baz"));
156 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
159 wxVERIFY_FILECONFIG( _T(""), fc
);
161 fc
.Write(_T("/Bar/Baz"), _T("baz"));
162 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc
);
164 fc
.Write(_T("/Foo"), _T("foo"));
165 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
169 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
174 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
176 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
179 va_start(ap
, nEntries
);
183 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
185 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
187 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
190 CPPUNIT_ASSERT( nEntries
== 0 );
196 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
201 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
203 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
206 va_start(ap
, nGroups
);
210 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
212 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
214 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
217 CPPUNIT_ASSERT( nGroups
== 0 );
222 void FileConfigTestCase::GetEntries()
224 wxStringInputStream
sis(testconfig
);
225 wxFileConfig
fc(sis
);
227 CheckGroupEntries(fc
, _T(""), 0);
228 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
229 CheckGroupEntries(fc
, _T("/root/group1"), 0);
230 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
231 2, _T("subentry"), _T("subentry2"));
234 void FileConfigTestCase::GetGroups()
236 wxStringInputStream
sis(testconfig
);
237 wxFileConfig
fc(sis
);
239 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
240 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
241 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
242 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
245 void FileConfigTestCase::HasEntry()
247 wxStringInputStream
sis(testconfig
);
248 wxFileConfig
fc(sis
);
250 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
251 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
252 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
253 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
254 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
255 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
256 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
257 CPPUNIT_ASSERT( !fc
.HasEntry(_T("/root/no_such_group/entry")) );
258 CPPUNIT_ASSERT( !fc
.HasGroup(_T("/root/no_such_group")) );
261 void FileConfigTestCase::HasGroup()
263 wxStringInputStream
sis(testconfig
);
264 wxFileConfig
fc(sis
);
266 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
267 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
268 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
269 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
270 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
271 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
272 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
273 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot/subgroup")) );
274 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
277 void FileConfigTestCase::Binary()
279 wxStringInputStream
sis(
283 wxFileConfig
fc(sis
);
286 fc
.Read("/root/binary", &buf
);
288 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
291 buf
.AppendData("\0\1\2", 3);
292 fc
.Write("/root/012", buf
);
301 void FileConfigTestCase::Save()
303 wxStringInputStream
sis(testconfig
);
304 wxFileConfig
fc(sis
);
305 wxVERIFY_FILECONFIG( testconfig
, fc
);
308 void FileConfigTestCase::DeleteEntry()
310 wxStringInputStream
sis(testconfig
);
311 wxFileConfig
fc(sis
);
313 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
315 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
316 wxVERIFY_FILECONFIG( _T("[root]\n")
318 _T("[root/group1]\n")
319 _T("[root/group1/subgroup]\n")
320 _T("subentry2=subvalue2\n")
321 _T("[root/group2]\n"),
324 // group should be deleted now as well as it became empty
325 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
326 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
327 wxVERIFY_FILECONFIG( _T("[root]\n")
329 _T("[root/group1]\n")
330 _T("[root/group2]\n"),
334 void FileConfigTestCase::DeleteAndWriteEntry()
336 wxStringInputStream
sis(
338 "subentry=subvalue\n"
339 "subentry2=subvalue2\n"
340 "subentry3=subvalue3\n"
343 wxFileConfig
fc(sis
);
345 fc
.DeleteEntry("/root/group1/subentry2");
346 fc
.Write("/root/group1/subentry2", "testvalue");
347 fc
.DeleteEntry("/root/group2/subentry2");
348 fc
.Write("/root/group2/subentry2", "testvalue2");
349 fc
.DeleteEntry("/root/group1/subentry2");
350 fc
.Write("/root/group1/subentry2", "testvalue");
351 fc
.DeleteEntry("/root/group2/subentry2");
352 fc
.Write("/root/group2/subentry2", "testvalue2");
354 wxVERIFY_FILECONFIG( "[root/group1]\n"
355 "subentry=subvalue\n"
356 "subentry3=subvalue3\n"
357 "subentry2=testvalue\n"
359 "subentry2=testvalue2\n",
362 fc
.DeleteEntry("/root/group2/subentry2");
363 wxVERIFY_FILECONFIG( "[root/group1]\n"
364 "subentry=subvalue\n"
365 "subentry3=subvalue3\n"
366 "subentry2=testvalue\n",
369 fc
.DeleteEntry("/root/group1/subentry2");
370 fc
.DeleteEntry("/root/group1/subentry");
371 fc
.DeleteEntry("/root/group1/subentry3");
372 wxVERIFY_FILECONFIG( "", fc
);
375 void FileConfigTestCase::DeleteGroup()
377 wxStringInputStream
sis(testconfig
);
378 wxFileConfig
fc(sis
);
380 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
382 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
383 wxVERIFY_FILECONFIG( _T("[root]\n")
385 _T("[root/group2]\n"),
388 // notice trailing slash: it should be ignored
389 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2/")) );
390 wxVERIFY_FILECONFIG( _T("[root]\n")
394 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
395 CPPUNIT_ASSERT( Dump(fc
).empty() );
398 void FileConfigTestCase::DeleteAll()
400 wxStringInputStream
sis(testconfig
);
401 wxFileConfig
fc(sis
);
403 CPPUNIT_ASSERT( fc
.DeleteAll() );
404 CPPUNIT_ASSERT( Dump(fc
).empty() );
407 void FileConfigTestCase::RenameEntry()
409 wxStringInputStream
sis(testconfig
);
410 wxFileConfig
fc(sis
);
412 fc
.SetPath(_T("root"));
413 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
414 wxVERIFY_FILECONFIG( _T("[root]\n")
415 _T("newname=value\n")
416 _T("[root/group1]\n")
417 _T("[root/group1/subgroup]\n")
418 _T("subentry=subvalue\n")
419 _T("subentry2=subvalue2\n")
420 _T("[root/group2]\n"),
423 fc
.SetPath(_T("group1/subgroup"));
424 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
425 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
427 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
428 wxVERIFY_FILECONFIG( _T("[root]\n")
429 _T("newname=value\n")
430 _T("[root/group1]\n")
431 _T("[root/group1/subgroup]\n")
432 _T("subentry2=subvalue2\n")
433 _T("subentry1=subvalue\n")
434 _T("[root/group2]\n"),
438 void FileConfigTestCase::RenameGroup()
440 wxStringInputStream
sis(testconfig
);
441 wxFileConfig
fc(sis
);
443 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
444 wxVERIFY_FILECONFIG( _T("[foot]\n")
446 _T("[foot/group1]\n")
447 _T("[foot/group1/subgroup]\n")
448 _T("subentry=subvalue\n")
449 _T("subentry2=subvalue2\n")
450 _T("[foot/group2]\n"),
453 // renaming a path doesn't work, it must be the immediate group
454 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("foot/group1"), _T("group2")) );
457 fc
.SetPath(_T("foot"));
459 // renaming to a name of existing group doesn't work
460 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("group1"), _T("group2")) );
462 // try exchanging the groups names and then restore them back
463 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
464 wxVERIFY_FILECONFIG( _T("[foot]\n")
466 _T("[foot/groupTmp]\n")
467 _T("[foot/groupTmp/subgroup]\n")
468 _T("subentry=subvalue\n")
469 _T("subentry2=subvalue2\n")
470 _T("[foot/group2]\n"),
473 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
474 wxVERIFY_FILECONFIG( _T("[foot]\n")
476 _T("[foot/groupTmp]\n")
477 _T("[foot/groupTmp/subgroup]\n")
478 _T("subentry=subvalue\n")
479 _T("subentry2=subvalue2\n")
480 _T("[foot/group1]\n"),
483 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
484 wxVERIFY_FILECONFIG( _T("[foot]\n")
486 _T("[foot/group2]\n")
487 _T("[foot/group2/subgroup]\n")
488 _T("subentry=subvalue\n")
489 _T("subentry2=subvalue2\n")
490 _T("[foot/group1]\n"),
493 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
494 wxVERIFY_FILECONFIG( _T("[foot]\n")
496 _T("[foot/group2]\n")
497 _T("[foot/group2/subgroup]\n")
498 _T("subentry=subvalue\n")
499 _T("subentry2=subvalue2\n")
500 _T("[foot/groupTmp]\n"),
503 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
504 wxVERIFY_FILECONFIG( _T("[foot]\n")
506 _T("[foot/group1]\n")
507 _T("[foot/group1/subgroup]\n")
508 _T("subentry=subvalue\n")
509 _T("subentry2=subvalue2\n")
510 _T("[foot/groupTmp]\n"),
513 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
514 wxVERIFY_FILECONFIG( _T("[foot]\n")
516 _T("[foot/group1]\n")
517 _T("[foot/group1/subgroup]\n")
518 _T("subentry=subvalue\n")
519 _T("subentry2=subvalue2\n")
520 _T("[foot/group2]\n"),
524 void FileConfigTestCase::CreateSubgroupAndEntries()
527 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
528 fc
.Write(_T("first"), _T("one"));
530 wxVERIFY_FILECONFIG( _T("first=one\n")
532 _T("sub_first=sub_one\n"),
536 void FileConfigTestCase::CreateEntriesAndSubgroup()
539 fc
.Write(_T("first"), _T("one"));
540 fc
.Write(_T("second"), _T("two"));
541 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
543 wxVERIFY_FILECONFIG( _T("first=one\n")
546 _T("sub_first=sub_one\n"),
550 static void EmptyConfigAndWriteKey()
552 wxFileConfig
fc(_T("deleteconftest"));
554 const wxString groupPath
= _T("/root");
556 if ( fc
.Exists(groupPath
) )
558 // using DeleteGroup exposes the problem, using DeleteAll doesn't
559 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
562 // the config must be empty for the problem to arise
563 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
564 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
567 // this crashes on second call of this function
568 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
571 void FileConfigTestCase::DeleteLastGroup()
574 We make 2 of the same calls, first to create a file config with a single
577 ::EmptyConfigAndWriteKey();
580 ... then the same but this time the key's group is deleted before the
581 key is written again. This causes a crash.
583 ::EmptyConfigAndWriteKey();
588 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
591 void FileConfigTestCase::DeleteAndRecreateGroup()
593 static const wxChar
*confInitial
=
599 wxStringInputStream
sis(confInitial
);
600 wxFileConfig
fc(sis
);
602 fc
.DeleteGroup(_T("Second"));
603 wxVERIFY_FILECONFIG( _T("[First]\n")
607 fc
.Write(_T("Second/Value2"), _T("New"));
608 wxVERIFY_FILECONFIG( _T("[First]\n")
615 void FileConfigTestCase::AddToExistingRoot()
617 static const wxChar
*confInitial
=
621 wxStringInputStream
sis(confInitial
);
622 wxFileConfig
fc(sis
);
624 fc
.Write(_T("/value1"), _T("bar"));
633 void FileConfigTestCase::ReadNonExistent()
635 static const char *confTest
=
636 "community=censored\n"
645 wxStringInputStream
sis(confTest
);
646 wxFileConfig
fc(sis
);
649 CPPUNIT_ASSERT( !fc
.Read("URL", &url
) );
652 #endif // wxUSE_FILECONFIG