initial commit
Signed-off-by: Peter Siegmund <mars3142@noreply.mars3142.dev>
This commit is contained in:
215
libs/wxWidgets-3.3.1/tests/config/config.cpp
Normal file
215
libs/wxWidgets-3.3.1/tests/config/config.cpp
Normal file
@@ -0,0 +1,215 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/config/config.cpp
|
||||
// Purpose: wxConfig unit test
|
||||
// Author: Marcin Wojdyr
|
||||
// Created: 2007-07-07
|
||||
// Copyright: (c) 2007 Marcin Wojdyr
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// NOTE: this test is compiled both as part of the non-GUI test and test_gui
|
||||
// because it can only use wxColour in the latter.
|
||||
//
|
||||
// See also tests/config/fileconf.cpp for wxFileConfig specific tests and
|
||||
// tests/config/regconf.cpp for wxRegConfig specific tests.
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
#if wxUSE_CONFIG
|
||||
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/wx.h"
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "wx/config.h"
|
||||
|
||||
// Tests using wxColour can only be done when using GUI library and they
|
||||
// require template functions that are not supported by some ancient compilers.
|
||||
#if wxUSE_GUI && defined(wxHAS_CONFIG_TEMPLATE_RW)
|
||||
#define TEST_WXCOLOUR
|
||||
#endif
|
||||
|
||||
#ifdef TEST_WXCOLOUR
|
||||
#include "wx/colour.h"
|
||||
#endif
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// the tests
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("wxConfig::ReadWriteLocal", "[config]")
|
||||
{
|
||||
wxString app = "wxConfigTestCase";
|
||||
wxString vendor = "wxWidgets";
|
||||
|
||||
// See comment in regconf.cpp.
|
||||
const wxLongLong_t val64 = wxLL(0x8000000000000008);
|
||||
const wxULongLong_t uval64 = wxULL(0x9000000000000009);
|
||||
|
||||
{
|
||||
wxConfig config(app, vendor, "", "", wxCONFIG_USE_LOCAL_FILE);
|
||||
config.DeleteAll();
|
||||
config.Write("string1", "abc");
|
||||
config.Write("string2", wxString("def"));
|
||||
config.Write("int1", 123);
|
||||
config.Write(wxString("long1"), 234L);
|
||||
config.Write("double1", 345.67);
|
||||
config.Write("bool1", true);
|
||||
|
||||
config.Write("ll", val64);
|
||||
|
||||
config.Write("ull", uval64);
|
||||
|
||||
config.Write("size", size_t(UINT_MAX));
|
||||
#ifdef TEST_WXCOLOUR
|
||||
config.Write("color1", wxColour(11,22,33,44));
|
||||
#endif // TEST_WXCOLOUR
|
||||
config.Flush();
|
||||
}
|
||||
|
||||
wxConfig config(app, vendor, "", "", wxCONFIG_USE_LOCAL_FILE);
|
||||
wxString string1 = config.Read("string1");
|
||||
CHECK( string1 == "abc" );
|
||||
string1 = config.Read("string1", "defaultvalue");
|
||||
CHECK( string1 == "abc" );
|
||||
|
||||
wxString string2;
|
||||
bool r = config.Read("string2", &string2);
|
||||
CHECK( r );
|
||||
CHECK( string2 == "def" );
|
||||
|
||||
r = config.Read("string2", &string2, "defaultvalue");
|
||||
CHECK( r );
|
||||
CHECK( string2 == "def" );
|
||||
|
||||
int int1 = config.Read("int1", 5);
|
||||
CHECK( int1 == 123 );
|
||||
|
||||
long long1;
|
||||
r = config.Read("long1", &long1);
|
||||
CHECK( r );
|
||||
CHECK( long1 == 234L );
|
||||
|
||||
CHECK( config.ReadLong("long1", 0) == 234 );
|
||||
|
||||
double double1;
|
||||
r = config.Read("double1", &double1);
|
||||
CHECK( r );
|
||||
CHECK( double1 == 345.67 );
|
||||
|
||||
CHECK( config.ReadDouble("double1", 0) == double1 );
|
||||
|
||||
bool bool1;
|
||||
r = config.Read("foo", &bool1); // there is no "foo" key
|
||||
CHECK( !r );
|
||||
|
||||
r = config.Read("bool1", &bool1);
|
||||
CHECK( r );
|
||||
CHECK( bool1 == true );
|
||||
|
||||
CHECK( config.ReadBool("bool1", false) == bool1 );
|
||||
|
||||
wxLongLong_t ll;
|
||||
CHECK( config.Read("ll", &ll) );
|
||||
CHECK( ll == val64 );
|
||||
CHECK( config.ReadLongLong("ll", 0) == val64 );
|
||||
|
||||
CHECK( config.Read("ull", &ll) );
|
||||
CHECK( ll == static_cast<wxLongLong_t>(uval64) );
|
||||
CHECK( config.ReadLongLong("ull", 0) == static_cast<wxLongLong_t>(uval64) );
|
||||
|
||||
size_t size;
|
||||
CHECK( config.Read("size", &size) );
|
||||
CHECK( size == UINT_MAX );
|
||||
|
||||
#ifdef TEST_WXCOLOUR
|
||||
wxColour color1;
|
||||
r = config.Read("color1", &color1);
|
||||
CHECK( r );
|
||||
CHECK( color1 == wxColour(11,22,33,44) );
|
||||
|
||||
CHECK( config.ReadObject("color1", wxNullColour) == color1 );
|
||||
#endif // TEST_WXCOLOUR
|
||||
|
||||
config.DeleteAll();
|
||||
}
|
||||
|
||||
// Helper of RecordingDefaultsTest() test.
|
||||
static
|
||||
size_t ReadValues(const wxConfig& config, bool has_values)
|
||||
{
|
||||
size_t read = 0;
|
||||
bool r;
|
||||
|
||||
config.Read("string1", "abc");
|
||||
read++;
|
||||
|
||||
config.Read("string2", wxString("def"));
|
||||
read++;
|
||||
|
||||
wxString string3;
|
||||
r = config.Read("string3", &string3, "abc");
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
|
||||
wxString string4;
|
||||
r = config.Read("string4", &string4, wxString("def"));
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
|
||||
int int1;
|
||||
r = config.Read("int1", &int1, 123);
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
|
||||
int int2 = config.Read("int2", 1234);
|
||||
CHECK( 1234 == int2 );
|
||||
read++;
|
||||
|
||||
long long1;
|
||||
r = config.Read(wxString("long1"), &long1, 234L);
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
|
||||
double double1;
|
||||
r = config.Read("double1", &double1, 345.67);
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
|
||||
bool bool1;
|
||||
r = config.Read("bool1", &bool1, true);
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
|
||||
#ifdef TEST_WXCOLOUR
|
||||
wxColour color1;
|
||||
r = config.Read("color1", &color1, wxColour(11,22,33,44));
|
||||
CHECK( r == has_values );
|
||||
read++;
|
||||
#endif // TEST_WXCOLOUR
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("wxConfig::RecordingDefaults", "[config]")
|
||||
{
|
||||
wxString app = "wxConfigTestCaseRD";
|
||||
wxString vendor = "wxWidgets";
|
||||
wxConfig config(app, vendor, "", "", wxCONFIG_USE_LOCAL_FILE);
|
||||
config.DeleteAll();
|
||||
config.SetRecordDefaults(false); // by default it is false
|
||||
ReadValues(config, false);
|
||||
CHECK( config.GetNumberOfEntries() == 0 );
|
||||
config.SetRecordDefaults(true);
|
||||
size_t read = ReadValues(config, false);
|
||||
CHECK( config.GetNumberOfEntries() == read );
|
||||
ReadValues(config, true);
|
||||
config.DeleteAll();
|
||||
}
|
||||
|
||||
#endif //wxUSE_CONFIG
|
||||
664
libs/wxWidgets-3.3.1/tests/config/fileconf.cpp
Normal file
664
libs/wxWidgets-3.3.1/tests/config/fileconf.cpp
Normal file
@@ -0,0 +1,664 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/fileconf/fileconf.cpp
|
||||
// Purpose: wxFileConf unit test
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2004-09-19
|
||||
// Copyright: (c) 2004 Vadim Zeitlin
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#if wxUSE_FILECONFIG
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "wx/fileconf.h"
|
||||
#include "wx/sstream.h"
|
||||
#include "wx/log.h"
|
||||
|
||||
#include "testlog.h"
|
||||
|
||||
static const char *testconfig =
|
||||
"[root]\n"
|
||||
"entry=value\n"
|
||||
"[root/group1]\n"
|
||||
"[root/group1/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[root/group2]\n"
|
||||
;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// local functions
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static wxString Dump(wxFileConfig& fc)
|
||||
{
|
||||
wxStringOutputStream sos;
|
||||
fc.Save(sos);
|
||||
return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
|
||||
}
|
||||
|
||||
// helper macro to test wxFileConfig contents
|
||||
#define wxVERIFY_FILECONFIG(t, fc) CHECK(Dump(fc) == t)
|
||||
|
||||
static wxString ChangePath(wxFileConfig& fc, const char *path)
|
||||
{
|
||||
fc.SetPath(path);
|
||||
|
||||
return fc.GetPath();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test class
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("wxFileConfig::Path", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( ChangePath(fc, "") == "" );
|
||||
CHECK( ChangePath(fc, "/") == "" );
|
||||
CHECK( ChangePath(fc, "root") == "/root" );
|
||||
CHECK( ChangePath(fc, "/root") == "/root" );
|
||||
CHECK( ChangePath(fc, "/root/group1/subgroup") == "/root/group1/subgroup" );
|
||||
CHECK( ChangePath(fc, "/root/group2") == "/root/group2" );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::AddEntries", "[fileconfig][config]")
|
||||
{
|
||||
wxFileConfig fc;
|
||||
|
||||
wxVERIFY_FILECONFIG( "", fc );
|
||||
|
||||
fc.Write("/Foo", "foo");
|
||||
wxVERIFY_FILECONFIG( "Foo=foo\n", fc );
|
||||
|
||||
fc.Write("/Bar/Baz", "baz");
|
||||
wxVERIFY_FILECONFIG( "Foo=foo\n[Bar]\nBaz=baz\n", fc );
|
||||
|
||||
fc.DeleteAll();
|
||||
wxVERIFY_FILECONFIG( "", fc );
|
||||
|
||||
fc.Write("/Bar/Baz", "baz");
|
||||
wxVERIFY_FILECONFIG( "[Bar]\nBaz=baz\n", fc );
|
||||
|
||||
fc.Write("/Foo", "foo");
|
||||
wxVERIFY_FILECONFIG( "Foo=foo\n[Bar]\nBaz=baz\n", fc );
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void
|
||||
CheckGroupEntries(const wxFileConfig& fc,
|
||||
const char *path,
|
||||
size_t nEntries,
|
||||
...)
|
||||
{
|
||||
wxConfigPathChanger change(&fc, wxString(path) + "/");
|
||||
|
||||
CHECK( fc.GetNumberOfEntries() == nEntries );
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, nEntries);
|
||||
|
||||
long cookie;
|
||||
wxString name;
|
||||
for ( bool cont = fc.GetFirstEntry(name, cookie);
|
||||
cont;
|
||||
cont = fc.GetNextEntry(name, cookie), nEntries-- )
|
||||
{
|
||||
CHECK( name == va_arg(ap, char *) );
|
||||
}
|
||||
|
||||
CHECK( nEntries == 0 );
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
CheckGroupSubgroups(const wxFileConfig& fc,
|
||||
const char *path,
|
||||
size_t nGroups,
|
||||
...)
|
||||
{
|
||||
wxConfigPathChanger change(&fc, wxString(path) + "/");
|
||||
|
||||
CHECK( fc.GetNumberOfGroups() == nGroups );
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, nGroups);
|
||||
|
||||
long cookie;
|
||||
wxString name;
|
||||
for ( bool cont = fc.GetFirstGroup(name, cookie);
|
||||
cont;
|
||||
cont = fc.GetNextGroup(name, cookie), nGroups-- )
|
||||
{
|
||||
CHECK( name == va_arg(ap, char *) );
|
||||
}
|
||||
|
||||
CHECK( nGroups == 0 );
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
TEST_CASE("wxFileConfig::GetEntries", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CheckGroupEntries(fc, "", 0);
|
||||
CheckGroupEntries(fc, "/root", 1, "entry");
|
||||
CheckGroupEntries(fc, "/root/group1", 0);
|
||||
CheckGroupEntries(fc, "/root/group1/subgroup",
|
||||
2, "subentry", "subentry2");
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::GetGroups", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CheckGroupSubgroups(fc, "", 1, "root");
|
||||
CheckGroupSubgroups(fc, "/root", 2, "group1", "group2");
|
||||
CheckGroupSubgroups(fc, "/root/group1", 1, "subgroup");
|
||||
CheckGroupSubgroups(fc, "/root/group2", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::HasEntry", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( !fc.HasEntry("root") );
|
||||
CHECK( fc.HasEntry("root/entry") );
|
||||
CHECK( fc.HasEntry("/root/entry") );
|
||||
CHECK( fc.HasEntry("root/group1/subgroup/subentry") );
|
||||
CHECK( !fc.HasEntry("") );
|
||||
CHECK( !fc.HasEntry("root/group1") );
|
||||
CHECK( !fc.HasEntry("subgroup/subentry") );
|
||||
CHECK( !fc.HasEntry("/root/no_such_group/entry") );
|
||||
CHECK( !fc.HasGroup("/root/no_such_group") );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::HasGroup", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( fc.HasGroup("root") );
|
||||
CHECK( fc.HasGroup("root/group1") );
|
||||
CHECK( fc.HasGroup("root/group1/subgroup") );
|
||||
CHECK( fc.HasGroup("root/group2") );
|
||||
CHECK( !fc.HasGroup("") );
|
||||
CHECK( !fc.HasGroup("root/group") );
|
||||
CHECK( !fc.HasGroup("root//subgroup") );
|
||||
CHECK( !fc.HasGroup("foot/subgroup") );
|
||||
CHECK( !fc.HasGroup("foot") );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::Binary", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(
|
||||
"[root]\n"
|
||||
"binary=Zm9vCg==\n"
|
||||
);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
wxMemoryBuffer buf;
|
||||
fc.Read("/root/binary", &buf);
|
||||
|
||||
CHECK( memcmp("foo\n", buf.GetData(), buf.GetDataLen()) == 0 );
|
||||
|
||||
buf.SetDataLen(0);
|
||||
buf.AppendData("\0\1\2", 3);
|
||||
fc.Write("/root/012", buf);
|
||||
wxVERIFY_FILECONFIG(
|
||||
"[root]\n"
|
||||
"binary=Zm9vCg==\n"
|
||||
"012=AAEC\n",
|
||||
fc
|
||||
);
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::Save", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
wxVERIFY_FILECONFIG( testconfig, fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteEntry", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( !fc.DeleteEntry("foo") );
|
||||
|
||||
CHECK( fc.DeleteEntry("root/group1/subgroup/subentry") );
|
||||
wxVERIFY_FILECONFIG( "[root]\n"
|
||||
"entry=value\n"
|
||||
"[root/group1]\n"
|
||||
"[root/group1/subgroup]\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[root/group2]\n",
|
||||
fc );
|
||||
|
||||
// group should be deleted now as well as it became empty
|
||||
wxConfigPathChanger change(&fc, "root/group1/subgroup/subentry2");
|
||||
CHECK( fc.DeleteEntry("subentry2") );
|
||||
wxVERIFY_FILECONFIG( "[root]\n"
|
||||
"entry=value\n"
|
||||
"[root/group1]\n"
|
||||
"[root/group2]\n",
|
||||
fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteAndWriteEntry", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(
|
||||
"[root/group1]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"subentry3=subvalue3\n"
|
||||
);
|
||||
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
fc.DeleteEntry("/root/group1/subentry2");
|
||||
fc.Write("/root/group1/subentry2", "testvalue");
|
||||
fc.DeleteEntry("/root/group2/subentry2");
|
||||
fc.Write("/root/group2/subentry2", "testvalue2");
|
||||
fc.DeleteEntry("/root/group1/subentry2");
|
||||
fc.Write("/root/group1/subentry2", "testvalue");
|
||||
fc.DeleteEntry("/root/group2/subentry2");
|
||||
fc.Write("/root/group2/subentry2", "testvalue2");
|
||||
|
||||
wxVERIFY_FILECONFIG( "[root/group1]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry3=subvalue3\n"
|
||||
"subentry2=testvalue\n"
|
||||
"[root/group2]\n"
|
||||
"subentry2=testvalue2\n",
|
||||
fc );
|
||||
|
||||
fc.DeleteEntry("/root/group2/subentry2");
|
||||
wxVERIFY_FILECONFIG( "[root/group1]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry3=subvalue3\n"
|
||||
"subentry2=testvalue\n",
|
||||
fc );
|
||||
|
||||
fc.DeleteEntry("/root/group1/subentry2");
|
||||
fc.DeleteEntry("/root/group1/subentry");
|
||||
fc.DeleteEntry("/root/group1/subentry3");
|
||||
wxVERIFY_FILECONFIG( "", fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteLastRootEntry", "[fileconfig][config]")
|
||||
{
|
||||
// This tests for the bug which occurred when the last entry of the root
|
||||
// group was deleted: this corrupted internal state and resulted in a crash
|
||||
// after trying to write the just deleted entry again.
|
||||
wxStringInputStream sis("");
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
fc.Write("key", "value");
|
||||
wxVERIFY_FILECONFIG( "key=value\n", fc );
|
||||
|
||||
fc.DeleteEntry("key");
|
||||
wxVERIFY_FILECONFIG( "", fc );
|
||||
|
||||
fc.Write("key", "value");
|
||||
wxVERIFY_FILECONFIG( "key=value\n", fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteGroup", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( !fc.DeleteGroup("foo") );
|
||||
|
||||
CHECK( fc.DeleteGroup("root/group1") );
|
||||
wxVERIFY_FILECONFIG( "[root]\n"
|
||||
"entry=value\n"
|
||||
"[root/group2]\n",
|
||||
fc );
|
||||
|
||||
// notice trailing slash: it should be ignored
|
||||
CHECK( fc.DeleteGroup("root/group2/") );
|
||||
wxVERIFY_FILECONFIG( "[root]\n"
|
||||
"entry=value\n",
|
||||
fc );
|
||||
|
||||
CHECK( fc.DeleteGroup("root") );
|
||||
CHECK( Dump(fc).empty() );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteAll", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( fc.DeleteAll() );
|
||||
CHECK( Dump(fc).empty() );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::RenameEntry", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
fc.SetPath("root");
|
||||
CHECK( fc.RenameEntry("entry", "newname") );
|
||||
wxVERIFY_FILECONFIG( "[root]\n"
|
||||
"newname=value\n"
|
||||
"[root/group1]\n"
|
||||
"[root/group1/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[root/group2]\n",
|
||||
fc );
|
||||
|
||||
fc.SetPath("group1/subgroup");
|
||||
CHECK( !fc.RenameEntry("entry", "newname") );
|
||||
CHECK( !fc.RenameEntry("subentry", "subentry2") );
|
||||
|
||||
CHECK( fc.RenameEntry("subentry", "subentry1") );
|
||||
wxVERIFY_FILECONFIG( "[root]\n"
|
||||
"newname=value\n"
|
||||
"[root/group1]\n"
|
||||
"[root/group1/subgroup]\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"subentry1=subvalue\n"
|
||||
"[root/group2]\n",
|
||||
fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::RenameGroup", "[fileconfig][config]")
|
||||
{
|
||||
wxStringInputStream sis(testconfig);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK( fc.RenameGroup("root", "foot") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/group1]\n"
|
||||
"[foot/group1/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/group2]\n",
|
||||
fc );
|
||||
|
||||
// renaming a path doesn't work, it must be the immediate group
|
||||
CHECK( !fc.RenameGroup("foot/group1", "group2") );
|
||||
|
||||
|
||||
fc.SetPath("foot");
|
||||
|
||||
// renaming to a name of existing group doesn't work
|
||||
CHECK( !fc.RenameGroup("group1", "group2") );
|
||||
|
||||
// try exchanging the groups names and then restore them back
|
||||
CHECK( fc.RenameGroup("group1", "groupTmp") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/groupTmp]\n"
|
||||
"[foot/groupTmp/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/group2]\n",
|
||||
fc );
|
||||
|
||||
CHECK( fc.RenameGroup("group2", "group1") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/groupTmp]\n"
|
||||
"[foot/groupTmp/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/group1]\n",
|
||||
fc );
|
||||
|
||||
CHECK( fc.RenameGroup("groupTmp", "group2") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/group2]\n"
|
||||
"[foot/group2/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/group1]\n",
|
||||
fc );
|
||||
|
||||
CHECK( fc.RenameGroup("group1", "groupTmp") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/group2]\n"
|
||||
"[foot/group2/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/groupTmp]\n",
|
||||
fc );
|
||||
|
||||
CHECK( fc.RenameGroup("group2", "group1") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/group1]\n"
|
||||
"[foot/group1/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/groupTmp]\n",
|
||||
fc );
|
||||
|
||||
CHECK( fc.RenameGroup("groupTmp", "group2") );
|
||||
wxVERIFY_FILECONFIG( "[foot]\n"
|
||||
"entry=value\n"
|
||||
"[foot/group1]\n"
|
||||
"[foot/group1/subgroup]\n"
|
||||
"subentry=subvalue\n"
|
||||
"subentry2=subvalue2\n"
|
||||
"[foot/group2]\n",
|
||||
fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::CreateSubgroupAndEntries", "[fileconfig][config]")
|
||||
{
|
||||
wxFileConfig fc;
|
||||
fc.Write("sub/sub_first", "sub_one");
|
||||
fc.Write("first", "one");
|
||||
|
||||
wxVERIFY_FILECONFIG( "first=one\n"
|
||||
"[sub]\n"
|
||||
"sub_first=sub_one\n",
|
||||
fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::CreateEntriesAndSubgroup", "[fileconfig][config]")
|
||||
{
|
||||
wxFileConfig fc;
|
||||
fc.Write("first", "one");
|
||||
fc.Write("second", "two");
|
||||
fc.Write("sub/sub_first", "sub_one");
|
||||
|
||||
wxVERIFY_FILECONFIG( "first=one\n"
|
||||
"second=two\n"
|
||||
"[sub]\n"
|
||||
"sub_first=sub_one\n",
|
||||
fc );
|
||||
}
|
||||
|
||||
static void EmptyConfigAndWriteKey()
|
||||
{
|
||||
wxFileConfig fc("deleteconftest");
|
||||
|
||||
const wxString groupPath = "/root";
|
||||
|
||||
if ( fc.Exists(groupPath) )
|
||||
{
|
||||
// using DeleteGroup exposes the problem, using DeleteAll doesn't
|
||||
CHECK( fc.DeleteGroup(groupPath) );
|
||||
}
|
||||
|
||||
// the config must be empty for the problem to arise
|
||||
CHECK( !fc.GetNumberOfEntries(true) );
|
||||
CHECK( !fc.GetNumberOfGroups(true) );
|
||||
|
||||
|
||||
// this crashes on second call of this function
|
||||
CHECK( fc.Write(groupPath + "/entry", "value") );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteLastGroup", "[fileconfig][config]")
|
||||
{
|
||||
/*
|
||||
We make 2 of the same calls, first to create a file config with a single
|
||||
group and key...
|
||||
*/
|
||||
::EmptyConfigAndWriteKey();
|
||||
|
||||
/*
|
||||
... then the same but this time the key's group is deleted before the
|
||||
key is written again. This causes a crash.
|
||||
*/
|
||||
::EmptyConfigAndWriteKey();
|
||||
|
||||
|
||||
// clean up
|
||||
wxLogNull noLogging;
|
||||
(void) ::wxRemoveFile(wxFileConfig::GetLocalFileName("deleteconftest"));
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::DeleteAndRecreateGroup", "[fileconfig][config]")
|
||||
{
|
||||
static const char *confInitial =
|
||||
"[First]\n"
|
||||
"Value1=Foo\n"
|
||||
"[Second]\n"
|
||||
"Value2=Bar\n";
|
||||
|
||||
wxStringInputStream sis(confInitial);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
fc.DeleteGroup("Second");
|
||||
wxVERIFY_FILECONFIG( "[First]\n"
|
||||
"Value1=Foo\n",
|
||||
fc );
|
||||
|
||||
fc.Write("Second/Value2", "New");
|
||||
wxVERIFY_FILECONFIG( "[First]\n"
|
||||
"Value1=Foo\n"
|
||||
"[Second]\n"
|
||||
"Value2=New\n",
|
||||
fc );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::AddToExistingRoot", "[fileconfig][config]")
|
||||
{
|
||||
static const char *confInitial =
|
||||
"[Group]\n"
|
||||
"value1=foo\n";
|
||||
|
||||
wxStringInputStream sis(confInitial);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
fc.Write("/value1", "bar");
|
||||
wxVERIFY_FILECONFIG(
|
||||
"value1=bar\n"
|
||||
"[Group]\n"
|
||||
"value1=foo\n",
|
||||
fc
|
||||
);
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::ReadNonExistent", "[fileconfig][config]")
|
||||
{
|
||||
static const char *confTest =
|
||||
"community=censored\n"
|
||||
"[City1]\n"
|
||||
"URL=www.fake1.na\n"
|
||||
"[City1/A1]\n"
|
||||
"[City1/A1/1]\n"
|
||||
"IP=192.168.1.66\n"
|
||||
"URL=www.fake2.na\n"
|
||||
;
|
||||
|
||||
wxStringInputStream sis(confTest);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
wxString url;
|
||||
CHECK( !fc.Read("URL", &url) );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::ReadEmpty", "[fileconfig][config]")
|
||||
{
|
||||
static const char *confTest = "";
|
||||
|
||||
wxStringInputStream sis(confTest);
|
||||
wxFileConfig fc(sis);
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::ReadFloat", "[fileconfig][config]")
|
||||
{
|
||||
static const char *confTest =
|
||||
"x=1.234\n"
|
||||
"y=-9876.5432\n"
|
||||
"z=2e+308\n"
|
||||
;
|
||||
|
||||
wxStringInputStream sis(confTest);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
float f;
|
||||
CHECK( fc.Read("x", &f) );
|
||||
CHECK( f == 1.234f );
|
||||
|
||||
CHECK( fc.Read("y", &f) );
|
||||
CHECK( f == -9876.5432f );
|
||||
}
|
||||
|
||||
TEST_CASE("wxFileConfig::LongLong", "[fileconfig][config][longlong]")
|
||||
{
|
||||
wxFileConfig fc("", "", "", "", 0); // Don't use any files.
|
||||
|
||||
// See comment near val64 definition in regconf.cpp.
|
||||
const wxLongLong_t val = wxLL(0x8000000000000008);
|
||||
REQUIRE( fc.Write("ll", val) );
|
||||
|
||||
wxLongLong_t ll;
|
||||
REQUIRE( fc.Read("ll", &ll) );
|
||||
CHECK( ll == val );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(LogTestCase, "wxFileConfig::Error", "[fileconfig][error]")
|
||||
{
|
||||
const auto checkWarning = [this](const char* contents, const char* expected)
|
||||
{
|
||||
wxStringInputStream sis(contents);
|
||||
wxFileConfig fc(sis);
|
||||
|
||||
CHECK_THAT( m_log->GetLog(wxLOG_Warning).utf8_string(),
|
||||
Catch::Contains(expected) );
|
||||
m_log->Clear();
|
||||
};
|
||||
|
||||
// Check that the expected warning is logged.
|
||||
checkWarning("foo=\\", "trailing backslash");
|
||||
|
||||
// Check that it's the second quote which is unexpected, not the first one.
|
||||
checkWarning(R"(foo="x"y)", R"(unexpected " at position 3)");
|
||||
}
|
||||
|
||||
#endif // wxUSE_FILECONFIG
|
||||
|
||||
91
libs/wxWidgets-3.3.1/tests/config/regconf.cpp
Normal file
91
libs/wxWidgets-3.3.1/tests/config/regconf.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/config/regconf.cpp
|
||||
// Purpose: wxRegConfig unit test
|
||||
// Author: Francesco Montorsi (extracted from console sample)
|
||||
// Created: 2010-06-02
|
||||
// Copyright: (c) 2010 wxWidgets team
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#if wxUSE_CONFIG && wxUSE_REGKEY
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "wx/msw/regconf.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test class
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("wxRegConfig::ReadWrite", "[regconfig][config][registry]")
|
||||
{
|
||||
wxString app = "wxRegConfigTestCase";
|
||||
wxString vendor = "wxWidgets";
|
||||
|
||||
// NOTE: we use wxCONFIG_USE_LOCAL_FILE explicitly to test wxRegConfig
|
||||
// with something different from the default value wxCONFIG_USE_GLOBAL_FILE
|
||||
wxRegConfig config(app, vendor, "", "", wxCONFIG_USE_LOCAL_FILE);
|
||||
|
||||
// test writing
|
||||
config.SetPath("/group1");
|
||||
CHECK( config.Write("entry1", "foo") );
|
||||
config.SetPath("/group2");
|
||||
CHECK( config.Write("entry1", "bar") );
|
||||
|
||||
CHECK( config.Write("int32", 1234567) );
|
||||
|
||||
// Note that type of wxLL(0x8000000000000008) literal is somehow unsigned
|
||||
// long long with MinGW, not sure if it's a bug or not, but work around it
|
||||
// by specifying the type explicitly.
|
||||
const wxLongLong_t val64 = wxLL(0x8000000000000008);
|
||||
CHECK( config.Write("int64", val64) );
|
||||
|
||||
// test reading
|
||||
wxString str;
|
||||
long dummy;
|
||||
|
||||
config.SetPath("/");
|
||||
CHECK( config.GetFirstGroup(str, dummy) );
|
||||
CHECK( str == "group1" );
|
||||
CHECK( config.Read("group1/entry1", "INVALID DEFAULT") == "foo" );
|
||||
CHECK( config.GetNextGroup(str, dummy) );
|
||||
CHECK( str == "group2" );
|
||||
CHECK( config.Read("group2/entry1", "INVALID DEFAULT") == "bar" );
|
||||
|
||||
CHECK( config.ReadLong("group2/int32", 0) == 1234567 );
|
||||
CHECK( config.ReadLongLong("group2/int64", 0) == val64 );
|
||||
|
||||
config.DeleteAll();
|
||||
}
|
||||
|
||||
TEST_CASE("wxRegKey::DeleteFromRedirectedView", "[registry][64bits]")
|
||||
{
|
||||
if ( !wxIsPlatform64Bit() )
|
||||
{
|
||||
// Test needs WoW64.
|
||||
return;
|
||||
}
|
||||
|
||||
// Test inside a key that's known to be redirected and is in HKCU so that
|
||||
// admin rights are not required (unlike with HKLM).
|
||||
wxRegKey key(wxRegKey::HKCU, "SOFTWARE\\Classes\\CLSID\\wxWidgetsTestKey",
|
||||
sizeof(void *) == 4
|
||||
? wxRegKey::WOW64ViewMode_64
|
||||
: wxRegKey::WOW64ViewMode_32);
|
||||
|
||||
REQUIRE( key.Create() );
|
||||
CHECK( key.DeleteSelf() );
|
||||
CHECK( !key.Exists() );
|
||||
}
|
||||
|
||||
#endif // wxUSE_CONFIG && wxUSE_REGKEY
|
||||
|
||||
Reference in New Issue
Block a user