Fix regression in QJSValueIterator::next() since Qt 5.1.1
authorGunnar Sletta <gunnar.sletta@jollamobile.com>
Wed, 14 May 2014 07:30:37 +0000 (09:30 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 14 May 2014 10:16:12 +0000 (12:16 +0200)
Iteration on the form "while (next) { .. }" would skip
the last element.

Change-Id: I50692a5a75e23e423e82b7a39e1892f505e4c612
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/jsapi/qjsvalueiterator.cpp
tests/auto/qml/qjsvalueiterator/tst_qjsvalueiterator.cpp

index 9e02310..6dcfafa 100644 (file)
@@ -135,13 +135,10 @@ bool QJSValueIterator::hasNext() const
 
 /*!
     Advances the iterator by one position.
-    Returns true if there is at least one item ahead of the iterator
-    (i.e. the iterator is \e not at the back of the property sequence);
+    Returns true if there was at least one item ahead of the iterator
+    (i.e. the iterator was \e not already at the back of the property sequence);
     otherwise returns false.
 
-    Calling this function on an iterator located at the back of the
-    container leads to undefined results.
-
     \sa hasNext(), name()
 */
 bool QJSValueIterator::next()
@@ -159,7 +156,7 @@ bool QJSValueIterator::next()
     QV4::Scope scope(v4);
     QV4::Scoped<QV4::ForEachIteratorObject> it(scope, d_ptr->iterator.value());
     it->it.next(d_ptr->nextName, &d_ptr->nextIndex, &d_ptr->nextProperty, &d_ptr->nextAttributes);
-    return !!d_ptr->nextName || d_ptr->nextIndex != UINT_MAX;
+    return !!d_ptr->currentName || d_ptr->currentIndex != UINT_MAX;
 }
 
 /*!
index fa6ac32..73c880c 100644 (file)
@@ -66,6 +66,7 @@ private slots:
     void assignObjectToIterator();
     void iterateNonObject();
     void iterateOverObjectFromDeletedEngine();
+    void iterateWithNext();
 };
 
 tst_QJSValueIterator::tst_QJSValueIterator()
@@ -516,5 +517,31 @@ void tst_QJSValueIterator::iterateOverObjectFromDeletedEngine()
 
 }
 
+void tst_QJSValueIterator::iterateWithNext()
+{
+    QJSEngine engine;
+    QJSValue value = engine.newObject();
+    value.setProperty("one", 1);
+    value.setProperty("two", 2);
+    value.setProperty("three", 3);
+
+    QStringList list;
+    list << QStringLiteral("one") << QStringLiteral("three") << QStringLiteral("two");
+
+    int counter = 0;
+    QJSValueIterator it(value);
+    QStringList actualList;
+    while (it.next()) {
+        ++counter;
+        actualList << it.name();
+    }
+
+    std::sort(actualList.begin(), actualList.end());
+
+    QCOMPARE(counter, 3);
+    QCOMPARE(list, actualList);
+
+}
+
 QTEST_MAIN(tst_QJSValueIterator)
 #include "tst_qjsvalueiterator.moc"