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