summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2022-02-23 10:39:15 +0100
committerSona Kurazyan <sona.kurazyan@qt.io>2022-03-04 01:58:10 +0100
commite440fec7fc729342dce7c8421618a95dab11a36b (patch)
tree64e2bc568b0da05adb69d84850bb0761105738b1 /tests
parentd45d62f09dd276faf3d960d18aba191e4c3c78ba (diff)
Add literal operators for QLatin1String and QLatin1Char
The operators are declared in the Qt::Literals::StringLiterals namespace, to avoid collisions in the global namespace. [ChangeLog][QtCore][QLatin1String] Added literal operator""_L1 that converts string literals and chars to QLatin1String and QLatin1Char. Fixes: QTBUG-98434 Change-Id: Ia945a6acf4b8d4fbbb5f803264e4d79d7b17a8da Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp b/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp
index 49e87d8a14..c1ca1f74c0 100644
--- a/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp
+++ b/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp
@@ -46,6 +46,7 @@ class tst_QLatin1String : public QObject
private Q_SLOTS:
void constExpr();
void construction();
+ void userDefinedLiterals();
void at();
void arg() const;
void midLeftRight();
@@ -146,6 +147,50 @@ void tst_QLatin1String::construction()
}
}
+void tst_QLatin1String::userDefinedLiterals()
+{
+ {
+ using namespace Qt::Literals::StringLiterals;
+
+ auto str = "abcd"_L1;
+ static_assert(std::is_same_v<decltype(str), QLatin1String>);
+ QCOMPARE(str.size(), 4);
+ QCOMPARE(str, QLatin1String("abcd"));
+ QCOMPARE(str.latin1(), "abcd");
+ QCOMPARE("abcd"_L1, str.latin1());
+ QCOMPARE("M\xE5rten"_L1, QLatin1String("M\xE5rten"));
+
+ auto ch = 'a'_L1;
+ static_assert(std::is_same_v<decltype(ch), QLatin1Char>);
+ QCOMPARE(ch, QLatin1Char('a'));
+ QCOMPARE(ch.toLatin1(), 'a');
+ QCOMPARE('a'_L1, ch.toLatin1());
+ QCOMPARE('\xE5'_L1, QLatin1Char('\xE5'));
+ }
+ {
+ using namespace Qt::Literals;
+
+ auto str = "abcd"_L1;
+ static_assert(std::is_same_v<decltype(str), QLatin1String>);
+ QCOMPARE(str, QLatin1String("abcd"));
+
+ auto ch = 'a'_L1;
+ static_assert(std::is_same_v<decltype(ch), QLatin1Char>);
+ QCOMPARE(ch, QLatin1Char('a'));
+ }
+ {
+ using namespace Qt;
+
+ auto str = "abcd"_L1;
+ static_assert(std::is_same_v<decltype(str), QLatin1String>);
+ QCOMPARE(str, QLatin1String("abcd"));
+
+ auto ch = 'a'_L1;
+ static_assert(std::is_same_v<decltype(ch), QLatin1Char>);
+ QCOMPARE(ch, QLatin1Char('a'));
+ }
+}
+
void tst_QLatin1String::at()
{
const QLatin1String l1("Hello World");