From dd350ec2e70c68471e7758d0ea53d4181dc83281 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 27 Aug 2014 23:16:13 +0200 Subject: [PATCH] tst_QString: refactor the insert tests It is tedious to test all the overloads of QString::insert, so get the help of the compiler to do it. The framework was originally developed for testing append. This change showed that the insert(int, QStringRef), insert(int, const char*) and insert(int, QByteArray) overloads were missing. They will be added in a separate commit. Change-Id: I609b8a05203f8a1ea12827d1798313ace5b719f2 Reviewed-by: Thiago Macieira --- .../corelib/tools/qstring/tst_qstring.cpp | 114 +++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 6cadac8447..d9f7acd6c9 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -93,6 +93,10 @@ public: template void apply0(QString &s, MemFun mf) const { Q_FOREACH (QChar ch, this->pinned) (s.*mf)(ch); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { Q_FOREACH (QChar ch, this->pinned) (s.*mf)(a1, ch); } }; template <> @@ -105,6 +109,7 @@ public: } using Arg::apply0; + using Arg::apply1; }; template <> @@ -116,6 +121,10 @@ public: template void apply0(QString &s, MemFun mf) const { (s.*mf)(this->pinned); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, this->pinned); } }; template <> @@ -129,6 +138,10 @@ public: template void apply0(QString &s, MemFun mf) const { (s.*mf)(ref()); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, ref()); } }; template <> @@ -140,6 +153,10 @@ public: template void apply0(QString &s, MemFun mf) const { (s.*mf)(this->pinned.constData(), this->pinned.length()); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, this->pinned.constData(), this->pinned.length()); } }; template <> @@ -152,6 +169,10 @@ public: template void apply0(QString &s, MemFun mf) const { (s.*mf)(l1); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, l1); } }; template <> @@ -170,6 +191,15 @@ public: (s.*mf)(*it); } } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { + if (str) { + for (const char *it = str; *it; ++it) + (s.*mf)(a1, *it); + } + } }; template <> @@ -186,6 +216,7 @@ public: ~Arg() { delete[] str; } using Arg::apply0; + using Arg::apply1; }; template <> @@ -198,6 +229,10 @@ public: template void apply0(QString &s, MemFun mf) const { (s.*mf)(str); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, str); } }; template <> @@ -210,6 +245,10 @@ public: template void apply0(QString &s, MemFun mf) const { (s.*mf)(ba); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, ba); } }; // const char* is not allowed as columns in data-driven tests (causes static_assert failure), @@ -241,6 +280,21 @@ static void do_apply0(MemFun mf) QCOMPARE(s.isNull(), expected.isNull()); } +template +static void do_apply1(MemFun mf) +{ + QFETCH(QString, s); + QFETCH(CharStarContainer, arg); + QFETCH(A1, a1); + QFETCH(QString, expected); + + Arg(arg).apply1(s, mf, a1); + + QCOMPARE(s, expected); + QCOMPARE(s.isEmpty(), expected.isEmpty()); + QCOMPARE(s.isNull(), expected.isNull()); +} + class tst_QString : public QObject { Q_OBJECT @@ -260,6 +314,11 @@ class tst_QString : public QObject template void prepend_impl() const { prepend_impl(); } void prepend_data(bool emptyIsNoop = false); + template + void insert_impl() const { do_apply1(MemFun(&QString::insert)); } + template + void insert_impl() const { insert_impl(); } + void insert_data(bool emptyIsNoop = false); public: tst_QString(); public slots: @@ -350,7 +409,19 @@ private slots: void operator_eqeq_bytearray(); void operator_eqeq_nullstring(); void operator_smaller(); - void insert(); + + void insert_qstring() { insert_impl(); } + void insert_qstring_data() { insert_data(true); } + void insert_qlatin1string() { insert_impl(); } + void insert_qlatin1string_data() { insert_data(true); } + void insert_qcharstar_int() { insert_impl, QString &(QString::*)(int, const QChar*, int) >(); } + void insert_qcharstar_int_data() { insert_data(true); } + void insert_qchar() { insert_impl, QString &(QString::*)(int, QChar)>(); } + void insert_qchar_data() { insert_data(true); } + void insert_char() { insert_impl, QString &(QString::*)(int, QChar)>(); } + void insert_char_data() { insert_data(true); } + void insert_special_cases(); + void simplified_data(); void simplified(); void trimmed(); @@ -2278,7 +2349,46 @@ void tst_QString::simplified() QCOMPARE(qMove(full).simplified(), simple); } -void tst_QString::insert() +void tst_QString::insert_data(bool emptyIsNoop) +{ + QTest::addColumn("s"); + QTest::addColumn("arg"); + QTest::addColumn("a1"); + QTest::addColumn("expected"); + + const CharStarContainer nullC; + const CharStarContainer emptyC(""); + const CharStarContainer aC("a"); + const CharStarContainer bC("b"); + //const CharStarContainer abC("ab"); + const CharStarContainer baC("ba"); + + const QString null; + const QString empty(""); + const QString a("a"); + const QString b("b"); + const QString ab("ab"); + const QString ba("ba"); + + QTest::newRow("null.insert(0, null)") << null << nullC << 0 << null; + QTest::newRow("null.insert(0, empty)") << null << emptyC << 0 << (emptyIsNoop ? null : empty); + QTest::newRow("null.insert(0, a)") << null << aC << 0 << a; + QTest::newRow("empty.insert(0, null)") << empty << nullC << 0 << empty; + QTest::newRow("empty.insert(0, empty)") << empty << emptyC << 0 << empty; + QTest::newRow("empty.insert(0, a)") << empty << aC << 0 << a; + QTest::newRow("a.insert(0, null)") << a << nullC << 0 << a; + QTest::newRow("a.insert(0, empty)") << a << emptyC << 0 << a; + QTest::newRow("a.insert(0, b)") << a << bC << 0 << ba; + QTest::newRow("a.insert(0, ba)") << a << baC << 0 << (ba + a); + QTest::newRow("a.insert(1, null)") << a << nullC << 1 << a; + QTest::newRow("a.insert(1, empty)") << a << emptyC << 1 << a; + QTest::newRow("a.insert(1, b)") << a << bC << 1 << ab; + QTest::newRow("a.insert(1, ba)") << a << baC << 1 << (a + ba); + QTest::newRow("ba.insert(1, a)") << ba << aC << 1 << (ba + a); + QTest::newRow("ba.insert(2, b)") << ba << bC << 2 << (ba + b); +} + +void tst_QString::insert_special_cases() { QString a;