Extract wxLocaleSetter from the tests into a private header

This will allow using this class in the library code too.

No real changes yet, this is a pure refactoring.

This commit is best viewed using git --color-moved option.
This commit is contained in:
Vadim Zeitlin
2021-08-03 01:14:24 +02:00
parent ebec1ff9f6
commit a4940bf696
7 changed files with 64 additions and 38 deletions

View File

@@ -0,0 +1,49 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/private/localeset.h
// Purpose: Define helper wxLocaleSetter class.
// Author: Vadim Zeitlin
// Created: 2021-08-03 (extracted from tests/testprec.h)
// Copyright: (c) 2021 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PRIVATE_LOCALESET_H_
#define _WX_PRIVATE_LOCALESET_H_
#include "wx/crt.h" // wxStrdupA()
#include <locale.h>
// Helper class setting the locale to the given one for its lifetime.
class wxLocaleSetter
{
public:
wxLocaleSetter(const char *loc)
: m_locOld(wxStrdupA(setlocale(LC_ALL, NULL)))
{
setlocale(LC_ALL, loc);
}
~wxLocaleSetter()
{
setlocale(LC_ALL, m_locOld);
free(m_locOld);
}
private:
char * const m_locOld;
wxDECLARE_NO_COPY_CLASS(wxLocaleSetter);
};
// An even simpler helper for setting the locale to "C" one during its lifetime.
class wxCLocaleSetter : private wxLocaleSetter
{
public:
wxCLocaleSetter() : wxLocaleSetter("C") { }
private:
wxDECLARE_NO_COPY_CLASS(wxCLocaleSetter);
};
#endif // _WX_PRIVATE_LOCALESET_H_

View File

@@ -32,6 +32,8 @@
#include "wx/stopwatch.h" #include "wx/stopwatch.h"
#endif #endif
#include "wx/private/localeset.h"
#include "textentrytest.h" #include "textentrytest.h"
#include "testableframe.h" #include "testableframe.h"
#include "asserthelper.h" #include "asserthelper.h"
@@ -288,7 +290,7 @@ void TextCtrlTestCase::StreamInput()
#ifndef __WXOSX__ #ifndef __WXOSX__
{ {
// Ensure we use decimal point and not a comma. // Ensure we use decimal point and not a comma.
LocaleSetter setCLocale("C"); wxCLocaleSetter setCLocale;
*m_text << "stringinput" *m_text << "stringinput"
<< 10 << 10

View File

@@ -22,6 +22,8 @@
#include "wx/wxcrt.h" // for wxStrstr() #include "wx/wxcrt.h" // for wxStrstr()
#include "wx/private/localeset.h"
// to test Today() meaningfully we must be able to change the system date which // to test Today() meaningfully we must be able to change the system date which
// is not usually the case, but if we're under Win32 we can try it -- define // is not usually the case, but if we're under Win32 we can try it -- define
// the macro below to do it // the macro below to do it
@@ -1304,7 +1306,7 @@ void DateTimeTestCase::TestDateTimeParse()
// the test strings here use "PM" which is not available in all locales so // the test strings here use "PM" which is not available in all locales so
// we need to use "C" locale for them // we need to use "C" locale for them
CLocaleSetter cloc; wxCLocaleSetter cloc;
wxDateTime dt; wxDateTime dt;
for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ ) for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )

View File

@@ -22,6 +22,8 @@
#include "wx/stdpaths.h" #include "wx/stdpaths.h"
#include "wx/scopeguard.h" #include "wx/scopeguard.h"
#include "wx/private/localeset.h"
#ifdef __WINDOWS__ #ifdef __WINDOWS__
#include "wx/msw/registry.h" #include "wx/msw/registry.h"
#include "wx/msw/wrapshl.h" #include "wx/msw/wrapshl.h"
@@ -493,7 +495,7 @@ TEST_CASE("wxFileName::GetHumanReadable", "[filename]")
{ "304 KB", 304351, 0, wxSIZE_CONV_SI }, { "304 KB", 304351, 0, wxSIZE_CONV_SI },
}; };
CLocaleSetter loc; // we want to use "C" locale for LC_NUMERIC wxCLocaleSetter loc; // we want to use "C" locale for LC_NUMERIC
// so that regardless of the system's locale // so that regardless of the system's locale
// the decimal point used by GetHumanReadableSize() // the decimal point used by GetHumanReadableSize()
// is always '.' // is always '.'

View File

@@ -22,6 +22,8 @@
#include "wx/txtstrm.h" #include "wx/txtstrm.h"
#include "wx/mstream.h" #include "wx/mstream.h"
#include "wx/private/localeset.h"
#if defined wxHAVE_TCHAR_SUPPORT && !defined HAVE_WCHAR_H #if defined wxHAVE_TCHAR_SUPPORT && !defined HAVE_WCHAR_H
#define HAVE_WCHAR_H #define HAVE_WCHAR_H
#endif #endif
@@ -1106,7 +1108,7 @@ void MBConvTestCase::LibcTests()
// supposed to use the same CRT -- no idea why and unfortunately gdb is too // supposed to use the same CRT -- no idea why and unfortunately gdb is too
// flaky to debug it) // flaky to debug it)
#ifdef __VISUALC__ #ifdef __VISUALC__
LocaleSetter loc("English_United States.1252"); wxLocaleSetter loc("English_United States.1252");
wxMBConvLibc convLibc; wxMBConvLibc convLibc;
TestCoder( TestCoder(

View File

@@ -24,6 +24,7 @@
#include "wx/wxchar.h" #include "wx/wxchar.h"
#endif // WX_PRECOMP #endif // WX_PRECOMP
#include "wx/private/localeset.h"
// NOTE: for more info about the specification of wxVsnprintf() behaviour you can // NOTE: for more info about the specification of wxVsnprintf() behaviour you can
// refer to the following page of the GNU libc manual: // refer to the following page of the GNU libc manual:
@@ -85,10 +86,10 @@ wxUnsafeSnprintf(T *buf, size_t len, const wxChar *fmt, ...)
// Explicitly set C locale to avoid check failures when running on machines // Explicitly set C locale to avoid check failures when running on machines
// with a locale where the decimal point is not '.' // with a locale where the decimal point is not '.'
class VsnprintfTestCase : CLocaleSetter class VsnprintfTestCase : wxCLocaleSetter
{ {
public: public:
VsnprintfTestCase() : CLocaleSetter() { } VsnprintfTestCase() : wxCLocaleSetter() { }
protected: protected:
template<typename T> template<typename T>

View File

@@ -147,38 +147,6 @@ extern bool IsAutomaticTest();
extern bool IsRunningUnderXVFB(); extern bool IsRunningUnderXVFB();
// Helper class setting the locale to the given one for its lifetime.
class LocaleSetter
{
public:
LocaleSetter(const char *loc)
: m_locOld(wxStrdupA(setlocale(LC_ALL, NULL)))
{
setlocale(LC_ALL, loc);
}
~LocaleSetter()
{
setlocale(LC_ALL, m_locOld);
free(m_locOld);
}
private:
char * const m_locOld;
wxDECLARE_NO_COPY_CLASS(LocaleSetter);
};
// An even simpler helper for setting the locale to "C" one during its lifetime.
class CLocaleSetter : private LocaleSetter
{
public:
CLocaleSetter() : LocaleSetter("C") { }
private:
wxDECLARE_NO_COPY_CLASS(CLocaleSetter);
};
#if wxUSE_GUI #if wxUSE_GUI
// Return true if the UI tests are enabled, used by WXUISIM_TEST(). // Return true if the UI tests are enabled, used by WXUISIM_TEST().