From 8a6a9295ebd3409b1b818c6e04dc210ca93e51c0 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 16 Sep 2023 23:36:16 +0300 Subject: [PATCH] tst_QStringList: refactor a unittest to be table-driven Change-Id: I9a102a42eba42f1bec71cc680f6de6dbeaf7ad58 Reviewed-by: Thiago Macieira --- .../text/qstringlist/tst_qstringlist.cpp | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index 5c23b8f01c..573357abd2 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -23,6 +23,7 @@ private slots: void replaceInStrings(); void removeDuplicates(); void removeDuplicates_data(); + void contains_data(); void contains(); void indexOf_data(); void indexOf(); @@ -241,37 +242,35 @@ void tst_QStringList::replaceInStrings() QCOMPARE(copy, (QStringList{"ylphy", "bety", "gymmy"})); } +void tst_QStringList::contains_data() +{ + QTest::addColumn("needle"); + QTest::addColumn("cs"); + QTest::addColumn("expected"); + + QTest::newRow("arthur") << u"arthur"_s << Qt::CaseSensitive << true; + QTest::newRow("ArthuR") << u"ArthuR"_s << Qt::CaseSensitive << false; + QTest::newRow("arthur") << u"arthur"_s << Qt::CaseInsensitive << true; + QTest::newRow("ArthuR") << u"ArthuR"_s << Qt::CaseInsensitive << true; + QTest::newRow("ARTHUR") << u"ARTHUR"_s << Qt::CaseInsensitive << true; + QTest::newRow("Hans") << u"Hans"_s << Qt::CaseSensitive << false; + QTest::newRow("hans") << u"hans"_s << Qt::CaseInsensitive << false; + QTest::newRow("dent") << u"dent"_s << Qt::CaseInsensitive << true; +} + void tst_QStringList::contains() { - QStringList list; - list << "arthur" << "Arthur" << "arthuR" << "ARTHUR" << "Dent" << "Hans Dent"; + QFETCH(QString, needle); + QFETCH(Qt::CaseSensitivity, cs); + QFETCH(bool, expected); - QVERIFY(list.contains("arthur")); - QVERIFY(!list.contains("ArthuR")); - QVERIFY(!list.contains("Hans")); - QVERIFY(list.contains("arthur", Qt::CaseInsensitive)); - QVERIFY(list.contains("ArthuR", Qt::CaseInsensitive)); - QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive)); - QVERIFY(list.contains("dent", Qt::CaseInsensitive)); - QVERIFY(!list.contains("hans", Qt::CaseInsensitive)); + const QStringList list = { + u"arthur"_s, u"Arthur"_s, u"arthuR"_s, u"ARTHUR"_s, u"Dent"_s, u"Hans Dent"_s + }; - QVERIFY(list.contains(QLatin1String("arthur"))); - QVERIFY(!list.contains(QLatin1String("ArthuR"))); - QVERIFY(!list.contains(QLatin1String("Hans"))); - QVERIFY(list.contains(QLatin1String("arthur"), Qt::CaseInsensitive)); - QVERIFY(list.contains(QLatin1String("ArthuR"), Qt::CaseInsensitive)); - QVERIFY(list.contains(QLatin1String("ARTHUR"), Qt::CaseInsensitive)); - QVERIFY(list.contains(QLatin1String("dent"), Qt::CaseInsensitive)); - QVERIFY(!list.contains(QLatin1String("hans"), Qt::CaseInsensitive)); - - QVERIFY(list.contains(QStringView(QString("arthur")))); - QVERIFY(!list.contains(QStringView(QString("ArthuR")))); - QVERIFY(!list.contains(QStringView(QString("Hans")))); - QVERIFY(list.contains(QStringView(QString("arthur")), Qt::CaseInsensitive)); - QVERIFY(list.contains(QStringView(QString("ArthuR")), Qt::CaseInsensitive)); - QVERIFY(list.contains(QStringView(QString("ARTHUR")), Qt::CaseInsensitive)); - QVERIFY(list.contains(QStringView(QString("dent")), Qt::CaseInsensitive)); - QVERIFY(!list.contains(QStringView(QString("hans")), Qt::CaseInsensitive)); + QCOMPARE(list.contains(needle, cs), expected); + QCOMPARE(list.contains(QStringView(needle), cs), expected); + QCOMPARE(list.contains(QLatin1StringView(needle.toLatin1()), cs), expected); } void tst_QStringList::removeDuplicates_data()