Skip to content

Commit

Permalink
tst_QLocale: fix restoration of default locale after tests change it
Browse files Browse the repository at this point in the history
Use qScopeGuard() to ensure we always restore cleanly. Some of them
didn't even need to set default locale, and one only needed a minor
tweak to save the need for it.  Remove tst_QLocale's red-flag comments
about its tests that exercise setDefault(), since they should now be
harmless and not care about their order relative to other tests.

Change-Id: I552218989c66c4e7b238b3a949a6aeae3a7c2825
Reviewed-by: Tor Arne Vestbø <[email protected]>
  • Loading branch information
ediosyncratic committed Nov 28, 2024
1 parent a3caffc commit 9f4a99e
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions tests/auto/corelib/text/qlocale/tst_qlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,12 @@ private slots:
void lcsToCode();
void codeToLcs();

// *** ORDER-DEPENDENCY *** (This Is Bad.)
// Test order is determined by order of declaration here: *all* tests that
// QLocale::setDefault() *must* appear *after* all other tests !
void defaulted_ctor(); // This one must be the first of these.
void defaulted_ctor();
void legacyNames();
void unixLocaleName_data();
void unixLocaleName();
void testNames_data();
void testNames();
// DO NOT add tests here unless they QLocale::setDefault(); see above.
private:
QString m_decimal, m_thousand, m_sdate, m_ldate, m_time;
QString m_sysapp;
Expand Down Expand Up @@ -391,6 +387,9 @@ void tst_QLocale::ctor_match_land()
void tst_QLocale::defaulted_ctor()
{
QLocale priorDefault;
const auto restoreDefault = qScopeGuard([priorDefault]() {
QLocale::setDefault(priorDefault);
});
QLocale::Language defaultLanguage = priorDefault.language();
QLocale::Territory defaultTerritory = priorDefault.territory();

Expand Down Expand Up @@ -501,6 +500,7 @@ void tst_QLocale::defaulted_ctor()
TEST_CTOR(Swedish, AnyTerritory, QLocale::Swedish, QLocale::Sweden);
TEST_CTOR(Uzbek, AnyTerritory, QLocale::Uzbek, QLocale::Uzbekistan);

#undef CHECK_DEFAULT
#undef TEST_CTOR
#define TEST_CTOR(req_lc, exp_lang, exp_country) \
do { \
Expand All @@ -512,7 +512,6 @@ void tst_QLocale::defaulted_ctor()
QCOMPARE(qHash(l), qHash(m)); \
} while (false)

QLocale::setDefault(QLocale(QLocale::C));
const QString empty;

TEST_CTOR("C", C, AnyTerritory);
Expand Down Expand Up @@ -583,7 +582,6 @@ void tst_QLocale::defaulted_ctor()
TEST_CTOR("ru_Cyrl", Russian, CyrillicScript, RussianFederation);

#undef TEST_CTOR
#undef CHECK_DEFAULT
}

#if QT_CONFIG(process)
Expand Down Expand Up @@ -746,8 +744,6 @@ void tst_QLocale::systemLocale()

void tst_QLocale::legacyNames()
{
QLocale::setDefault(QLocale(QLocale::C));

#define TEST_CTOR(req_lc, exp_lang, exp_country) \
do { \
const QLocale l(req_lc); \
Expand Down Expand Up @@ -821,8 +817,8 @@ void tst_QLocale::unixLocaleName_data()
ADDROW("C_any", C, AnyTerritory, "C");
ADDROW("en_any", English, AnyTerritory, "en_US");
ADDROW("en_GB", English, UnitedKingdom, "en_GB");
ADDROW("ay_GB", Aymara, UnitedKingdom, "C");
#undef ADDROW
QTest::newRow("ay_GB") << QLocale::Aymara << QLocale::UnitedKingdom << QLocale().name();
}

void tst_QLocale::unixLocaleName()
Expand All @@ -836,8 +832,6 @@ void tst_QLocale::unixLocaleName()
return copy.replace(u'_', ch);
};

QLocale::setDefault(QLocale(QLocale::C));

const QLocale locale(lang, land);
QCOMPARE(locale.name(), expect);
QCOMPARE(locale.name(QLocale::TagSeparator::Dash), expected(u'-'));
Expand Down Expand Up @@ -3073,8 +3067,6 @@ void tst_QLocale::testNames_data()
QTest::addColumn<QLocale::Language>("language");
QTest::addColumn<QLocale::Territory>("country");

QLocale::setDefault(QLocale(QLocale::C)); // Ensures predictable fall-backs

#ifdef QT_BUILD_INTERNAL
bool ok = QLocaleData::allLocaleDataRows([](qsizetype index, const QLocaleData &item) {
const QByteArray lang =
Expand Down

0 comments on commit 9f4a99e

Please sign in to comment.