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()
{
foreach (const QModelIndex &idx, m_persistentIndexes) {
for (const auto &idx : qAsConst(m_persistentIndexes)) {
QVERIFY(!idx.isValid());
}
}

View File

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

View File

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