]>
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 | // Copyright: (c) 2004 Vadim Zeitlin | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | // ---------------------------------------------------------------------------- | |
10 | // headers | |
11 | // ---------------------------------------------------------------------------- | |
12 | ||
13 | #include "testprec.h" | |
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" | |
26 | #include "wx/log.h" | |
27 | ||
28 | static const wxChar *testconfig = | |
29 | wxT("[root]\n") | |
30 | wxT("entry=value\n") | |
31 | wxT("[root/group1]\n") | |
32 | wxT("[root/group1/subgroup]\n") | |
33 | wxT("subentry=subvalue\n") | |
34 | wxT("subentry2=subvalue2\n") | |
35 | wxT("[root/group2]\n") | |
36 | ; | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // local functions | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | static 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 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // test class | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | class FileConfigTestCase : public CppUnit::TestCase | |
57 | { | |
58 | public: | |
59 | FileConfigTestCase() { } | |
60 | ||
61 | private: | |
62 | CPPUNIT_TEST_SUITE( FileConfigTestCase ); | |
63 | CPPUNIT_TEST( Path ); | |
64 | CPPUNIT_TEST( AddEntries ); | |
65 | CPPUNIT_TEST( GetEntries ); | |
66 | CPPUNIT_TEST( GetGroups ); | |
67 | CPPUNIT_TEST( HasEntry ); | |
68 | CPPUNIT_TEST( HasGroup ); | |
69 | CPPUNIT_TEST( Binary ); | |
70 | CPPUNIT_TEST( Save ); | |
71 | CPPUNIT_TEST( DeleteEntry ); | |
72 | CPPUNIT_TEST( DeleteAndWriteEntry ); | |
73 | CPPUNIT_TEST( DeleteLastRootEntry ); | |
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( ReadFloat ); | |
86 | CPPUNIT_TEST_SUITE_END(); | |
87 | ||
88 | void Path(); | |
89 | void AddEntries(); | |
90 | void GetEntries(); | |
91 | void GetGroups(); | |
92 | void HasEntry(); | |
93 | void HasGroup(); | |
94 | void Binary(); | |
95 | void Save(); | |
96 | void DeleteEntry(); | |
97 | void DeleteAndWriteEntry(); | |
98 | void DeleteLastRootEntry(); | |
99 | void DeleteGroup(); | |
100 | void DeleteAll(); | |
101 | void RenameEntry(); | |
102 | void RenameGroup(); | |
103 | void CreateEntriesAndSubgroup(); | |
104 | void CreateSubgroupAndEntries(); | |
105 | void DeleteLastGroup(); | |
106 | void DeleteAndRecreateGroup(); | |
107 | void AddToExistingRoot(); | |
108 | void ReadNonExistent(); | |
109 | void ReadEmpty(); | |
110 | void ReadFloat(); | |
111 | ||
112 | ||
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 | ||
129 | DECLARE_NO_COPY_CLASS(FileConfigTestCase) | |
130 | }; | |
131 | ||
132 | // register in the unnamed registry so that these tests are run by default | |
133 | CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase ); | |
134 | ||
135 | // also include in its own registry so that these tests can be run alone | |
136 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" ); | |
137 | ||
138 | void FileConfigTestCase::Path() | |
139 | { | |
140 | wxStringInputStream sis(testconfig); | |
141 | wxFileConfig fc(sis); | |
142 | ||
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") ); | |
149 | } | |
150 | ||
151 | void FileConfigTestCase::AddEntries() | |
152 | { | |
153 | wxFileConfig fc; | |
154 | ||
155 | wxVERIFY_FILECONFIG( wxT(""), fc ); | |
156 | ||
157 | fc.Write(wxT("/Foo"), wxT("foo")); | |
158 | wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc ); | |
159 | ||
160 | fc.Write(wxT("/Bar/Baz"), wxT("baz")); | |
161 | wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc ); | |
162 | ||
163 | fc.DeleteAll(); | |
164 | wxVERIFY_FILECONFIG( wxT(""), fc ); | |
165 | ||
166 | fc.Write(wxT("/Bar/Baz"), wxT("baz")); | |
167 | wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc ); | |
168 | ||
169 | fc.Write(wxT("/Foo"), wxT("foo")); | |
170 | wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc ); | |
171 | } | |
172 | ||
173 | void | |
174 | FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc, | |
175 | const wxChar *path, | |
176 | size_t nEntries, | |
177 | ...) | |
178 | { | |
179 | wxConfigPathChanger change(&fc, wxString(path) + wxT("/")); | |
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 | ||
200 | void | |
201 | FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc, | |
202 | const wxChar *path, | |
203 | size_t nGroups, | |
204 | ...) | |
205 | { | |
206 | wxConfigPathChanger change(&fc, wxString(path) + wxT("/")); | |
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 | ||
227 | void FileConfigTestCase::GetEntries() | |
228 | { | |
229 | wxStringInputStream sis(testconfig); | |
230 | wxFileConfig fc(sis); | |
231 | ||
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")); | |
237 | } | |
238 | ||
239 | void FileConfigTestCase::GetGroups() | |
240 | { | |
241 | wxStringInputStream sis(testconfig); | |
242 | wxFileConfig fc(sis); | |
243 | ||
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); | |
248 | } | |
249 | ||
250 | void FileConfigTestCase::HasEntry() | |
251 | { | |
252 | wxStringInputStream sis(testconfig); | |
253 | wxFileConfig fc(sis); | |
254 | ||
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")) ); | |
264 | } | |
265 | ||
266 | void FileConfigTestCase::HasGroup() | |
267 | { | |
268 | wxStringInputStream sis(testconfig); | |
269 | wxFileConfig fc(sis); | |
270 | ||
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")) ); | |
280 | } | |
281 | ||
282 | void 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 | ||
306 | void FileConfigTestCase::Save() | |
307 | { | |
308 | wxStringInputStream sis(testconfig); | |
309 | wxFileConfig fc(sis); | |
310 | wxVERIFY_FILECONFIG( testconfig, fc ); | |
311 | } | |
312 | ||
313 | void FileConfigTestCase::DeleteEntry() | |
314 | { | |
315 | wxStringInputStream sis(testconfig); | |
316 | wxFileConfig fc(sis); | |
317 | ||
318 | CPPUNIT_ASSERT( !fc.DeleteEntry(wxT("foo")) ); | |
319 | ||
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"), | |
327 | fc ); | |
328 | ||
329 | // group should be deleted now as well as it became empty | |
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"), | |
336 | fc ); | |
337 | } | |
338 | ||
339 | void 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 | ||
380 | void 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 | ||
398 | void FileConfigTestCase::DeleteGroup() | |
399 | { | |
400 | wxStringInputStream sis(testconfig); | |
401 | wxFileConfig fc(sis); | |
402 | ||
403 | CPPUNIT_ASSERT( !fc.DeleteGroup(wxT("foo")) ); | |
404 | ||
405 | CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group1")) ); | |
406 | wxVERIFY_FILECONFIG( wxT("[root]\n") | |
407 | wxT("entry=value\n") | |
408 | wxT("[root/group2]\n"), | |
409 | fc ); | |
410 | ||
411 | // notice trailing slash: it should be ignored | |
412 | CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group2/")) ); | |
413 | wxVERIFY_FILECONFIG( wxT("[root]\n") | |
414 | wxT("entry=value\n"), | |
415 | fc ); | |
416 | ||
417 | CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root")) ); | |
418 | CPPUNIT_ASSERT( Dump(fc).empty() ); | |
419 | } | |
420 | ||
421 | void 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 | ||
430 | void FileConfigTestCase::RenameEntry() | |
431 | { | |
432 | wxStringInputStream sis(testconfig); | |
433 | wxFileConfig fc(sis); | |
434 | ||
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"), | |
444 | fc ); | |
445 | ||
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"), | |
458 | fc ); | |
459 | } | |
460 | ||
461 | void FileConfigTestCase::RenameGroup() | |
462 | { | |
463 | wxStringInputStream sis(testconfig); | |
464 | wxFileConfig fc(sis); | |
465 | ||
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"), | |
474 | fc ); | |
475 | ||
476 | // renaming a path doesn't work, it must be the immediate group | |
477 | CPPUNIT_ASSERT( !fc.RenameGroup(wxT("foot/group1"), wxT("group2")) ); | |
478 | ||
479 | ||
480 | fc.SetPath(wxT("foot")); | |
481 | ||
482 | // renaming to a name of existing group doesn't work | |
483 | CPPUNIT_ASSERT( !fc.RenameGroup(wxT("group1"), wxT("group2")) ); | |
484 | ||
485 | // try exchanging the groups names and then restore them back | |
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"), | |
494 | fc ); | |
495 | ||
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"), | |
504 | fc ); | |
505 | ||
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"), | |
514 | fc ); | |
515 | ||
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"), | |
524 | fc ); | |
525 | ||
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"), | |
534 | fc ); | |
535 | ||
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"), | |
544 | fc ); | |
545 | } | |
546 | ||
547 | void FileConfigTestCase::CreateSubgroupAndEntries() | |
548 | { | |
549 | wxFileConfig fc; | |
550 | fc.Write(wxT("sub/sub_first"), wxT("sub_one")); | |
551 | fc.Write(wxT("first"), wxT("one")); | |
552 | ||
553 | wxVERIFY_FILECONFIG( wxT("first=one\n") | |
554 | wxT("[sub]\n") | |
555 | wxT("sub_first=sub_one\n"), | |
556 | fc ); | |
557 | } | |
558 | ||
559 | void FileConfigTestCase::CreateEntriesAndSubgroup() | |
560 | { | |
561 | wxFileConfig fc; | |
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"), | |
570 | fc ); | |
571 | } | |
572 | ||
573 | static void EmptyConfigAndWriteKey() | |
574 | { | |
575 | wxFileConfig fc(wxT("deleteconftest")); | |
576 | ||
577 | const wxString groupPath = wxT("/root"); | |
578 | ||
579 | if ( fc.Exists(groupPath) ) | |
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 | |
591 | CPPUNIT_ASSERT( fc.Write(groupPath + wxT("/entry"), wxT("value")) ); | |
592 | } | |
593 | ||
594 | void 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; | |
611 | (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest"))); | |
612 | } | |
613 | ||
614 | void FileConfigTestCase::DeleteAndRecreateGroup() | |
615 | { | |
616 | static const wxChar *confInitial = | |
617 | wxT("[First]\n") | |
618 | wxT("Value1=Foo\n") | |
619 | wxT("[Second]\n") | |
620 | wxT("Value2=Bar\n"); | |
621 | ||
622 | wxStringInputStream sis(confInitial); | |
623 | wxFileConfig fc(sis); | |
624 | ||
625 | fc.DeleteGroup(wxT("Second")); | |
626 | wxVERIFY_FILECONFIG( wxT("[First]\n") | |
627 | wxT("Value1=Foo\n"), | |
628 | fc ); | |
629 | ||
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"), | |
635 | fc ); | |
636 | } | |
637 | ||
638 | void FileConfigTestCase::AddToExistingRoot() | |
639 | { | |
640 | static const wxChar *confInitial = | |
641 | wxT("[Group]\n") | |
642 | wxT("value1=foo\n"); | |
643 | ||
644 | wxStringInputStream sis(confInitial); | |
645 | wxFileConfig fc(sis); | |
646 | ||
647 | fc.Write(wxT("/value1"), wxT("bar")); | |
648 | wxVERIFY_FILECONFIG( | |
649 | wxT("value1=bar\n") | |
650 | wxT("[Group]\n") | |
651 | wxT("value1=foo\n"), | |
652 | fc | |
653 | ); | |
654 | } | |
655 | ||
656 | void 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 | ||
675 | void FileConfigTestCase::ReadEmpty() | |
676 | { | |
677 | static const char *confTest = ""; | |
678 | ||
679 | wxStringInputStream sis(confTest); | |
680 | wxFileConfig fc(sis); | |
681 | } | |
682 | ||
683 | void 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 | ||
702 | #endif // wxUSE_FILECONFIG | |
703 |