]> git.saurik.com Git - wxWidgets.git/blame - tests/fileconf/fileconftest.cpp
DEBUGREPORT
[wxWidgets.git] / tests / fileconf / fileconftest.cpp
CommitLineData
17c24d37
VZ
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
8899b155 14#include "testprec.h"
17c24d37
VZ
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"
ca7766b4 27#include "wx/log.h"
17c24d37
VZ
28
29static const wxChar *testconfig =
30_T("[root]\n")
31_T("entry=value\n")
32_T("[root/group1]\n")
33_T("[root/group1/subgroup]\n")
34_T("subentry=subvalue\n")
9db6158a 35_T("subentry2=subvalue2\n")
17c24d37
VZ
36_T("[root/group2]\n")
37;
38
39// ----------------------------------------------------------------------------
40// test class
41// ----------------------------------------------------------------------------
42
43class FileConfigTestCase : public CppUnit::TestCase
44{
45public:
8899b155 46 FileConfigTestCase() { }
17c24d37
VZ
47
48private:
49 CPPUNIT_TEST_SUITE( FileConfigTestCase );
9db6158a 50 CPPUNIT_TEST( Path );
bd2fe27e 51 CPPUNIT_TEST( AddEntries );
9db6158a
VZ
52 CPPUNIT_TEST( GetEntries );
53 CPPUNIT_TEST( GetGroups );
54 CPPUNIT_TEST( HasEntry );
17c24d37 55 CPPUNIT_TEST( HasGroup );
94139118
VZ
56 CPPUNIT_TEST( Save );
57 CPPUNIT_TEST( DeleteEntry );
58 CPPUNIT_TEST( DeleteGroup );
59 CPPUNIT_TEST( DeleteAll );
60 CPPUNIT_TEST( RenameEntry );
61 CPPUNIT_TEST( RenameGroup );
cbc9c06f 62 CPPUNIT_TEST( DeleteLastGroup );
17c24d37
VZ
63 CPPUNIT_TEST_SUITE_END();
64
9db6158a 65 void Path();
bd2fe27e 66 void AddEntries();
9db6158a
VZ
67 void GetEntries();
68 void GetGroups();
69 void HasEntry();
17c24d37 70 void HasGroup();
94139118
VZ
71 void Save();
72 void DeleteEntry();
73 void DeleteGroup();
74 void DeleteAll();
75 void RenameEntry();
76 void RenameGroup();
cbc9c06f 77 void DeleteLastGroup();
17c24d37 78
9db6158a
VZ
79 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
80 {
81 fc.SetPath(path);
82
83 return fc.GetPath();
84 }
85
94139118
VZ
86 static wxString Dump(wxFileConfig& fc)
87 {
8899b155
RN
88 wxStringOutputStream sos;
89 fc.Save(sos);
90 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
94139118
VZ
91 }
92
9db6158a
VZ
93 void CheckGroupEntries(const wxFileConfig& fc,
94 const wxChar *path,
95 size_t nEntries,
96 ...);
97 void CheckGroupSubgroups(const wxFileConfig& fc,
98 const wxChar *path,
99 size_t nGroups,
100 ...);
101
17c24d37
VZ
102 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
103};
104
105// register in the unnamed registry so that these tests are run by default
106CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
107
108// also include in it's own registry so that these tests can be run alone
109CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
110
9db6158a
VZ
111void FileConfigTestCase::Path()
112{
113 wxStringInputStream sis(testconfig);
114 wxFileConfig fc(sis);
115
116 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
117 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
118 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
119 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
120 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
121 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
122}
123
bd2fe27e
VZ
124void FileConfigTestCase::AddEntries()
125{
126 wxFileConfig fc;
127
128 CPPUNIT_ASSERT( Dump(fc) == _T("") );
129
130 fc.Write(_T("/Foo"), _T("foo"));
131 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n") );
132
133 fc.Write(_T("/Bar/Baz"), _T("baz"));
134 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
135
136 fc.DeleteAll();
137 CPPUNIT_ASSERT( Dump(fc) == _T("") );
138
139 fc.Write(_T("/Bar/Baz"), _T("baz"));
140 CPPUNIT_ASSERT( Dump(fc) == _T("[Bar]\nBaz=baz\n") );
141
142 fc.Write(_T("/Foo"), _T("foo"));
143 CPPUNIT_ASSERT( Dump(fc) == _T("Foo=foo\n[Bar]\nBaz=baz\n") );
144}
145
9db6158a
VZ
146void
147FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
148 const wxChar *path,
149 size_t nEntries,
150 ...)
151{
152 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
153
154 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
155
156 va_list ap;
157 va_start(ap, nEntries);
158
159 long cookie;
160 wxString name;
161 for ( bool cont = fc.GetFirstEntry(name, cookie);
162 cont;
163 cont = fc.GetNextEntry(name, cookie), nEntries-- )
164 {
165 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
166 }
167
168 CPPUNIT_ASSERT( nEntries == 0 );
169
170 va_end(ap);
171}
172
173void
174FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
175 const wxChar *path,
176 size_t nGroups,
177 ...)
178{
179 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
180
181 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
182
183 va_list ap;
184 va_start(ap, nGroups);
185
186 long cookie;
187 wxString name;
188 for ( bool cont = fc.GetFirstGroup(name, cookie);
189 cont;
190 cont = fc.GetNextGroup(name, cookie), nGroups-- )
191 {
192 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
193 }
194
195 CPPUNIT_ASSERT( nGroups == 0 );
196
197 va_end(ap);
198}
199
200void FileConfigTestCase::GetEntries()
201{
202 wxStringInputStream sis(testconfig);
203 wxFileConfig fc(sis);
204
205 CheckGroupEntries(fc, _T(""), 0);
206 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
207 CheckGroupEntries(fc, _T("/root/group1"), 0);
208 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
209 2, _T("subentry"), _T("subentry2"));
210}
211
212void FileConfigTestCase::GetGroups()
213{
214 wxStringInputStream sis(testconfig);
215 wxFileConfig fc(sis);
216
217 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
218 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
219 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
220 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
221}
222
223void FileConfigTestCase::HasEntry()
224{
225 wxStringInputStream sis(testconfig);
226 wxFileConfig fc(sis);
227
228 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
229 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
230 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
231 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
232 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
233 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
234 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
235}
236
17c24d37
VZ
237void FileConfigTestCase::HasGroup()
238{
239 wxStringInputStream sis(testconfig);
240 wxFileConfig fc(sis);
241
242 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
243 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
244 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
245 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
246 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
247 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
248 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
249 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
250}
251
94139118
VZ
252void FileConfigTestCase::Save()
253{
254 wxStringInputStream sis(testconfig);
255 wxFileConfig fc(sis);
256 CPPUNIT_ASSERT( Dump(fc) == testconfig );
257}
258
259void FileConfigTestCase::DeleteEntry()
260{
261 wxStringInputStream sis(testconfig);
262 wxFileConfig fc(sis);
263
264 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
265
266 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
267 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
268 _T("entry=value\n")
269 _T("[root/group1]\n")
270 _T("[root/group1/subgroup]\n")
271 _T("subentry2=subvalue2\n")
272 _T("[root/group2]\n") );
273
274 // group should be deleted now as well as it became empty
275 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
276 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
277 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
278 _T("entry=value\n")
279 _T("[root/group1]\n")
280 _T("[root/group2]\n") );
281}
282
283void FileConfigTestCase::DeleteGroup()
284{
285 wxStringInputStream sis(testconfig);
286 wxFileConfig fc(sis);
287
288 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
289
290 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
291 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
292 _T("entry=value\n")
293 _T("[root/group2]\n") );
294
295 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2")) );
296 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
297 _T("entry=value\n") );
298
299 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
300 CPPUNIT_ASSERT( Dump(fc).empty() );
301}
302
303void FileConfigTestCase::DeleteAll()
304{
305 wxStringInputStream sis(testconfig);
306 wxFileConfig fc(sis);
307
308 CPPUNIT_ASSERT( fc.DeleteAll() );
309 CPPUNIT_ASSERT( Dump(fc).empty() );
310}
311
312void FileConfigTestCase::RenameEntry()
313{
314 wxStringInputStream sis(testconfig);
315 wxFileConfig fc(sis);
316
317 fc.SetPath(_T("root"));
318 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
319 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
320 _T("newname=value\n")
321 _T("[root/group1]\n")
322 _T("[root/group1/subgroup]\n")
323 _T("subentry=subvalue\n")
324 _T("subentry2=subvalue2\n")
325 _T("[root/group2]\n") );
326
327 fc.SetPath(_T("group1/subgroup"));
328 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
329 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
330
331 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
332 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
333 _T("newname=value\n")
334 _T("[root/group1]\n")
335 _T("[root/group1/subgroup]\n")
336 _T("subentry2=subvalue2\n")
337 _T("subentry1=subvalue\n")
338 _T("[root/group2]\n") );
339}
340
341void FileConfigTestCase::RenameGroup()
342{
343 wxStringInputStream sis(testconfig);
344 wxFileConfig fc(sis);
345
346 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
347 CPPUNIT_ASSERT( Dump(fc) == _T("[foot]\n")
348 _T("entry=value\n")
349 _T("[foot/group1]\n")
350 _T("[foot/group1/subgroup]\n")
351 _T("subentry=subvalue\n")
352 _T("subentry2=subvalue2\n")
353 _T("[foot/group2]\n") );
354}
355
cbc9c06f
DS
356
357static void EmptyConfigAndWriteKey()
358{
359 wxFileConfig fc(_T("deleteconftest"));
360
361 const wxString groupPath = _T("/root");
362
363 if (fc.Exists(groupPath))
364 {
365 // using DeleteGroup exposes the problem, using DeleteAll doesn't
366 CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
367 }
368
369 // the config must be empty for the problem to arise
370 CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
371 CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
372
373
374 // this crashes on second call of this function
375 CPPUNIT_ASSERT( fc.Write(groupPath + _T("/entry"), _T("value")) );
376}
377
378void FileConfigTestCase::DeleteLastGroup()
379{
380 /*
381 We make 2 of the same calls, first to create a file config with a single
382 group and key...
383 */
384 ::EmptyConfigAndWriteKey();
385
386 /*
387 ... then the same but this time the key's group is deleted before the
388 key is written again. This causes a crash.
389 */
390 ::EmptyConfigAndWriteKey();
391
392
393 // clean up
394 wxLogNull noLogging;
395 (void) ::wxRemoveFile(
396 wxFileConfig::GetLocalFileName(_T("deleteconftest")) );
397}
398
17c24d37
VZ
399#endif // wxUSE_FILECONFIG
400