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