]>
git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.cpp
766e2accaf4ea098ec78f99a7fab249d1bc89043
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 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
27 #include "wx/wfstream.h"
29 #include "wx/cppunit.h"
31 static const wxChar
*testconfig
=
35 _T("[root/group1/subgroup]\n")
36 _T("subentry=subvalue\n")
37 _T("subentry2=subvalue2\n")
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 class FileConfigTestCase
: public CppUnit::TestCase
53 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
55 CPPUNIT_TEST( AddEntries
);
56 CPPUNIT_TEST( GetEntries
);
57 CPPUNIT_TEST( GetGroups
);
58 CPPUNIT_TEST( HasEntry
);
59 CPPUNIT_TEST( HasGroup
);
61 CPPUNIT_TEST( DeleteEntry
);
62 CPPUNIT_TEST( DeleteGroup
);
63 CPPUNIT_TEST( DeleteAll
);
64 CPPUNIT_TEST( RenameEntry
);
65 CPPUNIT_TEST( RenameGroup
);
66 CPPUNIT_TEST_SUITE_END();
81 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
88 static wxString
Dump(wxFileConfig
& fc
)
90 wxFileOutputStream
* pOutFile
= new wxFileOutputStream(_T("outconf.txt"));
94 wxFileInputStream
* pInFile
= new wxFileInputStream(_T("outconf.txt"));
95 char* szOut
= new char[pInFile
->GetSize()];
96 pInFile
->Read(szOut
, pInFile
->GetSize());
98 wxString realString
= wxTextFile::Translate(
99 wxString(szOut
, wxConvLocal
, pInFile
->GetSize()),
109 void CheckGroupEntries(const wxFileConfig
& fc
,
113 void CheckGroupSubgroups(const wxFileConfig
& fc
,
118 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
121 // register in the unnamed registry so that these tests are run by default
122 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
124 // also include in it's own registry so that these tests can be run alone
125 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
127 void FileConfigTestCase::Path()
129 wxStringInputStream
sis(testconfig
);
130 wxFileConfig
fc(sis
);
132 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
133 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
134 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
135 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
136 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
137 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
140 void FileConfigTestCase::AddEntries()
144 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
146 fc
.Write(_T("/Foo"), _T("foo"));
147 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n") );
149 fc
.Write(_T("/Bar/Baz"), _T("baz"));
150 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
153 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
155 fc
.Write(_T("/Bar/Baz"), _T("baz"));
156 CPPUNIT_ASSERT( Dump(fc
) == _T("[Bar]\nBaz=baz\n") );
158 fc
.Write(_T("/Foo"), _T("foo"));
159 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
163 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
168 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
170 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
173 va_start(ap
, nEntries
);
177 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
179 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
181 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
184 CPPUNIT_ASSERT( nEntries
== 0 );
190 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
195 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
197 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
200 va_start(ap
, nGroups
);
204 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
206 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
208 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
211 CPPUNIT_ASSERT( nGroups
== 0 );
216 void FileConfigTestCase::GetEntries()
218 wxStringInputStream
sis(testconfig
);
219 wxFileConfig
fc(sis
);
221 CheckGroupEntries(fc
, _T(""), 0);
222 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
223 CheckGroupEntries(fc
, _T("/root/group1"), 0);
224 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
225 2, _T("subentry"), _T("subentry2"));
228 void FileConfigTestCase::GetGroups()
230 wxStringInputStream
sis(testconfig
);
231 wxFileConfig
fc(sis
);
233 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
234 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
235 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
236 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
239 void FileConfigTestCase::HasEntry()
241 wxStringInputStream
sis(testconfig
);
242 wxFileConfig
fc(sis
);
244 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
245 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
246 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
247 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
248 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
249 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
250 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
253 void FileConfigTestCase::HasGroup()
255 wxStringInputStream
sis(testconfig
);
256 wxFileConfig
fc(sis
);
258 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
259 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
260 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
261 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
262 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
263 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
264 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
265 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
268 void FileConfigTestCase::Save()
270 wxStringInputStream
sis(testconfig
);
271 wxFileConfig
fc(sis
);
272 CPPUNIT_ASSERT( Dump(fc
) == testconfig
);
275 void FileConfigTestCase::DeleteEntry()
277 wxStringInputStream
sis(testconfig
);
278 wxFileConfig
fc(sis
);
280 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
282 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
283 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
285 _T("[root/group1]\n")
286 _T("[root/group1/subgroup]\n")
287 _T("subentry2=subvalue2\n")
288 _T("[root/group2]\n") );
290 // group should be deleted now as well as it became empty
291 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
292 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
293 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
295 _T("[root/group1]\n")
296 _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 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
309 _T("[root/group2]\n") );
311 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
312 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
313 _T("entry=value\n") );
315 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
316 CPPUNIT_ASSERT( Dump(fc
).empty() );
319 void FileConfigTestCase::DeleteAll()
321 wxStringInputStream
sis(testconfig
);
322 wxFileConfig
fc(sis
);
324 CPPUNIT_ASSERT( fc
.DeleteAll() );
325 CPPUNIT_ASSERT( Dump(fc
).empty() );
328 void FileConfigTestCase::RenameEntry()
330 wxStringInputStream
sis(testconfig
);
331 wxFileConfig
fc(sis
);
333 fc
.SetPath(_T("root"));
334 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
335 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
336 _T("newname=value\n")
337 _T("[root/group1]\n")
338 _T("[root/group1/subgroup]\n")
339 _T("subentry=subvalue\n")
340 _T("subentry2=subvalue2\n")
341 _T("[root/group2]\n") );
343 fc
.SetPath(_T("group1/subgroup"));
344 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
345 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
347 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
348 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
349 _T("newname=value\n")
350 _T("[root/group1]\n")
351 _T("[root/group1/subgroup]\n")
352 _T("subentry2=subvalue2\n")
353 _T("subentry1=subvalue\n")
354 _T("[root/group2]\n") );
357 void FileConfigTestCase::RenameGroup()
359 wxStringInputStream
sis(testconfig
);
360 wxFileConfig
fc(sis
);
362 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
363 CPPUNIT_ASSERT( Dump(fc
) == _T("[foot]\n")
365 _T("[foot/group1]\n")
366 _T("[foot/group1/subgroup]\n")
367 _T("subentry=subvalue\n")
368 _T("subentry2=subvalue2\n")
369 _T("[foot/group2]\n") );
372 #endif // wxUSE_FILECONFIG