Itemmodel tests: remove foreach usage

Replace foreach with range-based for loop, replace some int values with
the correct Qt flag enum as drive-by.

Change-Id: I41c52f6ae6c537fa9ad4f9e169485533936952d1
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-08-18 10:53:01 +02:00
parent f5c118b6b8
commit 427995a518
3 changed files with 60 additions and 55 deletions

View File

@ -1830,7 +1830,7 @@ void ListenerObject::slotAboutToBeReset()
void ListenerObject::slotReset() void ListenerObject::slotReset()
{ {
foreach (const QModelIndex &idx, m_persistentIndexes) { for (const auto &idx : qAsConst(m_persistentIndexes)) {
QVERIFY(!idx.isValid()); QVERIFY(!idx.isValid());
} }
} }

View File

@ -32,6 +32,9 @@
#include <algorithm> #include <algorithm>
Q_DECLARE_METATYPE(QItemSelectionModel::SelectionFlag)
Q_DECLARE_METATYPE(Qt::SortOrder)
class tst_QItemSelectionModel : public QObject class tst_QItemSelectionModel : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -1463,7 +1466,7 @@ void tst_QItemSelectionModel::persistentselections()
QFETCH(IntList, insertColumns); QFETCH(IntList, insertColumns);
QFETCH(IntList, deleteRows); QFETCH(IntList, deleteRows);
QFETCH(IntList, deleteColumns); QFETCH(IntList, deleteColumns);
QFETCH(PairList, expectedList); QFETCH(const PairList, expectedList);
// make sure the model is sane (5x5) // make sure the model is sane (5x5)
QCOMPARE(model->rowCount(QModelIndex()), 5); QCOMPARE(model->rowCount(QModelIndex()), 5);
@ -1504,7 +1507,7 @@ void tst_QItemSelectionModel::persistentselections()
// check that the selected items are the correct number and indexes // check that the selected items are the correct number and indexes
QModelIndexList selectedList = selection->selectedIndexes(); QModelIndexList selectedList = selection->selectedIndexes();
QCOMPARE(selectedList.count(), expectedList.count()); QCOMPARE(selectedList.count(), expectedList.count());
foreach(IntPair pair, expectedList) { for (const auto &pair : expectedList) {
QModelIndex index = model->index(pair.first, pair.second, QModelIndex()); QModelIndex index = model->index(pair.first, pair.second, QModelIndex());
QVERIFY(selectedList.contains(index)); QVERIFY(selectedList.contains(index));
} }
@ -1679,7 +1682,7 @@ void tst_QItemSelectionModel::modelLayoutChanged_data()
{ {
QTest::addColumn<IntListList>("items"); QTest::addColumn<IntListList>("items");
QTest::addColumn<IntPairPairList>("initialSelectedRanges"); QTest::addColumn<IntPairPairList>("initialSelectedRanges");
QTest::addColumn<int>("sortOrder"); QTest::addColumn<Qt::SortOrder>("sortOrder");
QTest::addColumn<int>("sortColumn"); QTest::addColumn<int>("sortColumn");
QTest::addColumn<IntPairPairList>("expectedSelectedRanges"); QTest::addColumn<IntPairPairList>("expectedSelectedRanges");
@ -1689,7 +1692,7 @@ void tst_QItemSelectionModel::modelLayoutChanged_data()
<< (IntList() << 3 << 2 << 1 << 0)) << (IntList() << 3 << 2 << 1 << 0))
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(0, 0), IntPair(3, 1))) << IntPairPair(IntPair(0, 0), IntPair(3, 1)))
<< int(Qt::DescendingOrder) << Qt::DescendingOrder
<< 0 << 0
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(0, 0), IntPair(3, 1))); << IntPairPair(IntPair(0, 0), IntPair(3, 1)));
@ -1699,7 +1702,7 @@ void tst_QItemSelectionModel::modelLayoutChanged_data()
<< (IntList() << 3 << 2 << 1 << 0)) << (IntList() << 3 << 2 << 1 << 0))
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(0, 0), IntPair(1, 1))) << IntPairPair(IntPair(0, 0), IntPair(1, 1)))
<< int(Qt::DescendingOrder) << Qt::DescendingOrder
<< 0 << 0
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(2, 0), IntPair(3, 1))); << IntPairPair(IntPair(2, 0), IntPair(3, 1)));
@ -1709,7 +1712,7 @@ void tst_QItemSelectionModel::modelLayoutChanged_data()
<< (IntList() << 3 << 2 << 1 << 0)) << (IntList() << 3 << 2 << 1 << 0))
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(1, 0), IntPair(2, 1))) << IntPairPair(IntPair(1, 0), IntPair(2, 1)))
<< int(Qt::DescendingOrder) << Qt::DescendingOrder
<< 0 << 0
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(1, 0), IntPair(2, 1))); << IntPairPair(IntPair(1, 0), IntPair(2, 1)));
@ -1720,7 +1723,7 @@ void tst_QItemSelectionModel::modelLayoutChanged_data()
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(1, 0), IntPair(1, 1)) << IntPairPair(IntPair(1, 0), IntPair(1, 1))
<< IntPairPair(IntPair(3, 0), IntPair(3, 1))) << IntPairPair(IntPair(3, 0), IntPair(3, 1)))
<< int(Qt::AscendingOrder) << Qt::AscendingOrder
<< 0 << 0
<< (IntPairPairList() << (IntPairPairList()
<< IntPairPair(IntPair(0, 0), IntPair(0, 1)) << IntPairPair(IntPair(0, 0), IntPair(0, 1))
@ -1730,8 +1733,8 @@ void tst_QItemSelectionModel::modelLayoutChanged_data()
void tst_QItemSelectionModel::modelLayoutChanged() void tst_QItemSelectionModel::modelLayoutChanged()
{ {
QFETCH(IntListList, items); QFETCH(IntListList, items);
QFETCH(IntPairPairList, initialSelectedRanges); QFETCH(const IntPairPairList, initialSelectedRanges);
QFETCH(int, sortOrder); QFETCH(Qt::SortOrder, sortOrder);
QFETCH(int, sortColumn); QFETCH(int, sortColumn);
QFETCH(IntPairPairList, expectedSelectedRanges); QFETCH(IntPairPairList, expectedSelectedRanges);
@ -1746,9 +1749,9 @@ void tst_QItemSelectionModel::modelLayoutChanged()
// select initial ranges // select initial ranges
QItemSelectionModel selectionModel(&model); QItemSelectionModel selectionModel(&model);
foreach (IntPairPair range, initialSelectedRanges) { for (const auto &range : initialSelectedRanges) {
IntPair tl = range.first; const auto &tl = range.first;
IntPair br = range.second; const auto &br = range.second;
QItemSelection selection( QItemSelection selection(
model.index(tl.first, tl.second), model.index(tl.first, tl.second),
model.index(br.first, br.second)); model.index(br.first, br.second));
@ -1756,7 +1759,7 @@ void tst_QItemSelectionModel::modelLayoutChanged()
} }
// sort the model // sort the model
model.sort(sortColumn, Qt::SortOrder(sortOrder)); model.sort(sortColumn, sortOrder);
// verify that selection is as expected // verify that selection is as expected
QItemSelection selection = selectionModel.selection(); QItemSelection selection = selectionModel.selection();
@ -2126,43 +2129,43 @@ void tst_QItemSelectionModel::merge_data()
{ {
QTest::addColumn<QItemSelection>("init"); QTest::addColumn<QItemSelection>("init");
QTest::addColumn<QItemSelection>("other"); QTest::addColumn<QItemSelection>("other");
QTest::addColumn<int>("command"); QTest::addColumn<QItemSelectionModel::SelectionFlag>("command");
QTest::addColumn<QItemSelection>("result"); QTest::addColumn<QItemSelection>("result");
QTest::newRow("Simple select") QTest::newRow("Simple select")
<< QItemSelection() << QItemSelection()
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< int(QItemSelectionModel::Select) << QItemSelectionModel::Select
<< QItemSelection(model->index(2, 1) , model->index(3, 4)); << QItemSelection(model->index(2, 1) , model->index(3, 4));
QTest::newRow("Simple deselect") QTest::newRow("Simple deselect")
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< int(QItemSelectionModel::Deselect) << QItemSelectionModel::Deselect
<< QItemSelection(); << QItemSelection();
QTest::newRow("Simple Toggle deselect") QTest::newRow("Simple Toggle deselect")
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< int(QItemSelectionModel::Toggle) << QItemSelectionModel::Toggle
<< QItemSelection(); << QItemSelection();
QTest::newRow("Simple Toggle select") QTest::newRow("Simple Toggle select")
<< QItemSelection() << QItemSelection()
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< int(QItemSelectionModel::Toggle) << QItemSelectionModel::Toggle
<< QItemSelection(model->index(2, 1) , model->index(3, 4)); << QItemSelection(model->index(2, 1) , model->index(3, 4));
QTest::newRow("Add select") QTest::newRow("Add select")
<< QItemSelection(model->index(2, 1) , model->index(3, 3)) << QItemSelection(model->index(2, 1) , model->index(3, 3))
<< QItemSelection(model->index(2, 2) , model->index(3, 4)) << QItemSelection(model->index(2, 2) , model->index(3, 4))
<< int(QItemSelectionModel::Select) << QItemSelectionModel::Select
<< QItemSelection(model->index(2, 1) , model->index(3, 4)); << QItemSelection(model->index(2, 1) , model->index(3, 4));
QTest::newRow("Deselect") QTest::newRow("Deselect")
<< QItemSelection(model->index(2, 1) , model->index(3, 4)) << QItemSelection(model->index(2, 1) , model->index(3, 4))
<< QItemSelection(model->index(2, 2) , model->index(3, 4)) << QItemSelection(model->index(2, 2) , model->index(3, 4))
<< int(QItemSelectionModel::Deselect) << QItemSelectionModel::Deselect
<< QItemSelection(model->index(2, 1) , model->index(3, 1)); << QItemSelection(model->index(2, 1) , model->index(3, 1));
QItemSelection r1(model->index(2, 1) , model->index(3, 1)); QItemSelection r1(model->index(2, 1) , model->index(3, 1));
@ -2170,7 +2173,7 @@ void tst_QItemSelectionModel::merge_data()
QTest::newRow("Toggle") QTest::newRow("Toggle")
<< QItemSelection(model->index(2, 1) , model->index(3, 3)) << QItemSelection(model->index(2, 1) , model->index(3, 3))
<< QItemSelection(model->index(2, 2) , model->index(3, 4)) << QItemSelection(model->index(2, 2) , model->index(3, 4))
<< int(QItemSelectionModel::Toggle) << QItemSelectionModel::Toggle
<< r1; << r1;
} }
@ -2178,15 +2181,18 @@ void tst_QItemSelectionModel::merge()
{ {
QFETCH(QItemSelection, init); QFETCH(QItemSelection, init);
QFETCH(QItemSelection, other); QFETCH(QItemSelection, other);
QFETCH(int, command); QFETCH(QItemSelectionModel::SelectionFlag, command);
QFETCH(QItemSelection, result); QFETCH(QItemSelection, result);
init.merge(other, QItemSelectionModel::SelectionFlags(command)); init.merge(other, command);
foreach(const QModelIndex &idx, init.indexes()) auto verify = [](const QModelIndexList &a, const QItemSelection &b)
QVERIFY(result.contains(idx)); {
foreach(const QModelIndex &idx, result.indexes()) for (const QModelIndex &idx : a)
QVERIFY(init.contains(idx)); QVERIFY(b.contains(idx));
};
verify(init.indexes(), result);
verify(result.indexes(), init);
} }
void tst_QItemSelectionModel::isRowSelected() void tst_QItemSelectionModel::isRowSelected()
@ -2267,13 +2273,12 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected1()
proxy.setFilterRegularExpression(QRegularExpression("f")); proxy.setFilterRegularExpression(QRegularExpression("f"));
QCOMPARE(proxy.rowCount(), 2); QCOMPARE(proxy.rowCount(), 2);
QList<QPersistentModelIndex> indexList; const QList<QPersistentModelIndex> indexList({proxy.index(0,0), proxy.index(1,0)});
indexList << proxy.index(0,0) << proxy.index(1,0);
selection.select(QItemSelection(indexList.first(), indexList.last()), QItemSelectionModel::Select); selection.select(QItemSelection(indexList.first(), indexList.last()), QItemSelectionModel::Select);
//let's check the selection hasn't changed //let's check the selection hasn't changed
QCOMPARE(selection.selectedIndexes().count(), indexList.count()); QCOMPARE(selection.selectedIndexes().count(), indexList.count());
foreach(QPersistentModelIndex index, indexList) for (const auto &index : indexList)
QVERIFY(selection.isSelected(index)); QVERIFY(selection.isSelected(index));
proxy.setFilterRegularExpression(QRegularExpression()); proxy.setFilterRegularExpression(QRegularExpression());
@ -2281,7 +2286,7 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected1()
//let's check the selection hasn't changed //let's check the selection hasn't changed
QCOMPARE(selection.selectedIndexes().count(), indexList.count()); QCOMPARE(selection.selectedIndexes().count(), indexList.count());
foreach(QPersistentModelIndex index, indexList) for (const auto &index : indexList)
QVERIFY(selection.isSelected(index)); QVERIFY(selection.isSelected(index));
} }
@ -2321,9 +2326,8 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected2()
selection.select( QItemSelection(proxy.index(0,0), proxy.index(proxy.rowCount() - 1, proxy.columnCount() - 1)), QItemSelectionModel::Select); selection.select( QItemSelection(proxy.index(0,0), proxy.index(proxy.rowCount() - 1, proxy.columnCount() - 1)), QItemSelectionModel::Select);
QList<QPersistentModelIndex> indexList; const auto selIndexes = selection.selectedIndexes();
foreach(const QModelIndex &id, selection.selectedIndexes()) const QList<QPersistentModelIndex> indexList(selIndexes.begin(), selIndexes.end());
indexList << id;
proxy.filtering = false; proxy.filtering = false;
proxy.invalidate(); proxy.invalidate();
@ -2331,7 +2335,7 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected2()
//let's check the selection hasn't changed //let's check the selection hasn't changed
QCOMPARE(selection.selectedIndexes().count(), indexList.count()); QCOMPARE(selection.selectedIndexes().count(), indexList.count());
foreach(QPersistentModelIndex index, indexList) for (const auto &index : indexList)
QVERIFY(selection.isSelected(index)); QVERIFY(selection.isSelected(index));
} }
@ -2375,7 +2379,8 @@ public:
public slots: public slots:
void selectionChanged(const QItemSelection & /* selected */, const QItemSelection &deselected) void selectionChanged(const QItemSelection & /* selected */, const QItemSelection &deselected)
{ {
foreach(const QModelIndex &index, deselected.indexes()) { const auto deselIndexes = deselected.indexes();
for (const auto &index : deselIndexes) {
QVERIFY(!m_itemSelectionModel->selection().contains(index)); QVERIFY(!m_itemSelectionModel->selection().contains(index));
} }
QCOMPARE(m_itemSelectionModel->selection().size(), 2); QCOMPARE(m_itemSelectionModel->selection().size(), 2);
@ -2633,9 +2638,9 @@ private slots:
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{ {
foreach(const QItemSelectionRange &range, selected) for (const auto &range : selected)
QVERIFY(range.isValid()); QVERIFY(range.isValid());
foreach(const QItemSelectionRange &range, deselected) for (const auto &range : deselected)
QVERIFY(range.isValid()); QVERIFY(range.isValid());
} }

View File

@ -829,7 +829,7 @@ void tst_QSortFilterProxyModel::removeRows_data()
void tst_QSortFilterProxyModel::removeRows() void tst_QSortFilterProxyModel::removeRows()
{ {
QFETCH(QStringList, initial); QFETCH(const QStringList, initial);
QFETCH(int, sortOrder); QFETCH(int, sortOrder);
QFETCH(QString, filter); QFETCH(QString, filter);
QFETCH(int, position); QFETCH(int, position);
@ -843,7 +843,7 @@ void tst_QSortFilterProxyModel::removeRows()
proxy.setSourceModel(&model); proxy.setSourceModel(&model);
// prepare model // prepare model
foreach (QString s, initial) for (const auto &s : initial)
model.appendRow(new QStandardItem(s)); model.appendRow(new QStandardItem(s));
if (sortOrder != -1) if (sortOrder != -1)
@ -3035,15 +3035,15 @@ void tst_QSortFilterProxyModel::removeRowsRecursive()
QList<QPersistentModelIndex> sourceIndexes; QList<QPersistentModelIndex> sourceIndexes;
QList<QPersistentModelIndex> proxyIndexes; QList<QPersistentModelIndex> proxyIndexes;
foreach (QStandardItem *item, items) { for (const auto item : qAsConst(items)) {
QModelIndex idx = item->index(); QModelIndex idx = item->index();
sourceIndexes << idx; sourceIndexes << idx;
proxyIndexes << proxy.mapFromSource(idx); proxyIndexes << proxy.mapFromSource(idx);
} }
foreach (const QPersistentModelIndex &pidx, sourceIndexes) for (const auto &pidx : qAsConst(sourceIndexes))
QVERIFY(pidx.isValid()); QVERIFY(pidx.isValid());
foreach (const QPersistentModelIndex &pidx, proxyIndexes) for (const auto &pidx : qAsConst(proxyIndexes))
QVERIFY(pidx.isValid()); QVERIFY(pidx.isValid());
QList<QStandardItem*> itemRow = pItem1->takeRow(0); QList<QStandardItem*> itemRow = pItem1->takeRow(0);
@ -3051,9 +3051,9 @@ void tst_QSortFilterProxyModel::removeRowsRecursive()
QCOMPARE(itemRow.count(), 1); QCOMPARE(itemRow.count(), 1);
QCOMPARE(itemRow.first(), pItem11); QCOMPARE(itemRow.first(), pItem11);
foreach (const QPersistentModelIndex &pidx, sourceIndexes) for (const auto &pidx : qAsConst(sourceIndexes))
QVERIFY(!pidx.isValid()); QVERIFY(!pidx.isValid());
foreach (const QPersistentModelIndex &pidx, proxyIndexes) for (const auto &pidx : qAsConst(proxyIndexes))
QVERIFY(!pidx.isValid()); QVERIFY(!pidx.isValid());
delete pItem11; delete pItem11;
@ -3280,10 +3280,8 @@ void tst_QSortFilterProxyModel::testMultipleProxiesWithSelection()
static bool isValid(const QItemSelection &selection) static bool isValid(const QItemSelection &selection)
{ {
foreach (const QItemSelectionRange &range, selection) return std::all_of(selection.begin(), selection.end(),
if (!range.isValid()) [](const QItemSelectionRange &range) { return range.isValid(); });
return false;
return true;
} }
void tst_QSortFilterProxyModel::mapSelectionFromSource() void tst_QSortFilterProxyModel::mapSelectionFromSource()
@ -3737,14 +3735,16 @@ void tst_QSortFilterProxyModel::testParentLayoutChanged()
QVERIFY(beforeParents.first() == proxy.mapFromSource(model.indexFromItem(model.invisibleRootItem()->child(1)))); QVERIFY(beforeParents.first() == proxy.mapFromSource(model.indexFromItem(model.invisibleRootItem()->child(1))));
QList<QPersistentModelIndex> proxy2BeforeList = proxy2ParentsAboutToBeChangedSpy.first().first().value<QList<QPersistentModelIndex> >(); const QList<QPersistentModelIndex> proxy2BeforeList =
QList<QPersistentModelIndex> proxy2AfterList = proxy2ParentsChangedSpy.first().first().value<QList<QPersistentModelIndex> >(); proxy2ParentsAboutToBeChangedSpy.first().first().value<QList<QPersistentModelIndex> >();
const QList<QPersistentModelIndex> proxy2AfterList =
proxy2ParentsChangedSpy.first().first().value<QList<QPersistentModelIndex> >();
QCOMPARE(proxy2BeforeList.size(), beforeParents.size()); QCOMPARE(proxy2BeforeList.size(), beforeParents.size());
QCOMPARE(proxy2AfterList.size(), afterParents.size()); QCOMPARE(proxy2AfterList.size(), afterParents.size());
foreach (const QPersistentModelIndex &idx, proxy2BeforeList) for (const QPersistentModelIndex &idx : proxy2BeforeList)
QVERIFY(beforeParents.contains(proxy2.mapToSource(idx))); QVERIFY(beforeParents.contains(proxy2.mapToSource(idx)));
foreach (const QPersistentModelIndex &idx, proxy2AfterList) for (const QPersistentModelIndex &idx : proxy2AfterList)
QVERIFY(afterParents.contains(proxy2.mapToSource(idx))); QVERIFY(afterParents.contains(proxy2.mapToSource(idx)));
} }