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