]>
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 // ----------------------------------------------------------------------------
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( AddEntries
);
53 CPPUNIT_TEST( GetEntries
);
54 CPPUNIT_TEST( GetGroups
);
55 CPPUNIT_TEST( HasEntry
);
56 CPPUNIT_TEST( HasGroup
);
58 CPPUNIT_TEST( DeleteEntry
);
59 CPPUNIT_TEST( DeleteGroup
);
60 CPPUNIT_TEST( DeleteAll
);
61 CPPUNIT_TEST( RenameEntry
);
62 CPPUNIT_TEST( RenameGroup
);
63 CPPUNIT_TEST_SUITE_END();
78 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
85 static wxString
Dump(wxFileConfig
& fc
)
87 wxStringOutputStream sos
;
89 return wxTextFile::Translate(sos
.GetString(), wxTextFileType_Unix
);
92 void CheckGroupEntries(const wxFileConfig
& fc
,
96 void CheckGroupSubgroups(const wxFileConfig
& fc
,
101 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
104 // register in the unnamed registry so that these tests are run by default
105 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
107 // also include in it's own registry so that these tests can be run alone
108 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
110 void FileConfigTestCase::Path()
112 wxStringInputStream
sis(testconfig
);
113 wxFileConfig
fc(sis
);
115 CPPUNIT_ASSERT( ChangePath(fc
, _T("")) == _T("") );
116 CPPUNIT_ASSERT( ChangePath(fc
, _T("/")) == _T("") );
117 CPPUNIT_ASSERT( ChangePath(fc
, _T("root")) == _T("/root") );
118 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root")) == _T("/root") );
119 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
120 CPPUNIT_ASSERT( ChangePath(fc
, _T("/root/group2")) == _T("/root/group2") );
123 void FileConfigTestCase::AddEntries()
127 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
129 fc
.Write(_T("/Foo"), _T("foo"));
130 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n") );
132 fc
.Write(_T("/Bar/Baz"), _T("baz"));
133 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
136 CPPUNIT_ASSERT( Dump(fc
) == _T("") );
138 fc
.Write(_T("/Bar/Baz"), _T("baz"));
139 CPPUNIT_ASSERT( Dump(fc
) == _T("[Bar]\nBaz=baz\n") );
141 fc
.Write(_T("/Foo"), _T("foo"));
142 CPPUNIT_ASSERT( Dump(fc
) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
146 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
151 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
153 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
156 va_start(ap
, nEntries
);
160 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
162 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
164 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
167 CPPUNIT_ASSERT( nEntries
== 0 );
173 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
178 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
180 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
183 va_start(ap
, nGroups
);
187 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
189 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
191 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
194 CPPUNIT_ASSERT( nGroups
== 0 );
199 void FileConfigTestCase::GetEntries()
201 wxStringInputStream
sis(testconfig
);
202 wxFileConfig
fc(sis
);
204 CheckGroupEntries(fc
, _T(""), 0);
205 CheckGroupEntries(fc
, _T("/root"), 1, _T("entry"));
206 CheckGroupEntries(fc
, _T("/root/group1"), 0);
207 CheckGroupEntries(fc
, _T("/root/group1/subgroup"),
208 2, _T("subentry"), _T("subentry2"));
211 void FileConfigTestCase::GetGroups()
213 wxStringInputStream
sis(testconfig
);
214 wxFileConfig
fc(sis
);
216 CheckGroupSubgroups(fc
, _T(""), 1, _T("root"));
217 CheckGroupSubgroups(fc
, _T("/root"), 2, _T("group1"), _T("group2"));
218 CheckGroupSubgroups(fc
, _T("/root/group1"), 1, _T("subgroup"));
219 CheckGroupSubgroups(fc
, _T("/root/group2"), 0);
222 void FileConfigTestCase::HasEntry()
224 wxStringInputStream
sis(testconfig
);
225 wxFileConfig
fc(sis
);
227 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root")) );
228 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/entry")) );
229 CPPUNIT_ASSERT( fc
.HasEntry(_T("/root/entry")) );
230 CPPUNIT_ASSERT( fc
.HasEntry(_T("root/group1/subgroup/subentry")) );
231 CPPUNIT_ASSERT( !fc
.HasEntry(_T("")) );
232 CPPUNIT_ASSERT( !fc
.HasEntry(_T("root/group1")) );
233 CPPUNIT_ASSERT( !fc
.HasEntry(_T("subgroup/subentry")) );
236 void FileConfigTestCase::HasGroup()
238 wxStringInputStream
sis(testconfig
);
239 wxFileConfig
fc(sis
);
241 CPPUNIT_ASSERT( fc
.HasGroup(_T("root")) );
242 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1")) );
243 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group1/subgroup")) );
244 CPPUNIT_ASSERT( fc
.HasGroup(_T("root/group2")) );
245 CPPUNIT_ASSERT( !fc
.HasGroup(_T("foot")) );
246 CPPUNIT_ASSERT( !fc
.HasGroup(_T("")) );
247 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root/group")) );
248 CPPUNIT_ASSERT( !fc
.HasGroup(_T("root//subgroup")) );
251 void FileConfigTestCase::Save()
253 wxStringInputStream
sis(testconfig
);
254 wxFileConfig
fc(sis
);
255 CPPUNIT_ASSERT( Dump(fc
) == testconfig
);
258 void FileConfigTestCase::DeleteEntry()
260 wxStringInputStream
sis(testconfig
);
261 wxFileConfig
fc(sis
);
263 CPPUNIT_ASSERT( !fc
.DeleteEntry(_T("foo")) );
265 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("root/group1/subgroup/subentry")) );
266 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
268 _T("[root/group1]\n")
269 _T("[root/group1/subgroup]\n")
270 _T("subentry2=subvalue2\n")
271 _T("[root/group2]\n") );
273 // group should be deleted now as well as it became empty
274 wxConfigPathChanger
change(&fc
, _T("root/group1/subgroup/subentry2"));
275 CPPUNIT_ASSERT( fc
.DeleteEntry(_T("subentry2")) );
276 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
278 _T("[root/group1]\n")
279 _T("[root/group2]\n") );
282 void FileConfigTestCase::DeleteGroup()
284 wxStringInputStream
sis(testconfig
);
285 wxFileConfig
fc(sis
);
287 CPPUNIT_ASSERT( !fc
.DeleteGroup(_T("foo")) );
289 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group1")) );
290 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
292 _T("[root/group2]\n") );
294 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root/group2")) );
295 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
296 _T("entry=value\n") );
298 CPPUNIT_ASSERT( fc
.DeleteGroup(_T("root")) );
299 CPPUNIT_ASSERT( Dump(fc
).empty() );
302 void FileConfigTestCase::DeleteAll()
304 wxStringInputStream
sis(testconfig
);
305 wxFileConfig
fc(sis
);
307 CPPUNIT_ASSERT( fc
.DeleteAll() );
308 CPPUNIT_ASSERT( Dump(fc
).empty() );
311 void FileConfigTestCase::RenameEntry()
313 wxStringInputStream
sis(testconfig
);
314 wxFileConfig
fc(sis
);
316 fc
.SetPath(_T("root"));
317 CPPUNIT_ASSERT( fc
.RenameEntry(_T("entry"), _T("newname")) );
318 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
319 _T("newname=value\n")
320 _T("[root/group1]\n")
321 _T("[root/group1/subgroup]\n")
322 _T("subentry=subvalue\n")
323 _T("subentry2=subvalue2\n")
324 _T("[root/group2]\n") );
326 fc
.SetPath(_T("group1/subgroup"));
327 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("entry"), _T("newname")) );
328 CPPUNIT_ASSERT( !fc
.RenameEntry(_T("subentry"), _T("subentry2")) );
330 CPPUNIT_ASSERT( fc
.RenameEntry(_T("subentry"), _T("subentry1")) );
331 CPPUNIT_ASSERT( Dump(fc
) == _T("[root]\n")
332 _T("newname=value\n")
333 _T("[root/group1]\n")
334 _T("[root/group1/subgroup]\n")
335 _T("subentry2=subvalue2\n")
336 _T("subentry1=subvalue\n")
337 _T("[root/group2]\n") );
340 void FileConfigTestCase::RenameGroup()
342 wxStringInputStream
sis(testconfig
);
343 wxFileConfig
fc(sis
);
345 CPPUNIT_ASSERT( fc
.RenameGroup(_T("root"), _T("foot")) );
346 CPPUNIT_ASSERT( Dump(fc
) == _T("[foot]\n")
348 _T("[foot/group1]\n")
349 _T("[foot/group1/subgroup]\n")
350 _T("subentry=subvalue\n")
351 _T("subentry2=subvalue2\n")
352 _T("[foot/group2]\n") );
355 #endif // wxUSE_FILECONFIG