]>
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(); | |
58d1a316 | 46 | void ReadValues(wxConfig *config, bool has_values); |
3e1512cd VZ |
47 | |
48 | DECLARE_NO_COPY_CLASS(ConfigTestCase) | |
49 | }; | |
50 | ||
51 | // register in the unnamed registry so that these tests are run by default | |
52 | CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase ); | |
53 | ||
54 | // also include in it's own registry so that these tests can be run alone | |
55 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" ); | |
56 | ||
57 | void ConfigTestCase::ReadWriteLocalTest() | |
58 | { | |
59 | wxString app = wxT("wxConfigTestCase"); | |
60 | wxString vendor = wxT("wxWidgets"); | |
61 | wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""), | |
62 | wxCONFIG_USE_LOCAL_FILE); | |
63 | config->DeleteAll(); | |
64 | config->Write(wxT("string1"), wxT("abc")); | |
65 | config->Write(wxT("string2"), wxString(wxT("def"))); | |
66 | config->Write(wxT("int1"), 123); | |
67 | config->Write(wxString(wxT("long1")), 234L); | |
68 | config->Write(wxT("double1"), 345.67); | |
69 | config->Write(wxT("bool1"), true); | |
f9bb777f | 70 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
3e1512cd | 71 | config->Write(wxT("color1"), wxColour(11,22,33,44)); |
f9bb777f | 72 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
3e1512cd VZ |
73 | config->Flush(); |
74 | delete config; | |
75 | ||
76 | config = new wxConfig(app, vendor, wxT(""), wxT(""), | |
77 | wxCONFIG_USE_LOCAL_FILE); | |
78 | wxString string1 = config->Read(wxT("string1")); | |
58d1a316 | 79 | CPPUNIT_ASSERT_EQUAL( string1, wxString(wxT("abc")) ); |
3e1512cd | 80 | string1 = config->Read(wxT("string1"), wxT("defaultvalue")); |
58d1a316 VZ |
81 | CPPUNIT_ASSERT_EQUAL( string1, wxString(wxT("abc")) ); |
82 | ||
3e1512cd VZ |
83 | wxString string2; |
84 | bool r = config->Read(wxT("string2"), &string2); | |
58d1a316 VZ |
85 | CPPUNIT_ASSERT( r ); |
86 | CPPUNIT_ASSERT_EQUAL( string2, wxString(wxT("def")) ); | |
87 | ||
3e1512cd | 88 | r = config->Read(wxT("string2"), &string2, wxT("defaultvalue")); |
58d1a316 VZ |
89 | CPPUNIT_ASSERT( r ); |
90 | CPPUNIT_ASSERT_EQUAL( string2, wxString(wxT("def")) ); | |
91 | ||
3e1512cd | 92 | int int1 = config->Read(wxT("int1"), 5); |
58d1a316 VZ |
93 | CPPUNIT_ASSERT_EQUAL( int1, 123 ); |
94 | ||
3e1512cd VZ |
95 | long long1; |
96 | r = config->Read(wxT("long1"), &long1); | |
58d1a316 VZ |
97 | CPPUNIT_ASSERT( r ); |
98 | CPPUNIT_ASSERT_EQUAL( long1, 234L ); | |
99 | ||
56601ff2 VZ |
100 | CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 ); |
101 | ||
102 | double double1; | |
103 | r = config->Read(wxT("double1"), &double1); | |
104 | CPPUNIT_ASSERT( r ); | |
105 | CPPUNIT_ASSERT_EQUAL( double1, 345.67 ); | |
106 | ||
107 | CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 ); | |
108 | ||
3e1512cd | 109 | bool bool1; |
56601ff2 | 110 | r = config->Read(wxT("foo"), &bool1); // there is no "foo" key |
58d1a316 VZ |
111 | CPPUNIT_ASSERT( !r ); |
112 | ||
3e1512cd | 113 | r = config->Read(wxT("bool1"), &bool1); |
58d1a316 VZ |
114 | CPPUNIT_ASSERT( r ); |
115 | CPPUNIT_ASSERT_EQUAL( bool1, true ); | |
116 | ||
56601ff2 VZ |
117 | CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 ); |
118 | ||
f9bb777f | 119 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
3e1512cd VZ |
120 | wxColour color1; |
121 | r = config->Read(wxT("color1"), &color1); | |
58d1a316 | 122 | CPPUNIT_ASSERT( r ); |
3e1512cd | 123 | CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) ); |
58d1a316 | 124 | |
56601ff2 | 125 | CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 ); |
f9bb777f | 126 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
56601ff2 | 127 | |
3e1512cd VZ |
128 | config->DeleteAll(); |
129 | delete config; | |
130 | } | |
131 | ||
58d1a316 | 132 | void ConfigTestCase::ReadValues(wxConfig *config, bool has_values) |
3e1512cd | 133 | { |
3e1512cd | 134 | bool r; |
58d1a316 VZ |
135 | wxString string1 = config->Read(wxT("string1"), wxT("abc")); |
136 | wxString string2 = config->Read(wxT("string2"), wxString(wxT("def"))); | |
137 | wxString string3, string4; | |
3e1512cd | 138 | r = config->Read(wxT("string3"), &string3, wxT("abc")); |
58d1a316 | 139 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
3e1512cd | 140 | r = config->Read(wxT("string4"), &string4, wxString(wxT("def"))); |
58d1a316 VZ |
141 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
142 | int int1; | |
3e1512cd | 143 | r = config->Read(wxT("int1"), &int1, 123); |
58d1a316 VZ |
144 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
145 | int int2 = config->Read(wxT("int2"), 1234); | |
146 | CPPUNIT_ASSERT_EQUAL( int2, 1234 ); | |
147 | long long1; | |
3e1512cd | 148 | r = config->Read(wxString(wxT("long1")), &long1, 234L); |
58d1a316 VZ |
149 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
150 | double double1; | |
3e1512cd | 151 | r = config->Read(wxT("double1"), &double1, 345.67); |
58d1a316 VZ |
152 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
153 | bool bool1; | |
3e1512cd | 154 | r = config->Read(wxT("bool1"), &bool1, true); |
58d1a316 | 155 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
f9bb777f | 156 | #ifdef wxHAS_CONFIG_TEMPLATE_RW |
58d1a316 | 157 | wxColour color1; |
3e1512cd | 158 | r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44)); |
58d1a316 | 159 | CPPUNIT_ASSERT_EQUAL( r, has_values ); |
f9bb777f | 160 | #endif // wxHAS_CONFIG_TEMPLATE_RW |
58d1a316 | 161 | } |
3e1512cd | 162 | |
3e1512cd | 163 | |
58d1a316 VZ |
164 | void ConfigTestCase::RecordingDefaultsTest() |
165 | { | |
166 | wxString app = wxT("wxConfigTestCaseRD"); | |
167 | wxString vendor = wxT("wxWidgets"); | |
168 | wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""), | |
169 | wxCONFIG_USE_LOCAL_FILE); | |
170 | config->DeleteAll(); | |
171 | config->SetRecordDefaults(false); // by default it is false | |
172 | ReadValues(config, false); | |
173 | CPPUNIT_ASSERT_EQUAL( (int) config->GetNumberOfEntries(), 0 ); | |
174 | config->SetRecordDefaults(true); | |
175 | ReadValues(config, false); | |
176 | CPPUNIT_ASSERT_EQUAL( (int) config->GetNumberOfEntries(), 10 ); | |
177 | ReadValues(config, true); | |
3e1512cd VZ |
178 | config->DeleteAll(); |
179 | delete config; | |
180 | } | |
181 | ||
182 |