]>
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
6 // Copyright: (c) 2004 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
24 #include "wx/fileconf.h"
25 #include "wx/sstream.h"
28 static const wxChar
*testconfig
=
31 wxT("[root/group1]\n")
32 wxT("[root/group1/subgroup]\n")
33 wxT("subentry=subvalue\n")
34 wxT("subentry2=subvalue2\n")
35 wxT("[root/group2]\n")
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 static wxString
Dump(wxFileConfig
& fc
)
44 wxStringOutputStream sos
;
46 return wxTextFile::Translate(sos
.GetString(), wxTextFileType_Unix
);
49 // helper macro to test wxFileConfig contents
50 #define wxVERIFY_FILECONFIG(t, fc) CPPUNIT_ASSERT_EQUAL(wxString(t), Dump(fc))
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 class FileConfigTestCase
: public CppUnit::TestCase
59 FileConfigTestCase() { }
62 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
64 CPPUNIT_TEST( AddEntries
);
65 CPPUNIT_TEST( GetEntries
);
66 CPPUNIT_TEST( GetGroups
);
67 CPPUNIT_TEST( HasEntry
);
68 CPPUNIT_TEST( HasGroup
);
69 CPPUNIT_TEST( Binary
);
71 CPPUNIT_TEST( DeleteEntry
);
72 CPPUNIT_TEST( DeleteAndWriteEntry
);
73 CPPUNIT_TEST( DeleteLastRootEntry
);
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( ReadEmpty
);
85 CPPUNIT_TEST( ReadFloat
);
86 CPPUNIT_TEST_SUITE_END();
97 void DeleteAndWriteEntry();
98 void DeleteLastRootEntry();
103 void CreateEntriesAndSubgroup();
104 void CreateSubgroupAndEntries();
105 void DeleteLastGroup();
106 void DeleteAndRecreateGroup();
107 void AddToExistingRoot();
108 void ReadNonExistent();
113 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
120 void CheckGroupEntries(const wxFileConfig
& fc
,
124 void CheckGroupSubgroups(const wxFileConfig
& fc
,
129 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
132 // register in the unnamed registry so that these tests are run by default
133 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
135 // also include in its own registry so that these tests can be run alone
136 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
138 void FileConfigTestCase::Path()
140 wxStringInputStream
sis(testconfig
);
141 wxFileConfig
fc(sis
);
143 CPPUNIT_ASSERT( ChangePath(fc
, wxT("")) == wxT("") );
144 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/")) == wxT("") );
145 CPPUNIT_ASSERT( ChangePath(fc
, wxT("root")) == wxT("/root") );
146 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root")) == wxT("/root") );
147 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group1/subgroup")) == wxT("/root/group1/subgroup") );
148 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group2")) == wxT("/root/group2") );
151 void FileConfigTestCase::AddEntries()
155 wxVERIFY_FILECONFIG( wxT(""), fc
);
157 fc
.Write(wxT("/Foo"), wxT("foo"));
158 wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc
);
160 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
161 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
164 wxVERIFY_FILECONFIG( wxT(""), fc
);
166 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
167 wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc
);
169 fc
.Write(wxT("/Foo"), wxT("foo"));
170 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
174 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
179 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
181 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
184 va_start(ap
, nEntries
);
188 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
190 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
192 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
195 CPPUNIT_ASSERT( nEntries
== 0 );
201 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
206 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
208 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
211 va_start(ap
, nGroups
);
215 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
217 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
219 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
222 CPPUNIT_ASSERT( nGroups
== 0 );
227 void FileConfigTestCase::GetEntries()
229 wxStringInputStream
sis(testconfig
);
230 wxFileConfig
fc(sis
);
232 CheckGroupEntries(fc
, wxT(""), 0);
233 CheckGroupEntries(fc
, wxT("/root"), 1, wxT("entry"));
234 CheckGroupEntries(fc
, wxT("/root/group1"), 0);
235 CheckGroupEntries(fc
, wxT("/root/group1/subgroup"),
236 2, wxT("subentry"), wxT("subentry2"));
239 void FileConfigTestCase::GetGroups()
241 wxStringInputStream
sis(testconfig
);
242 wxFileConfig
fc(sis
);
244 CheckGroupSubgroups(fc
, wxT(""), 1, wxT("root"));
245 CheckGroupSubgroups(fc
, wxT("/root"), 2, wxT("group1"), wxT("group2"));
246 CheckGroupSubgroups(fc
, wxT("/root/group1"), 1, wxT("subgroup"));
247 CheckGroupSubgroups(fc
, wxT("/root/group2"), 0);
250 void FileConfigTestCase::HasEntry()
252 wxStringInputStream
sis(testconfig
);
253 wxFileConfig
fc(sis
);
255 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root")) );
256 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/entry")) );
257 CPPUNIT_ASSERT( fc
.HasEntry(wxT("/root/entry")) );
258 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/group1/subgroup/subentry")) );
259 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("")) );
260 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root/group1")) );
261 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("subgroup/subentry")) );
262 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("/root/no_such_group/entry")) );
263 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("/root/no_such_group")) );
266 void FileConfigTestCase::HasGroup()
268 wxStringInputStream
sis(testconfig
);
269 wxFileConfig
fc(sis
);
271 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root")) );
272 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1")) );
273 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1/subgroup")) );
274 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group2")) );
275 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("")) );
276 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root/group")) );
277 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root//subgroup")) );
278 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot/subgroup")) );
279 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot")) );
282 void FileConfigTestCase::Binary()
284 wxStringInputStream
sis(
288 wxFileConfig
fc(sis
);
291 fc
.Read("/root/binary", &buf
);
293 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
296 buf
.AppendData("\0\1\2", 3);
297 fc
.Write("/root/012", buf
);
306 void FileConfigTestCase::Save()
308 wxStringInputStream
sis(testconfig
);
309 wxFileConfig
fc(sis
);
310 wxVERIFY_FILECONFIG( testconfig
, fc
);
313 void FileConfigTestCase::DeleteEntry()
315 wxStringInputStream
sis(testconfig
);
316 wxFileConfig
fc(sis
);
318 CPPUNIT_ASSERT( !fc
.DeleteEntry(wxT("foo")) );
320 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("root/group1/subgroup/subentry")) );
321 wxVERIFY_FILECONFIG( wxT("[root]\n")
323 wxT("[root/group1]\n")
324 wxT("[root/group1/subgroup]\n")
325 wxT("subentry2=subvalue2\n")
326 wxT("[root/group2]\n"),
329 // group should be deleted now as well as it became empty
330 wxConfigPathChanger
change(&fc
, wxT("root/group1/subgroup/subentry2"));
331 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("subentry2")) );
332 wxVERIFY_FILECONFIG( wxT("[root]\n")
334 wxT("[root/group1]\n")
335 wxT("[root/group2]\n"),
339 void FileConfigTestCase::DeleteAndWriteEntry()
341 wxStringInputStream
sis(
343 "subentry=subvalue\n"
344 "subentry2=subvalue2\n"
345 "subentry3=subvalue3\n"
348 wxFileConfig
fc(sis
);
350 fc
.DeleteEntry("/root/group1/subentry2");
351 fc
.Write("/root/group1/subentry2", "testvalue");
352 fc
.DeleteEntry("/root/group2/subentry2");
353 fc
.Write("/root/group2/subentry2", "testvalue2");
354 fc
.DeleteEntry("/root/group1/subentry2");
355 fc
.Write("/root/group1/subentry2", "testvalue");
356 fc
.DeleteEntry("/root/group2/subentry2");
357 fc
.Write("/root/group2/subentry2", "testvalue2");
359 wxVERIFY_FILECONFIG( "[root/group1]\n"
360 "subentry=subvalue\n"
361 "subentry3=subvalue3\n"
362 "subentry2=testvalue\n"
364 "subentry2=testvalue2\n",
367 fc
.DeleteEntry("/root/group2/subentry2");
368 wxVERIFY_FILECONFIG( "[root/group1]\n"
369 "subentry=subvalue\n"
370 "subentry3=subvalue3\n"
371 "subentry2=testvalue\n",
374 fc
.DeleteEntry("/root/group1/subentry2");
375 fc
.DeleteEntry("/root/group1/subentry");
376 fc
.DeleteEntry("/root/group1/subentry3");
377 wxVERIFY_FILECONFIG( "", fc
);
380 void FileConfigTestCase::DeleteLastRootEntry()
382 // This tests for the bug which occurred when the last entry of the root
383 // group was deleted: this corrupted internal state and resulted in a crash
384 // after trying to write the just deleted entry again.
385 wxStringInputStream
sis("");
386 wxFileConfig
fc(sis
);
388 fc
.Write("key", "value");
389 wxVERIFY_FILECONFIG( "key=value\n", fc
);
391 fc
.DeleteEntry("key");
392 wxVERIFY_FILECONFIG( "", fc
);
394 fc
.Write("key", "value");
395 wxVERIFY_FILECONFIG( "key=value\n", fc
);
398 void FileConfigTestCase::DeleteGroup()
400 wxStringInputStream
sis(testconfig
);
401 wxFileConfig
fc(sis
);
403 CPPUNIT_ASSERT( !fc
.DeleteGroup(wxT("foo")) );
405 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group1")) );
406 wxVERIFY_FILECONFIG( wxT("[root]\n")
408 wxT("[root/group2]\n"),
411 // notice trailing slash: it should be ignored
412 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group2/")) );
413 wxVERIFY_FILECONFIG( wxT("[root]\n")
414 wxT("entry=value\n"),
417 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root")) );
418 CPPUNIT_ASSERT( Dump(fc
).empty() );
421 void FileConfigTestCase::DeleteAll()
423 wxStringInputStream
sis(testconfig
);
424 wxFileConfig
fc(sis
);
426 CPPUNIT_ASSERT( fc
.DeleteAll() );
427 CPPUNIT_ASSERT( Dump(fc
).empty() );
430 void FileConfigTestCase::RenameEntry()
432 wxStringInputStream
sis(testconfig
);
433 wxFileConfig
fc(sis
);
435 fc
.SetPath(wxT("root"));
436 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("entry"), wxT("newname")) );
437 wxVERIFY_FILECONFIG( wxT("[root]\n")
438 wxT("newname=value\n")
439 wxT("[root/group1]\n")
440 wxT("[root/group1/subgroup]\n")
441 wxT("subentry=subvalue\n")
442 wxT("subentry2=subvalue2\n")
443 wxT("[root/group2]\n"),
446 fc
.SetPath(wxT("group1/subgroup"));
447 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("entry"), wxT("newname")) );
448 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("subentry"), wxT("subentry2")) );
450 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("subentry"), wxT("subentry1")) );
451 wxVERIFY_FILECONFIG( wxT("[root]\n")
452 wxT("newname=value\n")
453 wxT("[root/group1]\n")
454 wxT("[root/group1/subgroup]\n")
455 wxT("subentry2=subvalue2\n")
456 wxT("subentry1=subvalue\n")
457 wxT("[root/group2]\n"),
461 void FileConfigTestCase::RenameGroup()
463 wxStringInputStream
sis(testconfig
);
464 wxFileConfig
fc(sis
);
466 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("root"), wxT("foot")) );
467 wxVERIFY_FILECONFIG( wxT("[foot]\n")
469 wxT("[foot/group1]\n")
470 wxT("[foot/group1/subgroup]\n")
471 wxT("subentry=subvalue\n")
472 wxT("subentry2=subvalue2\n")
473 wxT("[foot/group2]\n"),
476 // renaming a path doesn't work, it must be the immediate group
477 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("foot/group1"), wxT("group2")) );
480 fc
.SetPath(wxT("foot"));
482 // renaming to a name of existing group doesn't work
483 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("group1"), wxT("group2")) );
485 // try exchanging the groups names and then restore them back
486 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
487 wxVERIFY_FILECONFIG( wxT("[foot]\n")
489 wxT("[foot/groupTmp]\n")
490 wxT("[foot/groupTmp/subgroup]\n")
491 wxT("subentry=subvalue\n")
492 wxT("subentry2=subvalue2\n")
493 wxT("[foot/group2]\n"),
496 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
497 wxVERIFY_FILECONFIG( wxT("[foot]\n")
499 wxT("[foot/groupTmp]\n")
500 wxT("[foot/groupTmp/subgroup]\n")
501 wxT("subentry=subvalue\n")
502 wxT("subentry2=subvalue2\n")
503 wxT("[foot/group1]\n"),
506 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
507 wxVERIFY_FILECONFIG( wxT("[foot]\n")
509 wxT("[foot/group2]\n")
510 wxT("[foot/group2/subgroup]\n")
511 wxT("subentry=subvalue\n")
512 wxT("subentry2=subvalue2\n")
513 wxT("[foot/group1]\n"),
516 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
517 wxVERIFY_FILECONFIG( wxT("[foot]\n")
519 wxT("[foot/group2]\n")
520 wxT("[foot/group2/subgroup]\n")
521 wxT("subentry=subvalue\n")
522 wxT("subentry2=subvalue2\n")
523 wxT("[foot/groupTmp]\n"),
526 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
527 wxVERIFY_FILECONFIG( wxT("[foot]\n")
529 wxT("[foot/group1]\n")
530 wxT("[foot/group1/subgroup]\n")
531 wxT("subentry=subvalue\n")
532 wxT("subentry2=subvalue2\n")
533 wxT("[foot/groupTmp]\n"),
536 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
537 wxVERIFY_FILECONFIG( wxT("[foot]\n")
539 wxT("[foot/group1]\n")
540 wxT("[foot/group1/subgroup]\n")
541 wxT("subentry=subvalue\n")
542 wxT("subentry2=subvalue2\n")
543 wxT("[foot/group2]\n"),
547 void FileConfigTestCase::CreateSubgroupAndEntries()
550 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
551 fc
.Write(wxT("first"), wxT("one"));
553 wxVERIFY_FILECONFIG( wxT("first=one\n")
555 wxT("sub_first=sub_one\n"),
559 void FileConfigTestCase::CreateEntriesAndSubgroup()
562 fc
.Write(wxT("first"), wxT("one"));
563 fc
.Write(wxT("second"), wxT("two"));
564 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
566 wxVERIFY_FILECONFIG( wxT("first=one\n")
569 wxT("sub_first=sub_one\n"),
573 static void EmptyConfigAndWriteKey()
575 wxFileConfig
fc(wxT("deleteconftest"));
577 const wxString groupPath
= wxT("/root");
579 if ( fc
.Exists(groupPath
) )
581 // using DeleteGroup exposes the problem, using DeleteAll doesn't
582 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
585 // the config must be empty for the problem to arise
586 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
587 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
590 // this crashes on second call of this function
591 CPPUNIT_ASSERT( fc
.Write(groupPath
+ wxT("/entry"), wxT("value")) );
594 void FileConfigTestCase::DeleteLastGroup()
597 We make 2 of the same calls, first to create a file config with a single
600 ::EmptyConfigAndWriteKey();
603 ... then the same but this time the key's group is deleted before the
604 key is written again. This causes a crash.
606 ::EmptyConfigAndWriteKey();
611 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest")));
614 void FileConfigTestCase::DeleteAndRecreateGroup()
616 static const wxChar
*confInitial
=
622 wxStringInputStream
sis(confInitial
);
623 wxFileConfig
fc(sis
);
625 fc
.DeleteGroup(wxT("Second"));
626 wxVERIFY_FILECONFIG( wxT("[First]\n")
630 fc
.Write(wxT("Second/Value2"), wxT("New"));
631 wxVERIFY_FILECONFIG( wxT("[First]\n")
638 void FileConfigTestCase::AddToExistingRoot()
640 static const wxChar
*confInitial
=
644 wxStringInputStream
sis(confInitial
);
645 wxFileConfig
fc(sis
);
647 fc
.Write(wxT("/value1"), wxT("bar"));
656 void FileConfigTestCase::ReadNonExistent()
658 static const char *confTest
=
659 "community=censored\n"
668 wxStringInputStream
sis(confTest
);
669 wxFileConfig
fc(sis
);
672 CPPUNIT_ASSERT( !fc
.Read("URL", &url
) );
675 void FileConfigTestCase::ReadEmpty()
677 static const char *confTest
= "";
679 wxStringInputStream
sis(confTest
);
680 wxFileConfig
fc(sis
);
683 void FileConfigTestCase::ReadFloat()
685 static const char *confTest
=
691 wxStringInputStream
sis(confTest
);
692 wxFileConfig
fc(sis
);
695 CPPUNIT_ASSERT( fc
.Read("x", &f
) );
696 CPPUNIT_ASSERT_EQUAL( 1.234f
, f
);
698 CPPUNIT_ASSERT( fc
.Read("y", &f
) );
699 CPPUNIT_ASSERT_EQUAL( -9876.5432f
, f
);
702 #endif // wxUSE_FILECONFIG