]>
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_SUITE_END();
93 void DeleteAndWriteEntry();
98 void CreateEntriesAndSubgroup();
99 void CreateSubgroupAndEntries();
100 void DeleteLastGroup();
101 void DeleteAndRecreateGroup();
103 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
110 void CheckGroupEntries(const wxFileConfig
& fc
,
114 void CheckGroupSubgroups(const wxFileConfig
& fc
,
119 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
122 // register in the unnamed registry so that these tests are run by default
123 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
125 // also include in it's own registry so that these tests can be run alone
126 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
128 void FileConfigTestCase::Path()
130 wxStringInputStream
sis(testconfig
);
131 wxFileConfig
fc(sis
);
133 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
134 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
135 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
136 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
137 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
138 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
141 void FileConfigTestCase::AddEntries()
145 wxVERIFY_FILECONFIG( _T(""), fc
);
147 fc
.Write(_T("/Foo"), _T("foo"));
148 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc
);
150 fc
.Write(_T("/Bar/Baz"), _T("baz"));
151 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
154 wxVERIFY_FILECONFIG( _T(""), fc
);
156 fc
.Write(_T("/Bar/Baz"), _T("baz"));
157 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc
);
159 fc
.Write(_T("/Foo"), _T("foo"));
160 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
164 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
169 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
171 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
174 va_start(ap
, nEntries
);
178 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
180 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
182 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
185 CPPUNIT_ASSERT( nEntries
== 0 );
191 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
196 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
198 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
201 va_start(ap
, nGroups
);
205 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
207 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
209 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
212 CPPUNIT_ASSERT( nGroups
== 0 );
217 void FileConfigTestCase::GetEntries()
219 wxStringInputStream
sis(testconfig
);
220 wxFileConfig
fc(sis
);
222 CheckGroupEntries(fc
, _T(""), 0);
223 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
224 CheckGroupEntries(fc
, _T("/root/group1"), 0);
225 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
226 2, _T("subentry"), _T("subentry2"));
229 void FileConfigTestCase::GetGroups()
231 wxStringInputStream
sis(testconfig
);
232 wxFileConfig
fc(sis
);
234 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
235 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
236 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
237 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
240 void FileConfigTestCase::HasEntry()
242 wxStringInputStream
sis(testconfig
);
243 wxFileConfig
fc(sis
);
245 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
246 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
247 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
248 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
249 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
250 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
251 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
252 CPPUNIT_ASSERT( !fc
.HasEntry(_T("/root/no_such_group/entry")) );
253 CPPUNIT_ASSERT( !fc
.HasGroup(_T("/root/no_such_group")) );
256 void FileConfigTestCase::HasGroup()
258 wxStringInputStream
sis(testconfig
);
259 wxFileConfig
fc(sis
);
261 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
262 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
263 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
264 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
265 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
266 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
267 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
268 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot/subgroup")) );
269 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
272 void FileConfigTestCase::Binary()
274 wxStringInputStream
sis(
278 wxFileConfig
fc(sis
);
281 fc
.Read("/root/binary", &buf
);
283 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
286 buf
.AppendData("\0\1\2", 3);
287 fc
.Write("/root/012", buf
);
296 void FileConfigTestCase::Save()
298 wxStringInputStream
sis(testconfig
);
299 wxFileConfig
fc(sis
);
300 wxVERIFY_FILECONFIG( testconfig
, fc
);
303 void FileConfigTestCase::DeleteEntry()
305 wxStringInputStream
sis(testconfig
);
306 wxFileConfig
fc(sis
);
308 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
310 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
311 wxVERIFY_FILECONFIG( _T("[root]\n")
313 _T("[root/group1]\n")
314 _T("[root/group1/subgroup]\n")
315 _T("subentry2=subvalue2\n")
316 _T("[root/group2]\n"),
319 // group should be deleted now as well as it became empty
320 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
321 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
322 wxVERIFY_FILECONFIG( _T("[root]\n")
324 _T("[root/group1]\n")
325 _T("[root/group2]\n"),
329 void FileConfigTestCase::DeleteAndWriteEntry()
331 wxStringInputStream
sis(
333 "subentry=subvalue\n"
334 "subentry2=subvalue2\n"
335 "subentry3=subvalue3\n"
338 wxFileConfig
fc(sis
);
340 fc
.DeleteEntry("/root/group1/subentry2");
341 fc
.Write("/root/group1/subentry2", "testvalue");
342 fc
.DeleteEntry("/root/group2/subentry2");
343 fc
.Write("/root/group2/subentry2", "testvalue2");
344 fc
.DeleteEntry("/root/group1/subentry2");
345 fc
.Write("/root/group1/subentry2", "testvalue");
346 fc
.DeleteEntry("/root/group2/subentry2");
347 fc
.Write("/root/group2/subentry2", "testvalue2");
349 wxVERIFY_FILECONFIG( "[root/group1]\n"
350 "subentry=subvalue\n"
351 "subentry3=subvalue3\n"
352 "subentry2=testvalue\n"
354 "subentry2=testvalue2\n",
357 fc
.DeleteEntry("/root/group2/subentry2");
358 wxVERIFY_FILECONFIG( "[root/group1]\n"
359 "subentry=subvalue\n"
360 "subentry3=subvalue3\n"
361 "subentry2=testvalue\n",
364 fc
.DeleteEntry("/root/group1/subentry2");
365 fc
.DeleteEntry("/root/group1/subentry");
366 fc
.DeleteEntry("/root/group1/subentry3");
367 wxVERIFY_FILECONFIG( "", fc
);
370 void FileConfigTestCase::DeleteGroup()
372 wxStringInputStream
sis(testconfig
);
373 wxFileConfig
fc(sis
);
375 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
377 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
378 wxVERIFY_FILECONFIG( _T("[root]\n")
380 _T("[root/group2]\n"),
383 // notice trailing slash: it should be ignored
384 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2/")) );
385 wxVERIFY_FILECONFIG( _T("[root]\n")
389 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
390 CPPUNIT_ASSERT( Dump(fc
).empty() );
393 void FileConfigTestCase::DeleteAll()
395 wxStringInputStream
sis(testconfig
);
396 wxFileConfig
fc(sis
);
398 CPPUNIT_ASSERT( fc
.DeleteAll() );
399 CPPUNIT_ASSERT( Dump(fc
).empty() );
402 void FileConfigTestCase::RenameEntry()
404 wxStringInputStream
sis(testconfig
);
405 wxFileConfig
fc(sis
);
407 fc
.SetPath(_T("root"));
408 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
409 wxVERIFY_FILECONFIG( _T("[root]\n")
410 _T("newname=value\n")
411 _T("[root/group1]\n")
412 _T("[root/group1/subgroup]\n")
413 _T("subentry=subvalue\n")
414 _T("subentry2=subvalue2\n")
415 _T("[root/group2]\n"),
418 fc
.SetPath(_T("group1/subgroup"));
419 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
420 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
422 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
423 wxVERIFY_FILECONFIG( _T("[root]\n")
424 _T("newname=value\n")
425 _T("[root/group1]\n")
426 _T("[root/group1/subgroup]\n")
427 _T("subentry2=subvalue2\n")
428 _T("subentry1=subvalue\n")
429 _T("[root/group2]\n"),
433 void FileConfigTestCase::RenameGroup()
435 wxStringInputStream
sis(testconfig
);
436 wxFileConfig
fc(sis
);
438 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
439 wxVERIFY_FILECONFIG( _T("[foot]\n")
441 _T("[foot/group1]\n")
442 _T("[foot/group1/subgroup]\n")
443 _T("subentry=subvalue\n")
444 _T("subentry2=subvalue2\n")
445 _T("[foot/group2]\n"),
448 // renaming a path doesn't work, it must be the immediate group
449 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("foot/group1"), _T("group2")) );
452 fc
.SetPath(_T("foot"));
454 // renaming to a name of existing group doesn't work
455 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("group1"), _T("group2")) );
457 // try exchanging the groups names and then restore them back
458 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
459 wxVERIFY_FILECONFIG( _T("[foot]\n")
461 _T("[foot/groupTmp]\n")
462 _T("[foot/groupTmp/subgroup]\n")
463 _T("subentry=subvalue\n")
464 _T("subentry2=subvalue2\n")
465 _T("[foot/group2]\n"),
468 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
469 wxVERIFY_FILECONFIG( _T("[foot]\n")
471 _T("[foot/groupTmp]\n")
472 _T("[foot/groupTmp/subgroup]\n")
473 _T("subentry=subvalue\n")
474 _T("subentry2=subvalue2\n")
475 _T("[foot/group1]\n"),
478 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
479 wxVERIFY_FILECONFIG( _T("[foot]\n")
481 _T("[foot/group2]\n")
482 _T("[foot/group2/subgroup]\n")
483 _T("subentry=subvalue\n")
484 _T("subentry2=subvalue2\n")
485 _T("[foot/group1]\n"),
488 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
489 wxVERIFY_FILECONFIG( _T("[foot]\n")
491 _T("[foot/group2]\n")
492 _T("[foot/group2/subgroup]\n")
493 _T("subentry=subvalue\n")
494 _T("subentry2=subvalue2\n")
495 _T("[foot/groupTmp]\n"),
498 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
499 wxVERIFY_FILECONFIG( _T("[foot]\n")
501 _T("[foot/group1]\n")
502 _T("[foot/group1/subgroup]\n")
503 _T("subentry=subvalue\n")
504 _T("subentry2=subvalue2\n")
505 _T("[foot/groupTmp]\n"),
508 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
509 wxVERIFY_FILECONFIG( _T("[foot]\n")
511 _T("[foot/group1]\n")
512 _T("[foot/group1/subgroup]\n")
513 _T("subentry=subvalue\n")
514 _T("subentry2=subvalue2\n")
515 _T("[foot/group2]\n"),
519 void FileConfigTestCase::CreateSubgroupAndEntries()
522 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
523 fc
.Write(_T("first"), _T("one"));
525 wxVERIFY_FILECONFIG( _T("first=one\n")
527 _T("sub_first=sub_one\n"),
531 void FileConfigTestCase::CreateEntriesAndSubgroup()
534 fc
.Write(_T("first"), _T("one"));
535 fc
.Write(_T("second"), _T("two"));
536 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
538 wxVERIFY_FILECONFIG( _T("first=one\n")
541 _T("sub_first=sub_one\n"),
545 static void EmptyConfigAndWriteKey()
547 wxFileConfig
fc(_T("deleteconftest"));
549 const wxString groupPath
= _T("/root");
551 if ( fc
.Exists(groupPath
) )
553 // using DeleteGroup exposes the problem, using DeleteAll doesn't
554 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
557 // the config must be empty for the problem to arise
558 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
559 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
562 // this crashes on second call of this function
563 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
566 void FileConfigTestCase::DeleteLastGroup()
569 We make 2 of the same calls, first to create a file config with a single
572 ::EmptyConfigAndWriteKey();
575 ... then the same but this time the key's group is deleted before the
576 key is written again. This causes a crash.
578 ::EmptyConfigAndWriteKey();
583 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
586 void FileConfigTestCase::DeleteAndRecreateGroup()
588 static const wxChar
*confInitial
=
594 wxStringInputStream
sis(confInitial
);
595 wxFileConfig
fc(sis
);
597 fc
.DeleteGroup(_T("Second"));
598 wxVERIFY_FILECONFIG( _T("[First]\n")
602 fc
.Write(_T("Second/Value2"), _T("New"));
603 wxVERIFY_FILECONFIG( _T("[First]\n")
610 #endif // wxUSE_FILECONFIG