aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-06-26 09:33:38 +0200
committerUlf Hermann <ulf.hermann@qt.io>2024-07-03 16:36:49 +0200
commitf2431370ad075a71b940dbd03eb3fa34f46b2405 (patch)
tree0601ae8a165c8af751a014f982e73d259a34fe6c /tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
parentb666cda75c38f421e35f3755c8c12a0a6a932347 (diff)
QML: Allow conversion between different list types
We universally allow this pretty much everywhere else. We should also allow it when evaluating bindings. To facilitate this, generalize the SequencePrototype::toVariant() method so that it works with any array-like and faithfully coerces the elements by the type coercion rules. Pick-to: 6.8 Fixes: QTBUG-126398 Change-Id: I520cd40e5f74bee5ac4b418aa86dc043774efcbe Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp')
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp60
1 files changed, 53 insertions, 7 deletions
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index 22479d3d9c..5505e30e3b 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -2899,24 +2899,70 @@ void tst_QmlCppCodegen::jsmoduleImport()
void tst_QmlCppCodegen::jsonArrayToStringList()
{
QQmlEngine engine;
- QQmlComponent component(&engine, QUrl(u"qrc:/qt/qml/TestTypes/jsonArrayToStringList.qml"_s));
+ const QString urlString = u"qrc:/qt/qml/TestTypes/jsonArrayToStringList.qml"_s;
+ QQmlComponent component(&engine, QUrl(urlString));
QVERIFY2(!component.isError(), component.errorString().toUtf8());
- QTest::ignoreMessage(QtDebugMsg, "json [1,aa,2,null,null]");
- // TODO: Enable when fixed. We cannot QEXPECT_FAIL on ignoreMessage
- // QTest::ignoreMessage(QtDebugMsg, "strings [1,aa,2,null,null]");
- // QTest::ignoreMessage(QtDebugMsg, "strings2 [1,aa,2,null,null]");
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "Converting array value at position 0 from double to QStringList via QJSValue even "
+ "though they are not directly convertible");
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "Converting array value at position 2 from double to QStringList via QJSValue even "
+ "though they are not directly convertible");
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "Converting array value at position 3 from std::nullptr_t to QStringList via QJSValue "
+ "even though they are not directly convertible");
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "Converting array value at position 4 from std::nullptr_t to QStringList via QJSValue "
+ "even though they are not directly convertible");
+
+ QTest::ignoreMessage(QtDebugMsg, "json [1,aa,2,null,null]");
+ QTest::ignoreMessage(QtDebugMsg, "strings [1,aa,2,null,null]");
+ QTest::ignoreMessage(QtDebugMsg, "strings2 [1,aa,2,null,null]");
QScopedPointer<QObject> object(component.create());
QVERIFY(!object.isNull());
QListProvider *provider = qobject_cast<QListProvider *>(object.data());
QCOMPARE(provider->json(), QJsonDocument::fromJson("[1,\"aa\",2,null,null]").array());
- QEXPECT_FAIL("", "assignment to string list is broken", Continue);
QCOMPARE(provider->strings(), (QStringList { u"1"_s, u"aa"_s, u"2"_s, u"null"_s, u"null"_s }));
- QEXPECT_FAIL("", "assignment to string list is broken", Continue);
QCOMPARE(provider->property("strings2").toStringList(),
(QStringList { u"1"_s, u"aa"_s, u"2"_s, u"null"_s, u"null"_s }));
+
+ QCOMPARE(provider->stringStrings(), (QList<QStringList> {
+ QStringList(),
+ QStringList { u"aa"_s }, // QVariant::convert auto-converts QString to QStringList ...
+ QStringList(),
+ QStringList(),
+ QStringList(),
+ }));
+
+ // Note1: Converting a generic array to string is basically ArrayPrototype.join().
+ // This produces an empty string for null. Try [null, null].join() for that.
+ // Converting a single null to string (in an element-by-element operation)
+ // produces the string "null". Try (null + "") for that. The inconsistency is
+ // in JavaScript itself, not in our implementation.
+ // Note2: Converting an array to string produces an un-delimited string. If you
+ // convert a nested array to string, you don't see the boundaries anymore.
+ // Try [[1, 2],[3, 4]].join() for that. Again, that's JavaScript itself.
+
+ provider->setJson(QJsonDocument::fromJson(R"([
+ ["c", null, [99], {"x": 12}],
+ [1, 2, 3, "bbb", [16, "a", null]]
+ ])").array());
+
+ QCOMPARE(provider->strings(),
+ (QStringList { u"c,,99,[object Object]"_s, u"1,2,3,bbb,16,a,"_s }));
+ QCOMPARE(provider->property("strings2").toStringList(),
+ (QStringList { u"c,,99,[object Object]"_s, u"1,2,3,bbb,16,a,"_s }));
+ QCOMPARE(provider->stringStrings(), (QList<QStringList> {
+ QStringList { u"c"_s, u"null"_s, u"99"_s, u"[object Object]"_s },
+ QStringList { u"1"_s, u"2"_s, u"3"_s, u"bbb"_s, u"16,a,"_s}
+ }));
}
void tst_QmlCppCodegen::lengthAccessArraySequenceCompat()