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