]>
git.saurik.com Git - wxWidgets.git/blob - tests/config/config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/config/config.cpp
3 // Purpose: wxConfig unit test
4 // Author: Marcin Wojdyr
7 // Copyright: (c) 2007 Marcin Wojdyr
8 ///////////////////////////////////////////////////////////////////////////////
10 // see also tests/fileconf/fileconftest.cpp for wxFileConfig specific tests
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
26 #include "wx/config.h"
27 #include "wx/colour.h"
29 // --------------------------------------------------------------------------
31 // --------------------------------------------------------------------------
33 class ConfigTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( ConfigTestCase
);
40 CPPUNIT_TEST( ReadWriteLocalTest
);
41 CPPUNIT_TEST( RecordingDefaultsTest
);
42 CPPUNIT_TEST_SUITE_END();
44 void ReadWriteLocalTest();
45 void RecordingDefaultsTest();
47 DECLARE_NO_COPY_CLASS(ConfigTestCase
)
50 // register in the unnamed registry so that these tests are run by default
51 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase
);
53 // also include in it's own registry so that these tests can be run alone
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase
, "ConfigTestCase" );
56 void ConfigTestCase::ReadWriteLocalTest()
58 wxString app
= wxT("wxConfigTestCase");
59 wxString vendor
= wxT("wxWidgets");
60 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
61 wxCONFIG_USE_LOCAL_FILE
);
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));
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") );
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 );
89 r
= config
->Read(wxT("long1"), &long1
);
90 CPPUNIT_ASSERT( r
== true );
91 CPPUNIT_ASSERT( long1
== 234 );
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 );
99 r
= config
->Read(wxT("color1"), &color1
);
100 CPPUNIT_ASSERT( r
== true );
101 CPPUNIT_ASSERT( color1
== wxColour(11,22,33,44) );
106 void ConfigTestCase::RecordingDefaultsTest()
108 wxString app
= wxT("wxConfigTestCaseRD");
109 wxString vendor
= wxT("wxWidgets");
110 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
111 wxCONFIG_USE_LOCAL_FILE
);
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")));
120 config
->Read(wxT("int1"), &int1
, 123);
121 int2
= config
->Read(wxT("int2"), 1234);
123 config
->Read(wxString(wxT("long1")), &long1
, 234L);
125 config
->Read(wxT("double1"), &double1
, 345.67);
127 config
->Read(wxT("bool1"), &bool1
, true);
129 config
->Read(wxT("color1"), &color1
, wxColour(11,22,33,44));
131 CPPUNIT_ASSERT ( config
->GetNumberOfEntries() == 0 );
133 config
->SetRecordDefaults(true);
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 );
154 CPPUNIT_ASSERT ( config
->GetNumberOfEntries() == 10 );
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 );