]>
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
=
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( 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_SUITE_END();
96 void DeleteAndWriteEntry();
101 void CreateEntriesAndSubgroup();
102 void CreateSubgroupAndEntries();
103 void DeleteLastGroup();
104 void DeleteAndRecreateGroup();
105 void AddToExistingRoot();
106 void ReadNonExistent();
110 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
117 void CheckGroupEntries(const wxFileConfig
& fc
,
121 void CheckGroupSubgroups(const wxFileConfig
& fc
,
126 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
129 // register in the unnamed registry so that these tests are run by default
130 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
132 // also include in it's own registry so that these tests can be run alone
133 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
135 void FileConfigTestCase::Path()
137 wxStringInputStream
sis(testconfig
);
138 wxFileConfig
fc(sis
);
140 CPPUNIT_ASSERT( ChangePath(fc
, wxT("")) == wxT("") );
141 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/")) == wxT("") );
142 CPPUNIT_ASSERT( ChangePath(fc
, wxT("root")) == wxT("/root") );
143 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root")) == wxT("/root") );
144 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group1/subgroup")) == wxT("/root/group1/subgroup") );
145 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group2")) == wxT("/root/group2") );
148 void FileConfigTestCase::AddEntries()
152 wxVERIFY_FILECONFIG( wxT(""), fc
);
154 fc
.Write(wxT("/Foo"), wxT("foo"));
155 wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc
);
157 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
158 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
161 wxVERIFY_FILECONFIG( wxT(""), fc
);
163 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
164 wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc
);
166 fc
.Write(wxT("/Foo"), wxT("foo"));
167 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
171 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
176 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
178 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
181 va_start(ap
, nEntries
);
185 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
187 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
189 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
192 CPPUNIT_ASSERT( nEntries
== 0 );
198 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
203 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
205 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
208 va_start(ap
, nGroups
);
212 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
214 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
216 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
219 CPPUNIT_ASSERT( nGroups
== 0 );
224 void FileConfigTestCase::GetEntries()
226 wxStringInputStream
sis(testconfig
);
227 wxFileConfig
fc(sis
);
229 CheckGroupEntries(fc
, wxT(""), 0);
230 CheckGroupEntries(fc
, wxT("/root"), 1, wxT("entry"));
231 CheckGroupEntries(fc
, wxT("/root/group1"), 0);
232 CheckGroupEntries(fc
, wxT("/root/group1/subgroup"),
233 2, wxT("subentry"), wxT("subentry2"));
236 void FileConfigTestCase::GetGroups()
238 wxStringInputStream
sis(testconfig
);
239 wxFileConfig
fc(sis
);
241 CheckGroupSubgroups(fc
, wxT(""), 1, wxT("root"));
242 CheckGroupSubgroups(fc
, wxT("/root"), 2, wxT("group1"), wxT("group2"));
243 CheckGroupSubgroups(fc
, wxT("/root/group1"), 1, wxT("subgroup"));
244 CheckGroupSubgroups(fc
, wxT("/root/group2"), 0);
247 void FileConfigTestCase::HasEntry()
249 wxStringInputStream
sis(testconfig
);
250 wxFileConfig
fc(sis
);
252 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root")) );
253 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/entry")) );
254 CPPUNIT_ASSERT( fc
.HasEntry(wxT("/root/entry")) );
255 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/group1/subgroup/subentry")) );
256 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("")) );
257 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root/group1")) );
258 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("subgroup/subentry")) );
259 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("/root/no_such_group/entry")) );
260 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("/root/no_such_group")) );
263 void FileConfigTestCase::HasGroup()
265 wxStringInputStream
sis(testconfig
);
266 wxFileConfig
fc(sis
);
268 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root")) );
269 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1")) );
270 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1/subgroup")) );
271 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group2")) );
272 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("")) );
273 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root/group")) );
274 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root//subgroup")) );
275 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot/subgroup")) );
276 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot")) );
279 void FileConfigTestCase::Binary()
281 wxStringInputStream
sis(
285 wxFileConfig
fc(sis
);
288 fc
.Read("/root/binary", &buf
);
290 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
293 buf
.AppendData("\0\1\2", 3);
294 fc
.Write("/root/012", buf
);
303 void FileConfigTestCase::Save()
305 wxStringInputStream
sis(testconfig
);
306 wxFileConfig
fc(sis
);
307 wxVERIFY_FILECONFIG( testconfig
, fc
);
310 void FileConfigTestCase::DeleteEntry()
312 wxStringInputStream
sis(testconfig
);
313 wxFileConfig
fc(sis
);
315 CPPUNIT_ASSERT( !fc
.DeleteEntry(wxT("foo")) );
317 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("root/group1/subgroup/subentry")) );
318 wxVERIFY_FILECONFIG( wxT("[root]\n")
320 wxT("[root/group1]\n")
321 wxT("[root/group1/subgroup]\n")
322 wxT("subentry2=subvalue2\n")
323 wxT("[root/group2]\n"),
326 // group should be deleted now as well as it became empty
327 wxConfigPathChanger
change(&fc
, wxT("root/group1/subgroup/subentry2"));
328 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("subentry2")) );
329 wxVERIFY_FILECONFIG( wxT("[root]\n")
331 wxT("[root/group1]\n")
332 wxT("[root/group2]\n"),
336 void FileConfigTestCase::DeleteAndWriteEntry()
338 wxStringInputStream
sis(
340 "subentry=subvalue\n"
341 "subentry2=subvalue2\n"
342 "subentry3=subvalue3\n"
345 wxFileConfig
fc(sis
);
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");
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");
356 wxVERIFY_FILECONFIG( "[root/group1]\n"
357 "subentry=subvalue\n"
358 "subentry3=subvalue3\n"
359 "subentry2=testvalue\n"
361 "subentry2=testvalue2\n",
364 fc
.DeleteEntry("/root/group2/subentry2");
365 wxVERIFY_FILECONFIG( "[root/group1]\n"
366 "subentry=subvalue\n"
367 "subentry3=subvalue3\n"
368 "subentry2=testvalue\n",
371 fc
.DeleteEntry("/root/group1/subentry2");
372 fc
.DeleteEntry("/root/group1/subentry");
373 fc
.DeleteEntry("/root/group1/subentry3");
374 wxVERIFY_FILECONFIG( "", fc
);
377 void FileConfigTestCase::DeleteGroup()
379 wxStringInputStream
sis(testconfig
);
380 wxFileConfig
fc(sis
);
382 CPPUNIT_ASSERT( !fc
.DeleteGroup(wxT("foo")) );
384 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group1")) );
385 wxVERIFY_FILECONFIG( wxT("[root]\n")
387 wxT("[root/group2]\n"),
390 // notice trailing slash: it should be ignored
391 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group2/")) );
392 wxVERIFY_FILECONFIG( wxT("[root]\n")
393 wxT("entry=value\n"),
396 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root")) );
397 CPPUNIT_ASSERT( Dump(fc
).empty() );
400 void FileConfigTestCase::DeleteAll()
402 wxStringInputStream
sis(testconfig
);
403 wxFileConfig
fc(sis
);
405 CPPUNIT_ASSERT( fc
.DeleteAll() );
406 CPPUNIT_ASSERT( Dump(fc
).empty() );
409 void FileConfigTestCase::RenameEntry()
411 wxStringInputStream
sis(testconfig
);
412 wxFileConfig
fc(sis
);
414 fc
.SetPath(wxT("root"));
415 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("entry"), wxT("newname")) );
416 wxVERIFY_FILECONFIG( wxT("[root]\n")
417 wxT("newname=value\n")
418 wxT("[root/group1]\n")
419 wxT("[root/group1/subgroup]\n")
420 wxT("subentry=subvalue\n")
421 wxT("subentry2=subvalue2\n")
422 wxT("[root/group2]\n"),
425 fc
.SetPath(wxT("group1/subgroup"));
426 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("entry"), wxT("newname")) );
427 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("subentry"), wxT("subentry2")) );
429 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("subentry"), wxT("subentry1")) );
430 wxVERIFY_FILECONFIG( wxT("[root]\n")
431 wxT("newname=value\n")
432 wxT("[root/group1]\n")
433 wxT("[root/group1/subgroup]\n")
434 wxT("subentry2=subvalue2\n")
435 wxT("subentry1=subvalue\n")
436 wxT("[root/group2]\n"),
440 void FileConfigTestCase::RenameGroup()
442 wxStringInputStream
sis(testconfig
);
443 wxFileConfig
fc(sis
);
445 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("root"), wxT("foot")) );
446 wxVERIFY_FILECONFIG( wxT("[foot]\n")
448 wxT("[foot/group1]\n")
449 wxT("[foot/group1/subgroup]\n")
450 wxT("subentry=subvalue\n")
451 wxT("subentry2=subvalue2\n")
452 wxT("[foot/group2]\n"),
455 // renaming a path doesn't work, it must be the immediate group
456 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("foot/group1"), wxT("group2")) );
459 fc
.SetPath(wxT("foot"));
461 // renaming to a name of existing group doesn't work
462 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("group1"), wxT("group2")) );
464 // try exchanging the groups names and then restore them back
465 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
466 wxVERIFY_FILECONFIG( wxT("[foot]\n")
468 wxT("[foot/groupTmp]\n")
469 wxT("[foot/groupTmp/subgroup]\n")
470 wxT("subentry=subvalue\n")
471 wxT("subentry2=subvalue2\n")
472 wxT("[foot/group2]\n"),
475 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
476 wxVERIFY_FILECONFIG( wxT("[foot]\n")
478 wxT("[foot/groupTmp]\n")
479 wxT("[foot/groupTmp/subgroup]\n")
480 wxT("subentry=subvalue\n")
481 wxT("subentry2=subvalue2\n")
482 wxT("[foot/group1]\n"),
485 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
486 wxVERIFY_FILECONFIG( wxT("[foot]\n")
488 wxT("[foot/group2]\n")
489 wxT("[foot/group2/subgroup]\n")
490 wxT("subentry=subvalue\n")
491 wxT("subentry2=subvalue2\n")
492 wxT("[foot/group1]\n"),
495 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
496 wxVERIFY_FILECONFIG( wxT("[foot]\n")
498 wxT("[foot/group2]\n")
499 wxT("[foot/group2/subgroup]\n")
500 wxT("subentry=subvalue\n")
501 wxT("subentry2=subvalue2\n")
502 wxT("[foot/groupTmp]\n"),
505 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
506 wxVERIFY_FILECONFIG( wxT("[foot]\n")
508 wxT("[foot/group1]\n")
509 wxT("[foot/group1/subgroup]\n")
510 wxT("subentry=subvalue\n")
511 wxT("subentry2=subvalue2\n")
512 wxT("[foot/groupTmp]\n"),
515 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
516 wxVERIFY_FILECONFIG( wxT("[foot]\n")
518 wxT("[foot/group1]\n")
519 wxT("[foot/group1/subgroup]\n")
520 wxT("subentry=subvalue\n")
521 wxT("subentry2=subvalue2\n")
522 wxT("[foot/group2]\n"),
526 void FileConfigTestCase::CreateSubgroupAndEntries()
529 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
530 fc
.Write(wxT("first"), wxT("one"));
532 wxVERIFY_FILECONFIG( wxT("first=one\n")
534 wxT("sub_first=sub_one\n"),
538 void FileConfigTestCase::CreateEntriesAndSubgroup()
541 fc
.Write(wxT("first"), wxT("one"));
542 fc
.Write(wxT("second"), wxT("two"));
543 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
545 wxVERIFY_FILECONFIG( wxT("first=one\n")
548 wxT("sub_first=sub_one\n"),
552 static void EmptyConfigAndWriteKey()
554 wxFileConfig
fc(wxT("deleteconftest"));
556 const wxString groupPath
= wxT("/root");
558 if ( fc
.Exists(groupPath
) )
560 // using DeleteGroup exposes the problem, using DeleteAll doesn't
561 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
564 // the config must be empty for the problem to arise
565 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
566 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
569 // this crashes on second call of this function
570 CPPUNIT_ASSERT( fc
.Write(groupPath
+ wxT("/entry"), wxT("value")) );
573 void FileConfigTestCase::DeleteLastGroup()
576 We make 2 of the same calls, first to create a file config with a single
579 ::EmptyConfigAndWriteKey();
582 ... then the same but this time the key's group is deleted before the
583 key is written again. This causes a crash.
585 ::EmptyConfigAndWriteKey();
590 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest")));
593 void FileConfigTestCase::DeleteAndRecreateGroup()
595 static const wxChar
*confInitial
=
601 wxStringInputStream
sis(confInitial
);
602 wxFileConfig
fc(sis
);
604 fc
.DeleteGroup(wxT("Second"));
605 wxVERIFY_FILECONFIG( wxT("[First]\n")
609 fc
.Write(wxT("Second/Value2"), wxT("New"));
610 wxVERIFY_FILECONFIG( wxT("[First]\n")
617 void FileConfigTestCase::AddToExistingRoot()
619 static const wxChar
*confInitial
=
623 wxStringInputStream
sis(confInitial
);
624 wxFileConfig
fc(sis
);
626 fc
.Write(wxT("/value1"), wxT("bar"));
635 void FileConfigTestCase::ReadNonExistent()
637 static const char *confTest
=
638 "community=censored\n"
647 wxStringInputStream
sis(confTest
);
648 wxFileConfig
fc(sis
);
651 CPPUNIT_ASSERT( !fc
.Read("URL", &url
) );
654 void FileConfigTestCase::ReadEmpty()
656 static const char *confTest
= "";
658 wxStringInputStream
sis(confTest
);
659 wxFileConfig
fc(sis
);
662 #endif // wxUSE_FILECONFIG