]>
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"
28 static const wxChar
*testconfig
=
32 _T("[root/group1/subgroup]\n")
33 _T("subentry=subvalue\n")
34 _T("subentry2=subvalue2\n")
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class FileConfigTestCase
: public CppUnit::TestCase
45 FileConfigTestCase() { }
48 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
50 CPPUNIT_TEST( AddEntries
);
51 CPPUNIT_TEST( GetEntries
);
52 CPPUNIT_TEST( GetGroups
);
53 CPPUNIT_TEST( HasEntry
);
54 CPPUNIT_TEST( HasGroup
);
56 CPPUNIT_TEST( DeleteEntry
);
57 CPPUNIT_TEST( DeleteGroup
);
58 CPPUNIT_TEST( DeleteAll
);
59 CPPUNIT_TEST( RenameEntry
);
60 CPPUNIT_TEST( RenameGroup
);
61 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") );
121 void FileConfigTestCase::AddEntries()
125 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
127 fc
.Write(_T("/Foo"), _T("foo"));
128 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n") );
130 fc
.Write(_T("/Bar/Baz"), _T("baz"));
131 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
134 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
136 fc
.Write(_T("/Bar/Baz"), _T("baz"));
137 CPPUNIT_ASSERT( Dump(fc
) == _T("[Bar]\nBaz=baz\n") );
139 fc
.Write(_T("/Foo"), _T("foo"));
140 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
144 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
149 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
151 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
154 va_start(ap
, nEntries
);
158 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
160 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
162 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
165 CPPUNIT_ASSERT( nEntries
== 0 );
171 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
176 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
178 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
181 va_start(ap
, nGroups
);
185 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
187 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
189 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
192 CPPUNIT_ASSERT( nGroups
== 0 );
197 void FileConfigTestCase::GetEntries()
199 wxStringInputStream
sis(testconfig
);
200 wxFileConfig
fc(sis
);
202 CheckGroupEntries(fc
, _T(""), 0);
203 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
204 CheckGroupEntries(fc
, _T("/root/group1"), 0);
205 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
206 2, _T("subentry"), _T("subentry2"));
209 void FileConfigTestCase::GetGroups()
211 wxStringInputStream
sis(testconfig
);
212 wxFileConfig
fc(sis
);
214 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
215 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
216 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
217 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
220 void FileConfigTestCase::HasEntry()
222 wxStringInputStream
sis(testconfig
);
223 wxFileConfig
fc(sis
);
225 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
226 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
227 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
228 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
229 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
230 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
231 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
234 void FileConfigTestCase::HasGroup()
236 wxStringInputStream
sis(testconfig
);
237 wxFileConfig
fc(sis
);
239 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
240 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
241 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
242 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
243 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
244 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
245 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
246 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
249 void FileConfigTestCase::Save()
251 wxStringInputStream
sis(testconfig
);
252 wxFileConfig
fc(sis
);
253 CPPUNIT_ASSERT( Dump(fc
) == testconfig
);
256 void FileConfigTestCase::DeleteEntry()
258 wxStringInputStream
sis(testconfig
);
259 wxFileConfig
fc(sis
);
261 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
263 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
264 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
266 _T("[root/group1]\n")
267 _T("[root/group1/subgroup]\n")
268 _T("subentry2=subvalue2\n")
269 _T("[root/group2]\n") );
271 // group should be deleted now as well as it became empty
272 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
273 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
274 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
276 _T("[root/group1]\n")
277 _T("[root/group2]\n") );
280 void FileConfigTestCase::DeleteGroup()
282 wxStringInputStream
sis(testconfig
);
283 wxFileConfig
fc(sis
);
285 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
287 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
288 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
290 _T("[root/group2]\n") );
292 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
293 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
294 _T("entry=value\n") );
296 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
297 CPPUNIT_ASSERT( Dump(fc
).empty() );
300 void FileConfigTestCase::DeleteAll()
302 wxStringInputStream
sis(testconfig
);
303 wxFileConfig
fc(sis
);
305 CPPUNIT_ASSERT( fc
.DeleteAll() );
306 CPPUNIT_ASSERT( Dump(fc
).empty() );
309 void FileConfigTestCase::RenameEntry()
311 wxStringInputStream
sis(testconfig
);
312 wxFileConfig
fc(sis
);
314 fc
.SetPath(_T("root"));
315 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
316 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
317 _T("newname=value\n")
318 _T("[root/group1]\n")
319 _T("[root/group1/subgroup]\n")
320 _T("subentry=subvalue\n")
321 _T("subentry2=subvalue2\n")
322 _T("[root/group2]\n") );
324 fc
.SetPath(_T("group1/subgroup"));
325 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
326 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
328 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
329 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
330 _T("newname=value\n")
331 _T("[root/group1]\n")
332 _T("[root/group1/subgroup]\n")
333 _T("subentry2=subvalue2\n")
334 _T("subentry1=subvalue\n")
335 _T("[root/group2]\n") );
338 void FileConfigTestCase::RenameGroup()
340 wxStringInputStream
sis(testconfig
);
341 wxFileConfig
fc(sis
);
343 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
344 CPPUNIT_ASSERT( Dump(fc
) == _T("[foot]\n")
346 _T("[foot/group1]\n")
347 _T("[foot/group1/subgroup]\n")
348 _T("subentry=subvalue\n")
349 _T("subentry2=subvalue2\n")
350 _T("[foot/group2]\n") );
353 #endif // wxUSE_FILECONFIG