summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2019-06-03 11:43:24 +0200
committerMarc Mutz <marc.mutz@kdab.com>2019-06-20 19:36:37 +0200
commit35431062bd7bf0d27b9bf785ab3cbc7fac5a69da (patch)
tree562a4339515e7e1db6981958e02796058012eb75 /tests
parent6f84829031f318bfda1deff5f409b5ea6c6a5c5f (diff)
QString: towards QStringView::arg() pt.3: Long live QStringView/QLatin1String::arg()
This version of arg(), unlike its QString counterpart, transparently accepts views without conversion to QString, and is also extensible to further argument types, say a future QFormattedNumber. [ChangeLog][QtCore][QStringView/QLatin1String] Added arg(), taking arbitrarily many strings. Change-Id: If40ef3c445f63383e32573f3f515fdda84c7fe3a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp42
-rw-r--r--tests/auto/corelib/tools/qstringview/tst_qstringview.cpp43
2 files changed, 85 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp b/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp
index dcfb0aa042..cf46159251 100644
--- a/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp
+++ b/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp
@@ -45,6 +45,7 @@ class tst_QLatin1String : public QObject
private Q_SLOTS:
void at();
+ void arg() const;
void midLeftRight();
void nullString();
void emptyString();
@@ -63,6 +64,47 @@ void tst_QLatin1String::at()
QCOMPARE(l1[l1.size() - 1], QLatin1Char('d'));
}
+void tst_QLatin1String::arg() const
+{
+#define CHECK1(pattern, arg1, expected) \
+ do { \
+ auto p = QLatin1String(pattern); \
+ QCOMPARE(p.arg(QLatin1String(arg1)), expected); \
+ QCOMPARE(p.arg(QStringViewLiteral(arg1)), expected); \
+ QCOMPARE(p.arg(QStringLiteral(arg1)), expected); \
+ QCOMPARE(p.arg(QString(QLatin1String(arg1))), expected); \
+ } while (false) \
+ /*end*/
+#define CHECK2(pattern, arg1, arg2, expected) \
+ do { \
+ auto p = QLatin1String(pattern); \
+ QCOMPARE(p.arg(QLatin1String(arg1), QLatin1String(arg2)), expected); \
+ QCOMPARE(p.arg(QStringViewLiteral(arg1), QLatin1String(arg2)), expected); \
+ QCOMPARE(p.arg(QLatin1String(arg1), QStringViewLiteral(arg2)), expected); \
+ QCOMPARE(p.arg(QStringViewLiteral(arg1), QStringViewLiteral(arg2)), expected); \
+ } while (false) \
+ /*end*/
+
+ CHECK1("", "World", "");
+ CHECK1("%1", "World", "World");
+ CHECK1("!%1?", "World", "!World?");
+ CHECK1("%1%1", "World", "WorldWorld");
+ CHECK1("%1%2", "World", "World%2");
+ CHECK1("%2%1", "World", "%2World");
+
+ CHECK2("", "Hello", "World", "");
+ CHECK2("%1", "Hello", "World", "Hello");
+ CHECK2("!%1, %2?", "Hello", "World", "!Hello, World?");
+ CHECK2("%1%1", "Hello", "World", "HelloHello");
+ CHECK2("%1%2", "Hello", "World", "HelloWorld");
+ CHECK2("%2%1", "Hello", "World", "WorldHello");
+
+#undef CHECK2
+#undef CHECK1
+
+ QCOMPARE(QLatin1String(" %2 %2 %1 %3 ").arg(QLatin1Char('c'), QChar::CarriageReturn, u'C'), " \r \r c C ");
+}
+
void tst_QLatin1String::midLeftRight()
{
const QLatin1String l1("Hello World");
diff --git a/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp b/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp
index e800a0d794..794f39708a 100644
--- a/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp
+++ b/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp
@@ -134,6 +134,8 @@ private Q_SLOTS:
void literals() const;
void at() const;
+ void arg() const;
+
void fromQString() const;
void fromQStringRef() const;
@@ -425,6 +427,47 @@ void tst_QStringView::at() const
QCOMPARE(sv.at(4), QChar('o')); QCOMPARE(sv[4], QChar('o'));
}
+void tst_QStringView::arg() const
+{
+#define CHECK1(pattern, arg1, expected) \
+ do { \
+ auto p = QStringViewLiteral(pattern); \
+ QCOMPARE(p.arg(QLatin1String(arg1)), expected); \
+ QCOMPARE(p.arg(QStringViewLiteral(arg1)), expected); \
+ QCOMPARE(p.arg(QStringLiteral(arg1)), expected); \
+ QCOMPARE(p.arg(QString(QLatin1String(arg1))), expected); \
+ } while (false) \
+ /*end*/
+#define CHECK2(pattern, arg1, arg2, expected) \
+ do { \
+ auto p = QStringViewLiteral(pattern); \
+ QCOMPARE(p.arg(QLatin1String(arg1), QLatin1String(arg2)), expected); \
+ QCOMPARE(p.arg(QStringViewLiteral(arg1), QLatin1String(arg2)), expected); \
+ QCOMPARE(p.arg(QLatin1String(arg1), QStringViewLiteral(arg2)), expected); \
+ QCOMPARE(p.arg(QStringViewLiteral(arg1), QStringViewLiteral(arg2)), expected); \
+ } while (false) \
+ /*end*/
+
+ CHECK1("", "World", "");
+ CHECK1("%1", "World", "World");
+ CHECK1("!%1?", "World", "!World?");
+ CHECK1("%1%1", "World", "WorldWorld");
+ CHECK1("%1%2", "World", "World%2");
+ CHECK1("%2%1", "World", "%2World");
+
+ CHECK2("", "Hello", "World", "");
+ CHECK2("%1", "Hello", "World", "Hello");
+ CHECK2("!%1, %2?", "Hello", "World", "!Hello, World?");
+ CHECK2("%1%1", "Hello", "World", "HelloHello");
+ CHECK2("%1%2", "Hello", "World", "HelloWorld");
+ CHECK2("%2%1", "Hello", "World", "WorldHello");
+
+#undef CHECK2
+#undef CHECK1
+
+ QCOMPARE(QStringViewLiteral(" %2 %2 %1 %3 ").arg(QLatin1Char('c'), QChar::CarriageReturn, u'C'), " \r \r c C ");
+}
+
void tst_QStringView::fromQString() const
{
QString null;