]>
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( DeleteGroup
);
74 CPPUNIT_TEST( DeleteAll
);
75 CPPUNIT_TEST( RenameEntry
);
76 CPPUNIT_TEST( RenameGroup
);
77 CPPUNIT_TEST( CreateEntriesAndSubgroup
);
78 CPPUNIT_TEST( CreateSubgroupAndEntries
);
79 CPPUNIT_TEST( DeleteLastGroup
);
80 CPPUNIT_TEST( DeleteAndRecreateGroup
);
81 CPPUNIT_TEST_SUITE_END();
96 void CreateEntriesAndSubgroup();
97 void CreateSubgroupAndEntries();
98 void DeleteLastGroup();
99 void DeleteAndRecreateGroup();
101 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
108 void CheckGroupEntries(const wxFileConfig
& fc
,
112 void CheckGroupSubgroups(const wxFileConfig
& fc
,
117 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
120 // register in the unnamed registry so that these tests are run by default
121 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
123 // also include in it's own registry so that these tests can be run alone
124 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
126 void FileConfigTestCase::Path()
128 wxStringInputStream
sis(testconfig
);
129 wxFileConfig
fc(sis
);
131 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
132 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
133 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
134 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
135 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
136 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
139 void FileConfigTestCase::AddEntries()
143 wxVERIFY_FILECONFIG( _T(""), fc
);
145 fc
.Write(_T("/Foo"), _T("foo"));
146 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc
);
148 fc
.Write(_T("/Bar/Baz"), _T("baz"));
149 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
152 wxVERIFY_FILECONFIG( _T(""), fc
);
154 fc
.Write(_T("/Bar/Baz"), _T("baz"));
155 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc
);
157 fc
.Write(_T("/Foo"), _T("foo"));
158 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
162 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
167 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
169 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
172 va_start(ap
, nEntries
);
176 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
178 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
180 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
183 CPPUNIT_ASSERT( nEntries
== 0 );
189 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
194 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
196 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
199 va_start(ap
, nGroups
);
203 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
205 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
207 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
210 CPPUNIT_ASSERT( nGroups
== 0 );
215 void FileConfigTestCase::GetEntries()
217 wxStringInputStream
sis(testconfig
);
218 wxFileConfig
fc(sis
);
220 CheckGroupEntries(fc
, _T(""), 0);
221 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
222 CheckGroupEntries(fc
, _T("/root/group1"), 0);
223 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
224 2, _T("subentry"), _T("subentry2"));
227 void FileConfigTestCase::GetGroups()
229 wxStringInputStream
sis(testconfig
);
230 wxFileConfig
fc(sis
);
232 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
233 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
234 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
235 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
238 void FileConfigTestCase::HasEntry()
240 wxStringInputStream
sis(testconfig
);
241 wxFileConfig
fc(sis
);
243 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
244 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
245 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
246 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
247 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
248 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
249 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
250 CPPUNIT_ASSERT( !fc
.HasEntry(_T("/root/no_such_group/entry")) );
251 CPPUNIT_ASSERT( !fc
.HasGroup(_T("/root/no_such_group")) );
254 void FileConfigTestCase::HasGroup()
256 wxStringInputStream
sis(testconfig
);
257 wxFileConfig
fc(sis
);
259 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
260 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
261 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
262 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
263 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
264 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
265 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
266 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot/subgroup")) );
267 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
270 void FileConfigTestCase::Binary()
272 wxStringInputStream
sis(
276 wxFileConfig
fc(sis
);
279 fc
.Read("/root/binary", &buf
);
281 CPPUNIT_ASSERT( memcmp("foo\n", buf
.GetData(), buf
.GetDataLen()) == 0 );
284 buf
.AppendData("\0\1\2", 3);
285 fc
.Write("/root/012", buf
);
294 void FileConfigTestCase::Save()
296 wxStringInputStream
sis(testconfig
);
297 wxFileConfig
fc(sis
);
298 wxVERIFY_FILECONFIG( testconfig
, fc
);
301 void FileConfigTestCase::DeleteEntry()
303 wxStringInputStream
sis(testconfig
);
304 wxFileConfig
fc(sis
);
306 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
308 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
309 wxVERIFY_FILECONFIG( _T("[root]\n")
311 _T("[root/group1]\n")
312 _T("[root/group1/subgroup]\n")
313 _T("subentry2=subvalue2\n")
314 _T("[root/group2]\n"),
317 // group should be deleted now as well as it became empty
318 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
319 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
320 wxVERIFY_FILECONFIG( _T("[root]\n")
322 _T("[root/group1]\n")
323 _T("[root/group2]\n"),
327 void FileConfigTestCase::DeleteGroup()
329 wxStringInputStream
sis(testconfig
);
330 wxFileConfig
fc(sis
);
332 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
334 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
335 wxVERIFY_FILECONFIG( _T("[root]\n")
337 _T("[root/group2]\n"),
340 // notice trailing slash: it should be ignored
341 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2/")) );
342 wxVERIFY_FILECONFIG( _T("[root]\n")
346 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
347 CPPUNIT_ASSERT( Dump(fc
).empty() );
350 void FileConfigTestCase::DeleteAll()
352 wxStringInputStream
sis(testconfig
);
353 wxFileConfig
fc(sis
);
355 CPPUNIT_ASSERT( fc
.DeleteAll() );
356 CPPUNIT_ASSERT( Dump(fc
).empty() );
359 void FileConfigTestCase::RenameEntry()
361 wxStringInputStream
sis(testconfig
);
362 wxFileConfig
fc(sis
);
364 fc
.SetPath(_T("root"));
365 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
366 wxVERIFY_FILECONFIG( _T("[root]\n")
367 _T("newname=value\n")
368 _T("[root/group1]\n")
369 _T("[root/group1/subgroup]\n")
370 _T("subentry=subvalue\n")
371 _T("subentry2=subvalue2\n")
372 _T("[root/group2]\n"),
375 fc
.SetPath(_T("group1/subgroup"));
376 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
377 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
379 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
380 wxVERIFY_FILECONFIG( _T("[root]\n")
381 _T("newname=value\n")
382 _T("[root/group1]\n")
383 _T("[root/group1/subgroup]\n")
384 _T("subentry2=subvalue2\n")
385 _T("subentry1=subvalue\n")
386 _T("[root/group2]\n"),
390 void FileConfigTestCase::RenameGroup()
392 wxStringInputStream
sis(testconfig
);
393 wxFileConfig
fc(sis
);
395 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
396 wxVERIFY_FILECONFIG( _T("[foot]\n")
398 _T("[foot/group1]\n")
399 _T("[foot/group1/subgroup]\n")
400 _T("subentry=subvalue\n")
401 _T("subentry2=subvalue2\n")
402 _T("[foot/group2]\n"),
405 // renaming a path doesn't work, it must be the immediate group
406 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("foot/group1"), _T("group2")) );
409 fc
.SetPath(_T("foot"));
411 // renaming to a name of existing group doesn't work
412 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("group1"), _T("group2")) );
414 // try exchanging the groups names and then restore them back
415 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
416 wxVERIFY_FILECONFIG( _T("[foot]\n")
418 _T("[foot/groupTmp]\n")
419 _T("[foot/groupTmp/subgroup]\n")
420 _T("subentry=subvalue\n")
421 _T("subentry2=subvalue2\n")
422 _T("[foot/group2]\n"),
425 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
426 wxVERIFY_FILECONFIG( _T("[foot]\n")
428 _T("[foot/groupTmp]\n")
429 _T("[foot/groupTmp/subgroup]\n")
430 _T("subentry=subvalue\n")
431 _T("subentry2=subvalue2\n")
432 _T("[foot/group1]\n"),
435 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
436 wxVERIFY_FILECONFIG( _T("[foot]\n")
438 _T("[foot/group2]\n")
439 _T("[foot/group2/subgroup]\n")
440 _T("subentry=subvalue\n")
441 _T("subentry2=subvalue2\n")
442 _T("[foot/group1]\n"),
445 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
446 wxVERIFY_FILECONFIG( _T("[foot]\n")
448 _T("[foot/group2]\n")
449 _T("[foot/group2/subgroup]\n")
450 _T("subentry=subvalue\n")
451 _T("subentry2=subvalue2\n")
452 _T("[foot/groupTmp]\n"),
455 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
456 wxVERIFY_FILECONFIG( _T("[foot]\n")
458 _T("[foot/group1]\n")
459 _T("[foot/group1/subgroup]\n")
460 _T("subentry=subvalue\n")
461 _T("subentry2=subvalue2\n")
462 _T("[foot/groupTmp]\n"),
465 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
466 wxVERIFY_FILECONFIG( _T("[foot]\n")
468 _T("[foot/group1]\n")
469 _T("[foot/group1/subgroup]\n")
470 _T("subentry=subvalue\n")
471 _T("subentry2=subvalue2\n")
472 _T("[foot/group2]\n"),
476 void FileConfigTestCase::CreateSubgroupAndEntries()
479 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
480 fc
.Write(_T("first"), _T("one"));
482 wxVERIFY_FILECONFIG( _T("first=one\n")
484 _T("sub_first=sub_one\n"),
488 void FileConfigTestCase::CreateEntriesAndSubgroup()
491 fc
.Write(_T("first"), _T("one"));
492 fc
.Write(_T("second"), _T("two"));
493 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
495 wxVERIFY_FILECONFIG( _T("first=one\n")
498 _T("sub_first=sub_one\n"),
502 static void EmptyConfigAndWriteKey()
504 wxFileConfig
fc(_T("deleteconftest"));
506 const wxString groupPath
= _T("/root");
508 if ( fc
.Exists(groupPath
) )
510 // using DeleteGroup exposes the problem, using DeleteAll doesn't
511 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
514 // the config must be empty for the problem to arise
515 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
516 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
519 // this crashes on second call of this function
520 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
523 void FileConfigTestCase::DeleteLastGroup()
526 We make 2 of the same calls, first to create a file config with a single
529 ::EmptyConfigAndWriteKey();
532 ... then the same but this time the key's group is deleted before the
533 key is written again. This causes a crash.
535 ::EmptyConfigAndWriteKey();
540 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
543 void FileConfigTestCase::DeleteAndRecreateGroup()
545 static const wxChar
*confInitial
=
551 wxStringInputStream
sis(confInitial
);
552 wxFileConfig
fc(sis
);
554 fc
.DeleteGroup(_T("Second"));
555 wxVERIFY_FILECONFIG( _T("[First]\n")
559 fc
.Write(_T("Second/Value2"), _T("New"));
560 wxVERIFY_FILECONFIG( _T("[First]\n")
567 #endif // wxUSE_FILECONFIG