]> git.saurik.com Git - wxWidgets.git/blame - tests/config/fileconf.cpp
avoid deprecated functions and direct struct access
[wxWidgets.git] / tests / config / fileconf.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 =
9a83f860
VZ
30wxT("[root]\n")
31wxT("entry=value\n")
32wxT("[root/group1]\n")
33wxT("[root/group1/subgroup]\n")
34wxT("subentry=subvalue\n")
35wxT("subentry2=subvalue2\n")
36wxT("[root/group2]\n")
17c24d37
VZ
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 );
5814e8ba 70 CPPUNIT_TEST( Binary );
94139118
VZ
71 CPPUNIT_TEST( Save );
72 CPPUNIT_TEST( DeleteEntry );
e86882e3 73 CPPUNIT_TEST( DeleteAndWriteEntry );
94139118
VZ
74 CPPUNIT_TEST( DeleteGroup );
75 CPPUNIT_TEST( DeleteAll );
76 CPPUNIT_TEST( RenameEntry );
77 CPPUNIT_TEST( RenameGroup );
c406cd3d
VZ
78 CPPUNIT_TEST( CreateEntriesAndSubgroup );
79 CPPUNIT_TEST( CreateSubgroupAndEntries );
cbc9c06f 80 CPPUNIT_TEST( DeleteLastGroup );
6ad0a7d5 81 CPPUNIT_TEST( DeleteAndRecreateGroup );
9a7b7798 82 CPPUNIT_TEST( AddToExistingRoot );
6221357b 83 CPPUNIT_TEST( ReadNonExistent );
975fb32b 84 CPPUNIT_TEST( ReadEmpty );
384859f8 85 CPPUNIT_TEST( ReadFloat );
17c24d37
VZ
86 CPPUNIT_TEST_SUITE_END();
87
9db6158a 88 void Path();
bd2fe27e 89 void AddEntries();
9db6158a
VZ
90 void GetEntries();
91 void GetGroups();
92 void HasEntry();
17c24d37 93 void HasGroup();
5814e8ba 94 void Binary();
94139118
VZ
95 void Save();
96 void DeleteEntry();
e86882e3 97 void DeleteAndWriteEntry();
94139118
VZ
98 void DeleteGroup();
99 void DeleteAll();
100 void RenameEntry();
101 void RenameGroup();
c406cd3d
VZ
102 void CreateEntriesAndSubgroup();
103 void CreateSubgroupAndEntries();
cbc9c06f 104 void DeleteLastGroup();
6ad0a7d5 105 void DeleteAndRecreateGroup();
9a7b7798 106 void AddToExistingRoot();
6221357b 107 void ReadNonExistent();
975fb32b 108 void ReadEmpty();
384859f8 109 void ReadFloat();
9a7b7798 110
17c24d37 111
9db6158a
VZ
112 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
113 {
114 fc.SetPath(path);
115
116 return fc.GetPath();
117 }
118
119 void CheckGroupEntries(const wxFileConfig& fc,
120 const wxChar *path,
121 size_t nEntries,
122 ...);
123 void CheckGroupSubgroups(const wxFileConfig& fc,
124 const wxChar *path,
125 size_t nGroups,
126 ...);
127
17c24d37
VZ
128 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
129};
130
131// register in the unnamed registry so that these tests are run by default
132CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
133
134// also include in it's own registry so that these tests can be run alone
135CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
136
9db6158a
VZ
137void FileConfigTestCase::Path()
138{
139 wxStringInputStream sis(testconfig);
140 wxFileConfig fc(sis);
141
9a83f860
VZ
142 CPPUNIT_ASSERT( ChangePath(fc, wxT("")) == wxT("") );
143 CPPUNIT_ASSERT( ChangePath(fc, wxT("/")) == wxT("") );
144 CPPUNIT_ASSERT( ChangePath(fc, wxT("root")) == wxT("/root") );
145 CPPUNIT_ASSERT( ChangePath(fc, wxT("/root")) == wxT("/root") );
146 CPPUNIT_ASSERT( ChangePath(fc, wxT("/root/group1/subgroup")) == wxT("/root/group1/subgroup") );
147 CPPUNIT_ASSERT( ChangePath(fc, wxT("/root/group2")) == wxT("/root/group2") );
9db6158a
VZ
148}
149
bd2fe27e
VZ
150void FileConfigTestCase::AddEntries()
151{
152 wxFileConfig fc;
153
9a83f860 154 wxVERIFY_FILECONFIG( wxT(""), fc );
bd2fe27e 155
9a83f860
VZ
156 fc.Write(wxT("/Foo"), wxT("foo"));
157 wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc );
bd2fe27e 158
9a83f860
VZ
159 fc.Write(wxT("/Bar/Baz"), wxT("baz"));
160 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
bd2fe27e
VZ
161
162 fc.DeleteAll();
9a83f860 163 wxVERIFY_FILECONFIG( wxT(""), fc );
bd2fe27e 164
9a83f860
VZ
165 fc.Write(wxT("/Bar/Baz"), wxT("baz"));
166 wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc );
bd2fe27e 167
9a83f860
VZ
168 fc.Write(wxT("/Foo"), wxT("foo"));
169 wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
bd2fe27e
VZ
170}
171
9db6158a
VZ
172void
173FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
174 const wxChar *path,
175 size_t nEntries,
176 ...)
177{
9a83f860 178 wxConfigPathChanger change(&fc, wxString(path) + wxT("/"));
9db6158a
VZ
179
180 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
181
182 va_list ap;
183 va_start(ap, nEntries);
184
185 long cookie;
186 wxString name;
187 for ( bool cont = fc.GetFirstEntry(name, cookie);
188 cont;
189 cont = fc.GetNextEntry(name, cookie), nEntries-- )
190 {
191 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
192 }
193
194 CPPUNIT_ASSERT( nEntries == 0 );
195
196 va_end(ap);
197}
198
199void
200FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
201 const wxChar *path,
202 size_t nGroups,
203 ...)
204{
9a83f860 205 wxConfigPathChanger change(&fc, wxString(path) + wxT("/"));
9db6158a
VZ
206
207 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
208
209 va_list ap;
210 va_start(ap, nGroups);
211
212 long cookie;
213 wxString name;
214 for ( bool cont = fc.GetFirstGroup(name, cookie);
215 cont;
216 cont = fc.GetNextGroup(name, cookie), nGroups-- )
217 {
218 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
219 }
220
221 CPPUNIT_ASSERT( nGroups == 0 );
222
223 va_end(ap);
224}
225
226void FileConfigTestCase::GetEntries()
227{
228 wxStringInputStream sis(testconfig);
229 wxFileConfig fc(sis);
230
9a83f860
VZ
231 CheckGroupEntries(fc, wxT(""), 0);
232 CheckGroupEntries(fc, wxT("/root"), 1, wxT("entry"));
233 CheckGroupEntries(fc, wxT("/root/group1"), 0);
234 CheckGroupEntries(fc, wxT("/root/group1/subgroup"),
235 2, wxT("subentry"), wxT("subentry2"));
9db6158a
VZ
236}
237
238void FileConfigTestCase::GetGroups()
239{
240 wxStringInputStream sis(testconfig);
241 wxFileConfig fc(sis);
242
9a83f860
VZ
243 CheckGroupSubgroups(fc, wxT(""), 1, wxT("root"));
244 CheckGroupSubgroups(fc, wxT("/root"), 2, wxT("group1"), wxT("group2"));
245 CheckGroupSubgroups(fc, wxT("/root/group1"), 1, wxT("subgroup"));
246 CheckGroupSubgroups(fc, wxT("/root/group2"), 0);
9db6158a
VZ
247}
248
249void FileConfigTestCase::HasEntry()
250{
251 wxStringInputStream sis(testconfig);
252 wxFileConfig fc(sis);
253
9a83f860
VZ
254 CPPUNIT_ASSERT( !fc.HasEntry(wxT("root")) );
255 CPPUNIT_ASSERT( fc.HasEntry(wxT("root/entry")) );
256 CPPUNIT_ASSERT( fc.HasEntry(wxT("/root/entry")) );
257 CPPUNIT_ASSERT( fc.HasEntry(wxT("root/group1/subgroup/subentry")) );
258 CPPUNIT_ASSERT( !fc.HasEntry(wxT("")) );
259 CPPUNIT_ASSERT( !fc.HasEntry(wxT("root/group1")) );
260 CPPUNIT_ASSERT( !fc.HasEntry(wxT("subgroup/subentry")) );
261 CPPUNIT_ASSERT( !fc.HasEntry(wxT("/root/no_such_group/entry")) );
262 CPPUNIT_ASSERT( !fc.HasGroup(wxT("/root/no_such_group")) );
9db6158a
VZ
263}
264
17c24d37
VZ
265void FileConfigTestCase::HasGroup()
266{
267 wxStringInputStream sis(testconfig);
268 wxFileConfig fc(sis);
269
9a83f860
VZ
270 CPPUNIT_ASSERT( fc.HasGroup(wxT("root")) );
271 CPPUNIT_ASSERT( fc.HasGroup(wxT("root/group1")) );
272 CPPUNIT_ASSERT( fc.HasGroup(wxT("root/group1/subgroup")) );
273 CPPUNIT_ASSERT( fc.HasGroup(wxT("root/group2")) );
274 CPPUNIT_ASSERT( !fc.HasGroup(wxT("")) );
275 CPPUNIT_ASSERT( !fc.HasGroup(wxT("root/group")) );
276 CPPUNIT_ASSERT( !fc.HasGroup(wxT("root//subgroup")) );
277 CPPUNIT_ASSERT( !fc.HasGroup(wxT("foot/subgroup")) );
278 CPPUNIT_ASSERT( !fc.HasGroup(wxT("foot")) );
17c24d37
VZ
279}
280
5814e8ba
VZ
281void FileConfigTestCase::Binary()
282{
283 wxStringInputStream sis(
284 "[root]\n"
285 "binary=Zm9vCg==\n"
286 );
287 wxFileConfig fc(sis);
288
289 wxMemoryBuffer buf;
290 fc.Read("/root/binary", &buf);
291
292 CPPUNIT_ASSERT( memcmp("foo\n", buf.GetData(), buf.GetDataLen()) == 0 );
293
294 buf.SetDataLen(0);
295 buf.AppendData("\0\1\2", 3);
296 fc.Write("/root/012", buf);
297 wxVERIFY_FILECONFIG(
298 "[root]\n"
299 "binary=Zm9vCg==\n"
300 "012=AAEC\n",
301 fc
302 );
303}
304
94139118
VZ
305void FileConfigTestCase::Save()
306{
307 wxStringInputStream sis(testconfig);
308 wxFileConfig fc(sis);
e1a2c7a5 309 wxVERIFY_FILECONFIG( testconfig, fc );
94139118
VZ
310}
311
312void FileConfigTestCase::DeleteEntry()
313{
314 wxStringInputStream sis(testconfig);
315 wxFileConfig fc(sis);
316
9a83f860 317 CPPUNIT_ASSERT( !fc.DeleteEntry(wxT("foo")) );
94139118 318
9a83f860
VZ
319 CPPUNIT_ASSERT( fc.DeleteEntry(wxT("root/group1/subgroup/subentry")) );
320 wxVERIFY_FILECONFIG( wxT("[root]\n")
321 wxT("entry=value\n")
322 wxT("[root/group1]\n")
323 wxT("[root/group1/subgroup]\n")
324 wxT("subentry2=subvalue2\n")
325 wxT("[root/group2]\n"),
e1a2c7a5 326 fc );
94139118
VZ
327
328 // group should be deleted now as well as it became empty
9a83f860
VZ
329 wxConfigPathChanger change(&fc, wxT("root/group1/subgroup/subentry2"));
330 CPPUNIT_ASSERT( fc.DeleteEntry(wxT("subentry2")) );
331 wxVERIFY_FILECONFIG( wxT("[root]\n")
332 wxT("entry=value\n")
333 wxT("[root/group1]\n")
334 wxT("[root/group2]\n"),
e1a2c7a5 335 fc );
94139118
VZ
336}
337
e86882e3
VZ
338void FileConfigTestCase::DeleteAndWriteEntry()
339{
340 wxStringInputStream sis(
341 "[root/group1]\n"
342 "subentry=subvalue\n"
343 "subentry2=subvalue2\n"
344 "subentry3=subvalue3\n"
345 );
346
347 wxFileConfig fc(sis);
348
349 fc.DeleteEntry("/root/group1/subentry2");
350 fc.Write("/root/group1/subentry2", "testvalue");
351 fc.DeleteEntry("/root/group2/subentry2");
352 fc.Write("/root/group2/subentry2", "testvalue2");
353 fc.DeleteEntry("/root/group1/subentry2");
354 fc.Write("/root/group1/subentry2", "testvalue");
355 fc.DeleteEntry("/root/group2/subentry2");
356 fc.Write("/root/group2/subentry2", "testvalue2");
357
358 wxVERIFY_FILECONFIG( "[root/group1]\n"
359 "subentry=subvalue\n"
360 "subentry3=subvalue3\n"
361 "subentry2=testvalue\n"
362 "[root/group2]\n"
363 "subentry2=testvalue2\n",
364 fc );
365
366 fc.DeleteEntry("/root/group2/subentry2");
367 wxVERIFY_FILECONFIG( "[root/group1]\n"
368 "subentry=subvalue\n"
369 "subentry3=subvalue3\n"
370 "subentry2=testvalue\n",
371 fc );
372
373 fc.DeleteEntry("/root/group1/subentry2");
374 fc.DeleteEntry("/root/group1/subentry");
375 fc.DeleteEntry("/root/group1/subentry3");
376 wxVERIFY_FILECONFIG( "", fc );
377}
378
94139118
VZ
379void FileConfigTestCase::DeleteGroup()
380{
381 wxStringInputStream sis(testconfig);
382 wxFileConfig fc(sis);
383
9a83f860 384 CPPUNIT_ASSERT( !fc.DeleteGroup(wxT("foo")) );
94139118 385
9a83f860
VZ
386 CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group1")) );
387 wxVERIFY_FILECONFIG( wxT("[root]\n")
388 wxT("entry=value\n")
389 wxT("[root/group2]\n"),
e1a2c7a5 390 fc );
94139118 391
35c4b4da 392 // notice trailing slash: it should be ignored
9a83f860
VZ
393 CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group2/")) );
394 wxVERIFY_FILECONFIG( wxT("[root]\n")
395 wxT("entry=value\n"),
e1a2c7a5 396 fc );
94139118 397
9a83f860 398 CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root")) );
94139118
VZ
399 CPPUNIT_ASSERT( Dump(fc).empty() );
400}
401
402void FileConfigTestCase::DeleteAll()
403{
404 wxStringInputStream sis(testconfig);
405 wxFileConfig fc(sis);
406
407 CPPUNIT_ASSERT( fc.DeleteAll() );
408 CPPUNIT_ASSERT( Dump(fc).empty() );
409}
410
411void FileConfigTestCase::RenameEntry()
412{
413 wxStringInputStream sis(testconfig);
414 wxFileConfig fc(sis);
415
9a83f860
VZ
416 fc.SetPath(wxT("root"));
417 CPPUNIT_ASSERT( fc.RenameEntry(wxT("entry"), wxT("newname")) );
418 wxVERIFY_FILECONFIG( wxT("[root]\n")
419 wxT("newname=value\n")
420 wxT("[root/group1]\n")
421 wxT("[root/group1/subgroup]\n")
422 wxT("subentry=subvalue\n")
423 wxT("subentry2=subvalue2\n")
424 wxT("[root/group2]\n"),
e1a2c7a5 425 fc );
94139118 426
9a83f860
VZ
427 fc.SetPath(wxT("group1/subgroup"));
428 CPPUNIT_ASSERT( !fc.RenameEntry(wxT("entry"), wxT("newname")) );
429 CPPUNIT_ASSERT( !fc.RenameEntry(wxT("subentry"), wxT("subentry2")) );
430
431 CPPUNIT_ASSERT( fc.RenameEntry(wxT("subentry"), wxT("subentry1")) );
432 wxVERIFY_FILECONFIG( wxT("[root]\n")
433 wxT("newname=value\n")
434 wxT("[root/group1]\n")
435 wxT("[root/group1/subgroup]\n")
436 wxT("subentry2=subvalue2\n")
437 wxT("subentry1=subvalue\n")
438 wxT("[root/group2]\n"),
e1a2c7a5 439 fc );
94139118
VZ
440}
441
442void FileConfigTestCase::RenameGroup()
443{
444 wxStringInputStream sis(testconfig);
445 wxFileConfig fc(sis);
446
9a83f860
VZ
447 CPPUNIT_ASSERT( fc.RenameGroup(wxT("root"), wxT("foot")) );
448 wxVERIFY_FILECONFIG( wxT("[foot]\n")
449 wxT("entry=value\n")
450 wxT("[foot/group1]\n")
451 wxT("[foot/group1/subgroup]\n")
452 wxT("subentry=subvalue\n")
453 wxT("subentry2=subvalue2\n")
454 wxT("[foot/group2]\n"),
e1a2c7a5 455 fc );
3ca10461 456
126bd656 457 // renaming a path doesn't work, it must be the immediate group
9a83f860 458 CPPUNIT_ASSERT( !fc.RenameGroup(wxT("foot/group1"), wxT("group2")) );
126bd656
VZ
459
460
9a83f860 461 fc.SetPath(wxT("foot"));
3ca10461 462
126bd656 463 // renaming to a name of existing group doesn't work
9a83f860 464 CPPUNIT_ASSERT( !fc.RenameGroup(wxT("group1"), wxT("group2")) );
126bd656
VZ
465
466 // try exchanging the groups names and then restore them back
9a83f860
VZ
467 CPPUNIT_ASSERT( fc.RenameGroup(wxT("group1"), wxT("groupTmp")) );
468 wxVERIFY_FILECONFIG( wxT("[foot]\n")
469 wxT("entry=value\n")
470 wxT("[foot/groupTmp]\n")
471 wxT("[foot/groupTmp/subgroup]\n")
472 wxT("subentry=subvalue\n")
473 wxT("subentry2=subvalue2\n")
474 wxT("[foot/group2]\n"),
3ca10461
VZ
475 fc );
476
9a83f860
VZ
477 CPPUNIT_ASSERT( fc.RenameGroup(wxT("group2"), wxT("group1")) );
478 wxVERIFY_FILECONFIG( wxT("[foot]\n")
479 wxT("entry=value\n")
480 wxT("[foot/groupTmp]\n")
481 wxT("[foot/groupTmp/subgroup]\n")
482 wxT("subentry=subvalue\n")
483 wxT("subentry2=subvalue2\n")
484 wxT("[foot/group1]\n"),
3ca10461
VZ
485 fc );
486
9a83f860
VZ
487 CPPUNIT_ASSERT( fc.RenameGroup(wxT("groupTmp"), wxT("group2")) );
488 wxVERIFY_FILECONFIG( wxT("[foot]\n")
489 wxT("entry=value\n")
490 wxT("[foot/group2]\n")
491 wxT("[foot/group2/subgroup]\n")
492 wxT("subentry=subvalue\n")
493 wxT("subentry2=subvalue2\n")
494 wxT("[foot/group1]\n"),
3ca10461 495 fc );
126bd656 496
9a83f860
VZ
497 CPPUNIT_ASSERT( fc.RenameGroup(wxT("group1"), wxT("groupTmp")) );
498 wxVERIFY_FILECONFIG( wxT("[foot]\n")
499 wxT("entry=value\n")
500 wxT("[foot/group2]\n")
501 wxT("[foot/group2/subgroup]\n")
502 wxT("subentry=subvalue\n")
503 wxT("subentry2=subvalue2\n")
504 wxT("[foot/groupTmp]\n"),
126bd656
VZ
505 fc );
506
9a83f860
VZ
507 CPPUNIT_ASSERT( fc.RenameGroup(wxT("group2"), wxT("group1")) );
508 wxVERIFY_FILECONFIG( wxT("[foot]\n")
509 wxT("entry=value\n")
510 wxT("[foot/group1]\n")
511 wxT("[foot/group1/subgroup]\n")
512 wxT("subentry=subvalue\n")
513 wxT("subentry2=subvalue2\n")
514 wxT("[foot/groupTmp]\n"),
126bd656
VZ
515 fc );
516
9a83f860
VZ
517 CPPUNIT_ASSERT( fc.RenameGroup(wxT("groupTmp"), wxT("group2")) );
518 wxVERIFY_FILECONFIG( wxT("[foot]\n")
519 wxT("entry=value\n")
520 wxT("[foot/group1]\n")
521 wxT("[foot/group1/subgroup]\n")
522 wxT("subentry=subvalue\n")
523 wxT("subentry2=subvalue2\n")
524 wxT("[foot/group2]\n"),
126bd656 525 fc );
94139118
VZ
526}
527
c406cd3d
VZ
528void FileConfigTestCase::CreateSubgroupAndEntries()
529{
530 wxFileConfig fc;
9a83f860
VZ
531 fc.Write(wxT("sub/sub_first"), wxT("sub_one"));
532 fc.Write(wxT("first"), wxT("one"));
c406cd3d 533
9a83f860
VZ
534 wxVERIFY_FILECONFIG( wxT("first=one\n")
535 wxT("[sub]\n")
536 wxT("sub_first=sub_one\n"),
e1a2c7a5 537 fc );
c406cd3d
VZ
538}
539
540void FileConfigTestCase::CreateEntriesAndSubgroup()
541{
542 wxFileConfig fc;
9a83f860
VZ
543 fc.Write(wxT("first"), wxT("one"));
544 fc.Write(wxT("second"), wxT("two"));
545 fc.Write(wxT("sub/sub_first"), wxT("sub_one"));
546
547 wxVERIFY_FILECONFIG( wxT("first=one\n")
548 wxT("second=two\n")
549 wxT("[sub]\n")
550 wxT("sub_first=sub_one\n"),
e1a2c7a5 551 fc );
c406cd3d 552}
cbc9c06f
DS
553
554static void EmptyConfigAndWriteKey()
555{
9a83f860 556 wxFileConfig fc(wxT("deleteconftest"));
cbc9c06f 557
9a83f860 558 const wxString groupPath = wxT("/root");
cbc9c06f 559
c406cd3d 560 if ( fc.Exists(groupPath) )
cbc9c06f
DS
561 {
562 // using DeleteGroup exposes the problem, using DeleteAll doesn't
563 CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
564 }
565
566 // the config must be empty for the problem to arise
567 CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
568 CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
569
570
571 // this crashes on second call of this function
9a83f860 572 CPPUNIT_ASSERT( fc.Write(groupPath + wxT("/entry"), wxT("value")) );
cbc9c06f
DS
573}
574
575void FileConfigTestCase::DeleteLastGroup()
576{
577 /*
578 We make 2 of the same calls, first to create a file config with a single
579 group and key...
580 */
581 ::EmptyConfigAndWriteKey();
582
583 /*
584 ... then the same but this time the key's group is deleted before the
585 key is written again. This causes a crash.
586 */
587 ::EmptyConfigAndWriteKey();
588
589
590 // clean up
591 wxLogNull noLogging;
9a83f860 592 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest")));
cbc9c06f
DS
593}
594
6ad0a7d5
VZ
595void FileConfigTestCase::DeleteAndRecreateGroup()
596{
597 static const wxChar *confInitial =
9a83f860
VZ
598 wxT("[First]\n")
599 wxT("Value1=Foo\n")
600 wxT("[Second]\n")
601 wxT("Value2=Bar\n");
6ad0a7d5
VZ
602
603 wxStringInputStream sis(confInitial);
604 wxFileConfig fc(sis);
605
9a83f860
VZ
606 fc.DeleteGroup(wxT("Second"));
607 wxVERIFY_FILECONFIG( wxT("[First]\n")
608 wxT("Value1=Foo\n"),
6ad0a7d5
VZ
609 fc );
610
9a83f860
VZ
611 fc.Write(wxT("Second/Value2"), wxT("New"));
612 wxVERIFY_FILECONFIG( wxT("[First]\n")
613 wxT("Value1=Foo\n")
614 wxT("[Second]\n")
615 wxT("Value2=New\n"),
6ad0a7d5
VZ
616 fc );
617}
618
9a7b7798
VZ
619void FileConfigTestCase::AddToExistingRoot()
620{
621 static const wxChar *confInitial =
9a83f860
VZ
622 wxT("[Group]\n")
623 wxT("value1=foo\n");
9a7b7798
VZ
624
625 wxStringInputStream sis(confInitial);
626 wxFileConfig fc(sis);
627
9a83f860 628 fc.Write(wxT("/value1"), wxT("bar"));
9a7b7798 629 wxVERIFY_FILECONFIG(
9a83f860
VZ
630 wxT("value1=bar\n")
631 wxT("[Group]\n")
632 wxT("value1=foo\n"),
9a7b7798
VZ
633 fc
634 );
635}
636
6221357b
VZ
637void FileConfigTestCase::ReadNonExistent()
638{
639 static const char *confTest =
640 "community=censored\n"
641 "[City1]\n"
642 "URL=www.fake1.na\n"
643 "[City1/A1]\n"
644 "[City1/A1/1]\n"
645 "IP=192.168.1.66\n"
646 "URL=www.fake2.na\n"
647 ;
648
649 wxStringInputStream sis(confTest);
650 wxFileConfig fc(sis);
651
652 wxString url;
653 CPPUNIT_ASSERT( !fc.Read("URL", &url) );
654}
655
975fb32b
VZ
656void FileConfigTestCase::ReadEmpty()
657{
658 static const char *confTest = "";
659
660 wxStringInputStream sis(confTest);
661 wxFileConfig fc(sis);
662}
663
384859f8
VZ
664void FileConfigTestCase::ReadFloat()
665{
666 static const char *confTest =
667 "x=1.234\n"
668 "y=-9876.5432\n"
669 "z=2e+308\n"
670 ;
671
672 wxStringInputStream sis(confTest);
673 wxFileConfig fc(sis);
674
675 float f;
676 CPPUNIT_ASSERT( fc.Read("x", &f) );
677 CPPUNIT_ASSERT_EQUAL( 1.234f, f );
678
679 CPPUNIT_ASSERT( fc.Read("y", &f) );
680 CPPUNIT_ASSERT_EQUAL( -9876.5432f, f );
681}
682
17c24d37
VZ
683#endif // wxUSE_FILECONFIG
684