correct order of parameters in CPPUNIT_ASSERT_EQUAL
[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 void ReadValues(wxConfig *config, bool has_values);
48
49 DECLARE_NO_COPY_CLASS(ConfigTestCase)
50 };
51
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase );
54
55 // also include in it's own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" );
57
58 void ConfigTestCase::ReadWriteLocalTest()
59 {
60 wxString app = wxT("wxConfigTestCase");
61 wxString vendor = wxT("wxWidgets");
62 wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
63 wxCONFIG_USE_LOCAL_FILE);
64 config->DeleteAll();
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
74 config->Flush();
75 delete config;
76
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 );
83
84 wxString string2;
85 bool r = config->Read(wxT("string2"), &string2);
86 CPPUNIT_ASSERT( r );
87 CPPUNIT_ASSERT_EQUAL( "def", string2 );
88
89 r = config->Read(wxT("string2"), &string2, wxT("defaultvalue"));
90 CPPUNIT_ASSERT( r );
91 CPPUNIT_ASSERT_EQUAL( "def", string2 );
92
93 int int1 = config->Read(wxT("int1"), 5);
94 CPPUNIT_ASSERT_EQUAL( 123, int1 );
95
96 long long1;
97 r = config->Read(wxT("long1"), &long1);
98 CPPUNIT_ASSERT( r );
99 CPPUNIT_ASSERT_EQUAL( 234L, long1 );
100
101 CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 );
102
103 double double1;
104 r = config->Read(wxT("double1"), &double1);
105 CPPUNIT_ASSERT( r );
106 CPPUNIT_ASSERT_EQUAL( 345.67, double1 );
107
108 CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 );
109
110 bool bool1;
111 r = config->Read(wxT("foo"), &bool1); // there is no "foo" key
112 CPPUNIT_ASSERT( !r );
113
114 r = config->Read(wxT("bool1"), &bool1);
115 CPPUNIT_ASSERT( r );
116 CPPUNIT_ASSERT_EQUAL( true, bool1 );
117
118 CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 );
119
120 #ifdef wxHAS_CONFIG_TEMPLATE_RW
121 wxColour color1;
122 r = config->Read(wxT("color1"), &color1);
123 CPPUNIT_ASSERT( r );
124 CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) );
125
126 CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 );
127 #endif // wxHAS_CONFIG_TEMPLATE_RW
128
129 config->DeleteAll();
130 delete config;
131 }
132
133 void ConfigTestCase::ReadValues(wxConfig *config, bool has_values)
134 {
135 bool r;
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 );
143 int int1;
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 );
148 long long1;
149 r = config->Read(wxString(wxT("long1")), &long1, 234L);
150 CPPUNIT_ASSERT_EQUAL( has_values, r );
151 double double1;
152 r = config->Read(wxT("double1"), &double1, 345.67);
153 CPPUNIT_ASSERT_EQUAL( has_values, r );
154 bool bool1;
155 r = config->Read(wxT("bool1"), &bool1, true);
156 CPPUNIT_ASSERT_EQUAL( has_values, r );
157 #ifdef wxHAS_CONFIG_TEMPLATE_RW
158 wxColour color1;
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
162 }
163
164
165 void ConfigTestCase::RecordingDefaultsTest()
166 {
167 wxString app = wxT("wxConfigTestCaseRD");
168 wxString vendor = wxT("wxWidgets");
169 wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
170 wxCONFIG_USE_LOCAL_FILE);
171 config->DeleteAll();
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);
179 config->DeleteAll();
180 delete config;
181 }
182
183