]>
git.saurik.com Git - wxWidgets.git/blob - tests/fileconf/fileconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/fileconf/fileconf.cpp
3 // Purpose: wxFileConf unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
28 #include "wx/cppunit.h"
30 static const wxChar
*testconfig
=
34 _T("[root/group1/subgroup]\n")
35 _T("subentry=subvalue\n")
36 _T("subentry2=subvalue2\n")
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class FileConfigTestCase
: public CppUnit::TestCase
47 FileConfigTestCase() { }
50 CPPUNIT_TEST_SUITE( FileConfigTestCase
);
52 CPPUNIT_TEST( GetEntries
);
53 CPPUNIT_TEST( GetGroups
);
54 CPPUNIT_TEST( HasEntry
);
55 CPPUNIT_TEST( HasGroup
);
56 CPPUNIT_TEST_SUITE_END();
64 static wxString
ChangePath(wxFileConfig
& fc
, const wxChar
*path
)
71 void CheckGroupEntries(const wxFileConfig
& fc
,
75 void CheckGroupSubgroups(const wxFileConfig
& fc
,
80 DECLARE_NO_COPY_CLASS(FileConfigTestCase
)
83 // register in the unnamed registry so that these tests are run by default
84 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase
);
86 // also include in it's own registry so that these tests can be run alone
87 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase
, "FileConfigTestCase" );
89 void FileConfigTestCase::Path()
91 wxStringInputStream
sis(testconfig
);
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") );
103 FileConfigTestCase::CheckGroupEntries(const wxFileConfig
& fc
,
108 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
110 CPPUNIT_ASSERT( fc
.GetNumberOfEntries() == nEntries
);
113 va_start(ap
, nEntries
);
117 for ( bool cont
= fc
.GetFirstEntry(name
, cookie
);
119 cont
= fc
.GetNextEntry(name
, cookie
), nEntries
-- )
121 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
124 CPPUNIT_ASSERT( nEntries
== 0 );
130 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig
& fc
,
135 wxConfigPathChanger
change(&fc
, wxString(path
) + _T("/"));
137 CPPUNIT_ASSERT( fc
.GetNumberOfGroups() == nGroups
);
140 va_start(ap
, nGroups
);
144 for ( bool cont
= fc
.GetFirstGroup(name
, cookie
);
146 cont
= fc
.GetNextGroup(name
, cookie
), nGroups
-- )
148 CPPUNIT_ASSERT( name
== va_arg(ap
, wxChar
*) );
151 CPPUNIT_ASSERT( nGroups
== 0 );
156 void FileConfigTestCase::GetEntries()
158 wxStringInputStream
sis(testconfig
);
159 wxFileConfig
fc(sis
);
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"));
168 void FileConfigTestCase::GetGroups()
170 wxStringInputStream
sis(testconfig
);
171 wxFileConfig
fc(sis
);
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);
179 void FileConfigTestCase::HasEntry()
181 wxStringInputStream
sis(testconfig
);
182 wxFileConfig
fc(sis
);
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")) );
193 void FileConfigTestCase::HasGroup()
195 wxStringInputStream
sis(testconfig
);
196 wxFileConfig
fc(sis
);
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")) );
208 #endif // wxUSE_FILECONFIG