From 108c9015b960fccd79efc6de4459dbf05f6ced54 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 16 Nov 2018 14:29:42 +0100 Subject: 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 Reviewed-by: Ulf Hermann --- tests/auto/corelib/tools/qlocale/tst_qlocale.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp index b7cb8a1bdc..4803451399 100644 --- a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp @@ -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() -- cgit v1.2.3