diff options
author | Edward Welbourne <[email protected]> | 2024-11-27 11:27:42 +0100 |
---|---|---|
committer | Edward Welbourne <[email protected]> | 2024-11-28 10:07:23 +0100 |
commit | 1f4ca740355fe664223a2a8137596bfd00c7313b (patch) | |
tree | 87ba8bfff1b15685aa8ec01ac7d5dea760590c45 /src/testlib/doc/snippets | |
parent | 9f4a99e1eb95724ec501b40ceee9725ac68e1240 (diff) |
Fix restoration of default locale after tests that change it
Include an example of this in testlib's docs, as an illustration of
how to restore global state after a test that frobs it. Replace
various "restore as the test finishes" approaches with scope guard
forms that also work when the test fails. Add missing restoration to
tests that allowed their changes to leak into later tests. (If these
later tests actually depended on that, they should be amended to
control the default locale for themselves. This both makes the tests
more robust and documents the need for a specific locale.) Always
restore the actual prior default locale, rather than just assuming
it's the system locale (as several tests did).
For tst_QMimeDatabase this replaced a home-grown RAII class.
For tst_rcc, I included similar treatment of an unregistration of a
resource, since that clearly should follow the same pattern.
For tests that set the default locale in initTestCase(), I've moved
that to their constructor as that comes as close as possible to the
sense of doing it in main(), which is the natural place for a global
setting intended to apply to the whole program.
Change-Id: Ia7970a33b14c4fbd67c1d70dcafbba21f2fae3ef
Reviewed-by: Christian Ehrlicher <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
Diffstat (limited to 'src/testlib/doc/snippets')
-rw-r--r-- | src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp index 4716d06e55c..f810cf6241f 100644 --- a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp +++ b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp @@ -220,3 +220,13 @@ void compareListToInitializerList() #undef ARG //! [35] } + +void withRestoredDefaultLocale() +{ +//! [36] +const auto restoreDefaultLocale = qScopeGuard([prior = QLocale()]() { + QLocale::setDefault(prior); +}); +//! [36] +QLocale::setDefault(QLocale::c()); +} |