diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2023-01-12 10:06:35 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2023-01-17 21:58:32 +0100 |
commit | bda7b2a444562ca41ef54910163b74b034fab81c (patch) | |
tree | 56d4aa7dd9c61d99a217ddc3bad369e326b7618a /tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | |
parent | 5bc63de8819448e7342f3cb0ac08af667ccc81e5 (diff) | |
download | qtdeclarative-bda7b2a444562ca41ef54910163b74b034fab81c.tar.gz |
QmlCompiler: Handle various date and time conversions correctly
We can coerce QDateTime, QDate and QTime into each other because they
would all be represented by a Date object in JavaScript. Furthermore we
can coerce them all to QString. Technically, we could also coerce
strings to all of them, but we don't want to because that is terrible.
Fixes: QTBUG-109380
Change-Id: I176bfb5b715a6a6750cb5918c44261fa23fb8832
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp')
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index 65e882a089..1e88ecd7b7 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -1,5 +1,6 @@ // Copyright (C) 2021 The Qt Company Ltd. +#include "data/druggeljug.h" #include <data/birthdayparty.h> #include <data/cppbaseclass.h> #include <data/enumproblems.h> @@ -160,6 +161,7 @@ private slots: void infinitiesToInt(); void equalityVarAndNonStorable(); void equalityQObjects(); + void dateConversions(); }; void tst_QmlCppCodegen::initTestCase() @@ -3088,6 +3090,41 @@ void tst_QmlCppCodegen::equalityQObjects() QVERIFY(object->property("compareObjectWithNullObject").toBool()); } +void tst_QmlCppCodegen::dateConversions() +{ + QQmlEngine engine; + QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/dateConversions.qml"_s)); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer<QObject> o(c.create()); + + Druggeljug *ref = engine.singletonInstance<Druggeljug *>("TestTypes", "Druggeljug"); + + const QDateTime refDate = engine.coerceValue<QDate, QDateTime>(ref->myDate()); + const QDateTime refTime = engine.coerceValue<QTime, QDateTime>(ref->myTime()); + + QCOMPARE(o->property("date").value<QDateTime>(), refDate); + QCOMPARE(o->property("time").value<QDateTime>(), refTime); + + QCOMPARE(o->property("dateString").toString(), (engine.coerceValue<QDateTime, QString>(refDate))); + QCOMPARE(o->property("timeString").toString(), (engine.coerceValue<QDateTime, QString>(refTime))); + + QMetaObject::invokeMethod(o.data(), "shuffle"); + + QCOMPARE(ref->myDate(), (engine.coerceValue<QDateTime, QDate>(refDate))); + QCOMPARE(ref->myTime(), (engine.coerceValue<QDateTime, QTime>(refTime))); + + const QDate date = ref->myDate(); + const QTime time = ref->myTime(); + + QCOMPARE(o->property("dateString").toString(), (engine.coerceValue<QDate, QString>(date))); + QCOMPARE(o->property("timeString").toString(), (engine.coerceValue<QTime, QString>(time))); + + QMetaObject::invokeMethod(o.data(), "fool"); + + QCOMPARE(ref->myDate(), (engine.coerceValue<QTime, QDate>(time))); + QCOMPARE(ref->myTime(), (engine.coerceValue<QDate, QTime>(date))); +} + QTEST_MAIN(tst_QmlCppCodegen) #include "tst_qmlcppcodegen.moc" |