From 2ded1317af10b4565d497e4e1b8b6da85c49904f Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Mon, 27 Dec 2010 14:56:18 -0300 Subject: QScriptValueIterator: fix iterateArray and iterateString tests This removes the assumption that the properties will be ordered by index and that length will be visited after the indexes, which are not part of QScriptValueIterator API. Reviewed-by: Kent Hansen --- .../tst_qscriptvalueiterator.cpp | 230 +++++++++++++-------- 1 file changed, 143 insertions(+), 87 deletions(-) diff --git a/tests/auto/qscriptvalueiterator/tst_qscriptvalueiterator.cpp b/tests/auto/qscriptvalueiterator/tst_qscriptvalueiterator.cpp index eb7f94f..03e5c0f 100644 --- a/tests/auto/qscriptvalueiterator/tst_qscriptvalueiterator.cpp +++ b/tests/auto/qscriptvalueiterator/tst_qscriptvalueiterator.cpp @@ -212,106 +212,138 @@ void tst_QScriptValueIterator::iterateBackward() void tst_QScriptValueIterator::iterateArray_data() { - QTest::addColumn("inputPropertyNames"); - QTest::addColumn("inputPropertyValues"); QTest::addColumn("propertyNames"); QTest::addColumn("propertyValues"); - QTest::newRow("no elements") << QStringList() << QStringList() << QStringList() << QStringList(); + QTest::newRow("no elements") << QStringList() << QStringList(); QTest::newRow("0=foo, 1=barr") << (QStringList() << "0" << "1") - << (QStringList() << "foo" << "bar") - << (QStringList() << "0" << "1") << (QStringList() << "foo" << "bar"); QTest::newRow("0=foo, 3=barr") << (QStringList() << "0" << "1" << "2" << "3") - << (QStringList() << "foo" << "" << "" << "bar") - << (QStringList() << "0" << "1" << "2" << "3") << (QStringList() << "foo" << "" << "" << "bar"); } void tst_QScriptValueIterator::iterateArray() { - QFETCH(QStringList, inputPropertyNames); - QFETCH(QStringList, inputPropertyValues); QFETCH(QStringList, propertyNames); QFETCH(QStringList, propertyValues); QScriptEngine engine; QScriptValue array = engine.newArray(); - for (int i = 0; i < inputPropertyNames.size(); ++i) { - array.setProperty(inputPropertyNames.at(i), inputPropertyValues.at(i)); + + // Fill the array + for (int i = 0; i < propertyNames.size(); ++i) { + array.setProperty(propertyNames.at(i), propertyValues.at(i)); } + // Iterate thru array properties. Note that the QScriptValueIterator doesn't guarantee + // any order on the iteration! int length = array.property("length").toInt32(); QCOMPARE(length, propertyNames.size()); + + bool iteratedThruLength = false; + QHash arrayProperties; QScriptValueIterator it(array); - for (int i = 0; i < length; ++i) { - QCOMPARE(it.hasNext(), true); + + // Iterate forward + while (it.hasNext()) { it.next(); - QCOMPARE(it.name(), propertyNames.at(i)); - QCOMPARE(it.flags(), array.propertyFlags(propertyNames.at(i))); - QVERIFY(it.value().strictlyEquals(array.property(propertyNames.at(i)))); - QCOMPARE(it.value().toString(), propertyValues.at(i)); + + const QString name = it.name(); + if (name == QString::fromLatin1("length")) { + QVERIFY(it.value().isNumber()); + QCOMPARE(it.value().toInt32(), length); + QCOMPARE(it.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); + QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); + iteratedThruLength = true; + continue; + } + + // Storing the properties we iterate in a hash to compare with test data. + QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); + arrayProperties.insert(name, it.value()); + QCOMPARE(it.flags(), array.propertyFlags(name)); + QVERIFY(it.value().strictlyEquals(array.property(name))); } - QVERIFY(it.hasNext()); - it.next(); - QCOMPARE(it.name(), QString::fromLatin1("length")); - QVERIFY(it.value().isNumber()); - QCOMPARE(it.value().toInt32(), length); - QCOMPARE(it.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); - it.previous(); - QCOMPARE(it.hasPrevious(), length > 0); - for (int i = length - 1; i >= 0; --i) { - it.previous(); - QCOMPARE(it.name(), propertyNames.at(i)); - QCOMPARE(it.flags(), array.propertyFlags(propertyNames.at(i))); - QVERIFY(it.value().strictlyEquals(array.property(propertyNames.at(i)))); - QCOMPARE(it.value().toString(), propertyValues.at(i)); - QCOMPARE(it.hasPrevious(), i > 0); + // Verify properties + QVERIFY(iteratedThruLength); + QCOMPARE(arrayProperties.size(), propertyNames.size()); + for (int i = 0; i < propertyNames.size(); ++i) { + QVERIFY(arrayProperties.contains(propertyNames.at(i))); + QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } - QCOMPARE(it.hasPrevious(), false); - // hasNext() and hasPrevious() cache their result; verify that the result is in sync - if (length > 1) { - QVERIFY(it.hasNext()); - it.next(); - QCOMPARE(it.name(), QString::fromLatin1("0")); - QVERIFY(it.hasNext()); + // Iterate backwards + arrayProperties.clear(); + iteratedThruLength = false; + it.toBack(); + + while (it.hasPrevious()) { it.previous(); - QCOMPARE(it.name(), QString::fromLatin1("0")); - QVERIFY(!it.hasPrevious()); - it.next(); - QCOMPARE(it.name(), QString::fromLatin1("0")); - QVERIFY(it.hasPrevious()); - it.next(); - QCOMPARE(it.name(), QString::fromLatin1("1")); - } - { - // same test as object: - QScriptValue originalArray = engine.newArray(); - for (int i = 0; i < inputPropertyNames.size(); ++i) { - originalArray.setProperty(inputPropertyNames.at(i), inputPropertyValues.at(i)); + + const QString name = it.name(); + if (name == QString::fromLatin1("length")) { + QVERIFY(it.value().isNumber()); + QCOMPARE(it.value().toInt32(), length); + QCOMPARE(it.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); + QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); + iteratedThruLength = true; + continue; } - QScriptValue array = originalArray.toObject(); - int length = array.property("length").toInt32(); - QCOMPARE(length, propertyNames.size()); - QScriptValueIterator it(array); - for (int i = 0; i < length; ++i) { - QCOMPARE(it.hasNext(), true); - it.next(); - QCOMPARE(it.name(), propertyNames.at(i)); - QCOMPARE(it.flags(), array.propertyFlags(propertyNames.at(i))); - QVERIFY(it.value().strictlyEquals(array.property(propertyNames.at(i)))); - QCOMPARE(it.value().toString(), propertyValues.at(i)); + + // Storing the properties we iterate in a hash to compare with test data. + QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); + arrayProperties.insert(name, it.value()); + QCOMPARE(it.flags(), array.propertyFlags(name)); + QVERIFY(it.value().strictlyEquals(array.property(name))); + } + + // Verify properties + QVERIFY(iteratedThruLength); + QCOMPARE(arrayProperties.size(), propertyNames.size()); + for (int i = 0; i < propertyNames.size(); ++i) { + QVERIFY(arrayProperties.contains(propertyNames.at(i))); + QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); + } + + // ### Do we still need this test? + // Forward test again but as object + arrayProperties.clear(); + iteratedThruLength = false; + QScriptValue arrayObject = engine.toObject(array); + QScriptValueIterator it2(arrayObject); + + while (it2.hasNext()) { + it2.next(); + + const QString name = it2.name(); + if (name == QString::fromLatin1("length")) { + QVERIFY(it2.value().isNumber()); + QCOMPARE(it2.value().toInt32(), length); + QCOMPARE(it2.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); + QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); + iteratedThruLength = true; + continue; } - QCOMPARE(it.hasNext(), true); - it.next(); - QCOMPARE(it.name(), QString::fromLatin1("length")); + + // Storing the properties we iterate in a hash to compare with test data. + QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); + arrayProperties.insert(name, it2.value()); + QCOMPARE(it2.flags(), arrayObject.propertyFlags(name)); + QVERIFY(it2.value().strictlyEquals(arrayObject.property(name))); + } + + // Verify properties + QVERIFY(iteratedThruLength); + QCOMPARE(arrayProperties.size(), propertyNames.size()); + for (int i = 0; i < propertyNames.size(); ++i) { + QVERIFY(arrayProperties.contains(propertyNames.at(i))); + QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } } @@ -419,35 +451,59 @@ void tst_QScriptValueIterator::iterateString() QScriptValue str = QScriptValue(&engine, QString::fromLatin1("ciao")); QVERIFY(str.isString()); QScriptValue obj = str.toObject(); + QVERIFY(obj.property("length").isNumber()); int length = obj.property("length").toInt32(); QCOMPARE(length, 4); + QScriptValueIterator it(obj); - for (int i = 0; i < length; ++i) { - QCOMPARE(it.hasNext(), true); - QString indexStr = QScriptValue(&engine, i).toString(); + QHash stringProperties; + bool iteratedThruLength = false; + + while (it.hasNext()) { it.next(); - QCOMPARE(it.name(), indexStr); - QCOMPARE(it.flags(), obj.propertyFlags(indexStr)); - QCOMPARE(it.value().strictlyEquals(obj.property(indexStr)), true); + const QString name = it.name(); + + if (name == QString::fromLatin1("length")) { + QVERIFY(it.value().isNumber()); + QCOMPARE(it.value().toInt32(), length); + QCOMPARE(it.flags(), QScriptValue::ReadOnly | QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); + QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); + iteratedThruLength = true; + continue; + } + + QVERIFY2(!stringProperties.contains(name), "property appeared more than once during iteration."); + stringProperties.insert(name, it.value()); + QCOMPARE(it.flags(), obj.propertyFlags(name)); + QVERIFY(it.value().strictlyEquals(obj.property(name))); } - QVERIFY(it.hasNext()); - it.next(); - QCOMPARE(it.name(), QString::fromLatin1("length")); - QVERIFY(it.value().isNumber()); - QCOMPARE(it.value().toInt32(), length); - QCOMPARE(it.flags(), QScriptValue::ReadOnly | QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); - it.previous(); - QCOMPARE(it.hasPrevious(), length > 0); - for (int i = length - 1; i >= 0; --i) { + QVERIFY(iteratedThruLength); + QCOMPARE(stringProperties.size(), length); + + // And going backwards + iteratedThruLength = false; + stringProperties.clear(); + it.toBack(); + + while (it.hasPrevious()) { it.previous(); - QString indexStr = QScriptValue(&engine, i).toString(); - QCOMPARE(it.name(), indexStr); - QCOMPARE(it.flags(), obj.propertyFlags(indexStr)); - QCOMPARE(it.value().strictlyEquals(obj.property(indexStr)), true); - QCOMPARE(it.hasPrevious(), i > 0); + const QString name = it.name(); + + if (name == QString::fromLatin1("length")) { + QVERIFY(it.value().isNumber()); + QCOMPARE(it.value().toInt32(), length); + QCOMPARE(it.flags(), QScriptValue::ReadOnly | QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); + QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); + iteratedThruLength = true; + continue; + } + + QVERIFY2(!stringProperties.contains(name), "property appeared more than once during iteration."); + stringProperties.insert(name, it.value()); + QCOMPARE(it.flags(), obj.propertyFlags(name)); + QVERIFY(it.value().strictlyEquals(obj.property(name))); } - QCOMPARE(it.hasPrevious(), false); } static QScriptValue myGetterSetter(QScriptContext *ctx, QScriptEngine *) -- cgit v0.12 From c848769c06515675caa2dfaf32c6e20be87e869f Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Wed, 29 Dec 2010 15:10:03 -0300 Subject: QtScript: add more tests to QObject binding This adds a few more tests to exercise dynamic properties and child objects as properties of QObject binding. Some of these were implicitly tested elsewhere, but is good to have the "properties" clearly stated in our test suite. Reviewed-by: Jedrzej Nowacki --- .../qscriptextqobject/tst_qscriptextqobject.cpp | 111 +++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp b/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp index e9d2d1f..cb29586 100644 --- a/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp +++ b/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp @@ -543,6 +543,10 @@ private slots: void getSetStaticProperty_customGetterSetter(); void getSetStaticProperty_methodPersistence(); void getSetDynamicProperty(); + void getSetDynamicProperty_deleteFromCpp(); + void getSetDynamicProperty_hideChildObject(); + void getSetDynamicProperty_setBeforeGet(); + void getSetDynamicProperty_doNotHideJSProperty(); void getSetChildren(); void callQtInvokable(); void connectAndDisconnect(); @@ -555,6 +559,7 @@ private slots: void transferInvokable(); void findChild(); void findChildren(); + void childObjects(); void overloadedSlots(); void enumerate_data(); void enumerate(); @@ -1035,6 +1040,69 @@ void tst_QScriptExtQObject::getSetDynamicProperty() QCOMPARE(m_engine->evaluate("myObject.hasOwnProperty('dynamicProperty')").toBoolean(), false); } +void tst_QScriptExtQObject::getSetDynamicProperty_deleteFromCpp() +{ + QScriptValue val = m_engine->newQObject(m_myObject); + + m_myObject->setProperty("dynamicFromCpp", 1234); + QVERIFY(val.property("dynamicFromCpp").strictlyEquals(QScriptValue(m_engine, 1234))); + + m_myObject->setProperty("dynamicFromCpp", 4321); + QVERIFY(val.property("dynamicFromCpp").strictlyEquals(QScriptValue(m_engine, 4321))); + QCOMPARE(m_myObject->property("dynamicFromCpp").toInt(), 4321); + + // In this case we delete the property from C++ + m_myObject->setProperty("dynamicFromCpp", QVariant()); + QVERIFY(!m_myObject->property("dynamicFromCpp").isValid()); + QVERIFY(!val.property("dynamicFromCpp").isValid()); +} + +void tst_QScriptExtQObject::getSetDynamicProperty_hideChildObject() +{ + QScriptValue val = m_engine->newQObject(m_myObject); + + // Add our named child and access it + QObject *child = new QObject(m_myObject); + child->setObjectName("testName"); + QCOMPARE(val.property("testName").toQObject(), child); + + // Dynamic properties have precedence, hiding the child object + m_myObject->setProperty("testName", 42); + QVERIFY(val.property("testName").strictlyEquals(QScriptValue(m_engine, 42))); + + // Remove dynamic property + m_myObject->setProperty("testName", QVariant()); + QCOMPARE(val.property("testName").toQObject(), child); +} + +void tst_QScriptExtQObject::getSetDynamicProperty_setBeforeGet() +{ + QScriptValue val = m_engine->newQObject(m_myObject); + + m_myObject->setProperty("dynamic", 1111); + val.setProperty("dynamic", 42); + + QVERIFY(val.property("dynamic").strictlyEquals(QScriptValue(m_engine, 42))); + QCOMPARE(m_myObject->property("dynamic").toInt(), 42); +} + +void tst_QScriptExtQObject::getSetDynamicProperty_doNotHideJSProperty() +{ + QScriptValue val = m_engine->newQObject(m_myObject); + + // Set property on JS and dynamically on our QObject + val.setProperty("x", 42); + m_myObject->setProperty("x", 2222); + + QEXPECT_FAIL("", "", Continue); + + // JS should see the original JS value + QVERIFY(val.property("x").strictlyEquals(QScriptValue(m_engine, 42))); + + // The dynamic property is intact + QCOMPARE(m_myObject->property("x").toInt(), 2222); +} + void tst_QScriptExtQObject::getSetChildren() { QScriptValue mobj = m_engine->evaluate("myObject"); @@ -2661,6 +2729,49 @@ void tst_QScriptExtQObject::findChildren() delete child; } +void tst_QScriptExtQObject::childObjects() +{ + QObject *child1 = new QObject(m_myObject); + child1->setObjectName("child1"); + QObject *child2 = new QObject(m_myObject); + QScriptValue wrapped = m_engine->newQObject(m_myObject); + + QVERIFY(wrapped.property("child1").isQObject()); + QCOMPARE(wrapped.property("child1").toQObject(), child1); + QVERIFY(!wrapped.property("child2").isQObject()); + QVERIFY(!wrapped.property("child2").isValid()); + + // Setting the name later + child2->setObjectName("child2"); + + QVERIFY(wrapped.property("child1").isQObject()); + QCOMPARE(wrapped.property("child1").toQObject(), child1); + QVERIFY(wrapped.property("child2").isQObject()); + QCOMPARE(wrapped.property("child2").toQObject(), child2); + + // Adding a child later + QObject *child3 = new QObject(m_myObject); + child3->setObjectName("child3"); + + QVERIFY(wrapped.property("child3").isQObject()); + QCOMPARE(wrapped.property("child3").toQObject(), child3); + + // Changing a child name + child1->setObjectName("anotherName"); + + QVERIFY(!wrapped.property("child1").isValid()); + QVERIFY(wrapped.property("anotherName").isQObject()); + QCOMPARE(wrapped.property("anotherName").toQObject(), child1); + + // Creating another object wrapper excluding child from + // properties. + QScriptValue wrapped2 = m_engine->newQObject(m_myObject, QScriptEngine::QtOwnership, QScriptEngine::ExcludeChildObjects); + + QVERIFY(!wrapped2.property("anotherName").isValid()); + QVERIFY(!wrapped2.property("child2").isValid()); + QVERIFY(!wrapped2.property("child3").isValid()); +} + void tst_QScriptExtQObject::overloadedSlots() { // should pick myOverloadedSlot(double) -- cgit v0.12