]> git.saurik.com Git - wxWidgets.git/blame - tests/fileconf/fileconftest.cpp
removed a couple of useless #if wxUSE_UNICODE tests
[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
e1a2c7a5
VZ
39// ----------------------------------------------------------------------------
40// local functions
41// ----------------------------------------------------------------------------
42
43static wxString Dump(wxFileConfig& fc)
44{
45 wxStringOutputStream sos;
46 fc.Save(sos);
47 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
48}
49
50// helper macro to test wxFileConfig contents
51#define wxVERIFY_FILECONFIG(t, fc) CPPUNIT_ASSERT_EQUAL(wxString(t), Dump(fc))
52
17c24d37
VZ
53// ----------------------------------------------------------------------------
54// test class
55// ----------------------------------------------------------------------------
56
57class FileConfigTestCase : public CppUnit::TestCase
58{
59public:
8899b155 60 FileConfigTestCase() { }
17c24d37
VZ
61
62private:
63 CPPUNIT_TEST_SUITE( FileConfigTestCase );
9db6158a 64 CPPUNIT_TEST( Path );
bd2fe27e 65 CPPUNIT_TEST( AddEntries );
9db6158a
VZ
66 CPPUNIT_TEST( GetEntries );
67 CPPUNIT_TEST( GetGroups );
68 CPPUNIT_TEST( HasEntry );
17c24d37 69 CPPUNIT_TEST( HasGroup );
94139118
VZ
70 CPPUNIT_TEST( Save );
71 CPPUNIT_TEST( DeleteEntry );
72 CPPUNIT_TEST( DeleteGroup );
73 CPPUNIT_TEST( DeleteAll );
74 CPPUNIT_TEST( RenameEntry );
75 CPPUNIT_TEST( RenameGroup );
c406cd3d
VZ
76 CPPUNIT_TEST( CreateEntriesAndSubgroup );
77 CPPUNIT_TEST( CreateSubgroupAndEntries );
cbc9c06f 78 CPPUNIT_TEST( DeleteLastGroup );
17c24d37
VZ
79 CPPUNIT_TEST_SUITE_END();
80
9db6158a 81 void Path();
bd2fe27e 82 void AddEntries();
9db6158a
VZ
83 void GetEntries();
84 void GetGroups();
85 void HasEntry();
17c24d37 86 void HasGroup();
94139118
VZ
87 void Save();
88 void DeleteEntry();
89 void DeleteGroup();
90 void DeleteAll();
91 void RenameEntry();
92 void RenameGroup();
c406cd3d
VZ
93 void CreateEntriesAndSubgroup();
94 void CreateSubgroupAndEntries();
cbc9c06f 95 void DeleteLastGroup();
17c24d37 96
9db6158a
VZ
97 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
98 {
99 fc.SetPath(path);
100
101 return fc.GetPath();
102 }
103
104 void CheckGroupEntries(const wxFileConfig& fc,
105 const wxChar *path,
106 size_t nEntries,
107 ...);
108 void CheckGroupSubgroups(const wxFileConfig& fc,
109 const wxChar *path,
110 size_t nGroups,
111 ...);
112
17c24d37
VZ
113 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
114};
115
116// register in the unnamed registry so that these tests are run by default
117CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
118
119// also include in it's own registry so that these tests can be run alone
120CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
121
9db6158a
VZ
122void FileConfigTestCase::Path()
123{
124 wxStringInputStream sis(testconfig);
125 wxFileConfig fc(sis);
126
127 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
128 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
129 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
130 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
131 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
132 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
133}
134
bd2fe27e
VZ
135void FileConfigTestCase::AddEntries()
136{
137 wxFileConfig fc;
138
e1a2c7a5 139 wxVERIFY_FILECONFIG( _T(""), fc );
bd2fe27e
VZ
140
141 fc.Write(_T("/Foo"), _T("foo"));
e1a2c7a5 142 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc );
bd2fe27e
VZ
143
144 fc.Write(_T("/Bar/Baz"), _T("baz"));
e1a2c7a5 145 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
bd2fe27e
VZ
146
147 fc.DeleteAll();
e1a2c7a5 148 wxVERIFY_FILECONFIG( _T(""), fc );
bd2fe27e
VZ
149
150 fc.Write(_T("/Bar/Baz"), _T("baz"));
e1a2c7a5 151 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc );
bd2fe27e
VZ
152
153 fc.Write(_T("/Foo"), _T("foo"));
e1a2c7a5 154 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
bd2fe27e
VZ
155}
156
9db6158a
VZ
157void
158FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
159 const wxChar *path,
160 size_t nEntries,
161 ...)
162{
163 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
164
165 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
166
167 va_list ap;
168 va_start(ap, nEntries);
169
170 long cookie;
171 wxString name;
172 for ( bool cont = fc.GetFirstEntry(name, cookie);
173 cont;
174 cont = fc.GetNextEntry(name, cookie), nEntries-- )
175 {
176 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
177 }
178
179 CPPUNIT_ASSERT( nEntries == 0 );
180
181 va_end(ap);
182}
183
184void
185FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
186 const wxChar *path,
187 size_t nGroups,
188 ...)
189{
190 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
191
192 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
193
194 va_list ap;
195 va_start(ap, nGroups);
196
197 long cookie;
198 wxString name;
199 for ( bool cont = fc.GetFirstGroup(name, cookie);
200 cont;
201 cont = fc.GetNextGroup(name, cookie), nGroups-- )
202 {
203 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
204 }
205
206 CPPUNIT_ASSERT( nGroups == 0 );
207
208 va_end(ap);
209}
210
211void FileConfigTestCase::GetEntries()
212{
213 wxStringInputStream sis(testconfig);
214 wxFileConfig fc(sis);
215
216 CheckGroupEntries(fc, _T(""), 0);
217 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
218 CheckGroupEntries(fc, _T("/root/group1"), 0);
219 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
220 2, _T("subentry"), _T("subentry2"));
221}
222
223void FileConfigTestCase::GetGroups()
224{
225 wxStringInputStream sis(testconfig);
226 wxFileConfig fc(sis);
227
228 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
229 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
230 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
231 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
232}
233
234void FileConfigTestCase::HasEntry()
235{
236 wxStringInputStream sis(testconfig);
237 wxFileConfig fc(sis);
238
239 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
240 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
241 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
242 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
243 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
244 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
245 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
b0db68b2
VZ
246 CPPUNIT_ASSERT( !fc.HasEntry(_T("/root/no_such_group/entry")) );
247 CPPUNIT_ASSERT( !fc.HasGroup(_T("/root/no_such_group")) );
9db6158a
VZ
248}
249
17c24d37
VZ
250void FileConfigTestCase::HasGroup()
251{
252 wxStringInputStream sis(testconfig);
253 wxFileConfig fc(sis);
254
255 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
256 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
257 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
258 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
17c24d37
VZ
259 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
260 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
261 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
993896f1
VZ
262 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot/subgroup")) );
263 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
17c24d37
VZ
264}
265
94139118
VZ
266void FileConfigTestCase::Save()
267{
268 wxStringInputStream sis(testconfig);
269 wxFileConfig fc(sis);
e1a2c7a5 270 wxVERIFY_FILECONFIG( testconfig, fc );
94139118
VZ
271}
272
273void FileConfigTestCase::DeleteEntry()
274{
275 wxStringInputStream sis(testconfig);
276 wxFileConfig fc(sis);
277
278 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
279
280 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
e1a2c7a5
VZ
281 wxVERIFY_FILECONFIG( _T("[root]\n")
282 _T("entry=value\n")
283 _T("[root/group1]\n")
284 _T("[root/group1/subgroup]\n")
285 _T("subentry2=subvalue2\n")
286 _T("[root/group2]\n"),
287 fc );
94139118
VZ
288
289 // group should be deleted now as well as it became empty
290 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
291 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
e1a2c7a5
VZ
292 wxVERIFY_FILECONFIG( _T("[root]\n")
293 _T("entry=value\n")
294 _T("[root/group1]\n")
295 _T("[root/group2]\n"),
296 fc );
94139118
VZ
297}
298
299void 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")) );
e1a2c7a5
VZ
307 wxVERIFY_FILECONFIG( _T("[root]\n")
308 _T("entry=value\n")
309 _T("[root/group2]\n"),
310 fc );
94139118
VZ
311
312 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2")) );
e1a2c7a5
VZ
313 wxVERIFY_FILECONFIG( _T("[root]\n")
314 _T("entry=value\n"),
315 fc );
94139118
VZ
316
317 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
318 CPPUNIT_ASSERT( Dump(fc).empty() );
319}
320
321void FileConfigTestCase::DeleteAll()
322{
323 wxStringInputStream sis(testconfig);
324 wxFileConfig fc(sis);
325
326 CPPUNIT_ASSERT( fc.DeleteAll() );
327 CPPUNIT_ASSERT( Dump(fc).empty() );
328}
329
330void FileConfigTestCase::RenameEntry()
331{
332 wxStringInputStream sis(testconfig);
333 wxFileConfig fc(sis);
334
335 fc.SetPath(_T("root"));
336 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
e1a2c7a5
VZ
337 wxVERIFY_FILECONFIG( _T("[root]\n")
338 _T("newname=value\n")
339 _T("[root/group1]\n")
340 _T("[root/group1/subgroup]\n")
341 _T("subentry=subvalue\n")
342 _T("subentry2=subvalue2\n")
343 _T("[root/group2]\n"),
344 fc );
94139118
VZ
345
346 fc.SetPath(_T("group1/subgroup"));
347 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
348 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
349
350 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
e1a2c7a5
VZ
351 wxVERIFY_FILECONFIG( _T("[root]\n")
352 _T("newname=value\n")
353 _T("[root/group1]\n")
354 _T("[root/group1/subgroup]\n")
355 _T("subentry2=subvalue2\n")
356 _T("subentry1=subvalue\n")
357 _T("[root/group2]\n"),
358 fc );
94139118
VZ
359}
360
361void FileConfigTestCase::RenameGroup()
362{
363 wxStringInputStream sis(testconfig);
364 wxFileConfig fc(sis);
365
366 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
e1a2c7a5
VZ
367 wxVERIFY_FILECONFIG( _T("[foot]\n")
368 _T("entry=value\n")
369 _T("[foot/group1]\n")
370 _T("[foot/group1/subgroup]\n")
371 _T("subentry=subvalue\n")
372 _T("subentry2=subvalue2\n")
373 _T("[foot/group2]\n"),
374 fc );
3ca10461 375
126bd656
VZ
376 // renaming a path doesn't work, it must be the immediate group
377 CPPUNIT_ASSERT( !fc.RenameGroup(_T("foot/group1"), _T("group2")) );
378
379
3ca10461
VZ
380 fc.SetPath(_T("foot"));
381
126bd656
VZ
382 // renaming to a name of existing group doesn't work
383 CPPUNIT_ASSERT( !fc.RenameGroup(_T("group1"), _T("group2")) );
384
385 // try exchanging the groups names and then restore them back
3ca10461
VZ
386 CPPUNIT_ASSERT( fc.RenameGroup(_T("group1"), _T("groupTmp")) );
387 wxVERIFY_FILECONFIG( _T("[foot]\n")
388 _T("entry=value\n")
389 _T("[foot/groupTmp]\n")
390 _T("[foot/groupTmp/subgroup]\n")
391 _T("subentry=subvalue\n")
392 _T("subentry2=subvalue2\n")
393 _T("[foot/group2]\n"),
394 fc );
395
396 CPPUNIT_ASSERT( fc.RenameGroup(_T("group2"), _T("group1")) );
397 wxVERIFY_FILECONFIG( _T("[foot]\n")
398 _T("entry=value\n")
399 _T("[foot/groupTmp]\n")
400 _T("[foot/groupTmp/subgroup]\n")
401 _T("subentry=subvalue\n")
402 _T("subentry2=subvalue2\n")
403 _T("[foot/group1]\n"),
404 fc );
405
406 CPPUNIT_ASSERT( fc.RenameGroup(_T("groupTmp"), _T("group2")) );
407 wxVERIFY_FILECONFIG( _T("[foot]\n")
408 _T("entry=value\n")
409 _T("[foot/group2]\n")
410 _T("[foot/group2/subgroup]\n")
411 _T("subentry=subvalue\n")
412 _T("subentry2=subvalue2\n")
413 _T("[foot/group1]\n"),
414 fc );
126bd656
VZ
415
416 CPPUNIT_ASSERT( fc.RenameGroup(_T("group1"), _T("groupTmp")) );
417 wxVERIFY_FILECONFIG( _T("[foot]\n")
418 _T("entry=value\n")
419 _T("[foot/group2]\n")
420 _T("[foot/group2/subgroup]\n")
421 _T("subentry=subvalue\n")
422 _T("subentry2=subvalue2\n")
423 _T("[foot/groupTmp]\n"),
424 fc );
425
426 CPPUNIT_ASSERT( fc.RenameGroup(_T("group2"), _T("group1")) );
427 wxVERIFY_FILECONFIG( _T("[foot]\n")
428 _T("entry=value\n")
429 _T("[foot/group1]\n")
430 _T("[foot/group1/subgroup]\n")
431 _T("subentry=subvalue\n")
432 _T("subentry2=subvalue2\n")
433 _T("[foot/groupTmp]\n"),
434 fc );
435
436 CPPUNIT_ASSERT( fc.RenameGroup(_T("groupTmp"), _T("group2")) );
437 wxVERIFY_FILECONFIG( _T("[foot]\n")
438 _T("entry=value\n")
439 _T("[foot/group1]\n")
440 _T("[foot/group1/subgroup]\n")
441 _T("subentry=subvalue\n")
442 _T("subentry2=subvalue2\n")
443 _T("[foot/group2]\n"),
444 fc );
94139118
VZ
445}
446
c406cd3d
VZ
447void FileConfigTestCase::CreateSubgroupAndEntries()
448{
449 wxFileConfig fc;
450 fc.Write(_T("sub/sub_first"), _T("sub_one"));
451 fc.Write(_T("first"), _T("one"));
452
e1a2c7a5
VZ
453 wxVERIFY_FILECONFIG( _T("first=one\n")
454 _T("[sub]\n")
455 _T("sub_first=sub_one\n"),
456 fc );
c406cd3d
VZ
457}
458
459void FileConfigTestCase::CreateEntriesAndSubgroup()
460{
461 wxFileConfig fc;
462 fc.Write(_T("first"), _T("one"));
463 fc.Write(_T("second"), _T("two"));
464 fc.Write(_T("sub/sub_first"), _T("sub_one"));
465
e1a2c7a5
VZ
466 wxVERIFY_FILECONFIG( _T("first=one\n")
467 _T("second=two\n")
468 _T("[sub]\n")
469 _T("sub_first=sub_one\n"),
470 fc );
c406cd3d 471}
cbc9c06f
DS
472
473static void EmptyConfigAndWriteKey()
474{
475 wxFileConfig fc(_T("deleteconftest"));
476
477 const wxString groupPath = _T("/root");
478
c406cd3d 479 if ( fc.Exists(groupPath) )
cbc9c06f
DS
480 {
481 // using DeleteGroup exposes the problem, using DeleteAll doesn't
482 CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
483 }
484
485 // the config must be empty for the problem to arise
486 CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
487 CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
488
489
490 // this crashes on second call of this function
491 CPPUNIT_ASSERT( fc.Write(groupPath + _T("/entry"), _T("value")) );
492}
493
494void FileConfigTestCase::DeleteLastGroup()
495{
496 /*
497 We make 2 of the same calls, first to create a file config with a single
498 group and key...
499 */
500 ::EmptyConfigAndWriteKey();
501
502 /*
503 ... then the same but this time the key's group is deleted before the
504 key is written again. This causes a crash.
505 */
506 ::EmptyConfigAndWriteKey();
507
508
509 // clean up
510 wxLogNull noLogging;
c406cd3d 511 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
cbc9c06f
DS
512}
513
17c24d37
VZ
514#endif // wxUSE_FILECONFIG
515