]>
git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/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 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
28 #include "wx/cppunit.h"
30 static const wxChar
*testconfig
=
34 _T("[root/group1/subgroup]\n")
35 _T("subentry=subvalue\n")
36 _T("subentry2=subvalue2\n")
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class FileConfigTestCase
: public CppUnit::TestCase
47 FileConfigTestCase() { }
50 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
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_SUITE_END();
76 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
83 static wxString
Dump(wxFileConfig
& fc
)
85 wxStringOutputStream sos
;
87 return wxTextFile::Translate(sos
.GetString(), wxTextFileType_Unix
);
90 void CheckGroupEntries(const wxFileConfig
& fc
,
94 void CheckGroupSubgroups(const wxFileConfig
& fc
,
99 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
102 // register in the unnamed registry so that these tests are run by default
103 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
105 // also include in it's own registry so that these tests can be run alone
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
108 void FileConfigTestCase::Path()
110 wxStringInputStream
sis(testconfig
);
111 wxFileConfig
fc(sis
);
113 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
114 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
115 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
116 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
117 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
118 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
122 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
127 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
129 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
132 va_start(ap
, nEntries
);
136 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
138 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
140 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
143 CPPUNIT_ASSERT( nEntries
== 0 );
149 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
154 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
156 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
159 va_start(ap
, nGroups
);
163 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
165 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
167 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
170 CPPUNIT_ASSERT( nGroups
== 0 );
175 void FileConfigTestCase::GetEntries()
177 wxStringInputStream
sis(testconfig
);
178 wxFileConfig
fc(sis
);
180 CheckGroupEntries(fc
, _T(""), 0);
181 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
182 CheckGroupEntries(fc
, _T("/root/group1"), 0);
183 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
184 2, _T("subentry"), _T("subentry2"));
187 void FileConfigTestCase::GetGroups()
189 wxStringInputStream
sis(testconfig
);
190 wxFileConfig
fc(sis
);
192 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
193 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
194 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
195 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
198 void FileConfigTestCase::HasEntry()
200 wxStringInputStream
sis(testconfig
);
201 wxFileConfig
fc(sis
);
203 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
204 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
205 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
206 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
207 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
208 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
209 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
212 void FileConfigTestCase::HasGroup()
214 wxStringInputStream
sis(testconfig
);
215 wxFileConfig
fc(sis
);
217 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
218 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
219 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
220 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
221 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
222 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
223 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
224 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
227 void FileConfigTestCase::Save()
229 wxStringInputStream
sis(testconfig
);
230 wxFileConfig
fc(sis
);
231 CPPUNIT_ASSERT( Dump(fc
) == testconfig
);
234 void FileConfigTestCase::DeleteEntry()
236 wxStringInputStream
sis(testconfig
);
237 wxFileConfig
fc(sis
);
239 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
241 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
242 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
244 _T("[root/group1]\n")
245 _T("[root/group1/subgroup]\n")
246 _T("subentry2=subvalue2\n")
247 _T("[root/group2]\n") );
249 // group should be deleted now as well as it became empty
250 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
251 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
252 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
254 _T("[root/group1]\n")
255 _T("[root/group2]\n") );
258 void FileConfigTestCase::DeleteGroup()
260 wxStringInputStream
sis(testconfig
);
261 wxFileConfig
fc(sis
);
263 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
265 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
266 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
268 _T("[root/group2]\n") );
270 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
271 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
272 _T("entry=value\n") );
274 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
275 CPPUNIT_ASSERT( Dump(fc
).empty() );
278 void FileConfigTestCase::DeleteAll()
280 wxStringInputStream
sis(testconfig
);
281 wxFileConfig
fc(sis
);
283 CPPUNIT_ASSERT( fc
.DeleteAll() );
284 CPPUNIT_ASSERT( Dump(fc
).empty() );
287 void FileConfigTestCase::RenameEntry()
289 wxStringInputStream
sis(testconfig
);
290 wxFileConfig
fc(sis
);
292 fc
.SetPath(_T("root"));
293 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
294 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
295 _T("newname=value\n")
296 _T("[root/group1]\n")
297 _T("[root/group1/subgroup]\n")
298 _T("subentry=subvalue\n")
299 _T("subentry2=subvalue2\n")
300 _T("[root/group2]\n") );
302 fc
.SetPath(_T("group1/subgroup"));
303 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
304 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
306 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
307 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
308 _T("newname=value\n")
309 _T("[root/group1]\n")
310 _T("[root/group1/subgroup]\n")
311 _T("subentry2=subvalue2\n")
312 _T("subentry1=subvalue\n")
313 _T("[root/group2]\n") );
316 void FileConfigTestCase::RenameGroup()
318 wxStringInputStream
sis(testconfig
);
319 wxFileConfig
fc(sis
);
321 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
322 CPPUNIT_ASSERT( Dump(fc
) == _T("[foot]\n")
324 _T("[foot/group1]\n")
325 _T("[foot/group1/subgroup]\n")
326 _T("subentry=subvalue\n")
327 _T("subentry2=subvalue2\n")
328 _T("[foot/group2]\n") );
331 #endif // wxUSE_FILECONFIG