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