]>
Commit | Line | Data |
---|---|---|
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( DeleteGroup ); | |
75 | CPPUNIT_TEST( DeleteAll ); | |
76 | CPPUNIT_TEST( RenameEntry ); | |
77 | CPPUNIT_TEST( RenameGroup ); | |
78 | CPPUNIT_TEST( CreateEntriesAndSubgroup ); | |
79 | CPPUNIT_TEST( CreateSubgroupAndEntries ); | |
80 | CPPUNIT_TEST( DeleteLastGroup ); | |
81 | CPPUNIT_TEST( DeleteAndRecreateGroup ); | |
82 | CPPUNIT_TEST( AddToExistingRoot ); | |
83 | CPPUNIT_TEST( ReadNonExistent ); | |
84 | CPPUNIT_TEST( ReadEmpty ); | |
85 | CPPUNIT_TEST_SUITE_END(); | |
86 | ||
87 | void Path(); | |
88 | void AddEntries(); | |
89 | void GetEntries(); | |
90 | void GetGroups(); | |
91 | void HasEntry(); | |
92 | void HasGroup(); | |
93 | void Binary(); | |
94 | void Save(); | |
95 | void DeleteEntry(); | |
96 | void DeleteAndWriteEntry(); | |
97 | void DeleteGroup(); | |
98 | void DeleteAll(); | |
99 | void RenameEntry(); | |
100 | void RenameGroup(); | |
101 | void CreateEntriesAndSubgroup(); | |
102 | void CreateSubgroupAndEntries(); | |
103 | void DeleteLastGroup(); | |
104 | void DeleteAndRecreateGroup(); | |
105 | void AddToExistingRoot(); | |
106 | void ReadNonExistent(); | |
107 | void ReadEmpty(); | |
108 | ||
109 | ||
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 | ||
126 | DECLARE_NO_COPY_CLASS(FileConfigTestCase) | |
127 | }; | |
128 | ||
129 | // register in the unnamed registry so that these tests are run by default | |
130 | CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase ); | |
131 | ||
132 | // also include in it's own registry so that these tests can be run alone | |
133 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" ); | |
134 | ||
135 | void FileConfigTestCase::Path() | |
136 | { | |
137 | wxStringInputStream sis(testconfig); | |
138 | wxFileConfig fc(sis); | |
139 | ||
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") ); | |
146 | } | |
147 | ||
148 | void FileConfigTestCase::AddEntries() | |
149 | { | |
150 | wxFileConfig fc; | |
151 | ||
152 | wxVERIFY_FILECONFIG( wxT(""), fc ); | |
153 | ||
154 | fc.Write(wxT("/Foo"), wxT("foo")); | |
155 | wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc ); | |
156 | ||
157 | fc.Write(wxT("/Bar/Baz"), wxT("baz")); | |
158 | wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc ); | |
159 | ||
160 | fc.DeleteAll(); | |
161 | wxVERIFY_FILECONFIG( wxT(""), fc ); | |
162 | ||
163 | fc.Write(wxT("/Bar/Baz"), wxT("baz")); | |
164 | wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc ); | |
165 | ||
166 | fc.Write(wxT("/Foo"), wxT("foo")); | |
167 | wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc ); | |
168 | } | |
169 | ||
170 | void | |
171 | FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc, | |
172 | const wxChar *path, | |
173 | size_t nEntries, | |
174 | ...) | |
175 | { | |
176 | wxConfigPathChanger change(&fc, wxString(path) + wxT("/")); | |
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 | ||
197 | void | |
198 | FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc, | |
199 | const wxChar *path, | |
200 | size_t nGroups, | |
201 | ...) | |
202 | { | |
203 | wxConfigPathChanger change(&fc, wxString(path) + wxT("/")); | |
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 | ||
224 | void FileConfigTestCase::GetEntries() | |
225 | { | |
226 | wxStringInputStream sis(testconfig); | |
227 | wxFileConfig fc(sis); | |
228 | ||
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")); | |
234 | } | |
235 | ||
236 | void FileConfigTestCase::GetGroups() | |
237 | { | |
238 | wxStringInputStream sis(testconfig); | |
239 | wxFileConfig fc(sis); | |
240 | ||
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); | |
245 | } | |
246 | ||
247 | void FileConfigTestCase::HasEntry() | |
248 | { | |
249 | wxStringInputStream sis(testconfig); | |
250 | wxFileConfig fc(sis); | |
251 | ||
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")) ); | |
261 | } | |
262 | ||
263 | void FileConfigTestCase::HasGroup() | |
264 | { | |
265 | wxStringInputStream sis(testconfig); | |
266 | wxFileConfig fc(sis); | |
267 | ||
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")) ); | |
277 | } | |
278 | ||
279 | void 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 | ||
303 | void FileConfigTestCase::Save() | |
304 | { | |
305 | wxStringInputStream sis(testconfig); | |
306 | wxFileConfig fc(sis); | |
307 | wxVERIFY_FILECONFIG( testconfig, fc ); | |
308 | } | |
309 | ||
310 | void FileConfigTestCase::DeleteEntry() | |
311 | { | |
312 | wxStringInputStream sis(testconfig); | |
313 | wxFileConfig fc(sis); | |
314 | ||
315 | CPPUNIT_ASSERT( !fc.DeleteEntry(wxT("foo")) ); | |
316 | ||
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"), | |
324 | fc ); | |
325 | ||
326 | // group should be deleted now as well as it became empty | |
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"), | |
333 | fc ); | |
334 | } | |
335 | ||
336 | void 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 | ||
377 | void FileConfigTestCase::DeleteGroup() | |
378 | { | |
379 | wxStringInputStream sis(testconfig); | |
380 | wxFileConfig fc(sis); | |
381 | ||
382 | CPPUNIT_ASSERT( !fc.DeleteGroup(wxT("foo")) ); | |
383 | ||
384 | CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group1")) ); | |
385 | wxVERIFY_FILECONFIG( wxT("[root]\n") | |
386 | wxT("entry=value\n") | |
387 | wxT("[root/group2]\n"), | |
388 | fc ); | |
389 | ||
390 | // notice trailing slash: it should be ignored | |
391 | CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group2/")) ); | |
392 | wxVERIFY_FILECONFIG( wxT("[root]\n") | |
393 | wxT("entry=value\n"), | |
394 | fc ); | |
395 | ||
396 | CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root")) ); | |
397 | CPPUNIT_ASSERT( Dump(fc).empty() ); | |
398 | } | |
399 | ||
400 | void 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 | ||
409 | void FileConfigTestCase::RenameEntry() | |
410 | { | |
411 | wxStringInputStream sis(testconfig); | |
412 | wxFileConfig fc(sis); | |
413 | ||
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"), | |
423 | fc ); | |
424 | ||
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"), | |
437 | fc ); | |
438 | } | |
439 | ||
440 | void FileConfigTestCase::RenameGroup() | |
441 | { | |
442 | wxStringInputStream sis(testconfig); | |
443 | wxFileConfig fc(sis); | |
444 | ||
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"), | |
453 | fc ); | |
454 | ||
455 | // renaming a path doesn't work, it must be the immediate group | |
456 | CPPUNIT_ASSERT( !fc.RenameGroup(wxT("foot/group1"), wxT("group2")) ); | |
457 | ||
458 | ||
459 | fc.SetPath(wxT("foot")); | |
460 | ||
461 | // renaming to a name of existing group doesn't work | |
462 | CPPUNIT_ASSERT( !fc.RenameGroup(wxT("group1"), wxT("group2")) ); | |
463 | ||
464 | // try exchanging the groups names and then restore them back | |
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"), | |
473 | fc ); | |
474 | ||
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"), | |
483 | fc ); | |
484 | ||
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"), | |
493 | fc ); | |
494 | ||
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"), | |
503 | fc ); | |
504 | ||
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"), | |
513 | fc ); | |
514 | ||
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"), | |
523 | fc ); | |
524 | } | |
525 | ||
526 | void FileConfigTestCase::CreateSubgroupAndEntries() | |
527 | { | |
528 | wxFileConfig fc; | |
529 | fc.Write(wxT("sub/sub_first"), wxT("sub_one")); | |
530 | fc.Write(wxT("first"), wxT("one")); | |
531 | ||
532 | wxVERIFY_FILECONFIG( wxT("first=one\n") | |
533 | wxT("[sub]\n") | |
534 | wxT("sub_first=sub_one\n"), | |
535 | fc ); | |
536 | } | |
537 | ||
538 | void FileConfigTestCase::CreateEntriesAndSubgroup() | |
539 | { | |
540 | wxFileConfig fc; | |
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"), | |
549 | fc ); | |
550 | } | |
551 | ||
552 | static void EmptyConfigAndWriteKey() | |
553 | { | |
554 | wxFileConfig fc(wxT("deleteconftest")); | |
555 | ||
556 | const wxString groupPath = wxT("/root"); | |
557 | ||
558 | if ( fc.Exists(groupPath) ) | |
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 | |
570 | CPPUNIT_ASSERT( fc.Write(groupPath + wxT("/entry"), wxT("value")) ); | |
571 | } | |
572 | ||
573 | void 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; | |
590 | (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest"))); | |
591 | } | |
592 | ||
593 | void FileConfigTestCase::DeleteAndRecreateGroup() | |
594 | { | |
595 | static const wxChar *confInitial = | |
596 | wxT("[First]\n") | |
597 | wxT("Value1=Foo\n") | |
598 | wxT("[Second]\n") | |
599 | wxT("Value2=Bar\n"); | |
600 | ||
601 | wxStringInputStream sis(confInitial); | |
602 | wxFileConfig fc(sis); | |
603 | ||
604 | fc.DeleteGroup(wxT("Second")); | |
605 | wxVERIFY_FILECONFIG( wxT("[First]\n") | |
606 | wxT("Value1=Foo\n"), | |
607 | fc ); | |
608 | ||
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"), | |
614 | fc ); | |
615 | } | |
616 | ||
617 | void FileConfigTestCase::AddToExistingRoot() | |
618 | { | |
619 | static const wxChar *confInitial = | |
620 | wxT("[Group]\n") | |
621 | wxT("value1=foo\n"); | |
622 | ||
623 | wxStringInputStream sis(confInitial); | |
624 | wxFileConfig fc(sis); | |
625 | ||
626 | fc.Write(wxT("/value1"), wxT("bar")); | |
627 | wxVERIFY_FILECONFIG( | |
628 | wxT("value1=bar\n") | |
629 | wxT("[Group]\n") | |
630 | wxT("value1=foo\n"), | |
631 | fc | |
632 | ); | |
633 | } | |
634 | ||
635 | void 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 | ||
654 | void FileConfigTestCase::ReadEmpty() | |
655 | { | |
656 | static const char *confTest = ""; | |
657 | ||
658 | wxStringInputStream sis(confTest); | |
659 | wxFileConfig fc(sis); | |
660 | } | |
661 | ||
662 | #endif // wxUSE_FILECONFIG | |
663 |