aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlproperty
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-10-11 13:59:46 +0200
committerUlf Hermann <ulf.hermann@qt.io>2021-10-13 14:03:41 +0200
commitf2a15482ddd289a36b04316a2b6ebed83eb017c5 (patch)
treeb2588d056ae5a80ac4b726571f6e1e3b0e362f8c /tests/auto/qml/qqmlproperty
parent4e645c7c33b03adf2d9161b00d00267add0aa373 (diff)
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 <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlproperty')
-rw-r--r--tests/auto/qml/qqmlproperty/data/listBehaviorAppendPragma.qml39
-rw-r--r--tests/auto/qml/qqmlproperty/data/listBehaviorFail1.qml3
-rw-r--r--tests/auto/qml/qqmlproperty/data/listBehaviorFail2.qml5
-rw-r--r--tests/auto/qml/qqmlproperty/data/listBehaviorReplaceIfNotDefaultPragma.qml39
-rw-r--r--tests/auto/qml/qqmlproperty/data/listBehaviorReplacePragma.qml39
-rw-r--r--tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp49
6 files changed, 174 insertions, 0 deletions
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<QtObject> defaultList
+ }
+
+ component MyChild: WithDefault {
+ QtObject { objectName: "default1" }
+
+ property list<QtObject> myList: [
+ QtObject { objectName: "test1" }
+ ]
+ }
+
+ property MyChild myChild1: MyChild {
+ myList: [
+ QtObject { objectName: "test2" }
+ ]
+
+ QtObject { objectName: "default2" }
+ }
+
+ property MyChild myChild2: MyChild {
+ property list<QtObject> 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<QtObject> defaultList
+ }
+
+ component MyChild: WithDefault {
+ QtObject { objectName: "default1" }
+
+ property list<QtObject> myList: [
+ QtObject { objectName: "test1" }
+ ]
+ }
+
+ property MyChild myChild1: MyChild {
+ myList: [
+ QtObject { objectName: "test2" }
+ ]
+
+ QtObject { objectName: "default2" }
+ }
+
+ property MyChild myChild2: MyChild {
+ property list<QtObject> 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<QtObject> defaultList
+ }
+
+ component MyChild: WithDefault {
+ QtObject { objectName: "default1" }
+
+ property list<QtObject> myList: [
+ QtObject { objectName: "test1" }
+ ]
+ }
+
+ property MyChild myChild1: MyChild {
+ myList: [
+ QtObject { objectName: "test2" }
+ ]
+
+ QtObject { objectName: "default2" }
+ }
+
+ property MyChild myChild2: MyChild {
+ property list<QtObject> 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<QObject> 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<QObject> 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<QObject> 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()