]>
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();
46 void ReadValues(wxConfig
*config
, bool has_values
);
48 DECLARE_NO_COPY_CLASS(ConfigTestCase
)
51 // register in the unnamed registry so that these tests are run by default
52 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase
);
54 // also include in it's own registry so that these tests can be run alone
55 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase
, "ConfigTestCase" );
57 void ConfigTestCase::ReadWriteLocalTest()
59 wxString app
= wxT("wxConfigTestCase");
60 wxString vendor
= wxT("wxWidgets");
61 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
62 wxCONFIG_USE_LOCAL_FILE
);
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);
70 config
->Write(wxT("color1"), wxColour(11,22,33,44));
74 config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
75 wxCONFIG_USE_LOCAL_FILE
);
76 wxString string1
= config
->Read(wxT("string1"));
77 CPPUNIT_ASSERT_EQUAL( string1
, wxString(wxT("abc")) );
78 string1
= config
->Read(wxT("string1"), wxT("defaultvalue"));
79 CPPUNIT_ASSERT_EQUAL( string1
, wxString(wxT("abc")) );
82 bool r
= config
->Read(wxT("string2"), &string2
);
84 CPPUNIT_ASSERT_EQUAL( string2
, wxString(wxT("def")) );
86 r
= config
->Read(wxT("string2"), &string2
, wxT("defaultvalue"));
88 CPPUNIT_ASSERT_EQUAL( string2
, wxString(wxT("def")) );
90 int int1
= config
->Read(wxT("int1"), 5);
91 CPPUNIT_ASSERT_EQUAL( int1
, 123 );
94 r
= config
->Read(wxT("long1"), &long1
);
96 CPPUNIT_ASSERT_EQUAL( long1
, 234L );
99 r
= config
->Read(wxT("foo"), &bool1
);
100 CPPUNIT_ASSERT( !r
);
102 r
= config
->Read(wxT("bool1"), &bool1
);
104 CPPUNIT_ASSERT_EQUAL( bool1
, true );
107 r
= config
->Read(wxT("color1"), &color1
);
109 CPPUNIT_ASSERT( color1
== wxColour(11,22,33,44) );
115 void ConfigTestCase::ReadValues(wxConfig
*config
, bool has_values
)
118 wxString string1
= config
->Read(wxT("string1"), wxT("abc"));
119 wxString string2
= config
->Read(wxT("string2"), wxString(wxT("def")));
120 wxString string3
, string4
;
121 r
= config
->Read(wxT("string3"), &string3
, wxT("abc"));
122 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
123 r
= config
->Read(wxT("string4"), &string4
, wxString(wxT("def")));
124 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
126 r
= config
->Read(wxT("int1"), &int1
, 123);
127 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
128 int int2
= config
->Read(wxT("int2"), 1234);
129 CPPUNIT_ASSERT_EQUAL( int2
, 1234 );
131 r
= config
->Read(wxString(wxT("long1")), &long1
, 234L);
132 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
134 r
= config
->Read(wxT("double1"), &double1
, 345.67);
135 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
137 r
= config
->Read(wxT("bool1"), &bool1
, true);
138 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
140 r
= config
->Read(wxT("color1"), &color1
, wxColour(11,22,33,44));
141 CPPUNIT_ASSERT_EQUAL( r
, has_values
);
145 void ConfigTestCase::RecordingDefaultsTest()
147 wxString app
= wxT("wxConfigTestCaseRD");
148 wxString vendor
= wxT("wxWidgets");
149 wxConfig
*config
= new wxConfig(app
, vendor
, wxT(""), wxT(""),
150 wxCONFIG_USE_LOCAL_FILE
);
152 config
->SetRecordDefaults(false); // by default it is false
153 ReadValues(config
, false);
154 CPPUNIT_ASSERT_EQUAL( (int) config
->GetNumberOfEntries(), 0 );
155 config
->SetRecordDefaults(true);
156 ReadValues(config
, false);
157 CPPUNIT_ASSERT_EQUAL( (int) config
->GetNumberOfEntries(), 10 );
158 ReadValues(config
, true);