]>
Commit | Line | Data |
---|---|---|
3e1512cd VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/config/config.cpp | |
3 | // Purpose: wxConfig unit test | |
4 | // Author: Marcin Wojdyr | |
5 | // Created: 2007-07-07 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2007 Marcin Wojdyr | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
7b8ccf33 FM |
10 | // NOTE: this test is compiled in test_gui because it uses wxColour for |
11 | // its testing purpose. | |
12 | // See also tests/config/fileconf.cpp for wxFileConfig specific tests and | |
13 | // tests/config/regconf.cpp for wxRegConfig specific tests. | |
3e1512cd VZ |
14 | |
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #include "testprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/wx.h" | |
27 | #endif // WX_PRECOMP | |
28 | ||
29 | #include "wx/config.h" | |
30 | #include "wx/colour.h" | |
31 | ||
32 | // -------------------------------------------------------------------------- | |
33 | // test class | |
34 | // -------------------------------------------------------------------------- | |
35 | ||
36 | class ConfigTestCase : public CppUnit::TestCase | |
37 | { | |
38 | public: | |
39 | ConfigTestCase() {} | |
40 | ||
41 | private: | |
42 | CPPUNIT_TEST_SUITE( ConfigTestCase ); | |
43 | CPPUNIT_TEST( ReadWriteLocalTest ); | |
44 | CPPUNIT_TEST( RecordingDefaultsTest ); | |
45 | CPPUNIT_TEST_SUITE_END(); | |
46 | ||
47 | void ReadWriteLocalTest(); | |
48 | void RecordingDefaultsTest(); | |
5c8640d0 | 49 | |
955e1ce8 VZ |
50 | // return the number of values we (attempted to) read |
51 | int ReadValues(wxConfig *config, bool has_values); | |
3e1512cd VZ |
52 | |
53 | DECLARE_NO_COPY_CLASS(ConfigTestCase) | |
54 | }; | |
55 | ||
56 | // register in the unnamed registry so that these tests are run by default | |
57 | CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase ); | |
58 | ||
59 | // also include in it's own registry so that these tests can be run alone | |
60 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" ); | |
61 | ||
62 | void ConfigTestCase::ReadWriteLocalTest() | |
63 | { | |
64 | wxString app = wxT("wxConfigTestCase"); | |
65 | wxString vendor = wxT("wxWidgets"); | |
66 | wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""), | |
67 | wxCONFIG_USE_LOCAL_FILE); | |
68 | config->DeleteAll(); | |
69 | config->Write(wxT("string1"), wxT("abc")); | |
70 | config->Write(wxT("string2"), wxString(wxT("def"))); | |
71 | config->Write(wxT("int1"), 123); | |
72 | config->Write(wxString(wxT("long1")), 234L); | |
73 | config->Write(wxT("double1"), 345.67); | |
74 | config->Write(wxT("bool1"), true); | |
f9bb777f | 75 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
3e1512cd | 76 | config->Write(wxT("color1"), wxColour(11,22,33,44)); |
f9bb777f | 77 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
3e1512cd VZ |
78 | config->Flush(); |
79 | delete config; | |
80 | ||
81 | config = new wxConfig(app, vendor, wxT(""), wxT(""), | |
82 | wxCONFIG_USE_LOCAL_FILE); | |
83 | wxString string1 = config->Read(wxT("string1")); | |
5c8640d0 | 84 | CPPUNIT_ASSERT_EQUAL( "abc", string1 ); |
3e1512cd | 85 | string1 = config->Read(wxT("string1"), wxT("defaultvalue")); |
5c8640d0 | 86 | CPPUNIT_ASSERT_EQUAL( "abc", string1 ); |
58d1a316 | 87 | |
3e1512cd VZ |
88 | wxString string2; |
89 | bool r = config->Read(wxT("string2"), &string2); | |
58d1a316 | 90 | CPPUNIT_ASSERT( r ); |
5c8640d0 | 91 | CPPUNIT_ASSERT_EQUAL( "def", string2 ); |
58d1a316 | 92 | |
3e1512cd | 93 | r = config->Read(wxT("string2"), &string2, wxT("defaultvalue")); |
58d1a316 | 94 | CPPUNIT_ASSERT( r ); |
5c8640d0 | 95 | CPPUNIT_ASSERT_EQUAL( "def", string2 ); |
58d1a316 | 96 | |
3e1512cd | 97 | int int1 = config->Read(wxT("int1"), 5); |
5c8640d0 | 98 | CPPUNIT_ASSERT_EQUAL( 123, int1 ); |
58d1a316 | 99 | |
3e1512cd VZ |
100 | long long1; |
101 | r = config->Read(wxT("long1"), &long1); | |
58d1a316 | 102 | CPPUNIT_ASSERT( r ); |
5c8640d0 | 103 | CPPUNIT_ASSERT_EQUAL( 234L, long1 ); |
58d1a316 | 104 | |
56601ff2 VZ |
105 | CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 ); |
106 | ||
107 | double double1; | |
108 | r = config->Read(wxT("double1"), &double1); | |
109 | CPPUNIT_ASSERT( r ); | |
5c8640d0 | 110 | CPPUNIT_ASSERT_EQUAL( 345.67, double1 ); |
56601ff2 VZ |
111 | |
112 | CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 ); | |
113 | ||
3e1512cd | 114 | bool bool1; |
56601ff2 | 115 | r = config->Read(wxT("foo"), &bool1); // there is no "foo" key |
58d1a316 VZ |
116 | CPPUNIT_ASSERT( !r ); |
117 | ||
3e1512cd | 118 | r = config->Read(wxT("bool1"), &bool1); |
58d1a316 | 119 | CPPUNIT_ASSERT( r ); |
5c8640d0 | 120 | CPPUNIT_ASSERT_EQUAL( true, bool1 ); |
58d1a316 | 121 | |
56601ff2 VZ |
122 | CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 ); |
123 | ||
f9bb777f | 124 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
3e1512cd VZ |
125 | wxColour color1; |
126 | r = config->Read(wxT("color1"), &color1); | |
58d1a316 | 127 | CPPUNIT_ASSERT( r ); |
3e1512cd | 128 | CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) ); |
58d1a316 | 129 | |
56601ff2 | 130 | CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 ); |
f9bb777f | 131 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
56601ff2 | 132 | |
3e1512cd VZ |
133 | config->DeleteAll(); |
134 | delete config; | |
135 | } | |
136 | ||
955e1ce8 | 137 | int ConfigTestCase::ReadValues(wxConfig *config, bool has_values) |
3e1512cd | 138 | { |
955e1ce8 | 139 | int read = 0; |
3e1512cd | 140 | bool r; |
955e1ce8 | 141 | |
58d1a316 | 142 | wxString string1 = config->Read(wxT("string1"), wxT("abc")); |
955e1ce8 VZ |
143 | read++; |
144 | ||
58d1a316 | 145 | wxString string2 = config->Read(wxT("string2"), wxString(wxT("def"))); |
955e1ce8 VZ |
146 | read++; |
147 | ||
148 | wxString string3; | |
3e1512cd | 149 | r = config->Read(wxT("string3"), &string3, wxT("abc")); |
5c8640d0 | 150 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 VZ |
151 | read++; |
152 | ||
153 | wxString string4; | |
3e1512cd | 154 | r = config->Read(wxT("string4"), &string4, wxString(wxT("def"))); |
5c8640d0 | 155 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 VZ |
156 | read++; |
157 | ||
58d1a316 | 158 | int int1; |
3e1512cd | 159 | r = config->Read(wxT("int1"), &int1, 123); |
5c8640d0 | 160 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 VZ |
161 | read++; |
162 | ||
58d1a316 VZ |
163 | int int2 = config->Read(wxT("int2"), 1234); |
164 | CPPUNIT_ASSERT_EQUAL( int2, 1234 ); | |
955e1ce8 VZ |
165 | read++; |
166 | ||
58d1a316 | 167 | long long1; |
3e1512cd | 168 | r = config->Read(wxString(wxT("long1")), &long1, 234L); |
5c8640d0 | 169 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 VZ |
170 | read++; |
171 | ||
58d1a316 | 172 | double double1; |
3e1512cd | 173 | r = config->Read(wxT("double1"), &double1, 345.67); |
5c8640d0 | 174 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 VZ |
175 | read++; |
176 | ||
58d1a316 | 177 | bool bool1; |
3e1512cd | 178 | r = config->Read(wxT("bool1"), &bool1, true); |
5c8640d0 | 179 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 VZ |
180 | read++; |
181 | ||
f9bb777f | 182 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
58d1a316 | 183 | wxColour color1; |
3e1512cd | 184 | r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44)); |
5c8640d0 | 185 | CPPUNIT_ASSERT_EQUAL( has_values, r ); |
955e1ce8 | 186 | read++; |
f9bb777f | 187 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
955e1ce8 VZ |
188 | |
189 | return read; | |
58d1a316 | 190 | } |
3e1512cd | 191 | |
3e1512cd | 192 | |
58d1a316 VZ |
193 | void ConfigTestCase::RecordingDefaultsTest() |
194 | { | |
195 | wxString app = wxT("wxConfigTestCaseRD"); | |
196 | wxString vendor = wxT("wxWidgets"); | |
197 | wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""), | |
198 | wxCONFIG_USE_LOCAL_FILE); | |
199 | config->DeleteAll(); | |
200 | config->SetRecordDefaults(false); // by default it is false | |
201 | ReadValues(config, false); | |
5c8640d0 | 202 | CPPUNIT_ASSERT_EQUAL( 0, config->GetNumberOfEntries() ); |
58d1a316 | 203 | config->SetRecordDefaults(true); |
955e1ce8 VZ |
204 | int read = ReadValues(config, false); |
205 | CPPUNIT_ASSERT_EQUAL( read, config->GetNumberOfEntries() ); | |
58d1a316 | 206 | ReadValues(config, true); |
3e1512cd VZ |
207 | config->DeleteAll(); |
208 | delete config; | |
209 | } | |
210 | ||
211 |