]> git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconf.cpp
added tests for deleting and renaming entries and groups
[wxWidgets.git] / tests / fileconf / fileconf.cpp
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 "wx/wxprec.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
28 #include "wx/cppunit.h"
29
30 static const wxChar *testconfig =
31 _T("[root]\n")
32 _T("entry=value\n")
33 _T("[root/group1]\n")
34 _T("[root/group1/subgroup]\n")
35 _T("subentry=subvalue\n")
36 _T("subentry2=subvalue2\n")
37 _T("[root/group2]\n")
38 ;
39
40 // ----------------------------------------------------------------------------
41 // test class
42 // ----------------------------------------------------------------------------
43
44 class FileConfigTestCase : public CppUnit::TestCase
45 {
46 public:
47 FileConfigTestCase() { }
48
49 private:
50 CPPUNIT_TEST_SUITE( FileConfigTestCase );
51 CPPUNIT_TEST( Path );
52 CPPUNIT_TEST( GetEntries );
53 CPPUNIT_TEST( GetGroups );
54 CPPUNIT_TEST( HasEntry );
55 CPPUNIT_TEST( HasGroup );
56 CPPUNIT_TEST( Save );
57 CPPUNIT_TEST( DeleteEntry );
58 CPPUNIT_TEST( DeleteGroup );
59 CPPUNIT_TEST( DeleteAll );
60 CPPUNIT_TEST( RenameEntry );
61 CPPUNIT_TEST( RenameGroup );
62 CPPUNIT_TEST_SUITE_END();
63
64 void Path();
65 void GetEntries();
66 void GetGroups();
67 void HasEntry();
68 void HasGroup();
69 void Save();
70 void DeleteEntry();
71 void DeleteGroup();
72 void DeleteAll();
73 void RenameEntry();
74 void RenameGroup();
75
76 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
77 {
78 fc.SetPath(path);
79
80 return fc.GetPath();
81 }
82
83 static wxString Dump(wxFileConfig& fc)
84 {
85 wxStringOutputStream sos;
86 fc.Save(sos);
87 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
88 }
89
90 void CheckGroupEntries(const wxFileConfig& fc,
91 const wxChar *path,
92 size_t nEntries,
93 ...);
94 void CheckGroupSubgroups(const wxFileConfig& fc,
95 const wxChar *path,
96 size_t nGroups,
97 ...);
98
99 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
100 };
101
102 // register in the unnamed registry so that these tests are run by default
103 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
104
105 // also include in it's own registry so that these tests can be run alone
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
107
108 void FileConfigTestCase::Path()
109 {
110 wxStringInputStream sis(testconfig);
111 wxFileConfig fc(sis);
112
113 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
114 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
115 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
116 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
117 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
118 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
119 }
120
121 void
122 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
123 const wxChar *path,
124 size_t nEntries,
125 ...)
126 {
127 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
128
129 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
130
131 va_list ap;
132 va_start(ap, nEntries);
133
134 long cookie;
135 wxString name;
136 for ( bool cont = fc.GetFirstEntry(name, cookie);
137 cont;
138 cont = fc.GetNextEntry(name, cookie), nEntries-- )
139 {
140 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
141 }
142
143 CPPUNIT_ASSERT( nEntries == 0 );
144
145 va_end(ap);
146 }
147
148 void
149 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
150 const wxChar *path,
151 size_t nGroups,
152 ...)
153 {
154 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
155
156 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
157
158 va_list ap;
159 va_start(ap, nGroups);
160
161 long cookie;
162 wxString name;
163 for ( bool cont = fc.GetFirstGroup(name, cookie);
164 cont;
165 cont = fc.GetNextGroup(name, cookie), nGroups-- )
166 {
167 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
168 }
169
170 CPPUNIT_ASSERT( nGroups == 0 );
171
172 va_end(ap);
173 }
174
175 void FileConfigTestCase::GetEntries()
176 {
177 wxStringInputStream sis(testconfig);
178 wxFileConfig fc(sis);
179
180 CheckGroupEntries(fc, _T(""), 0);
181 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
182 CheckGroupEntries(fc, _T("/root/group1"), 0);
183 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
184 2, _T("subentry"), _T("subentry2"));
185 }
186
187 void FileConfigTestCase::GetGroups()
188 {
189 wxStringInputStream sis(testconfig);
190 wxFileConfig fc(sis);
191
192 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
193 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
194 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
195 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
196 }
197
198 void FileConfigTestCase::HasEntry()
199 {
200 wxStringInputStream sis(testconfig);
201 wxFileConfig fc(sis);
202
203 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
204 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
205 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
206 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
207 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
208 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
209 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
210 }
211
212 void FileConfigTestCase::HasGroup()
213 {
214 wxStringInputStream sis(testconfig);
215 wxFileConfig fc(sis);
216
217 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
218 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
219 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
220 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
221 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
222 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
223 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
224 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
225 }
226
227 void FileConfigTestCase::Save()
228 {
229 wxStringInputStream sis(testconfig);
230 wxFileConfig fc(sis);
231 CPPUNIT_ASSERT( Dump(fc) == testconfig );
232 }
233
234 void FileConfigTestCase::DeleteEntry()
235 {
236 wxStringInputStream sis(testconfig);
237 wxFileConfig fc(sis);
238
239 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
240
241 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
242 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
243 _T("entry=value\n")
244 _T("[root/group1]\n")
245 _T("[root/group1/subgroup]\n")
246 _T("subentry2=subvalue2\n")
247 _T("[root/group2]\n") );
248
249 // group should be deleted now as well as it became empty
250 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
251 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
252 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
253 _T("entry=value\n")
254 _T("[root/group1]\n")
255 _T("[root/group2]\n") );
256 }
257
258 void FileConfigTestCase::DeleteGroup()
259 {
260 wxStringInputStream sis(testconfig);
261 wxFileConfig fc(sis);
262
263 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
264
265 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
266 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
267 _T("entry=value\n")
268 _T("[root/group2]\n") );
269
270 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2")) );
271 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
272 _T("entry=value\n") );
273
274 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
275 CPPUNIT_ASSERT( Dump(fc).empty() );
276 }
277
278 void FileConfigTestCase::DeleteAll()
279 {
280 wxStringInputStream sis(testconfig);
281 wxFileConfig fc(sis);
282
283 CPPUNIT_ASSERT( fc.DeleteAll() );
284 CPPUNIT_ASSERT( Dump(fc).empty() );
285 }
286
287 void FileConfigTestCase::RenameEntry()
288 {
289 wxStringInputStream sis(testconfig);
290 wxFileConfig fc(sis);
291
292 fc.SetPath(_T("root"));
293 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
294 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
295 _T("newname=value\n")
296 _T("[root/group1]\n")
297 _T("[root/group1/subgroup]\n")
298 _T("subentry=subvalue\n")
299 _T("subentry2=subvalue2\n")
300 _T("[root/group2]\n") );
301
302 fc.SetPath(_T("group1/subgroup"));
303 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
304 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
305
306 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
307 CPPUNIT_ASSERT( Dump(fc) == _T("[root]\n")
308 _T("newname=value\n")
309 _T("[root/group1]\n")
310 _T("[root/group1/subgroup]\n")
311 _T("subentry2=subvalue2\n")
312 _T("subentry1=subvalue\n")
313 _T("[root/group2]\n") );
314 }
315
316 void FileConfigTestCase::RenameGroup()
317 {
318 wxStringInputStream sis(testconfig);
319 wxFileConfig fc(sis);
320
321 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
322 CPPUNIT_ASSERT( Dump(fc) == _T("[foot]\n")
323 _T("entry=value\n")
324 _T("[foot/group1]\n")
325 _T("[foot/group1/subgroup]\n")
326 _T("subentry=subvalue\n")
327 _T("subentry2=subvalue2\n")
328 _T("[foot/group2]\n") );
329 }
330
331 #endif // wxUSE_FILECONFIG
332