]> git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconf.cpp
don't add dummy empty line at the end when reading from stream
[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_SUITE_END();
57
58 void Path();
59 void GetEntries();
60 void GetGroups();
61 void HasEntry();
62 void HasGroup();
63
64 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
65 {
66 fc.SetPath(path);
67
68 return fc.GetPath();
69 }
70
71 void CheckGroupEntries(const wxFileConfig& fc,
72 const wxChar *path,
73 size_t nEntries,
74 ...);
75 void CheckGroupSubgroups(const wxFileConfig& fc,
76 const wxChar *path,
77 size_t nGroups,
78 ...);
79
80 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
81 };
82
83 // register in the unnamed registry so that these tests are run by default
84 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
85
86 // also include in it's own registry so that these tests can be run alone
87 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
88
89 void FileConfigTestCase::Path()
90 {
91 wxStringInputStream sis(testconfig);
92 wxFileConfig fc(sis);
93
94 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
95 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
96 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
97 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
98 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
99 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
100 }
101
102 void
103 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
104 const wxChar *path,
105 size_t nEntries,
106 ...)
107 {
108 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
109
110 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
111
112 va_list ap;
113 va_start(ap, nEntries);
114
115 long cookie;
116 wxString name;
117 for ( bool cont = fc.GetFirstEntry(name, cookie);
118 cont;
119 cont = fc.GetNextEntry(name, cookie), nEntries-- )
120 {
121 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
122 }
123
124 CPPUNIT_ASSERT( nEntries == 0 );
125
126 va_end(ap);
127 }
128
129 void
130 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
131 const wxChar *path,
132 size_t nGroups,
133 ...)
134 {
135 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
136
137 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
138
139 va_list ap;
140 va_start(ap, nGroups);
141
142 long cookie;
143 wxString name;
144 for ( bool cont = fc.GetFirstGroup(name, cookie);
145 cont;
146 cont = fc.GetNextGroup(name, cookie), nGroups-- )
147 {
148 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
149 }
150
151 CPPUNIT_ASSERT( nGroups == 0 );
152
153 va_end(ap);
154 }
155
156 void FileConfigTestCase::GetEntries()
157 {
158 wxStringInputStream sis(testconfig);
159 wxFileConfig fc(sis);
160
161 CheckGroupEntries(fc, _T(""), 0);
162 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
163 CheckGroupEntries(fc, _T("/root/group1"), 0);
164 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
165 2, _T("subentry"), _T("subentry2"));
166 }
167
168 void FileConfigTestCase::GetGroups()
169 {
170 wxStringInputStream sis(testconfig);
171 wxFileConfig fc(sis);
172
173 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
174 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
175 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
176 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
177 }
178
179 void FileConfigTestCase::HasEntry()
180 {
181 wxStringInputStream sis(testconfig);
182 wxFileConfig fc(sis);
183
184 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
185 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
186 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
187 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
188 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
189 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
190 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
191 }
192
193 void FileConfigTestCase::HasGroup()
194 {
195 wxStringInputStream sis(testconfig);
196 wxFileConfig fc(sis);
197
198 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
199 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
200 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
201 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
202 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
203 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
204 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
205 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
206 }
207
208 #endif // wxUSE_FILECONFIG
209