diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-12-18 15:46:11 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-01-06 17:15:03 +0100 |
commit | c9a11022692f9a4bd36beb0cd001686694a48915 (patch) | |
tree | 3237e2f88fc261067bed8a8a49db7bd949d7482d /tests | |
parent | 05146a77fce7f080eefad3f82a14e60c8e3ec464 (diff) |
Make QString and QByteArray sequentially iterable
As lists of QStrings and QByteArrays are sequentially iterable the base
types should really also be.
The only problem is that they don't have methods to remove items from
the back or the front, but that is well within what we can support with
QSequentialIterable.
Change-Id: I2ab551e7b11a092aba363fb4012d131bbc4b11b4
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 366d07cfa0..6b30419bb8 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -474,7 +474,7 @@ void tst_QVariant::canConvert_data() << var << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N; var = QVariant(QByteArray()); QTest::newRow("ByteArray") - << var << N << N << Y << N << Y << Y << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + << var << N << N << Y << N << Y << Y << N << N << N << Y << N << N << Y << N << N << Y << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; var = QVariant(QDate()); QTest::newRow("Date") << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N; @@ -513,7 +513,7 @@ void tst_QVariant::canConvert_data() << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N; var = QVariant(QString()); QTest::newRow("String") - << var << N << N << Y << N << Y << Y << N << Y << Y << Y << Y << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << Y << Y << Y << Y; + << var << N << N << Y << N << Y << Y << N << Y << Y << Y << Y << N << Y << N << Y << Y << Y << N << N << N << N << N << N << N << N << N << Y << Y << Y << Y << Y; var = QVariant(QStringList("entry")); QTest::newRow("StringList") << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << Y << N << N << N; @@ -4074,6 +4074,42 @@ struct ContainerAPI<Container, QByteArray> } }; +template<typename Container> +struct ContainerAPI<Container, QChar> +{ + static void insert(Container &container, int value) + { + container.push_back(QChar::fromLatin1(char(value) + '0')); + } + + static bool compare(const QVariant &variant, QChar value) + { + return variant.value<QChar>() == value; + } + static bool compare(QVariant variant, const QVariant &value) + { + return variant == value; + } +}; + +template<typename Container> +struct ContainerAPI<Container, char> +{ + static void insert(Container &container, int value) + { + container.push_back(char(value) + '0'); + } + + static bool compare(const QVariant &variant, char value) + { + return variant.value<char>() == value; + } + static bool compare(QVariant variant, const QVariant &value) + { + return variant == value; + } +}; + #ifdef __has_include # if __has_include(<forward_list>) # define TEST_FORWARD_LIST @@ -4287,14 +4323,20 @@ void testSequentialIteration() QCOMPARE(listIter.at(4), third); QCOMPARE(listIter.at(5), third); - listIter.removeValue(); - compareLists(); - QCOMPARE(listIter.size(), 5); - QCOMPARE(listIter.at(0), first); - QCOMPARE(listIter.at(1), first); - QCOMPARE(listIter.at(2), second); - QCOMPARE(listIter.at(3), second); - QCOMPARE(listIter.at(4), third); + if (listIter.metaContainer().canRemoveValue()) { + listIter.removeValue(); + compareLists(); + QCOMPARE(listIter.size(), 5); + QCOMPARE(listIter.at(0), first); + QCOMPARE(listIter.at(1), first); + QCOMPARE(listIter.at(2), second); + QCOMPARE(listIter.at(3), second); + QCOMPARE(listIter.at(4), third); + } else { + // QString and QByteArray have no pop_back or pop_front and it's unclear what other + // method we should use to remove an item. + QVERIFY((std::is_same_v<Container, QString> || std::is_same_v<Container, QByteArray>)); + } auto i = listIter.mutableBegin(); QVERIFY(i != listIter.mutableEnd()); @@ -4404,6 +4446,8 @@ void tst_QVariant::iterateContainerElements() testSequentialIteration<std::list<QString>>(); testSequentialIteration<QStringList>(); testSequentialIteration<QByteArrayList>(); + testSequentialIteration<QString>(); + testSequentialIteration<QByteArray>(); #ifdef TEST_FORWARD_LIST testSequentialIteration<std::forward_list<int>>(); |