feat(StatusQ/Tests): Added isNotSame(...) method for comparing models

It works similarly as isSame(...) but doesn't print warnings when
mismatch is found. Thanks to that may be used conveniently whenever
we expect differing models in a test.
This commit is contained in:
Michał Cieślak 2024-02-21 14:35:44 +01:00 committed by Michał
parent 6d502427e5
commit 1bcbb482a1
3 changed files with 37 additions and 8 deletions

View File

@ -1,6 +1,6 @@
#pragma once
#include <QAbstractListModel>
#include <QAbstractItemModel>
#include <QSignalSpy>
class ModelSignalsSpy

View File

@ -2,9 +2,10 @@
#include <QDebug>
namespace {
bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
bool recursive)
bool recursive, bool printWarning)
{
auto count1 = model1.rowCount();
auto count2 = model2.rowCount();
@ -35,15 +36,17 @@ bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
auto submodel2 = data2.value<QAbstractItemModel*>();
if (!isSame(*submodel1, *submodel2, true)) {
qDebug() << "submodels are not the same, index:"
<< i << roleNames1[role];
if (printWarning)
qWarning() << "submodels are not the same, index:"
<< i << roleNames1[role];
return false;
}
} else if (data1 != data2) {
qWarning()
<< QString("Mismatch at row %1, role '%2'. Model 1: %3, model 2: %4")
.arg(QString::number(i), QString(roleNames1[role]),
data1.toString(), data2.toString());
if (printWarning)
qWarning()
<< QString("Mismatch at row %1, role '%2'. Model 1: %3, model 2: %4")
.arg(QString::number(i), QString(roleNames1[role]),
data1.toString(), data2.toString());
return false;
}
}
@ -52,8 +55,29 @@ bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
return true;
}
} // unnamed namespace
bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
bool recursive)
{
return isSame(model1, model2, recursive, true);
}
bool isSame(const QAbstractItemModel* model1, const QAbstractItemModel* model2,
bool recursive)
{
return isSame(*model1, *model2, recursive);
}
bool isNotSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
bool recursive)
{
return !isSame(model1, model2, recursive, false);
}
bool isNotSame(const QAbstractItemModel* model1, const QAbstractItemModel* model2,
bool recursive)
{
return isNotSame(*model1, *model2, recursive);
}

View File

@ -7,3 +7,8 @@ bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
bool recursive = true);
bool isSame(const QAbstractItemModel* model1, const QAbstractItemModel* model2,
bool recursive = true);
bool isNotSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
bool recursive = true);
bool isNotSame(const QAbstractItemModel* model1, const QAbstractItemModel* model2,
bool recursive = true);