Implement transient locale as instantiating a class

A couple of QLocale tests were using setlocale twice to provide a
transient locale tweak in tests; however, if the test in between
fails, that can leave the program running in the "transient" locale
after.  So implement a proper class whose destructor ensures the
transient is tidied away.  Also change the locale in use by one of
these transient changes: it purported to be checking things didn't
depend on locale, but was using the same local as most of the
test-cases for its test.

Change-Id: I0d954edcc96019a8c2eb12b7a7c568e8b87a41d5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Edward Welbourne 2018-11-16 14:29:42 +01:00
parent 6dcc13d402
commit 108c9015b9

View File

@ -153,6 +153,16 @@ private:
QString m_decimal, m_thousand, m_sdate, m_ldate, m_time;
QString m_sysapp;
bool europeanTimeZone;
class TransientLocale
{
const int m_category;
const char *const m_prior;
public:
TransientLocale(int category, const char *locale)
: m_category(category), m_prior(setlocale(category, locale)) {}
~TransientLocale() { setlocale(m_category, m_prior); }
};
};
tst_QLocale::tst_QLocale()
@ -806,10 +816,12 @@ void tst_QLocale::stringToDouble()
double d = locale.toDouble(num_str, &ok);
QCOMPARE(ok, good);
char *currentLocale = setlocale(LC_ALL, "de_DE");
QCOMPARE(locale.toDouble(num_str, &ok), d); // make sure result is independent of locale
QCOMPARE(ok, good);
setlocale(LC_ALL, currentLocale);
{
// Make sure result is independent of locale:
TransientLocale ignoreme(LC_ALL, "ar_SA");
QCOMPARE(locale.toDouble(num_str, &ok), d);
QCOMPARE(ok, good);
}
if (ok) {
double diff = d - num;
@ -939,9 +951,8 @@ void tst_QLocale::doubleToString()
const QLocale locale(locale_name);
QCOMPARE(locale.toString(num, mode, precision), num_str);
char *currentLocale = setlocale(LC_ALL, "de_DE");
TransientLocale ignoreme(LC_ALL, "de_DE");
QCOMPARE(locale.toString(num, mode, precision), num_str);
setlocale(LC_ALL, currentLocale);
}
void tst_QLocale::strtod_data()