]>
git.saurik.com Git - wxWidgets.git/blob - tests/config/config.cpp
9a5eff85ff7c84498e49d2ab7c3405e633d9fe07
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 void ReadValues(wxConfig
*config
, bool has_values
);
49 DECLARE_NO_COPY_CLASS(ConfigTestCase
)
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase
);
55 // also include in it's own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase
, "ConfigTestCase" );
58 void ConfigTestCase::ReadWriteLocalTest()
60 wxString app
= wxT("wxConfigTestCase");
61 wxString vendor
= wxT("wxWidgets");
62 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
63 wxCONFIG_USE_LOCAL_FILE
);
65 config
->Write(wxT("string1"), wxT("abc"));
66 config
->Write(wxT("string2"), wxString(wxT("def")));
67 config
->Write(wxT("int1"), 123);
68 config
->Write(wxString(wxT("long1")), 234L);
69 config
->Write(wxT("double1"), 345.67);
70 config
->Write(wxT("bool1"), true);
71 #ifdef wxHAS_CONFIG_TEMPLATE_RW
72 config
->Write(wxT("color1"), wxColour(11,22,33,44));
73 #endif // wxHAS_CONFIG_TEMPLATE_RW
77 config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
78 wxCONFIG_USE_LOCAL_FILE
);
79 wxString string1
= config
->Read(wxT("string1"));
80 CPPUNIT_ASSERT_EQUAL( "abc", string1
);
81 string1
= config
->Read(wxT("string1"), wxT("defaultvalue"));
82 CPPUNIT_ASSERT_EQUAL( "abc", string1
);
85 bool r
= config
->Read(wxT("string2"), &string2
);
87 CPPUNIT_ASSERT_EQUAL( "def", string2
);
89 r
= config
->Read(wxT("string2"), &string2
, wxT("defaultvalue"));
91 CPPUNIT_ASSERT_EQUAL( "def", string2
);
93 int int1
= config
->Read(wxT("int1"), 5);
94 CPPUNIT_ASSERT_EQUAL( 123, int1
);
97 r
= config
->Read(wxT("long1"), &long1
);
99 CPPUNIT_ASSERT_EQUAL( 234L, long1
);
101 CPPUNIT_ASSERT( config
->ReadLong(wxT("long1"), 0) == 234 );
104 r
= config
->Read(wxT("double1"), &double1
);
106 CPPUNIT_ASSERT_EQUAL( 345.67, double1
);
108 CPPUNIT_ASSERT( config
->ReadDouble(wxT("double1"), 0) == double1
);
111 r
= config
->Read(wxT("foo"), &bool1
); // there is no "foo" key
112 CPPUNIT_ASSERT( !r
);
114 r
= config
->Read(wxT("bool1"), &bool1
);
116 CPPUNIT_ASSERT_EQUAL( true, bool1
);
118 CPPUNIT_ASSERT( config
->ReadBool(wxT("bool1"), false) == bool1
);
120 #ifdef wxHAS_CONFIG_TEMPLATE_RW
122 r
= config
->Read(wxT("color1"), &color1
);
124 CPPUNIT_ASSERT( color1
== wxColour(11,22,33,44) );
126 CPPUNIT_ASSERT( config
->ReadObject(wxT("color1"), wxNullColour
) == color1
);
127 #endif // wxHAS_CONFIG_TEMPLATE_RW
133 void ConfigTestCase::ReadValues(wxConfig
*config
, bool has_values
)
136 wxString string1
= config
->Read(wxT("string1"), wxT("abc"));
137 wxString string2
= config
->Read(wxT("string2"), wxString(wxT("def")));
138 wxString string3
, string4
;
139 r
= config
->Read(wxT("string3"), &string3
, wxT("abc"));
140 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
141 r
= config
->Read(wxT("string4"), &string4
, wxString(wxT("def")));
142 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
144 r
= config
->Read(wxT("int1"), &int1
, 123);
145 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
146 int int2
= config
->Read(wxT("int2"), 1234);
147 CPPUNIT_ASSERT_EQUAL( int2
, 1234 );
149 r
= config
->Read(wxString(wxT("long1")), &long1
, 234L);
150 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
152 r
= config
->Read(wxT("double1"), &double1
, 345.67);
153 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
155 r
= config
->Read(wxT("bool1"), &bool1
, true);
156 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
157 #ifdef wxHAS_CONFIG_TEMPLATE_RW
159 r
= config
->Read(wxT("color1"), &color1
, wxColour(11,22,33,44));
160 CPPUNIT_ASSERT_EQUAL( has_values
, r
);
161 #endif // wxHAS_CONFIG_TEMPLATE_RW
165 void ConfigTestCase::RecordingDefaultsTest()
167 wxString app
= wxT("wxConfigTestCaseRD");
168 wxString vendor
= wxT("wxWidgets");
169 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
170 wxCONFIG_USE_LOCAL_FILE
);
172 config
->SetRecordDefaults(false); // by default it is false
173 ReadValues(config
, false);
174 CPPUNIT_ASSERT_EQUAL( 0, config
->GetNumberOfEntries() );
175 config
->SetRecordDefaults(true);
176 ReadValues(config
, false);
177 CPPUNIT_ASSERT_EQUAL( 10, config
->GetNumberOfEntries() );
178 ReadValues(config
, true);