From ada0e8be5d9388e1a8eba9c8c6004563cfa90a53 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 10 Apr 2023 17:58:20 +0200 Subject: [PATCH] QStringList: test indexOf/lastIndexOf(QRegularExpression) overloads Change-Id: I8c62b0d36628c2a1519667cc553f3ec33b964dfc Reviewed-by: Thiago Macieira --- .../text/qstringlist/tst_qstringlist.cpp | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index 58d2fab09fe..3a3cbd5a814 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -75,26 +75,33 @@ void tst_QStringList::constructors() void tst_QStringList::indexOf_data() { + QTest::addColumn("list"); QTest::addColumn("search"); QTest::addColumn("from"); QTest::addColumn("expectedResult"); - QTest::newRow("harald") << "harald" << 0 << 0; - QTest::newRow("trond") << "trond" << 0 << 1; - QTest::newRow("vohi") << "vohi" << 0 << 2; - QTest::newRow("harald-1") << "harald" << 1 << 3; + QStringList searchIn{"harald", "trond", "vohi", "harald"}; + QTest::newRow("harald") << searchIn << "harald" << 0 << 0; + QTest::newRow("trond") << searchIn << "trond" << 0 << 1; + QTest::newRow("vohi") << searchIn << "vohi" << 0 << 2; + QTest::newRow("harald-1") << searchIn << "harald" << 1 << 3; - QTest::newRow("hans") << "hans" << 0 << -1; - QTest::newRow("trond-1") << "trond" << 2 << -1; - QTest::newRow("harald-2") << "harald" << -1 << 3; - QTest::newRow("vohi-1") << "vohi" << -3 << 2; + QTest::newRow("hans") << searchIn << "hans" << 0 << -1; + QTest::newRow("trond-1") << searchIn << "trond" << 2 << -1; + QTest::newRow("harald-2") << searchIn << "harald" << -1 << 3; + QTest::newRow("vohi-1") << searchIn << "vohi" << -3 << 2; + + QTest::newRow("from-bigger-than-size") << searchIn << "harald" << 100 << -1; + + searchIn = {"lost+found", "foo.bar"}; + QTest::newRow("string-with-regex-meta-char1") << searchIn << "lost+found" << 0 << 0; + QTest::newRow("string-with-regex-meta-char2") << searchIn << "foo.bar" << 0 << 1; + QTest::newRow("string-with-regex-meta-char3") << searchIn << "foo.bar" << 2 << -1; } void tst_QStringList::indexOf() { - QStringList list; - list << "harald" << "trond" << "vohi" << "harald"; - + QFETCH(QStringList, list); QFETCH(QString, search); QFETCH(int, from); QFETCH(int, expectedResult); @@ -102,30 +109,36 @@ void tst_QStringList::indexOf() QCOMPARE(list.indexOf(search, from), expectedResult); QCOMPARE(list.indexOf(QStringView(search), from), expectedResult); QCOMPARE(list.indexOf(QLatin1String(search.toLatin1()), from), expectedResult); + QCOMPARE(list.indexOf(QRegularExpression(QRegularExpression::escape(search)), from), expectedResult); } void tst_QStringList::lastIndexOf_data() { + QTest::addColumn("list"); QTest::addColumn("search"); QTest::addColumn("from"); QTest::addColumn("expectedResult"); - QTest::newRow("harald") << "harald" << -1 << 3; - QTest::newRow("trond") << "trond" << -1 << 1; - QTest::newRow("vohi") << "vohi" << -1 << 2; - QTest::newRow("harald-1") << "harald" << 2 << 0; + QStringList list{"harald", "trond", "vohi", "harald"}; + QTest::newRow("harald") << list << "harald" << -1 << 3; + QTest::newRow("trond") << list << "trond" << -1 << 1; + QTest::newRow("vohi") << list << "vohi" << -1 << 2; + QTest::newRow("harald-1") << list << "harald" << 2 << 0; - QTest::newRow("hans") << "hans" << -1 << -1; - QTest::newRow("vohi-1") << "vohi" << 1 << -1; - QTest::newRow("vohi-2") << "vohi" << -1 << 2; - QTest::newRow("vohi-3") << "vohi" << -3 << -1; + QTest::newRow("hans") << list << "hans" << -1 << -1; + QTest::newRow("vohi-1") << list << "vohi" << 1 << -1; + QTest::newRow("vohi-2") << list << "vohi" << -1 << 2; + QTest::newRow("vohi-3") << list << "vohi" << -3 << -1; + + list = {"lost+found", "foo.bar"}; + QTest::newRow("string-with-regex-meta-char1") << list << "lost+found" << -1 << 0; + QTest::newRow("string-with-regex-meta-char2") << list << "foo.bar" << -1 << 1; + QTest::newRow("string-with-regex-meta-char3") << list << "foo.bar" << -2 << -1; } void tst_QStringList::lastIndexOf() { - QStringList list; - list << "harald" << "trond" << "vohi" << "harald"; - + QFETCH(QStringList, list); QFETCH(QString, search); QFETCH(int, from); QFETCH(int, expectedResult); @@ -133,6 +146,7 @@ void tst_QStringList::lastIndexOf() QCOMPARE(list.lastIndexOf(search, from), expectedResult); QCOMPARE(list.lastIndexOf(QStringView(search), from), expectedResult); QCOMPARE(list.lastIndexOf(QLatin1String(search.toLatin1()), from), expectedResult); + QCOMPARE(list.lastIndexOf(QRegularExpression(QRegularExpression::escape(search)), from), expectedResult); } void tst_QStringList::filter()