]>
git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.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
);
71 CPPUNIT_TEST( DeleteEntry
);
72 CPPUNIT_TEST( DeleteGroup
);
73 CPPUNIT_TEST( DeleteAll
);
74 CPPUNIT_TEST( RenameEntry
);
75 CPPUNIT_TEST( RenameGroup
);
76 CPPUNIT_TEST( CreateEntriesAndSubgroup
);
77 CPPUNIT_TEST( CreateSubgroupAndEntries
);
78 CPPUNIT_TEST( DeleteLastGroup
);
79 CPPUNIT_TEST_SUITE_END();
93 void CreateEntriesAndSubgroup();
94 void CreateSubgroupAndEntries();
95 void DeleteLastGroup();
97 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
104 void CheckGroupEntries(const wxFileConfig
& fc
,
108 void CheckGroupSubgroups(const wxFileConfig
& fc
,
113 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
116 // register in the unnamed registry so that these tests are run by default
117 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
119 // also include in it's own registry so that these tests can be run alone
120 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
122 void FileConfigTestCase::Path()
124 wxStringInputStream
sis(testconfig
);
125 wxFileConfig
fc(sis
);
127 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
128 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
129 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
130 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
131 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
132 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
135 void FileConfigTestCase::AddEntries()
139 wxVERIFY_FILECONFIG( _T(""), fc
);
141 fc
.Write(_T("/Foo"), _T("foo"));
142 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc
);
144 fc
.Write(_T("/Bar/Baz"), _T("baz"));
145 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
148 wxVERIFY_FILECONFIG( _T(""), fc
);
150 fc
.Write(_T("/Bar/Baz"), _T("baz"));
151 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc
);
153 fc
.Write(_T("/Foo"), _T("foo"));
154 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc
);
158 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
163 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
165 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
168 va_start(ap
, nEntries
);
172 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
174 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
176 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
179 CPPUNIT_ASSERT( nEntries
== 0 );
185 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
190 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
192 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
195 va_start(ap
, nGroups
);
199 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
201 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
203 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
206 CPPUNIT_ASSERT( nGroups
== 0 );
211 void FileConfigTestCase::GetEntries()
213 wxStringInputStream
sis(testconfig
);
214 wxFileConfig
fc(sis
);
216 CheckGroupEntries(fc
, _T(""), 0);
217 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
218 CheckGroupEntries(fc
, _T("/root/group1"), 0);
219 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
220 2, _T("subentry"), _T("subentry2"));
223 void FileConfigTestCase::GetGroups()
225 wxStringInputStream
sis(testconfig
);
226 wxFileConfig
fc(sis
);
228 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
229 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
230 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
231 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
234 void FileConfigTestCase::HasEntry()
236 wxStringInputStream
sis(testconfig
);
237 wxFileConfig
fc(sis
);
239 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
240 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
241 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
242 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
243 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
244 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
245 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
246 CPPUNIT_ASSERT( !fc
.HasEntry(_T("/root/no_such_group/entry")) );
247 CPPUNIT_ASSERT( !fc
.HasGroup(_T("/root/no_such_group")) );
250 void FileConfigTestCase::HasGroup()
252 wxStringInputStream
sis(testconfig
);
253 wxFileConfig
fc(sis
);
255 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
256 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
257 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
258 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
259 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
260 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
261 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
262 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot/subgroup")) );
263 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
266 void FileConfigTestCase::Save()
268 wxStringInputStream
sis(testconfig
);
269 wxFileConfig
fc(sis
);
270 wxVERIFY_FILECONFIG( testconfig
, fc
);
273 void FileConfigTestCase::DeleteEntry()
275 wxStringInputStream
sis(testconfig
);
276 wxFileConfig
fc(sis
);
278 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
280 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
281 wxVERIFY_FILECONFIG( _T("[root]\n")
283 _T("[root/group1]\n")
284 _T("[root/group1/subgroup]\n")
285 _T("subentry2=subvalue2\n")
286 _T("[root/group2]\n"),
289 // group should be deleted now as well as it became empty
290 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
291 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
292 wxVERIFY_FILECONFIG( _T("[root]\n")
294 _T("[root/group1]\n")
295 _T("[root/group2]\n"),
299 void FileConfigTestCase::DeleteGroup()
301 wxStringInputStream
sis(testconfig
);
302 wxFileConfig
fc(sis
);
304 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
306 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
307 wxVERIFY_FILECONFIG( _T("[root]\n")
309 _T("[root/group2]\n"),
312 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
313 wxVERIFY_FILECONFIG( _T("[root]\n")
317 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
318 CPPUNIT_ASSERT( Dump(fc
).empty() );
321 void FileConfigTestCase::DeleteAll()
323 wxStringInputStream
sis(testconfig
);
324 wxFileConfig
fc(sis
);
326 CPPUNIT_ASSERT( fc
.DeleteAll() );
327 CPPUNIT_ASSERT( Dump(fc
).empty() );
330 void FileConfigTestCase::RenameEntry()
332 wxStringInputStream
sis(testconfig
);
333 wxFileConfig
fc(sis
);
335 fc
.SetPath(_T("root"));
336 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
337 wxVERIFY_FILECONFIG( _T("[root]\n")
338 _T("newname=value\n")
339 _T("[root/group1]\n")
340 _T("[root/group1/subgroup]\n")
341 _T("subentry=subvalue\n")
342 _T("subentry2=subvalue2\n")
343 _T("[root/group2]\n"),
346 fc
.SetPath(_T("group1/subgroup"));
347 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
348 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
350 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
351 wxVERIFY_FILECONFIG( _T("[root]\n")
352 _T("newname=value\n")
353 _T("[root/group1]\n")
354 _T("[root/group1/subgroup]\n")
355 _T("subentry2=subvalue2\n")
356 _T("subentry1=subvalue\n")
357 _T("[root/group2]\n"),
361 void FileConfigTestCase::RenameGroup()
363 wxStringInputStream
sis(testconfig
);
364 wxFileConfig
fc(sis
);
366 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
367 wxVERIFY_FILECONFIG( _T("[foot]\n")
369 _T("[foot/group1]\n")
370 _T("[foot/group1/subgroup]\n")
371 _T("subentry=subvalue\n")
372 _T("subentry2=subvalue2\n")
373 _T("[foot/group2]\n"),
376 // renaming a path doesn't work, it must be the immediate group
377 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("foot/group1"), _T("group2")) );
380 fc
.SetPath(_T("foot"));
382 // renaming to a name of existing group doesn't work
383 CPPUNIT_ASSERT( !fc
.RenameGroup(_T("group1"), _T("group2")) );
385 // try exchanging the groups names and then restore them back
386 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
387 wxVERIFY_FILECONFIG( _T("[foot]\n")
389 _T("[foot/groupTmp]\n")
390 _T("[foot/groupTmp/subgroup]\n")
391 _T("subentry=subvalue\n")
392 _T("subentry2=subvalue2\n")
393 _T("[foot/group2]\n"),
396 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
397 wxVERIFY_FILECONFIG( _T("[foot]\n")
399 _T("[foot/groupTmp]\n")
400 _T("[foot/groupTmp/subgroup]\n")
401 _T("subentry=subvalue\n")
402 _T("subentry2=subvalue2\n")
403 _T("[foot/group1]\n"),
406 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
407 wxVERIFY_FILECONFIG( _T("[foot]\n")
409 _T("[foot/group2]\n")
410 _T("[foot/group2/subgroup]\n")
411 _T("subentry=subvalue\n")
412 _T("subentry2=subvalue2\n")
413 _T("[foot/group1]\n"),
416 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group1"), _T("groupTmp")) );
417 wxVERIFY_FILECONFIG( _T("[foot]\n")
419 _T("[foot/group2]\n")
420 _T("[foot/group2/subgroup]\n")
421 _T("subentry=subvalue\n")
422 _T("subentry2=subvalue2\n")
423 _T("[foot/groupTmp]\n"),
426 CPPUNIT_ASSERT( fc
.RenameGroup(_T("group2"), _T("group1")) );
427 wxVERIFY_FILECONFIG( _T("[foot]\n")
429 _T("[foot/group1]\n")
430 _T("[foot/group1/subgroup]\n")
431 _T("subentry=subvalue\n")
432 _T("subentry2=subvalue2\n")
433 _T("[foot/groupTmp]\n"),
436 CPPUNIT_ASSERT( fc
.RenameGroup(_T("groupTmp"), _T("group2")) );
437 wxVERIFY_FILECONFIG( _T("[foot]\n")
439 _T("[foot/group1]\n")
440 _T("[foot/group1/subgroup]\n")
441 _T("subentry=subvalue\n")
442 _T("subentry2=subvalue2\n")
443 _T("[foot/group2]\n"),
447 void FileConfigTestCase::CreateSubgroupAndEntries()
450 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
451 fc
.Write(_T("first"), _T("one"));
453 wxVERIFY_FILECONFIG( _T("first=one\n")
455 _T("sub_first=sub_one\n"),
459 void FileConfigTestCase::CreateEntriesAndSubgroup()
462 fc
.Write(_T("first"), _T("one"));
463 fc
.Write(_T("second"), _T("two"));
464 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
466 wxVERIFY_FILECONFIG( _T("first=one\n")
469 _T("sub_first=sub_one\n"),
473 static void EmptyConfigAndWriteKey()
475 wxFileConfig
fc(_T("deleteconftest"));
477 const wxString groupPath
= _T("/root");
479 if ( fc
.Exists(groupPath
) )
481 // using DeleteGroup exposes the problem, using DeleteAll doesn't
482 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
485 // the config must be empty for the problem to arise
486 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
487 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
490 // this crashes on second call of this function
491 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
494 void FileConfigTestCase::DeleteLastGroup()
497 We make 2 of the same calls, first to create a file config with a single
500 ::EmptyConfigAndWriteKey();
503 ... then the same but this time the key's group is deleted before the
504 key is written again. This causes a crash.
506 ::EmptyConfigAndWriteKey();
511 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
514 #endif // wxUSE_FILECONFIG