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