From bda7b2a444562ca41ef54910163b74b034fab81c Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 12 Jan 2023 10:06:35 +0100 Subject: 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 Reviewed-by: Fabian Kosmale Reviewed-by: Sami Shalayel --- tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp') 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 #include #include @@ -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 o(c.create()); + + Druggeljug *ref = engine.singletonInstance("TestTypes", "Druggeljug"); + + const QDateTime refDate = engine.coerceValue(ref->myDate()); + const QDateTime refTime = engine.coerceValue(ref->myTime()); + + QCOMPARE(o->property("date").value(), refDate); + QCOMPARE(o->property("time").value(), refTime); + + QCOMPARE(o->property("dateString").toString(), (engine.coerceValue(refDate))); + QCOMPARE(o->property("timeString").toString(), (engine.coerceValue(refTime))); + + QMetaObject::invokeMethod(o.data(), "shuffle"); + + QCOMPARE(ref->myDate(), (engine.coerceValue(refDate))); + QCOMPARE(ref->myTime(), (engine.coerceValue(refTime))); + + const QDate date = ref->myDate(); + const QTime time = ref->myTime(); + + QCOMPARE(o->property("dateString").toString(), (engine.coerceValue(date))); + QCOMPARE(o->property("timeString").toString(), (engine.coerceValue(time))); + + QMetaObject::invokeMethod(o.data(), "fool"); + + QCOMPARE(ref->myDate(), (engine.coerceValue(time))); + QCOMPARE(ref->myTime(), (engine.coerceValue(date))); +} + QTEST_MAIN(tst_QmlCppCodegen) #include "tst_qmlcppcodegen.moc" -- cgit v1.2.3