2007-07-21 19:47:22 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/config/config.cpp
|
|
|
|
// Purpose: wxConfig unit test
|
|
|
|
// Author: Marcin Wojdyr
|
|
|
|
// Created: 2007-07-07
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2007 Marcin Wojdyr
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-02 10:12:07 -04:00
|
|
|
// NOTE: this test is compiled in test_gui because it uses wxColour for
|
|
|
|
// its testing purpose.
|
|
|
|
// See also tests/config/fileconf.cpp for wxFileConfig specific tests and
|
|
|
|
// tests/config/regconf.cpp for wxRegConfig specific tests.
|
2007-07-21 19:47:22 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
2010-08-22 18:16:05 -04:00
|
|
|
#if wxUSE_CONFIG
|
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/config.h"
|
|
|
|
#include "wx/colour.h"
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class ConfigTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConfigTestCase() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( ConfigTestCase );
|
|
|
|
CPPUNIT_TEST( ReadWriteLocalTest );
|
|
|
|
CPPUNIT_TEST( RecordingDefaultsTest );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void ReadWriteLocalTest();
|
|
|
|
void RecordingDefaultsTest();
|
2009-03-25 09:40:22 -04:00
|
|
|
|
2009-03-25 09:43:31 -04:00
|
|
|
// return the number of values we (attempted to) read
|
|
|
|
int ReadValues(wxConfig *config, bool has_values);
|
2007-07-21 19:47:22 -04:00
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(ConfigTestCase)
|
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase );
|
|
|
|
|
|
|
|
// also include in it's own registry so that these tests can be run alone
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" );
|
|
|
|
|
|
|
|
void ConfigTestCase::ReadWriteLocalTest()
|
|
|
|
{
|
|
|
|
wxString app = wxT("wxConfigTestCase");
|
|
|
|
wxString vendor = wxT("wxWidgets");
|
|
|
|
wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
|
|
|
|
wxCONFIG_USE_LOCAL_FILE);
|
|
|
|
config->DeleteAll();
|
|
|
|
config->Write(wxT("string1"), wxT("abc"));
|
|
|
|
config->Write(wxT("string2"), wxString(wxT("def")));
|
|
|
|
config->Write(wxT("int1"), 123);
|
|
|
|
config->Write(wxString(wxT("long1")), 234L);
|
|
|
|
config->Write(wxT("double1"), 345.67);
|
|
|
|
config->Write(wxT("bool1"), true);
|
2008-11-03 08:36:08 -05:00
|
|
|
#ifdef wxHAS_CONFIG_TEMPLATE_RW
|
2007-07-21 19:47:22 -04:00
|
|
|
config->Write(wxT("color1"), wxColour(11,22,33,44));
|
2008-11-03 08:36:08 -05:00
|
|
|
#endif // wxHAS_CONFIG_TEMPLATE_RW
|
2007-07-21 19:47:22 -04:00
|
|
|
config->Flush();
|
|
|
|
delete config;
|
|
|
|
|
|
|
|
config = new wxConfig(app, vendor, wxT(""), wxT(""),
|
|
|
|
wxCONFIG_USE_LOCAL_FILE);
|
|
|
|
wxString string1 = config->Read(wxT("string1"));
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( "abc", string1 );
|
2007-07-21 19:47:22 -04:00
|
|
|
string1 = config->Read(wxT("string1"), wxT("defaultvalue"));
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( "abc", string1 );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
wxString string2;
|
|
|
|
bool r = config->Read(wxT("string2"), &string2);
|
2007-07-28 07:54:30 -04:00
|
|
|
CPPUNIT_ASSERT( r );
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( "def", string2 );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("string2"), &string2, wxT("defaultvalue"));
|
2007-07-28 07:54:30 -04:00
|
|
|
CPPUNIT_ASSERT( r );
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( "def", string2 );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
int int1 = config->Read(wxT("int1"), 5);
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 123, int1 );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
long long1;
|
|
|
|
r = config->Read(wxT("long1"), &long1);
|
2007-07-28 07:54:30 -04:00
|
|
|
CPPUNIT_ASSERT( r );
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 234L, long1 );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-08-15 16:23:01 -04:00
|
|
|
CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 );
|
|
|
|
|
|
|
|
double double1;
|
|
|
|
r = config->Read(wxT("double1"), &double1);
|
|
|
|
CPPUNIT_ASSERT( r );
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 345.67, double1 );
|
2007-08-15 16:23:01 -04:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 );
|
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
bool bool1;
|
2007-08-15 16:23:01 -04:00
|
|
|
r = config->Read(wxT("foo"), &bool1); // there is no "foo" key
|
2007-07-28 07:54:30 -04:00
|
|
|
CPPUNIT_ASSERT( !r );
|
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("bool1"), &bool1);
|
2007-07-28 07:54:30 -04:00
|
|
|
CPPUNIT_ASSERT( r );
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( true, bool1 );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-08-15 16:23:01 -04:00
|
|
|
CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 );
|
|
|
|
|
2008-11-03 08:36:08 -05:00
|
|
|
#ifdef wxHAS_CONFIG_TEMPLATE_RW
|
2007-07-21 19:47:22 -04:00
|
|
|
wxColour color1;
|
|
|
|
r = config->Read(wxT("color1"), &color1);
|
2007-07-28 07:54:30 -04:00
|
|
|
CPPUNIT_ASSERT( r );
|
2007-07-21 19:47:22 -04:00
|
|
|
CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) );
|
2007-07-28 07:54:30 -04:00
|
|
|
|
2007-08-15 16:23:01 -04:00
|
|
|
CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 );
|
2008-11-03 08:36:08 -05:00
|
|
|
#endif // wxHAS_CONFIG_TEMPLATE_RW
|
2007-08-15 16:23:01 -04:00
|
|
|
|
2007-07-21 19:47:22 -04:00
|
|
|
config->DeleteAll();
|
|
|
|
delete config;
|
|
|
|
}
|
|
|
|
|
2009-03-25 09:43:31 -04:00
|
|
|
int ConfigTestCase::ReadValues(wxConfig *config, bool has_values)
|
2007-07-21 19:47:22 -04:00
|
|
|
{
|
2009-03-25 09:43:31 -04:00
|
|
|
int read = 0;
|
2007-07-21 19:47:22 -04:00
|
|
|
bool r;
|
2009-03-25 09:43:31 -04:00
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
wxString string1 = config->Read(wxT("string1"), wxT("abc"));
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
wxString string2 = config->Read(wxT("string2"), wxString(wxT("def")));
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
|
|
|
wxString string3;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("string3"), &string3, wxT("abc"));
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
|
|
|
wxString string4;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("string4"), &string4, wxString(wxT("def")));
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
int int1;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("int1"), &int1, 123);
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
int int2 = config->Read(wxT("int2"), 1234);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( int2, 1234 );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
long long1;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxString(wxT("long1")), &long1, 234L);
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
double double1;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("double1"), &double1, 345.67);
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
bool bool1;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("bool1"), &bool1, true);
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
|
|
|
|
2008-11-03 08:36:08 -05:00
|
|
|
#ifdef wxHAS_CONFIG_TEMPLATE_RW
|
2007-07-28 07:54:30 -04:00
|
|
|
wxColour color1;
|
2007-07-21 19:47:22 -04:00
|
|
|
r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( has_values, r );
|
2009-03-25 09:43:31 -04:00
|
|
|
read++;
|
2008-11-03 08:36:08 -05:00
|
|
|
#endif // wxHAS_CONFIG_TEMPLATE_RW
|
2009-03-25 09:43:31 -04:00
|
|
|
|
|
|
|
return read;
|
2007-07-28 07:54:30 -04:00
|
|
|
}
|
2007-07-21 19:47:22 -04:00
|
|
|
|
|
|
|
|
2007-07-28 07:54:30 -04:00
|
|
|
void ConfigTestCase::RecordingDefaultsTest()
|
|
|
|
{
|
|
|
|
wxString app = wxT("wxConfigTestCaseRD");
|
|
|
|
wxString vendor = wxT("wxWidgets");
|
|
|
|
wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
|
|
|
|
wxCONFIG_USE_LOCAL_FILE);
|
|
|
|
config->DeleteAll();
|
|
|
|
config->SetRecordDefaults(false); // by default it is false
|
|
|
|
ReadValues(config, false);
|
2009-03-25 09:40:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, config->GetNumberOfEntries() );
|
2007-07-28 07:54:30 -04:00
|
|
|
config->SetRecordDefaults(true);
|
2009-03-25 09:43:31 -04:00
|
|
|
int read = ReadValues(config, false);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( read, config->GetNumberOfEntries() );
|
2007-07-28 07:54:30 -04:00
|
|
|
ReadValues(config, true);
|
2007-07-21 19:47:22 -04:00
|
|
|
config->DeleteAll();
|
|
|
|
delete config;
|
|
|
|
}
|
|
|
|
|
2010-08-22 18:16:05 -04:00
|
|
|
#endif //wxUSE_CONFIG
|