aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
diff options
context:
space:
mode:
authorOlivier De Cannière <olivier.decanniere@qt.io>2023-11-24 18:13:36 +0100
committerUlf Hermann <ulf.hermann@qt.io>2024-02-16 20:31:30 +0100
commit2b336217b0d8c7406e53624f3f0d5b1285f99144 (patch)
treef018dec08def30c796b7c702705ca7c8c1c4d779 /tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
parentac382bd9aaff68be786c5e8b1ded7d91f3369887 (diff)
Compiler: Add tests for the nullish coalescing operator ??
Now that initial support for optional chaining was added, it would be good to add tests ensuring the nullish coalescing works as expected. Add some tests to enshrine the behavior. The compiler is currently not smart enough to detect that "(Anything as int) ?? 1" will always return an int. It returns an optional int instead. Created QTBUG-119437. Task-number: QTBUG-119437 Change-Id: If2d72b0c29e5844cd962dbf92406bfa9181a1bc7 Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io> 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.cpp50
1 files changed, 50 insertions, 0 deletions
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;