diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-07-30 13:45:46 -0700 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-11-01 22:30:23 +0200 |
commit | a9d8794cf0be617d9eedbf9bb0d6d5401735d6f2 (patch) | |
tree | 16fea80f32bebf3f815f0122371738403d9f015c /tests/auto/corelib/text/qcollator/tst_qcollator.cpp | |
parent | 25d807f6292e9918ccdbe06cf7f590f60ecae457 (diff) |
QCollator: add public, static functions to do comparisons
Collation with the default QCollator object (no numeric, punctuation or
case sensitivity changes) is a common-place occurrence, so add two
functions to do this work.
It's also what QString::localeAwareCompare() calls.
The test ends up testing that default, static collator updates after the
default QLocale changes too.
Task-number: QTBUG-95050
Change-Id: I7e0b82c2d2fe464082d8fffd1696ac77f32840b2
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/corelib/text/qcollator/tst_qcollator.cpp')
-rw-r--r-- | tests/auto/corelib/text/qcollator/tst_qcollator.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qcollator/tst_qcollator.cpp b/tests/auto/corelib/text/qcollator/tst_qcollator.cpp index ce1844f345..7c9ed76f77 100644 --- a/tests/auto/corelib/text/qcollator/tst_qcollator.cpp +++ b/tests/auto/corelib/text/qcollator/tst_qcollator.cpp @@ -31,6 +31,7 @@ #include <qlocale.h> #include <qcollator.h> #include <private/qglobal_p.h> +#include <QScopeGuard> #include <cstring> @@ -276,6 +277,13 @@ void tst_QCollator::compare() QFETCH(int, punctuationResult); QCollator collator((QLocale(locale))); + + // AFTER the QCollator initialization + auto localechanger = qScopeGuard([original = QLocale()] { + QLocale::setDefault(original); // reset back to what it was + }); + QLocale::setDefault(QLocale(locale)); + // Need to canonicalize sign to -1, 0 or 1, as .compare() can produce any -ve for <, any +ve for >. auto asSign = [](int compared) { return compared < 0 ? -1 : compared > 0 ? 1 : 0; @@ -310,10 +318,17 @@ void tst_QCollator::compare() // NOTE: currently QCollatorSortKey::compare is not working // properly without icu: see QTBUG-88704 for details QCOMPARE(asSign(collator.compare(s1, s2)), result); + if (!numericMode) + QCOMPARE(asSign(QCollator::defaultCompare(s1, s2)), result); #if QT_CONFIG(icu) auto key1 = collator.sortKey(s1); auto key2 = collator.sortKey(s2); QCOMPARE(asSign(key1.compare(key2)), keyCompareResult); + + key1 = QCollator::defaultSortKey(s1); + key2 = QCollator::defaultSortKey(s2); + if (!numericMode) + QCOMPARE(asSign(key1.compare(key2)), keyCompareResult); #endif collator.setCaseSensitivity(Qt::CaseInsensitive); QCOMPARE(asSign(collator.compare(s1, s2)), caseInsensitiveResult); |