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