diff --git a/include/wx/private/localeset.h b/include/wx/private/localeset.h new file mode 100644 index 0000000000..a977247d6b --- /dev/null +++ b/include/wx/private/localeset.h @@ -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 +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PRIVATE_LOCALESET_H_ +#define _WX_PRIVATE_LOCALESET_H_ + +#include "wx/crt.h" // wxStrdupA() + +#include + +// 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_ diff --git a/tests/controls/textctrltest.cpp b/tests/controls/textctrltest.cpp index 9d17f13892..2b79361abf 100644 --- a/tests/controls/textctrltest.cpp +++ b/tests/controls/textctrltest.cpp @@ -32,6 +32,8 @@ #include "wx/stopwatch.h" #endif +#include "wx/private/localeset.h" + #include "textentrytest.h" #include "testableframe.h" #include "asserthelper.h" @@ -288,7 +290,7 @@ void TextCtrlTestCase::StreamInput() #ifndef __WXOSX__ { // Ensure we use decimal point and not a comma. - LocaleSetter setCLocale("C"); + wxCLocaleSetter setCLocale; *m_text << "stringinput" << 10 diff --git a/tests/datetime/datetimetest.cpp b/tests/datetime/datetimetest.cpp index 8ecf70f2c6..0a03babc54 100644 --- a/tests/datetime/datetimetest.cpp +++ b/tests/datetime/datetimetest.cpp @@ -22,6 +22,8 @@ #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 // is not usually the case, but if we're under Win32 we can try it -- define // 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 // we need to use "C" locale for them - CLocaleSetter cloc; + wxCLocaleSetter cloc; wxDateTime dt; for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ ) diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index a650deff7e..3134bbe53d 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -22,6 +22,8 @@ #include "wx/stdpaths.h" #include "wx/scopeguard.h" +#include "wx/private/localeset.h" + #ifdef __WINDOWS__ #include "wx/msw/registry.h" #include "wx/msw/wrapshl.h" @@ -493,7 +495,7 @@ TEST_CASE("wxFileName::GetHumanReadable", "[filename]") { "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 // the decimal point used by GetHumanReadableSize() // is always '.' diff --git a/tests/mbconv/mbconvtest.cpp b/tests/mbconv/mbconvtest.cpp index 8306759fb0..53309c4b60 100644 --- a/tests/mbconv/mbconvtest.cpp +++ b/tests/mbconv/mbconvtest.cpp @@ -22,6 +22,8 @@ #include "wx/txtstrm.h" #include "wx/mstream.h" +#include "wx/private/localeset.h" + #if defined wxHAVE_TCHAR_SUPPORT && !defined HAVE_WCHAR_H #define HAVE_WCHAR_H #endif @@ -1106,7 +1108,7 @@ void MBConvTestCase::LibcTests() // supposed to use the same CRT -- no idea why and unfortunately gdb is too // flaky to debug it) #ifdef __VISUALC__ - LocaleSetter loc("English_United States.1252"); + wxLocaleSetter loc("English_United States.1252"); wxMBConvLibc convLibc; TestCoder( diff --git a/tests/strings/vsnprintf.cpp b/tests/strings/vsnprintf.cpp index 53f48c3e2e..f1fa10f99f 100644 --- a/tests/strings/vsnprintf.cpp +++ b/tests/strings/vsnprintf.cpp @@ -24,6 +24,7 @@ #include "wx/wxchar.h" #endif // WX_PRECOMP +#include "wx/private/localeset.h" // NOTE: for more info about the specification of wxVsnprintf() behaviour you can // 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 // with a locale where the decimal point is not '.' -class VsnprintfTestCase : CLocaleSetter +class VsnprintfTestCase : wxCLocaleSetter { public: - VsnprintfTestCase() : CLocaleSetter() { } + VsnprintfTestCase() : wxCLocaleSetter() { } protected: template diff --git a/tests/testprec.h b/tests/testprec.h index d3340eff03..c51fa34792 100644 --- a/tests/testprec.h +++ b/tests/testprec.h @@ -147,38 +147,6 @@ extern bool IsAutomaticTest(); 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 // Return true if the UI tests are enabled, used by WXUISIM_TEST().