Use RAII to handle setting of default locale in tst_QString

Various tests were setting the default locale and relying on cleanup()
to "restore" the C locale; which needn't actually be the locale we
started out in and, in any case, was the wrong locale for some tests.
So handle this via an RAII class that records the actual prior locale
and restores it on destruction.

Fixes: QTBUG-73116
Change-Id: If44f7cb8c6e0ce81be396ac1ea8bab7038a86729
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2019-01-21 13:46:11 +01:00
parent 9818af7d43
commit 76dfda1ad1

View File

@ -319,10 +319,18 @@ class tst_QString : public QObject
template <typename ArgType> template <typename ArgType>
void insert_impl() const { insert_impl<ArgType, QString &(QString::*)(int, const ArgType&)>(); } void insert_impl() const { insert_impl<ArgType, QString &(QString::*)(int, const ArgType&)>(); }
void insert_data(bool emptyIsNoop = false); void insert_data(bool emptyIsNoop = false);
class TransientDefaultLocale
{
const QLocale prior; // Records what *was* the default before we set it.
public:
TransientDefaultLocale(const QLocale &transient) { revise(transient); }
void revise(const QLocale &transient) { QLocale::setDefault(transient); }
~TransientDefaultLocale() { QLocale::setDefault(prior); }
};
public: public:
tst_QString(); tst_QString();
public slots:
void cleanup();
private slots: private slots:
void fromStdString(); void fromStdString();
void toStdString(); void toStdString();
@ -654,11 +662,6 @@ tst_QString::tst_QString()
QTextCodec::setCodecForLocale(QTextCodec::codecForName("ISO 8859-1")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("ISO 8859-1"));
} }
void tst_QString::cleanup()
{
QLocale::setDefault(QString("C"));
}
void tst_QString::remove_uint_uint_data() void tst_QString::remove_uint_uint_data()
{ {
replace_uint_uint_data(); replace_uint_uint_data();
@ -4750,7 +4753,7 @@ void tst_QString::arg()
is all messed up, because Qt Test itself uses QString::arg(). is all messed up, because Qt Test itself uses QString::arg().
*/ */
QLocale::setDefault(QString("de_DE")); TransientDefaultLocale transient(QString("de_DE"));
QString s4( "[%0]" ); QString s4( "[%0]" );
QString s5( "[%1]" ); QString s5( "[%1]" );
@ -4898,13 +4901,11 @@ void tst_QString::arg()
QCOMPARE(QString("%1").arg(-1., 3, 'g', -1, QChar('x')), QLatin1String("x-1")); QCOMPARE(QString("%1").arg(-1., 3, 'g', -1, QChar('x')), QLatin1String("x-1"));
QCOMPARE(QString("%1").arg(-100., 3, 'g', -1, QChar('x')), QLatin1String("-100")); QCOMPARE(QString("%1").arg(-100., 3, 'g', -1, QChar('x')), QLatin1String("-100"));
QLocale::setDefault(QString("ar")); transient.revise(QString("ar"));
QCOMPARE( QString("%L1").arg(12345.6789, 10, 'g', 7, QLatin1Char('0')), QCOMPARE( QString("%L1").arg(12345.6789, 10, 'g', 7, QLatin1Char('0')),
QString::fromUtf8("\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xac\xd9\xa3\xd9\xa4\xd9\xa5\xd9\xab\xd9\xa6\xd9\xa8") ); // "٠١٢٬٣٤٥٫٦٨" QString::fromUtf8("\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xac\xd9\xa3\xd9\xa4\xd9\xa5\xd9\xab\xd9\xa6\xd9\xa8") ); // "٠١٢٬٣٤٥٫٦٨"
QCOMPARE( QString("%L1").arg(123456789, 13, 10, QLatin1Char('0')), QCOMPARE( QString("%L1").arg(123456789, 13, 10, QLatin1Char('0')),
QString("\xd9\xa0\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xac\xd9\xa4\xd9\xa5\xd9\xa6\xd9\xac\xd9\xa7\xd9\xa8\xd9\xa9") ); // ٠٠١٢٣٬٤٥٦٬٧٨٩ QString("\xd9\xa0\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xac\xd9\xa4\xd9\xa5\xd9\xa6\xd9\xac\xd9\xa7\xd9\xa8\xd9\xa9") ); // ٠٠١٢٣٬٤٥٦٬٧٨٩
QLocale::setDefault(QLocale::system());
} }
void tst_QString::number() void tst_QString::number()
@ -6594,14 +6595,14 @@ void tst_QString::arg_locale()
QLocale l(QLocale::English, QLocale::UnitedKingdom); QLocale l(QLocale::English, QLocale::UnitedKingdom);
QString str("*%L1*%L2*"); QString str("*%L1*%L2*");
QLocale::setDefault(l); TransientDefaultLocale transient(l);
QCOMPARE(str.arg(123456).arg(1234.56), QString::fromLatin1("*123,456*1,234.56*")); QCOMPARE(str.arg(123456).arg(1234.56), QString::fromLatin1("*123,456*1,234.56*"));
l.setNumberOptions(QLocale::OmitGroupSeparator); l.setNumberOptions(QLocale::OmitGroupSeparator);
QLocale::setDefault(l); transient.revise(l);
QCOMPARE(str.arg(123456).arg(1234.56), QString::fromLatin1("*123456*1234.56*")); QCOMPARE(str.arg(123456).arg(1234.56), QString::fromLatin1("*123456*1234.56*"));
QLocale::setDefault(QLocale::C); transient.revise(QLocale::C);
QCOMPARE(str.arg(123456).arg(1234.56), QString::fromLatin1("*123456*1234.56*")); QCOMPARE(str.arg(123456).arg(1234.56), QString::fromLatin1("*123456*1234.56*"));
} }
@ -6615,7 +6616,7 @@ void tst_QString::toUpperLower_icu()
QCOMPARE(s.toUpper(), QString::fromLatin1("I")); QCOMPARE(s.toUpper(), QString::fromLatin1("I"));
QCOMPARE(s.toLower(), QString::fromLatin1("i")); QCOMPARE(s.toLower(), QString::fromLatin1("i"));
QLocale::setDefault(QLocale(QLocale::Turkish, QLocale::Turkey)); TransientDefaultLocale transient(QLocale(QLocale::Turkish, QLocale::Turkey));
QCOMPARE(s.toUpper(), QString::fromLatin1("I")); QCOMPARE(s.toUpper(), QString::fromLatin1("I"));
QCOMPARE(s.toLower(), QString::fromLatin1("i")); QCOMPARE(s.toLower(), QString::fromLatin1("i"));
@ -6639,8 +6640,6 @@ void tst_QString::toUpperLower_icu()
// nothing should happen here // nothing should happen here
QCOMPARE(l.toLower(sup), sup); QCOMPARE(l.toLower(sup), sup);
QCOMPARE(l.toLower(QString::fromLatin1("i")), QString::fromLatin1("i")); QCOMPARE(l.toLower(QString::fromLatin1("i")), QString::fromLatin1("i"));
// the cleanup function will restore the default locale
} }
#endif #endif