diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-09-02 11:29:51 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-09-03 14:00:53 +0200 |
commit | e300fc92044c33bfa0f1fd5e777be0fbebde60d8 (patch) | |
tree | 644d36d921d8d9d38f8841b2407d78aa28039ec2 /tests | |
parent | 340131b4be282063d5dbe352a58e4b2c45994f77 (diff) |
Make tst_QLocale::TransientLocale succeed at changing system locale
It was previously calling setlocale(), which makes no difference to
the Unix backend for the system locale, since that's based on
environment variables (that would normally be used to get default
values for the various setlocale() categories, but only the first time
each is asked for). So, on Unix, set the environment variable, too.
It's also necessary to instantiate a QSystemLocale instance
transiently to trigger a refresh of the system locale data.
Pick-to: 6.2
Change-Id: If92e44053f9021e96c814f4b289f4fadaa7027e0
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 52f0c899a8..57e5c5313d 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -175,10 +175,53 @@ private: { const int m_category; const char *const m_prior; +#if !defined(QT_NO_SYSTEMLOCALE) && defined(Q_OS_UNIX) \ + && (!defined(Q_OS_DARWIN) || defined(Q_OS_NACL)) +#define TRANSIENT_ENV + // Unix system locale consults environment variables, so we need to set + // the appropriate one, too. + const QByteArray m_envVar, m_envPrior; + const bool m_envSet; + static QByteArray categoryToEnv(int category) + { + switch (category) { +#define CASE(cat) case cat: return #cat + CASE(LC_ALL); CASE(LC_NUMERIC); CASE(LC_TIME); CASE(LC_MONETARY); + CASE(LC_MESSAGES); CASE(LC_MEASUREMENT); CASE(LC_COLLATE); +#undef CASE + // Nothing in our code pays attention to any other LC_* + default: + Q_UNREACHABLE(); + qFatal("You need to add a case for this category"); + } + } +#endif // TRANSIENT_ENV public: TransientLocale(int category, const char *locale) - : m_category(category), m_prior(setlocale(category, locale)) {} - ~TransientLocale() { setlocale(m_category, m_prior); } + : m_category(category), + m_prior(setlocale(category, locale)) +#ifdef TRANSIENT_ENV + , m_envVar(categoryToEnv(category)), + m_envPrior(qgetenv(m_envVar.constData())), + m_envSet(qputenv(m_envVar.constData(), locale)) +#endif + { + QSystemLocale dummy; // to provoke a refresh of the system locale + } + ~TransientLocale() + { +#ifdef TRANSIENT_ENV + if (m_envSet) { + if (m_envPrior.isEmpty()) + qunsetenv(m_envVar.constData()); + else + qputenv(m_envVar.constData(), m_envPrior); + } +#endif + setlocale(m_category, m_prior); + QSystemLocale dummy; // to provoke a refresh of the system locale + } +#undef TRANSIENT_ENV }; }; @@ -1180,6 +1223,7 @@ void tst_QLocale::doubleToString() const QLocale locale(localeName); QCOMPARE(locale.toString(num, mode, precision), numStr); + // System locale is irrelevant here: TransientLocale ignoreme(LC_ALL, "de_DE.UTF-8"); QCOMPARE(locale.toString(num, mode, precision), numStr); } |