diff options
4 files changed, 95 insertions, 4 deletions
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt index 48604823d9..8d771b18f9 100644 --- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt +++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt @@ -210,6 +210,7 @@ set(qml_files nullAccess.qml nullAccessInsideSignalHandler.qml nullComparison.qml + nullishCoalescing.qml numbersInJsPrimitive.qml objectInVar.qml objectLookupOnListElement.qml diff --git a/tests/auto/qml/qmlcppcodegen/data/getOptionalLookup.h b/tests/auto/qml/qmlcppcodegen/data/getOptionalLookup.h index d9b95a6af0..e8a24cd707 100644 --- a/tests/auto/qml/qmlcppcodegen/data/getOptionalLookup.h +++ b/tests/auto/qml/qmlcppcodegen/data/getOptionalLookup.h @@ -9,10 +9,10 @@ class GOL_Object : public QObject Q_OBJECT QML_ELEMENT - Q_PROPERTY(int i READ i WRITE setI) - Q_PROPERTY(QString s READ s WRITE setS) - Q_PROPERTY(GOL_Object *childA READ childA WRITE setChildA) - Q_PROPERTY(Enum e READ e) + Q_PROPERTY(int i READ i CONSTANT FINAL) + Q_PROPERTY(QString s READ s CONSTANT FINAL) + Q_PROPERTY(GOL_Object *childA READ childA WRITE setChildA NOTIFY childAChanged FINAL) + Q_PROPERTY(Enum e READ e CONSTANT FINAL) public: GOL_Object(QObject *parent = nullptr) : QObject(parent) { } @@ -30,6 +30,9 @@ public: Q_ENUM(Enum) Enum e() const { return Enum::V2; } +signals: + void childAChanged(); + private: int m_i = 5; QString m_s = "6"; diff --git a/tests/auto/qml/qmlcppcodegen/data/nullishCoalescing.qml b/tests/auto/qml/qmlcppcodegen/data/nullishCoalescing.qml new file mode 100644 index 0000000000..f84f93c5d2 --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/nullishCoalescing.qml @@ -0,0 +1,37 @@ +pragma Strict +pragma ValueTypeBehavior: Addressable + +import QtQuick + +GOL_Object { + id: root + + property int p1: 5 ?? -1 + property string p2: "6" ?? "-1" + + property var p3: undefined ?? undefined + property var p4: undefined ?? null + property var p5: undefined ?? -1 + property var p6: undefined ?? "-1" + + property var p7: null ?? undefined + property var p8: null ?? null + property var p9: null ?? -1 + property var p10: null ?? "-1" + + property int p11: GOL_Object.V2 ?? "-1" + + property int p12: 1 ?? 2 ?? 3 + property int p13: "1" ?? "2" ?? "3" + property var p14: undefined ?? "2" ?? undefined + property var p15: undefined ?? undefined ?? 1 + + property var p16 + property var p17 + + Component.onCompleted: { + p16 = (root.childA as GOL_Object)?.i ?? -1 + root.childA = root + p17 = (root.childA as GOL_Object)?.i ?? -1 + } +} diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index 668ed3efc8..3092f32b3a 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -173,6 +173,8 @@ private slots: void nullAccess(); void nullAccessInsideSignalHandler(); void nullComparison(); + void nullishCoalescing(); + void nullishCoalescing_data(); void numbersInJsPrimitive(); void objectInVar(); void objectLookupOnListElement(); @@ -3468,6 +3470,54 @@ void tst_QmlCppCodegen::nullComparison() QCOMPARE(o->property("z").toInt(), 18); } +void tst_QmlCppCodegen::nullishCoalescing_data() +{ + QTest::addColumn<QString>("propertyName"); + QTest::addColumn<QVariant>("expected"); + + const auto undefinedValue = QVariant(); + const auto nullValue = QVariant::fromMetaType(QMetaType::fromType<std::nullptr_t>(), nullptr); + + QTest::addRow("trivial-good-int") << "p1" << QVariant(5); + QTest::addRow("trivial-good-string") << "p2" << QVariant("6"); + + QTest::addRow("trivial-bad-undefined-undefined") << "p3" << undefinedValue; + QTest::addRow("trivial-bad-undefined-null") << "p4" << nullValue; + QTest::addRow("trivial-bad-undefined-int") << "p5" << QVariant(-1); + QTest::addRow("trivial-bad-undefined-string") << "p6" << QVariant("-1"); + + QTest::addRow("trivial-bad-null-undefined") << "p7" << undefinedValue; + QTest::addRow("trivial-bad-null-null") << "p8" << nullValue; + QTest::addRow("trivial-bad-null-int") << "p9" << QVariant(-1); + QTest::addRow("trivial-bad-null-string") << "p10" << QVariant("-1"); + + QTest::addRow("enum1") << "p11" << QVariant(1); + + QTest::addRow("multiple ?? int") << "p12" << QVariant(1); + QTest::addRow("multiple ?? string") << "p13" << QVariant("1"); + QTest::addRow("multiple ?? mixed2") << "p14" << QVariant("2"); + QTest::addRow("multiple ?? mixed3") << "p15" << QVariant(1); + + QTest::addRow("optional + nullish bad") << "p16" << QVariant(-1); + QTest::addRow("optional + nullish good") << "p17" << QVariant(5); +} + +void tst_QmlCppCodegen::nullishCoalescing() +{ + QQmlEngine engine; + const QUrl document(u"qrc:/qt/qml/TestTypes/nullishCoalescing.qml"_s); + QQmlComponent c(&engine, document); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer<QObject> o(c.create()); + QVERIFY(o); + + QFETCH(QString, propertyName); + QFETCH(QVariant, expected); + + QVariant actual = o->property(propertyName.toLocal8Bit()); + QCOMPARE(actual, expected); +} + void tst_QmlCppCodegen::numbersInJsPrimitive() { QQmlEngine engine; |