]>
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 class FileConfigTestCase
: public CppUnit::TestCase
46 FileConfigTestCase() { }
49 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
51 CPPUNIT_TEST( AddEntries
);
52 CPPUNIT_TEST( GetEntries
);
53 CPPUNIT_TEST( GetGroups
);
54 CPPUNIT_TEST( HasEntry
);
55 CPPUNIT_TEST( HasGroup
);
57 CPPUNIT_TEST( DeleteEntry
);
58 CPPUNIT_TEST( DeleteGroup
);
59 CPPUNIT_TEST( DeleteAll
);
60 CPPUNIT_TEST( RenameEntry
);
61 CPPUNIT_TEST( RenameGroup
);
62 CPPUNIT_TEST( DeleteLastGroup
);
63 CPPUNIT_TEST_SUITE_END();
77 void DeleteLastGroup();
79 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
86 static wxString
Dump(wxFileConfig
& fc
)
88 wxStringOutputStream sos
;
90 return wxTextFile::Translate(sos
.GetString(), wxTextFileType_Unix
);
93 void CheckGroupEntries(const wxFileConfig
& fc
,
97 void CheckGroupSubgroups(const wxFileConfig
& fc
,
102 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
105 // register in the unnamed registry so that these tests are run by default
106 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
108 // also include in it's own registry so that these tests can be run alone
109 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
111 void FileConfigTestCase::Path()
113 wxStringInputStream
sis(testconfig
);
114 wxFileConfig
fc(sis
);
116 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
117 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
118 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
119 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
120 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
121 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
124 void FileConfigTestCase::AddEntries()
128 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
130 fc
.Write(_T("/Foo"), _T("foo"));
131 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n") );
133 fc
.Write(_T("/Bar/Baz"), _T("baz"));
134 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
137 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
139 fc
.Write(_T("/Bar/Baz"), _T("baz"));
140 CPPUNIT_ASSERT( Dump(fc
) == _T("[Bar]\nBaz=baz\n") );
142 fc
.Write(_T("/Foo"), _T("foo"));
143 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
147 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
152 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
154 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
157 va_start(ap
, nEntries
);
161 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
163 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
165 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
168 CPPUNIT_ASSERT( nEntries
== 0 );
174 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
179 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
181 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
184 va_start(ap
, nGroups
);
188 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
190 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
192 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
195 CPPUNIT_ASSERT( nGroups
== 0 );
200 void FileConfigTestCase::GetEntries()
202 wxStringInputStream
sis(testconfig
);
203 wxFileConfig
fc(sis
);
205 CheckGroupEntries(fc
, _T(""), 0);
206 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
207 CheckGroupEntries(fc
, _T("/root/group1"), 0);
208 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
209 2, _T("subentry"), _T("subentry2"));
212 void FileConfigTestCase::GetGroups()
214 wxStringInputStream
sis(testconfig
);
215 wxFileConfig
fc(sis
);
217 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
218 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
219 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
220 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
223 void FileConfigTestCase::HasEntry()
225 wxStringInputStream
sis(testconfig
);
226 wxFileConfig
fc(sis
);
228 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
229 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
230 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
231 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
232 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
233 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
234 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
237 void FileConfigTestCase::HasGroup()
239 wxStringInputStream
sis(testconfig
);
240 wxFileConfig
fc(sis
);
242 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
243 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
244 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
245 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
246 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
247 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
248 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
249 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
252 void FileConfigTestCase::Save()
254 wxStringInputStream
sis(testconfig
);
255 wxFileConfig
fc(sis
);
256 CPPUNIT_ASSERT( Dump(fc
) == testconfig
);
259 void FileConfigTestCase::DeleteEntry()
261 wxStringInputStream
sis(testconfig
);
262 wxFileConfig
fc(sis
);
264 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
266 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
267 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
269 _T("[root/group1]\n")
270 _T("[root/group1/subgroup]\n")
271 _T("subentry2=subvalue2\n")
272 _T("[root/group2]\n") );
274 // group should be deleted now as well as it became empty
275 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
276 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
277 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
279 _T("[root/group1]\n")
280 _T("[root/group2]\n") );
283 void FileConfigTestCase::DeleteGroup()
285 wxStringInputStream
sis(testconfig
);
286 wxFileConfig
fc(sis
);
288 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
290 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
291 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
293 _T("[root/group2]\n") );
295 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
296 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
297 _T("entry=value\n") );
299 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
300 CPPUNIT_ASSERT( Dump(fc
).empty() );
303 void FileConfigTestCase::DeleteAll()
305 wxStringInputStream
sis(testconfig
);
306 wxFileConfig
fc(sis
);
308 CPPUNIT_ASSERT( fc
.DeleteAll() );
309 CPPUNIT_ASSERT( Dump(fc
).empty() );
312 void FileConfigTestCase::RenameEntry()
314 wxStringInputStream
sis(testconfig
);
315 wxFileConfig
fc(sis
);
317 fc
.SetPath(_T("root"));
318 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
319 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
320 _T("newname=value\n")
321 _T("[root/group1]\n")
322 _T("[root/group1/subgroup]\n")
323 _T("subentry=subvalue\n")
324 _T("subentry2=subvalue2\n")
325 _T("[root/group2]\n") );
327 fc
.SetPath(_T("group1/subgroup"));
328 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
329 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
331 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
332 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
333 _T("newname=value\n")
334 _T("[root/group1]\n")
335 _T("[root/group1/subgroup]\n")
336 _T("subentry2=subvalue2\n")
337 _T("subentry1=subvalue\n")
338 _T("[root/group2]\n") );
341 void FileConfigTestCase::RenameGroup()
343 wxStringInputStream
sis(testconfig
);
344 wxFileConfig
fc(sis
);
346 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
347 CPPUNIT_ASSERT( Dump(fc
) == _T("[foot]\n")
349 _T("[foot/group1]\n")
350 _T("[foot/group1/subgroup]\n")
351 _T("subentry=subvalue\n")
352 _T("subentry2=subvalue2\n")
353 _T("[foot/group2]\n") );
357 static void EmptyConfigAndWriteKey()
359 wxFileConfig
fc(_T("deleteconftest"));
361 const wxString groupPath
= _T("/root");
363 if (fc
.Exists(groupPath
))
365 // using DeleteGroup exposes the problem, using DeleteAll doesn't
366 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
369 // the config must be empty for the problem to arise
370 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
371 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
374 // this crashes on second call of this function
375 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
378 void FileConfigTestCase::DeleteLastGroup()
381 We make 2 of the same calls, first to create a file config with a single
384 ::EmptyConfigAndWriteKey();
387 ... then the same but this time the key's group is deleted before the
388 key is written again. This causes a crash.
390 ::EmptyConfigAndWriteKey();
395 (void) ::wxRemoveFile(
396 wxFileConfig::GetLocalFileName(_T("deleteconftest")) );
399 #endif // wxUSE_FILECONFIG