]>
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( ReadFloat
);
86 CPPUNIT_TEST_SUITE_END();
97 void DeleteAndWriteEntry();
102 void CreateEntriesAndSubgroup();
103 void CreateSubgroupAndEntries();
104 void DeleteLastGroup();
105 void DeleteAndRecreateGroup();
106 void AddToExistingRoot();
107 void ReadNonExistent();
112 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
119 void CheckGroupEntries(const wxFileConfig
& fc
,
123 void CheckGroupSubgroups(const wxFileConfig
& fc
,
128 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
131 // register in the unnamed registry so that these tests are run by default
132 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
134 // also include in its own registry so that these tests can be run alone
135 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
137 void FileConfigTestCase::Path()
139 wxStringInputStream
sis(testconfig
);
140 wxFileConfig
fc(sis
);
142 CPPUNIT_ASSERT( ChangePath(fc
, wxT("")) == wxT("") );
143 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/")) == wxT("") );
144 CPPUNIT_ASSERT( ChangePath(fc
, wxT("root")) == wxT("/root") );
145 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root")) == wxT("/root") );
146 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group1/subgroup")) == wxT("/root/group1/subgroup") );
147 CPPUNIT_ASSERT( ChangePath(fc
, wxT("/root/group2")) == wxT("/root/group2") );
150 void FileConfigTestCase::AddEntries()
154 wxVERIFY_FILECONFIG( wxT(""), fc
);
156 fc
.Write(wxT("/Foo"), wxT("foo"));
157 wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc
);
159 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
160 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
163 wxVERIFY_FILECONFIG( wxT(""), fc
);
165 fc
.Write(wxT("/Bar/Baz"), wxT("baz"));
166 wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc
);
168 fc
.Write(wxT("/Foo"), wxT("foo"));
169 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
173 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
178 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
180 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
183 va_start(ap
, nEntries
);
187 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
189 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
191 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
194 CPPUNIT_ASSERT( nEntries
== 0 );
200 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
205 wxConfigPathChanger
change(&fc
, wxString(path
) + wxT("/"));
207 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
210 va_start(ap
, nGroups
);
214 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
216 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
218 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
221 CPPUNIT_ASSERT( nGroups
== 0 );
226 void FileConfigTestCase::GetEntries()
228 wxStringInputStream
sis(testconfig
);
229 wxFileConfig
fc(sis
);
231 CheckGroupEntries(fc
, wxT(""), 0);
232 CheckGroupEntries(fc
, wxT("/root"), 1, wxT("entry"));
233 CheckGroupEntries(fc
, wxT("/root/group1"), 0);
234 CheckGroupEntries(fc
, wxT("/root/group1/subgroup"),
235 2, wxT("subentry"), wxT("subentry2"));
238 void FileConfigTestCase::GetGroups()
240 wxStringInputStream
sis(testconfig
);
241 wxFileConfig
fc(sis
);
243 CheckGroupSubgroups(fc
, wxT(""), 1, wxT("root"));
244 CheckGroupSubgroups(fc
, wxT("/root"), 2, wxT("group1"), wxT("group2"));
245 CheckGroupSubgroups(fc
, wxT("/root/group1"), 1, wxT("subgroup"));
246 CheckGroupSubgroups(fc
, wxT("/root/group2"), 0);
249 void FileConfigTestCase::HasEntry()
251 wxStringInputStream
sis(testconfig
);
252 wxFileConfig
fc(sis
);
254 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root")) );
255 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/entry")) );
256 CPPUNIT_ASSERT( fc
.HasEntry(wxT("/root/entry")) );
257 CPPUNIT_ASSERT( fc
.HasEntry(wxT("root/group1/subgroup/subentry")) );
258 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("")) );
259 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("root/group1")) );
260 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("subgroup/subentry")) );
261 CPPUNIT_ASSERT( !fc
.HasEntry(wxT("/root/no_such_group/entry")) );
262 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("/root/no_such_group")) );
265 void FileConfigTestCase::HasGroup()
267 wxStringInputStream
sis(testconfig
);
268 wxFileConfig
fc(sis
);
270 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root")) );
271 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1")) );
272 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group1/subgroup")) );
273 CPPUNIT_ASSERT( fc
.HasGroup(wxT("root/group2")) );
274 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("")) );
275 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root/group")) );
276 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("root//subgroup")) );
277 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot/subgroup")) );
278 CPPUNIT_ASSERT( !fc
.HasGroup(wxT("foot")) );
281 void FileConfigTestCase::Binary()
283 wxStringInputStream
sis(
287 wxFileConfig
fc(sis
);
290 fc
.Read("/root/binary", &buf
);
292 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
295 buf
.AppendData("\0\1\2", 3);
296 fc
.Write("/root/012", buf
);
305 void FileConfigTestCase::Save()
307 wxStringInputStream
sis(testconfig
);
308 wxFileConfig
fc(sis
);
309 wxVERIFY_FILECONFIG( testconfig
, fc
);
312 void FileConfigTestCase::DeleteEntry()
314 wxStringInputStream
sis(testconfig
);
315 wxFileConfig
fc(sis
);
317 CPPUNIT_ASSERT( !fc
.DeleteEntry(wxT("foo")) );
319 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("root/group1/subgroup/subentry")) );
320 wxVERIFY_FILECONFIG( wxT("[root]\n")
322 wxT("[root/group1]\n")
323 wxT("[root/group1/subgroup]\n")
324 wxT("subentry2=subvalue2\n")
325 wxT("[root/group2]\n"),
328 // group should be deleted now as well as it became empty
329 wxConfigPathChanger
change(&fc
, wxT("root/group1/subgroup/subentry2"));
330 CPPUNIT_ASSERT( fc
.DeleteEntry(wxT("subentry2")) );
331 wxVERIFY_FILECONFIG( wxT("[root]\n")
333 wxT("[root/group1]\n")
334 wxT("[root/group2]\n"),
338 void FileConfigTestCase::DeleteAndWriteEntry()
340 wxStringInputStream
sis(
342 "subentry=subvalue\n"
343 "subentry2=subvalue2\n"
344 "subentry3=subvalue3\n"
347 wxFileConfig
fc(sis
);
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");
353 fc
.DeleteEntry("/root/group1/subentry2");
354 fc
.Write("/root/group1/subentry2", "testvalue");
355 fc
.DeleteEntry("/root/group2/subentry2");
356 fc
.Write("/root/group2/subentry2", "testvalue2");
358 wxVERIFY_FILECONFIG( "[root/group1]\n"
359 "subentry=subvalue\n"
360 "subentry3=subvalue3\n"
361 "subentry2=testvalue\n"
363 "subentry2=testvalue2\n",
366 fc
.DeleteEntry("/root/group2/subentry2");
367 wxVERIFY_FILECONFIG( "[root/group1]\n"
368 "subentry=subvalue\n"
369 "subentry3=subvalue3\n"
370 "subentry2=testvalue\n",
373 fc
.DeleteEntry("/root/group1/subentry2");
374 fc
.DeleteEntry("/root/group1/subentry");
375 fc
.DeleteEntry("/root/group1/subentry3");
376 wxVERIFY_FILECONFIG( "", fc
);
379 void FileConfigTestCase::DeleteGroup()
381 wxStringInputStream
sis(testconfig
);
382 wxFileConfig
fc(sis
);
384 CPPUNIT_ASSERT( !fc
.DeleteGroup(wxT("foo")) );
386 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group1")) );
387 wxVERIFY_FILECONFIG( wxT("[root]\n")
389 wxT("[root/group2]\n"),
392 // notice trailing slash: it should be ignored
393 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root/group2/")) );
394 wxVERIFY_FILECONFIG( wxT("[root]\n")
395 wxT("entry=value\n"),
398 CPPUNIT_ASSERT( fc
.DeleteGroup(wxT("root")) );
399 CPPUNIT_ASSERT( Dump(fc
).empty() );
402 void FileConfigTestCase::DeleteAll()
404 wxStringInputStream
sis(testconfig
);
405 wxFileConfig
fc(sis
);
407 CPPUNIT_ASSERT( fc
.DeleteAll() );
408 CPPUNIT_ASSERT( Dump(fc
).empty() );
411 void FileConfigTestCase::RenameEntry()
413 wxStringInputStream
sis(testconfig
);
414 wxFileConfig
fc(sis
);
416 fc
.SetPath(wxT("root"));
417 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("entry"), wxT("newname")) );
418 wxVERIFY_FILECONFIG( wxT("[root]\n")
419 wxT("newname=value\n")
420 wxT("[root/group1]\n")
421 wxT("[root/group1/subgroup]\n")
422 wxT("subentry=subvalue\n")
423 wxT("subentry2=subvalue2\n")
424 wxT("[root/group2]\n"),
427 fc
.SetPath(wxT("group1/subgroup"));
428 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("entry"), wxT("newname")) );
429 CPPUNIT_ASSERT( !fc
.RenameEntry(wxT("subentry"), wxT("subentry2")) );
431 CPPUNIT_ASSERT( fc
.RenameEntry(wxT("subentry"), wxT("subentry1")) );
432 wxVERIFY_FILECONFIG( wxT("[root]\n")
433 wxT("newname=value\n")
434 wxT("[root/group1]\n")
435 wxT("[root/group1/subgroup]\n")
436 wxT("subentry2=subvalue2\n")
437 wxT("subentry1=subvalue\n")
438 wxT("[root/group2]\n"),
442 void FileConfigTestCase::RenameGroup()
444 wxStringInputStream
sis(testconfig
);
445 wxFileConfig
fc(sis
);
447 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("root"), wxT("foot")) );
448 wxVERIFY_FILECONFIG( wxT("[foot]\n")
450 wxT("[foot/group1]\n")
451 wxT("[foot/group1/subgroup]\n")
452 wxT("subentry=subvalue\n")
453 wxT("subentry2=subvalue2\n")
454 wxT("[foot/group2]\n"),
457 // renaming a path doesn't work, it must be the immediate group
458 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("foot/group1"), wxT("group2")) );
461 fc
.SetPath(wxT("foot"));
463 // renaming to a name of existing group doesn't work
464 CPPUNIT_ASSERT( !fc
.RenameGroup(wxT("group1"), wxT("group2")) );
466 // try exchanging the groups names and then restore them back
467 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
468 wxVERIFY_FILECONFIG( wxT("[foot]\n")
470 wxT("[foot/groupTmp]\n")
471 wxT("[foot/groupTmp/subgroup]\n")
472 wxT("subentry=subvalue\n")
473 wxT("subentry2=subvalue2\n")
474 wxT("[foot/group2]\n"),
477 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
478 wxVERIFY_FILECONFIG( wxT("[foot]\n")
480 wxT("[foot/groupTmp]\n")
481 wxT("[foot/groupTmp/subgroup]\n")
482 wxT("subentry=subvalue\n")
483 wxT("subentry2=subvalue2\n")
484 wxT("[foot/group1]\n"),
487 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
488 wxVERIFY_FILECONFIG( wxT("[foot]\n")
490 wxT("[foot/group2]\n")
491 wxT("[foot/group2/subgroup]\n")
492 wxT("subentry=subvalue\n")
493 wxT("subentry2=subvalue2\n")
494 wxT("[foot/group1]\n"),
497 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group1"), wxT("groupTmp")) );
498 wxVERIFY_FILECONFIG( wxT("[foot]\n")
500 wxT("[foot/group2]\n")
501 wxT("[foot/group2/subgroup]\n")
502 wxT("subentry=subvalue\n")
503 wxT("subentry2=subvalue2\n")
504 wxT("[foot/groupTmp]\n"),
507 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("group2"), wxT("group1")) );
508 wxVERIFY_FILECONFIG( wxT("[foot]\n")
510 wxT("[foot/group1]\n")
511 wxT("[foot/group1/subgroup]\n")
512 wxT("subentry=subvalue\n")
513 wxT("subentry2=subvalue2\n")
514 wxT("[foot/groupTmp]\n"),
517 CPPUNIT_ASSERT( fc
.RenameGroup(wxT("groupTmp"), wxT("group2")) );
518 wxVERIFY_FILECONFIG( wxT("[foot]\n")
520 wxT("[foot/group1]\n")
521 wxT("[foot/group1/subgroup]\n")
522 wxT("subentry=subvalue\n")
523 wxT("subentry2=subvalue2\n")
524 wxT("[foot/group2]\n"),
528 void FileConfigTestCase::CreateSubgroupAndEntries()
531 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
532 fc
.Write(wxT("first"), wxT("one"));
534 wxVERIFY_FILECONFIG( wxT("first=one\n")
536 wxT("sub_first=sub_one\n"),
540 void FileConfigTestCase::CreateEntriesAndSubgroup()
543 fc
.Write(wxT("first"), wxT("one"));
544 fc
.Write(wxT("second"), wxT("two"));
545 fc
.Write(wxT("sub/sub_first"), wxT("sub_one"));
547 wxVERIFY_FILECONFIG( wxT("first=one\n")
550 wxT("sub_first=sub_one\n"),
554 static void EmptyConfigAndWriteKey()
556 wxFileConfig
fc(wxT("deleteconftest"));
558 const wxString groupPath
= wxT("/root");
560 if ( fc
.Exists(groupPath
) )
562 // using DeleteGroup exposes the problem, using DeleteAll doesn't
563 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
566 // the config must be empty for the problem to arise
567 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
568 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
571 // this crashes on second call of this function
572 CPPUNIT_ASSERT( fc
.Write(groupPath
+ wxT("/entry"), wxT("value")) );
575 void FileConfigTestCase::DeleteLastGroup()
578 We make 2 of the same calls, first to create a file config with a single
581 ::EmptyConfigAndWriteKey();
584 ... then the same but this time the key's group is deleted before the
585 key is written again. This causes a crash.
587 ::EmptyConfigAndWriteKey();
592 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest")));
595 void FileConfigTestCase::DeleteAndRecreateGroup()
597 static const wxChar
*confInitial
=
603 wxStringInputStream
sis(confInitial
);
604 wxFileConfig
fc(sis
);
606 fc
.DeleteGroup(wxT("Second"));
607 wxVERIFY_FILECONFIG( wxT("[First]\n")
611 fc
.Write(wxT("Second/Value2"), wxT("New"));
612 wxVERIFY_FILECONFIG( wxT("[First]\n")
619 void FileConfigTestCase::AddToExistingRoot()
621 static const wxChar
*confInitial
=
625 wxStringInputStream
sis(confInitial
);
626 wxFileConfig
fc(sis
);
628 fc
.Write(wxT("/value1"), wxT("bar"));
637 void FileConfigTestCase::ReadNonExistent()
639 static const char *confTest
=
640 "community=censored\n"
649 wxStringInputStream
sis(confTest
);
650 wxFileConfig
fc(sis
);
653 CPPUNIT_ASSERT( !fc
.Read("URL", &url
) );
656 void FileConfigTestCase::ReadEmpty()
658 static const char *confTest
= "";
660 wxStringInputStream
sis(confTest
);
661 wxFileConfig
fc(sis
);
664 void FileConfigTestCase::ReadFloat()
666 static const char *confTest
=
672 wxStringInputStream
sis(confTest
);
673 wxFileConfig
fc(sis
);
676 CPPUNIT_ASSERT( fc
.Read("x", &f
) );
677 CPPUNIT_ASSERT_EQUAL( 1.234f
, f
);
679 CPPUNIT_ASSERT( fc
.Read("y", &f
) );
680 CPPUNIT_ASSERT_EQUAL( -9876.5432f
, f
);
683 #endif // wxUSE_FILECONFIG