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