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