diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-07-14 12:26:16 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-08-24 00:18:24 +0200 |
commit | 9d36032370f7b81279ff8774afd7dea4ea57ee6a (patch) | |
tree | d750ccd6cd317f056d13495a03fde5881134ec89 /tests/auto/corelib/kernel/qvariant | |
parent | 2d3b31171d8fce2ab364ca2ceea4e75020933334 (diff) |
Implement QMetaType::canConvert() and use it in QVariant
Use the fact that we return the conversion function as a lambda
to find out reliably whether a conversion between two types
can be done.
This requires some minor adjustments to our tests:
* Nothing can convert to an unknown type and vice versa
* Adjust results to the fact that we don't convert from char
to QString anymore (where the old method was incorrect)
* QStringList->QString requires some adjustments, as we only
convert if the string list has exactly one element. For now
we return true in canConvert(), but the conversion behavior
in this case is something we should rethink, as it is very
surprising.
Change-Id: I3f5f87ee9cb99d690f5a7d13b13d6a6313d8038e
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qvariant')
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/qvariant.pro | 2 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 16 |
3 files changed, 10 insertions, 9 deletions
diff --git a/tests/auto/corelib/kernel/qvariant/CMakeLists.txt b/tests/auto/corelib/kernel/qvariant/CMakeLists.txt index f1baaefe43..8b122a48e2 100644 --- a/tests/auto/corelib/kernel/qvariant/CMakeLists.txt +++ b/tests/auto/corelib/kernel/qvariant/CMakeLists.txt @@ -13,6 +13,7 @@ qt_add_test(tst_qvariant ../../../other/qvariant_common PUBLIC_LIBRARIES Qt::CorePrivate + Qt::Gui ) # Resources: diff --git a/tests/auto/corelib/kernel/qvariant/qvariant.pro b/tests/auto/corelib/kernel/qvariant/qvariant.pro index a620be0091..7416023808 100644 --- a/tests/auto/corelib/kernel/qvariant/qvariant.pro +++ b/tests/auto/corelib/kernel/qvariant/qvariant.pro @@ -1,6 +1,6 @@ CONFIG += testcase TARGET = tst_qvariant -QT = core-private testlib +QT = core-private gui testlib INCLUDEPATH += $$PWD/../../../other/qvariant_common SOURCES = tst_qvariant.cpp RESOURCES += qvariant.qrc diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index a722e53f48..d4af02baf1 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -477,7 +477,7 @@ void tst_QVariant::canConvert_data() << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; var = QVariant(); QTest::newRow("Invalid") - << var << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N; + << 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 << N << N << N << N << N << N << N; var = QVariant(QList<QVariant>()); QTest::newRow("List") << 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 << N << Y << N << N << N; @@ -520,12 +520,12 @@ void tst_QVariant::canConvert_data() var = QVariant::fromValue<signed char>(-1); QTest::newRow("SChar") << var << N << N << Y << N << Y << N << 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 = QVariant((short)-3); + var = QVariant::fromValue((short)-3); QTest::newRow("Short") - << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; - var = QVariant((ushort)7); + << var << N << N << Y << N << Y << N << 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 = QVariant::fromValue((ushort)7); QTest::newRow("UShort") - << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + << var << N << N << Y << N << Y << N << 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 = QVariant::fromValue<QJsonValue>(QJsonValue(QStringLiteral("hello"))); QTest::newRow("JsonValue") << var << N << N << Y << N << N << N << N << N << N << Y << N << N << Y << N << N << Y << Y << Y << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; @@ -2705,17 +2705,17 @@ void tst_QVariant::canConvertQStringList_data() const QTest::addColumn<QStringList>("input"); QTest::addColumn<QString>("result"); - QTest::newRow("An empty list") << false << QStringList() << QString(); + QTest::newRow("An empty list") << true << QStringList() << QString(); QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo"); QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString(); QStringList l; l << "a" << "b"; - QTest::newRow("Two items") << false << l << QString(); + QTest::newRow("Two items") << true << l << QString(); l << "c"; - QTest::newRow("Three items") << false << l << QString(); + QTest::newRow("Three items") << true << l << QString(); } template<typename T> void convertMetaType() |