]>
git.saurik.com Git - wxWidgets.git/blob - tests/config/fileconf.cpp
a92bdb6afe7d713f37b6d30800859fc39720d2fa
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
=
32 wxT("[root/group1]\n")
33 wxT("[root/group1/subgroup]\n")
34 wxT("subentry=subvalue\n")
35 wxT("subentry2=subvalue2\n")
36 wxT("[root/group2]\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( DeleteLastRootEntry
);
75 CPPUNIT_TEST( DeleteGroup
);
76 CPPUNIT_TEST( DeleteAll
);
77 CPPUNIT_TEST( RenameEntry
);
78 CPPUNIT_TEST( RenameGroup
);
79 CPPUNIT_TEST( CreateEntriesAndSubgroup
);
80 CPPUNIT_TEST( CreateSubgroupAndEntries
);
81 CPPUNIT_TEST( DeleteLastGroup
);
82 CPPUNIT_TEST( DeleteAndRecreateGroup
);
83 CPPUNIT_TEST( AddToExistingRoot
);
84 CPPUNIT_TEST( ReadNonExistent
);
85 CPPUNIT_TEST( ReadEmpty
);
86 CPPUNIT_TEST( ReadFloat
);
87 CPPUNIT_TEST_SUITE_END();
98 void DeleteAndWriteEntry();
99 void DeleteLastRootEntry();
104 void CreateEntriesAndSubgroup();
105 void CreateSubgroupAndEntries();
106 void DeleteLastGroup();
107 void DeleteAndRecreateGroup();
108 void AddToExistingRoot();
109 void ReadNonExistent();
114 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
121 void CheckGroupEntries(const wxFileConfig
& fc
,
125 void CheckGroupSubgroups(const wxFileConfig
& fc
,
130 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
133 // register in the unnamed registry so that these tests are run by default
134 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
136 // also include in its own registry so that these tests can be run alone
137 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
139 void FileConfigTestCase::Path()
141 wxStringInputStream
sis(testconfig
);
142 wxFileConfig
fc(sis
);
144 CPPUNIT_ASSERT( ChangePath(fc
, wxT("")) == wxT("") );
145 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/")) == wxT("") );
146 CPPUNIT_ASSERT( ChangePath(fc
, wxT("root")) == wxT("/root") );
147 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root")) == wxT("/root") );
148 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group1/subgroup")) == wxT("/root/group1/subgroup") );
149 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group2")) == wxT("/root/group2") );
152 void FileConfigTestCase::AddEntries()
156 wxVERIFY_FILECONFIG( wxT(""), fc
);
158 fc
.Write(wxT("/Foo"), wxT("foo"));
159 wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc
);
161 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
162 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
165 wxVERIFY_FILECONFIG( wxT(""), fc
);
167 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
168 wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc
);
170 fc
.Write(wxT("/Foo"), wxT("foo"));
171 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
175 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
180 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
182 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
185 va_start(ap
, nEntries
);
189 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
191 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
193 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
196 CPPUNIT_ASSERT( nEntries
== 0 );
202 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
207 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
209 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
212 va_start(ap
, nGroups
);
216 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
218 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
220 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
223 CPPUNIT_ASSERT( nGroups
== 0 );
228 void FileConfigTestCase::GetEntries()
230 wxStringInputStream
sis(testconfig
);
231 wxFileConfig
fc(sis
);
233 CheckGroupEntries(fc
, wxT(""), 0);
234 CheckGroupEntries(fc
, wxT("/root"), 1, wxT("entry"));
235 CheckGroupEntries(fc
, wxT("/root/group1"), 0);
236 CheckGroupEntries(fc
, wxT("/root/group1/subgroup"),
237 2, wxT("subentry"), wxT("subentry2"));
240 void FileConfigTestCase::GetGroups()
242 wxStringInputStream
sis(testconfig
);
243 wxFileConfig
fc(sis
);
245 CheckGroupSubgroups(fc
, wxT(""), 1, wxT("root"));
246 CheckGroupSubgroups(fc
, wxT("/root"), 2, wxT("group1"), wxT("group2"));
247 CheckGroupSubgroups(fc
, wxT("/root/group1"), 1, wxT("subgroup"));
248 CheckGroupSubgroups(fc
, wxT("/root/group2"), 0);
251 void FileConfigTestCase::HasEntry()
253 wxStringInputStream
sis(testconfig
);
254 wxFileConfig
fc(sis
);
256 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root")) );
257 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/entry")) );
258 CPPUNIT_ASSERT( fc
.HasEntry(wxT("/root/entry")) );
259 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/group1/subgroup/subentry")) );
260 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("")) );
261 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root/group1")) );
262 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("subgroup/subentry")) );
263 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("/root/no_such_group/entry")) );
264 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("/root/no_such_group")) );
267 void FileConfigTestCase::HasGroup()
269 wxStringInputStream
sis(testconfig
);
270 wxFileConfig
fc(sis
);
272 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root")) );
273 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1")) );
274 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1/subgroup")) );
275 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group2")) );
276 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("")) );
277 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root/group")) );
278 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root//subgroup")) );
279 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot/subgroup")) );
280 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot")) );
283 void FileConfigTestCase::Binary()
285 wxStringInputStream
sis(
289 wxFileConfig
fc(sis
);
292 fc
.Read("/root/binary", &buf
);
294 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
297 buf
.AppendData("\0\1\2", 3);
298 fc
.Write("/root/012", buf
);
307 void FileConfigTestCase::Save()
309 wxStringInputStream
sis(testconfig
);
310 wxFileConfig
fc(sis
);
311 wxVERIFY_FILECONFIG( testconfig
, fc
);
314 void FileConfigTestCase::DeleteEntry()
316 wxStringInputStream
sis(testconfig
);
317 wxFileConfig
fc(sis
);
319 CPPUNIT_ASSERT( !fc
.DeleteEntry(wxT("foo")) );
321 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("root/group1/subgroup/subentry")) );
322 wxVERIFY_FILECONFIG( wxT("[root]\n")
324 wxT("[root/group1]\n")
325 wxT("[root/group1/subgroup]\n")
326 wxT("subentry2=subvalue2\n")
327 wxT("[root/group2]\n"),
330 // group should be deleted now as well as it became empty
331 wxConfigPathChanger
change(&fc
, wxT("root/group1/subgroup/subentry2"));
332 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("subentry2")) );
333 wxVERIFY_FILECONFIG( wxT("[root]\n")
335 wxT("[root/group1]\n")
336 wxT("[root/group2]\n"),
340 void FileConfigTestCase::DeleteAndWriteEntry()
342 wxStringInputStream
sis(
344 "subentry=subvalue\n"
345 "subentry2=subvalue2\n"
346 "subentry3=subvalue3\n"
349 wxFileConfig
fc(sis
);
351 fc
.DeleteEntry("/root/group1/subentry2");
352 fc
.Write("/root/group1/subentry2", "testvalue");
353 fc
.DeleteEntry("/root/group2/subentry2");
354 fc
.Write("/root/group2/subentry2", "testvalue2");
355 fc
.DeleteEntry("/root/group1/subentry2");
356 fc
.Write("/root/group1/subentry2", "testvalue");
357 fc
.DeleteEntry("/root/group2/subentry2");
358 fc
.Write("/root/group2/subentry2", "testvalue2");
360 wxVERIFY_FILECONFIG( "[root/group1]\n"
361 "subentry=subvalue\n"
362 "subentry3=subvalue3\n"
363 "subentry2=testvalue\n"
365 "subentry2=testvalue2\n",
368 fc
.DeleteEntry("/root/group2/subentry2");
369 wxVERIFY_FILECONFIG( "[root/group1]\n"
370 "subentry=subvalue\n"
371 "subentry3=subvalue3\n"
372 "subentry2=testvalue\n",
375 fc
.DeleteEntry("/root/group1/subentry2");
376 fc
.DeleteEntry("/root/group1/subentry");
377 fc
.DeleteEntry("/root/group1/subentry3");
378 wxVERIFY_FILECONFIG( "", fc
);
381 void FileConfigTestCase::DeleteLastRootEntry()
383 // This tests for the bug which occurred when the last entry of the root
384 // group was deleted: this corrupted internal state and resulted in a crash
385 // after trying to write the just deleted entry again.
386 wxStringInputStream
sis("");
387 wxFileConfig
fc(sis
);
389 fc
.Write("key", "value");
390 wxVERIFY_FILECONFIG( "key=value\n", fc
);
392 fc
.DeleteEntry("key");
393 wxVERIFY_FILECONFIG( "", fc
);
395 fc
.Write("key", "value");
396 wxVERIFY_FILECONFIG( "key=value\n", fc
);
399 void FileConfigTestCase::DeleteGroup()
401 wxStringInputStream
sis(testconfig
);
402 wxFileConfig
fc(sis
);
404 CPPUNIT_ASSERT( !fc
.DeleteGroup(wxT("foo")) );
406 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group1")) );
407 wxVERIFY_FILECONFIG( wxT("[root]\n")
409 wxT("[root/group2]\n"),
412 // notice trailing slash: it should be ignored
413 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group2/")) );
414 wxVERIFY_FILECONFIG( wxT("[root]\n")
415 wxT("entry=value\n"),
418 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root")) );
419 CPPUNIT_ASSERT( Dump(fc
).empty() );
422 void FileConfigTestCase::DeleteAll()
424 wxStringInputStream
sis(testconfig
);
425 wxFileConfig
fc(sis
);
427 CPPUNIT_ASSERT( fc
.DeleteAll() );
428 CPPUNIT_ASSERT( Dump(fc
).empty() );
431 void FileConfigTestCase::RenameEntry()
433 wxStringInputStream
sis(testconfig
);
434 wxFileConfig
fc(sis
);
436 fc
.SetPath(wxT("root"));
437 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("entry"), wxT("newname")) );
438 wxVERIFY_FILECONFIG( wxT("[root]\n")
439 wxT("newname=value\n")
440 wxT("[root/group1]\n")
441 wxT("[root/group1/subgroup]\n")
442 wxT("subentry=subvalue\n")
443 wxT("subentry2=subvalue2\n")
444 wxT("[root/group2]\n"),
447 fc
.SetPath(wxT("group1/subgroup"));
448 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("entry"), wxT("newname")) );
449 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("subentry"), wxT("subentry2")) );
451 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("subentry"), wxT("subentry1")) );
452 wxVERIFY_FILECONFIG( wxT("[root]\n")
453 wxT("newname=value\n")
454 wxT("[root/group1]\n")
455 wxT("[root/group1/subgroup]\n")
456 wxT("subentry2=subvalue2\n")
457 wxT("subentry1=subvalue\n")
458 wxT("[root/group2]\n"),
462 void FileConfigTestCase::RenameGroup()
464 wxStringInputStream
sis(testconfig
);
465 wxFileConfig
fc(sis
);
467 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("root"), wxT("foot")) );
468 wxVERIFY_FILECONFIG( wxT("[foot]\n")
470 wxT("[foot/group1]\n")
471 wxT("[foot/group1/subgroup]\n")
472 wxT("subentry=subvalue\n")
473 wxT("subentry2=subvalue2\n")
474 wxT("[foot/group2]\n"),
477 // renaming a path doesn't work, it must be the immediate group
478 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("foot/group1"), wxT("group2")) );
481 fc
.SetPath(wxT("foot"));
483 // renaming to a name of existing group doesn't work
484 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("group1"), wxT("group2")) );
486 // try exchanging the groups names and then restore them back
487 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
488 wxVERIFY_FILECONFIG( wxT("[foot]\n")
490 wxT("[foot/groupTmp]\n")
491 wxT("[foot/groupTmp/subgroup]\n")
492 wxT("subentry=subvalue\n")
493 wxT("subentry2=subvalue2\n")
494 wxT("[foot/group2]\n"),
497 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
498 wxVERIFY_FILECONFIG( wxT("[foot]\n")
500 wxT("[foot/groupTmp]\n")
501 wxT("[foot/groupTmp/subgroup]\n")
502 wxT("subentry=subvalue\n")
503 wxT("subentry2=subvalue2\n")
504 wxT("[foot/group1]\n"),
507 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
508 wxVERIFY_FILECONFIG( wxT("[foot]\n")
510 wxT("[foot/group2]\n")
511 wxT("[foot/group2/subgroup]\n")
512 wxT("subentry=subvalue\n")
513 wxT("subentry2=subvalue2\n")
514 wxT("[foot/group1]\n"),
517 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
518 wxVERIFY_FILECONFIG( wxT("[foot]\n")
520 wxT("[foot/group2]\n")
521 wxT("[foot/group2/subgroup]\n")
522 wxT("subentry=subvalue\n")
523 wxT("subentry2=subvalue2\n")
524 wxT("[foot/groupTmp]\n"),
527 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
528 wxVERIFY_FILECONFIG( wxT("[foot]\n")
530 wxT("[foot/group1]\n")
531 wxT("[foot/group1/subgroup]\n")
532 wxT("subentry=subvalue\n")
533 wxT("subentry2=subvalue2\n")
534 wxT("[foot/groupTmp]\n"),
537 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
538 wxVERIFY_FILECONFIG( wxT("[foot]\n")
540 wxT("[foot/group1]\n")
541 wxT("[foot/group1/subgroup]\n")
542 wxT("subentry=subvalue\n")
543 wxT("subentry2=subvalue2\n")
544 wxT("[foot/group2]\n"),
548 void FileConfigTestCase::CreateSubgroupAndEntries()
551 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
552 fc
.Write(wxT("first"), wxT("one"));
554 wxVERIFY_FILECONFIG( wxT("first=one\n")
556 wxT("sub_first=sub_one\n"),
560 void FileConfigTestCase::CreateEntriesAndSubgroup()
563 fc
.Write(wxT("first"), wxT("one"));
564 fc
.Write(wxT("second"), wxT("two"));
565 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
567 wxVERIFY_FILECONFIG( wxT("first=one\n")
570 wxT("sub_first=sub_one\n"),
574 static void EmptyConfigAndWriteKey()
576 wxFileConfig
fc(wxT("deleteconftest"));
578 const wxString groupPath
= wxT("/root");
580 if ( fc
.Exists(groupPath
) )
582 // using DeleteGroup exposes the problem, using DeleteAll doesn't
583 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
586 // the config must be empty for the problem to arise
587 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
588 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
591 // this crashes on second call of this function
592 CPPUNIT_ASSERT( fc
.Write(groupPath
+ wxT("/entry"), wxT("value")) );
595 void FileConfigTestCase::DeleteLastGroup()
598 We make 2 of the same calls, first to create a file config with a single
601 ::EmptyConfigAndWriteKey();
604 ... then the same but this time the key's group is deleted before the
605 key is written again. This causes a crash.
607 ::EmptyConfigAndWriteKey();
612 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest")));
615 void FileConfigTestCase::DeleteAndRecreateGroup()
617 static const wxChar
*confInitial
=
623 wxStringInputStream
sis(confInitial
);
624 wxFileConfig
fc(sis
);
626 fc
.DeleteGroup(wxT("Second"));
627 wxVERIFY_FILECONFIG( wxT("[First]\n")
631 fc
.Write(wxT("Second/Value2"), wxT("New"));
632 wxVERIFY_FILECONFIG( wxT("[First]\n")
639 void FileConfigTestCase::AddToExistingRoot()
641 static const wxChar
*confInitial
=
645 wxStringInputStream
sis(confInitial
);
646 wxFileConfig
fc(sis
);
648 fc
.Write(wxT("/value1"), wxT("bar"));
657 void FileConfigTestCase::ReadNonExistent()
659 static const char *confTest
=
660 "community=censored\n"
669 wxStringInputStream
sis(confTest
);
670 wxFileConfig
fc(sis
);
673 CPPUNIT_ASSERT( !fc
.Read("URL", &url
) );
676 void FileConfigTestCase::ReadEmpty()
678 static const char *confTest
= "";
680 wxStringInputStream
sis(confTest
);
681 wxFileConfig
fc(sis
);
684 void FileConfigTestCase::ReadFloat()
686 static const char *confTest
=
692 wxStringInputStream
sis(confTest
);
693 wxFileConfig
fc(sis
);
696 CPPUNIT_ASSERT( fc
.Read("x", &f
) );
697 CPPUNIT_ASSERT_EQUAL( 1.234f
, f
);
699 CPPUNIT_ASSERT( fc
.Read("y", &f
) );
700 CPPUNIT_ASSERT_EQUAL( -9876.5432f
, f
);
703 #endif // wxUSE_FILECONFIG