From f2a15482ddd289a36b04316a2b6ebed83eb017c5 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 11 Oct 2021 13:59:46 +0200 Subject: Add a Pragma for list assign behavior [ChangeLog][QtQml] You can now specify the list property assignment behavior in QML using the "ListPropertyAssignBehavior" pragma. This is analogous to the macros you can use in C++. Fixes: QTBUG-93642 Change-Id: I9bdcf198031f1e24891f947b0990a3253d29a998 Reviewed-by: Fabian Kosmale --- .../qqmlproperty/data/listBehaviorAppendPragma.qml | 39 +++++++++++++++++ .../qml/qqmlproperty/data/listBehaviorFail1.qml | 3 ++ .../qml/qqmlproperty/data/listBehaviorFail2.qml | 5 +++ .../data/listBehaviorReplaceIfNotDefaultPragma.qml | 39 +++++++++++++++++ .../data/listBehaviorReplacePragma.qml | 39 +++++++++++++++++ tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp | 49 ++++++++++++++++++++++ 6 files changed, 174 insertions(+) create mode 100644 tests/auto/qml/qqmlproperty/data/listBehaviorAppendPragma.qml create mode 100644 tests/auto/qml/qqmlproperty/data/listBehaviorFail1.qml create mode 100644 tests/auto/qml/qqmlproperty/data/listBehaviorFail2.qml create mode 100644 tests/auto/qml/qqmlproperty/data/listBehaviorReplaceIfNotDefaultPragma.qml create mode 100644 tests/auto/qml/qqmlproperty/data/listBehaviorReplacePragma.qml (limited to 'tests/auto/qml/qqmlproperty') diff --git a/tests/auto/qml/qqmlproperty/data/listBehaviorAppendPragma.qml b/tests/auto/qml/qqmlproperty/data/listBehaviorAppendPragma.qml new file mode 100644 index 0000000000..f7844564a8 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/listBehaviorAppendPragma.qml @@ -0,0 +1,39 @@ +pragma ListPropertyAssignBehavior: Append + +import QtQml + +QtObject { + component WithDefault: QtObject { + default property list defaultList + } + + component MyChild: WithDefault { + QtObject { objectName: "default1" } + + property list myList: [ + QtObject { objectName: "test1" } + ] + } + + property MyChild myChild1: MyChild { + myList: [ + QtObject { objectName: "test2" } + ] + + QtObject { objectName: "default2" } + } + + property MyChild myChild2: MyChild { + property list myOwnList: [ + QtObject { property string str: "test3" } + ] + myList: myOwnList + defaultList: myOwnList + } + + property int length1: myChild1.myList.length + property int length2: myChild2.myList.length + + property int default1: myChild1.defaultList.length + property int default2: myChild2.defaultList.length +} diff --git a/tests/auto/qml/qqmlproperty/data/listBehaviorFail1.qml b/tests/auto/qml/qqmlproperty/data/listBehaviorFail1.qml new file mode 100644 index 0000000000..2723249e48 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/listBehaviorFail1.qml @@ -0,0 +1,3 @@ +pragma ListPropertyAssignBehavior: Foo +import QtQml +QtObject {} diff --git a/tests/auto/qml/qqmlproperty/data/listBehaviorFail2.qml b/tests/auto/qml/qqmlproperty/data/listBehaviorFail2.qml new file mode 100644 index 0000000000..63b79f85d1 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/listBehaviorFail2.qml @@ -0,0 +1,5 @@ +pragma ListPropertyAssignBehavior: Append +pragma Singleton +pragma ListPropertyAssignBehavior: Replace +import QtQml +QtObject {} diff --git a/tests/auto/qml/qqmlproperty/data/listBehaviorReplaceIfNotDefaultPragma.qml b/tests/auto/qml/qqmlproperty/data/listBehaviorReplaceIfNotDefaultPragma.qml new file mode 100644 index 0000000000..ea4de1d8a4 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/listBehaviorReplaceIfNotDefaultPragma.qml @@ -0,0 +1,39 @@ +pragma ListPropertyAssignBehavior: ReplaceIfNotDefault + +import QtQml + +QtObject { + component WithDefault: QtObject { + default property list defaultList + } + + component MyChild: WithDefault { + QtObject { objectName: "default1" } + + property list myList: [ + QtObject { objectName: "test1" } + ] + } + + property MyChild myChild1: MyChild { + myList: [ + QtObject { objectName: "test2" } + ] + + QtObject { objectName: "default2" } + } + + property MyChild myChild2: MyChild { + property list myOwnList: [ + QtObject { property string str: "test3" } + ] + myList: myOwnList + defaultList: myOwnList + } + + property int length1: myChild1.myList.length + property int length2: myChild2.myList.length + + property int default1: myChild1.defaultList.length + property int default2: myChild2.defaultList.length +} diff --git a/tests/auto/qml/qqmlproperty/data/listBehaviorReplacePragma.qml b/tests/auto/qml/qqmlproperty/data/listBehaviorReplacePragma.qml new file mode 100644 index 0000000000..e960a7cac5 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/listBehaviorReplacePragma.qml @@ -0,0 +1,39 @@ +pragma ListPropertyAssignBehavior: Replace + +import QtQml + +QtObject { + component WithDefault: QtObject { + default property list defaultList + } + + component MyChild: WithDefault { + QtObject { objectName: "default1" } + + property list myList: [ + QtObject { objectName: "test1" } + ] + } + + property MyChild myChild1: MyChild { + myList: [ + QtObject { objectName: "test2" } + ] + + QtObject { objectName: "default2" } + } + + property MyChild myChild2: MyChild { + property list myOwnList: [ + QtObject { property string str: "test3" } + ] + myList: myOwnList + defaultList: myOwnList + } + + property int length1: myChild1.myList.length + property int length2: myChild2.myList.length + + property int default1: myChild1.defaultList.length + property int default2: myChild2.defaultList.length +} diff --git a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp index fc045b777c..9cf622c2d7 100644 --- a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp +++ b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp @@ -1724,6 +1724,55 @@ void tst_qqmlproperty::listOverrideBehavior() QVERIFY(alwaysReplaceContainer != nullptr); QQmlListReference alwaysReplaceChildrenList(alwaysReplaceContainer, "children"); QCOMPARE(alwaysReplaceChildrenList.count(), 2); + + { + QQmlComponent appendQml(&engine, testFileUrl("listBehaviorAppendPragma.qml")); + QVERIFY2(appendQml.isReady(), qPrintable(appendQml.errorString())); + QScopedPointer o(appendQml.create()); + QVERIFY(o); + QCOMPARE(o->property("length1").toInt(), 2); + QCOMPARE(o->property("length2").toInt(), 1); + QCOMPARE(o->property("default1").toInt(), 2); + QCOMPARE(o->property("default2").toInt(), 1); + } + + { + QQmlComponent replaceQml(&engine, testFileUrl("listBehaviorReplacePragma.qml")); + QVERIFY2(replaceQml.isReady(), qPrintable(replaceQml.errorString())); + QScopedPointer o(replaceQml.create()); + QVERIFY(o); + QCOMPARE(o->property("length1").toInt(), 1); + QCOMPARE(o->property("length2").toInt(), 1); + QCOMPARE(o->property("default1").toInt(), 1); + QCOMPARE(o->property("default2").toInt(), 1); + } + + { + QQmlComponent replaceIfNotDefaultQml( + &engine, testFileUrl("listBehaviorReplaceIfNotDefaultPragma.qml")); + QVERIFY2(replaceIfNotDefaultQml.isReady(), + qPrintable(replaceIfNotDefaultQml.errorString())); + QScopedPointer o(replaceIfNotDefaultQml.create()); + QVERIFY(o); + QCOMPARE(o->property("length1").toInt(), 1); + QCOMPARE(o->property("length2").toInt(), 1); + QCOMPARE(o->property("default1").toInt(), 2); + QCOMPARE(o->property("default2").toInt(), 1); + } + + { + QQmlComponent fail1(&engine, testFileUrl("listBehaviorFail1.qml")); + QVERIFY(fail1.isError()); + QVERIFY(fail1.errorString().contains( + QStringLiteral("Unknown list property assign behavior 'Foo' in pragma"))); + } + + { + QQmlComponent fail2(&engine, testFileUrl("listBehaviorFail2.qml")); + QVERIFY(fail2.isError()); + QVERIFY(fail2.errorString().contains( + QStringLiteral("Multiple list property assign behavior pragmas found"))); + } } void tst_qqmlproperty::urlHandling_data() -- cgit v1.2.3