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