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