]>
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( CreateEntriesAndSubgroup
);
63 CPPUNIT_TEST( CreateSubgroupAndEntries
);
64 CPPUNIT_TEST( DeleteLastGroup
);
65 CPPUNIT_TEST_SUITE_END();
79 void CreateEntriesAndSubgroup();
80 void CreateSubgroupAndEntries();
81 void DeleteLastGroup();
83 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
90 static wxString
Dump(wxFileConfig
& fc
)
92 wxStringOutputStream sos
;
94 return wxTextFile::Translate(sos
.GetString(), wxTextFileType_Unix
);
97 void CheckGroupEntries(const wxFileConfig
& fc
,
101 void CheckGroupSubgroups(const wxFileConfig
& fc
,
106 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
109 // register in the unnamed registry so that these tests are run by default
110 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
112 // also include in it's own registry so that these tests can be run alone
113 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
115 void FileConfigTestCase::Path()
117 wxStringInputStream
sis(testconfig
);
118 wxFileConfig
fc(sis
);
120 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
121 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
122 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
123 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
124 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
125 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
128 void FileConfigTestCase::AddEntries()
132 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
134 fc
.Write(_T("/Foo"), _T("foo"));
135 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n") );
137 fc
.Write(_T("/Bar/Baz"), _T("baz"));
138 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
141 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
143 fc
.Write(_T("/Bar/Baz"), _T("baz"));
144 CPPUNIT_ASSERT( Dump(fc
) == _T("[Bar]\nBaz=baz\n") );
146 fc
.Write(_T("/Foo"), _T("foo"));
147 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
151 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
156 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
158 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
161 va_start(ap
, nEntries
);
165 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
167 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
169 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
172 CPPUNIT_ASSERT( nEntries
== 0 );
178 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
183 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
185 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
188 va_start(ap
, nGroups
);
192 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
194 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
196 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
199 CPPUNIT_ASSERT( nGroups
== 0 );
204 void FileConfigTestCase::GetEntries()
206 wxStringInputStream
sis(testconfig
);
207 wxFileConfig
fc(sis
);
209 CheckGroupEntries(fc
, _T(""), 0);
210 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
211 CheckGroupEntries(fc
, _T("/root/group1"), 0);
212 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
213 2, _T("subentry"), _T("subentry2"));
216 void FileConfigTestCase::GetGroups()
218 wxStringInputStream
sis(testconfig
);
219 wxFileConfig
fc(sis
);
221 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
222 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
223 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
224 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
227 void FileConfigTestCase::HasEntry()
229 wxStringInputStream
sis(testconfig
);
230 wxFileConfig
fc(sis
);
232 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
233 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
234 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
235 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
236 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
237 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
238 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
241 void FileConfigTestCase::HasGroup()
243 wxStringInputStream
sis(testconfig
);
244 wxFileConfig
fc(sis
);
246 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
247 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
248 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
249 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
250 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
251 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
252 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
253 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot/subgroup")) );
254 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
257 void FileConfigTestCase::Save()
259 wxStringInputStream
sis(testconfig
);
260 wxFileConfig
fc(sis
);
261 CPPUNIT_ASSERT( Dump(fc
) == testconfig
);
264 void FileConfigTestCase::DeleteEntry()
266 wxStringInputStream
sis(testconfig
);
267 wxFileConfig
fc(sis
);
269 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
271 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
272 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
274 _T("[root/group1]\n")
275 _T("[root/group1/subgroup]\n")
276 _T("subentry2=subvalue2\n")
277 _T("[root/group2]\n") );
279 // group should be deleted now as well as it became empty
280 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
281 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
282 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
284 _T("[root/group1]\n")
285 _T("[root/group2]\n") );
288 void FileConfigTestCase::DeleteGroup()
290 wxStringInputStream
sis(testconfig
);
291 wxFileConfig
fc(sis
);
293 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
295 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
296 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
298 _T("[root/group2]\n") );
300 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
301 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
302 _T("entry=value\n") );
304 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
305 CPPUNIT_ASSERT( Dump(fc
).empty() );
308 void FileConfigTestCase::DeleteAll()
310 wxStringInputStream
sis(testconfig
);
311 wxFileConfig
fc(sis
);
313 CPPUNIT_ASSERT( fc
.DeleteAll() );
314 CPPUNIT_ASSERT( Dump(fc
).empty() );
317 void FileConfigTestCase::RenameEntry()
319 wxStringInputStream
sis(testconfig
);
320 wxFileConfig
fc(sis
);
322 fc
.SetPath(_T("root"));
323 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
324 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
325 _T("newname=value\n")
326 _T("[root/group1]\n")
327 _T("[root/group1/subgroup]\n")
328 _T("subentry=subvalue\n")
329 _T("subentry2=subvalue2\n")
330 _T("[root/group2]\n") );
332 fc
.SetPath(_T("group1/subgroup"));
333 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
334 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
336 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
337 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
338 _T("newname=value\n")
339 _T("[root/group1]\n")
340 _T("[root/group1/subgroup]\n")
341 _T("subentry2=subvalue2\n")
342 _T("subentry1=subvalue\n")
343 _T("[root/group2]\n") );
346 void FileConfigTestCase::RenameGroup()
348 wxStringInputStream
sis(testconfig
);
349 wxFileConfig
fc(sis
);
351 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
352 CPPUNIT_ASSERT( Dump(fc
) == _T("[foot]\n")
354 _T("[foot/group1]\n")
355 _T("[foot/group1/subgroup]\n")
356 _T("subentry=subvalue\n")
357 _T("subentry2=subvalue2\n")
358 _T("[foot/group2]\n") );
361 void FileConfigTestCase::CreateSubgroupAndEntries()
364 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
365 fc
.Write(_T("first"), _T("one"));
367 CPPUNIT_ASSERT( Dump(fc
) == _T("first=one\n")
369 _T("sub_first=sub_one\n"));
372 void FileConfigTestCase::CreateEntriesAndSubgroup()
375 fc
.Write(_T("first"), _T("one"));
376 fc
.Write(_T("second"), _T("two"));
377 fc
.Write(_T("sub/sub_first"), _T("sub_one"));
379 CPPUNIT_ASSERT( Dump(fc
) == _T("first=one\n")
382 _T("sub_first=sub_one\n"));
385 static void EmptyConfigAndWriteKey()
387 wxFileConfig
fc(_T("deleteconftest"));
389 const wxString groupPath
= _T("/root");
391 if ( fc
.Exists(groupPath
) )
393 // using DeleteGroup exposes the problem, using DeleteAll doesn't
394 CPPUNIT_ASSERT( fc
.DeleteGroup(groupPath
) );
397 // the config must be empty for the problem to arise
398 CPPUNIT_ASSERT( !fc
.GetNumberOfEntries(true) );
399 CPPUNIT_ASSERT( !fc
.GetNumberOfGroups(true) );
402 // this crashes on second call of this function
403 CPPUNIT_ASSERT( fc
.Write(groupPath
+ _T("/entry"), _T("value")) );
406 void FileConfigTestCase::DeleteLastGroup()
409 We make 2 of the same calls, first to create a file config with a single
412 ::EmptyConfigAndWriteKey();
415 ... then the same but this time the key's group is deleted before the
416 key is written again. This causes a crash.
418 ::EmptyConfigAndWriteKey();
423 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
426 #endif // wxUSE_FILECONFIG