]> git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconftest.cpp
766e2accaf4ea098ec78f99a7fab249d1bc89043
[wxWidgets.git] / tests / fileconf / fileconftest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/fileconf/fileconf.cpp
3 // Purpose: wxFileConf unit test
4 // Author: Vadim Zeitlin
5 // Created: 2004-09-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_FILECONFIG
21
22 #ifndef WX_PRECOMP
23 #endif // WX_PRECOMP
24
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
27 #include "wx/wfstream.h"
28
29 #include "wx/cppunit.h"
30
31 static const wxChar *testconfig =
32 _T("[root]\n")
33 _T("entry=value\n")
34 _T("[root/group1]\n")
35 _T("[root/group1/subgroup]\n")
36 _T("subentry=subvalue\n")
37 _T("subentry2=subvalue2\n")
38 _T("[root/group2]\n")
39 ;
40
41 // ----------------------------------------------------------------------------
42 // test class
43 // ----------------------------------------------------------------------------
44
45 class FileConfigTestCase : public CppUnit::TestCase
46 {
47 public:
48 FileConfigTestCase()
49 {
50 }
51
52 private:
53 CPPUNIT_TEST_SUITE( FileConfigTestCase );
54 CPPUNIT_TEST( Path );
55 CPPUNIT_TEST( AddEntries );
56 CPPUNIT_TEST( GetEntries );
57 CPPUNIT_TEST( GetGroups );
58 CPPUNIT_TEST( HasEntry );
59 CPPUNIT_TEST( HasGroup );
60 CPPUNIT_TEST( Save );
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();
67
68 void Path();
69 void AddEntries();
70 void GetEntries();
71 void GetGroups();
72 void HasEntry();
73 void HasGroup();
74 void Save();
75 void DeleteEntry();
76 void DeleteGroup();
77 void DeleteAll();
78 void RenameEntry();
79 void RenameGroup();
80
81 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
82 {
83 fc.SetPath(path);
84
85 return fc.GetPath();
86 }
87
88 static wxString Dump(wxFileConfig& fc)
89 {
90 wxFileOutputStream* pOutFile = new wxFileOutputStream(_T("outconf.txt"));
91 fc.Save(*pOutFile);
92 delete pOutFile;
93
94 wxFileInputStream* pInFile = new wxFileInputStream(_T("outconf.txt"));
95 char* szOut = new char[pInFile->GetSize()];
96 pInFile->Read(szOut, pInFile->GetSize());
97
98 wxString realString = wxTextFile::Translate(
99 wxString(szOut, wxConvLocal, pInFile->GetSize()),
100 wxTextFileType_Unix
101 );
102
103 delete szOut;
104 delete pInFile;
105
106 return realString;
107 }
108
109 void CheckGroupEntries(const wxFileConfig& fc,
110 const wxChar *path,
111 size_t nEntries,
112 ...);
113 void CheckGroupSubgroups(const wxFileConfig& fc,
114 const wxChar *path,
115 size_t nGroups,
116 ...);
117
118 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
119 };
120
121 // register in the unnamed registry so that these tests are run by default
122 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
123
124 // also include in it's own registry so that these tests can be run alone
125 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
126
127 void FileConfigTestCase::Path()
128 {
129 wxStringInputStream sis(testconfig);
130 wxFileConfig fc(sis);
131
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") );
138 }
139
140 void FileConfigTestCase::AddEntries()
141 {
142 wxFileConfig fc;
143
144 CPPUNIT_ASSERT( Dump(fc) == _T("") );
145
146 fc.Write(_T("/Foo"), _T("foo"));
147 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n") );
148
149 fc.Write(_T("/Bar/Baz"), _T("baz"));
150 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
151
152 fc.DeleteAll();
153 CPPUNIT_ASSERT( Dump(fc) == _T("") );
154
155 fc.Write(_T("/Bar/Baz"), _T("baz"));
156 CPPUNIT_ASSERT( Dump(fc) == _T("[Bar]\nBaz=baz\n") );
157
158 fc.Write(_T("/Foo"), _T("foo"));
159 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
160 }
161
162 void
163 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
164 const wxChar *path,
165 size_t nEntries,
166 ...)
167 {
168 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
169
170 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
171
172 va_list ap;
173 va_start(ap, nEntries);
174
175 long cookie;
176 wxString name;
177 for ( bool cont = fc.GetFirstEntry(name, cookie);
178 cont;
179 cont = fc.GetNextEntry(name, cookie), nEntries-- )
180 {
181 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
182 }
183
184 CPPUNIT_ASSERT( nEntries == 0 );
185
186 va_end(ap);
187 }
188
189 void
190 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
191 const wxChar *path,
192 size_t nGroups,
193 ...)
194 {
195 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
196
197 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
198
199 va_list ap;
200 va_start(ap, nGroups);
201
202 long cookie;
203 wxString name;
204 for ( bool cont = fc.GetFirstGroup(name, cookie);
205 cont;
206 cont = fc.GetNextGroup(name, cookie), nGroups-- )
207 {
208 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
209 }
210
211 CPPUNIT_ASSERT( nGroups == 0 );
212
213 va_end(ap);
214 }
215
216 void FileConfigTestCase::GetEntries()
217 {
218 wxStringInputStream sis(testconfig);
219 wxFileConfig fc(sis);
220
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"));
226 }
227
228 void FileConfigTestCase::GetGroups()
229 {
230 wxStringInputStream sis(testconfig);
231 wxFileConfig fc(sis);
232
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);
237 }
238
239 void FileConfigTestCase::HasEntry()
240 {
241 wxStringInputStream sis(testconfig);
242 wxFileConfig fc(sis);
243
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")) );
251 }
252
253 void FileConfigTestCase::HasGroup()
254 {
255 wxStringInputStream sis(testconfig);
256 wxFileConfig fc(sis);
257
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")) );
266 }
267
268 void FileConfigTestCase::Save()
269 {
270 wxStringInputStream sis(testconfig);
271 wxFileConfig fc(sis);
272 CPPUNIT_ASSERT( Dump(fc) == testconfig );
273 }
274
275 void FileConfigTestCase::DeleteEntry()
276 {
277 wxStringInputStream sis(testconfig);
278 wxFileConfig fc(sis);
279
280 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
281
282 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
283 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
284 _T("entry=value\n")
285 _T("[root/group1]\n")
286 _T("[root/group1/subgroup]\n")
287 _T("subentry2=subvalue2\n")
288 _T("[root/group2]\n") );
289
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")
294 _T("entry=value\n")
295 _T("[root/group1]\n")
296 _T("[root/group2]\n") );
297 }
298
299 void FileConfigTestCase::DeleteGroup()
300 {
301 wxStringInputStream sis(testconfig);
302 wxFileConfig fc(sis);
303
304 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
305
306 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
307 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
308 _T("entry=value\n")
309 _T("[root/group2]\n") );
310
311 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2")) );
312 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
313 _T("entry=value\n") );
314
315 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
316 CPPUNIT_ASSERT( Dump(fc).empty() );
317 }
318
319 void FileConfigTestCase::DeleteAll()
320 {
321 wxStringInputStream sis(testconfig);
322 wxFileConfig fc(sis);
323
324 CPPUNIT_ASSERT( fc.DeleteAll() );
325 CPPUNIT_ASSERT( Dump(fc).empty() );
326 }
327
328 void FileConfigTestCase::RenameEntry()
329 {
330 wxStringInputStream sis(testconfig);
331 wxFileConfig fc(sis);
332
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") );
342
343 fc.SetPath(_T("group1/subgroup"));
344 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
345 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
346
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") );
355 }
356
357 void FileConfigTestCase::RenameGroup()
358 {
359 wxStringInputStream sis(testconfig);
360 wxFileConfig fc(sis);
361
362 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
363 CPPUNIT_ASSERT( Dump(fc) == _T("[foot]\n")
364 _T("entry=value\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") );
370 }
371
372 #endif // wxUSE_FILECONFIG
373