StatusQ: SnapshotModel testing utility
This commit is contained in:
parent
001e90a4c6
commit
22dcf8269f
|
@ -19,6 +19,10 @@ add_library(StatusQTestLib
|
|||
src/TestHelpers/modelaccessobserverproxy.h
|
||||
src/TestHelpers/modelsignalsspy.cpp
|
||||
src/TestHelpers/modelsignalsspy.h
|
||||
src/TestHelpers/modeltestutils.cpp
|
||||
src/TestHelpers/modeltestutils.h
|
||||
src/TestHelpers/snapshotmodel.cpp
|
||||
src/TestHelpers/snapshotmodel.h
|
||||
src/TestHelpers/testmodel.cpp
|
||||
src/TestHelpers/testmodel.h
|
||||
)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
#include <memory>
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
#include "modeltestutils.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
|
||||
bool recursive)
|
||||
{
|
||||
auto count1 = model1.rowCount();
|
||||
auto count2 = model2.rowCount();
|
||||
|
||||
auto roleNames1 = model1.roleNames();
|
||||
auto roleNames2 = model2.roleNames();
|
||||
|
||||
if (count1 != count2) {
|
||||
qWarning() << "Count mismatch:" << count1 << "/" << count2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (roleNames1 != roleNames2) {
|
||||
qWarning() << "Role names mismatch:" << roleNames1 << "/" << roleNames2;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto roles = roleNames1.keys();
|
||||
|
||||
for (auto role : roles) {
|
||||
for (int i = 0; i < count1; i++) {
|
||||
QVariant data1 = model1.data(model1.index(i, 0), role);
|
||||
QVariant data2 = model2.data(model2.index(i, 0), role);
|
||||
|
||||
if (recursive && data1.canConvert<QAbstractItemModel*>()
|
||||
&& data2.canConvert<QAbstractItemModel*>()) {
|
||||
auto submodel1 = data1.value<QAbstractItemModel*>();
|
||||
auto submodel2 = data2.value<QAbstractItemModel*>();
|
||||
|
||||
if (!isSame(*submodel1, *submodel2, true)) {
|
||||
qDebug() << "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());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isSame(const QAbstractItemModel* model1, const QAbstractItemModel* model2,
|
||||
bool recursive)
|
||||
{
|
||||
return isSame(*model1, *model2, recursive);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
|
||||
|
||||
bool isSame(const QAbstractItemModel& model1, const QAbstractItemModel& model2,
|
||||
bool recursive = true);
|
||||
bool isSame(const QAbstractItemModel* model1, const QAbstractItemModel* model2,
|
||||
bool recursive = true);
|
|
@ -0,0 +1,68 @@
|
|||
#include "snapshotmodel.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
SnapshotModel::SnapshotModel(QObject* parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
SnapshotModel::SnapshotModel(const QAbstractItemModel& model, bool recursive,
|
||||
QObject* parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
grabSnapshot(model, recursive);
|
||||
}
|
||||
|
||||
int SnapshotModel::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
if(parent.isValid())
|
||||
return 0;
|
||||
|
||||
return m_data.size() ? m_data.begin()->size() : 0;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SnapshotModel::roleNames() const
|
||||
{
|
||||
return m_roles;
|
||||
}
|
||||
|
||||
QVariant SnapshotModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
if (!index.isValid() || !m_roles.contains(role)
|
||||
|| index.row() >= rowCount()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return m_data[role][index.row()];
|
||||
}
|
||||
|
||||
void SnapshotModel::grabSnapshot(const QAbstractItemModel& model, bool recursive)
|
||||
{
|
||||
m_roles = model.roleNames();
|
||||
m_data.clear();
|
||||
|
||||
auto roles = m_roles.keys();
|
||||
auto count = model.rowCount();
|
||||
|
||||
for (auto role : roles) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
QVariant data = model.data(model.index(i, 0), role);
|
||||
|
||||
if (recursive && data.canConvert<QAbstractItemModel*>()) {
|
||||
const auto submodel = data.value<QAbstractItemModel*>();
|
||||
|
||||
m_data[role].push_back(
|
||||
QVariant::fromValue(
|
||||
new SnapshotModel(*submodel, true, this)));
|
||||
} else {
|
||||
m_data[role].push_back(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVariant SnapshotModel::data(int row, int role) const
|
||||
{
|
||||
return data(index(row), role);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
|
||||
class SnapshotModel : public QAbstractListModel {
|
||||
|
||||
public:
|
||||
explicit SnapshotModel(QObject* parent = nullptr);
|
||||
explicit SnapshotModel(const QAbstractItemModel& model, bool recursive = true,
|
||||
QObject* parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex& parent = {}) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
QVariant data(const QModelIndex& index, int role) const override;
|
||||
|
||||
void grabSnapshot(const QAbstractItemModel& model, bool recursive = true);
|
||||
QVariant data(int row, int role) const;
|
||||
|
||||
private:
|
||||
QHash<int, QList<QVariant>> m_data;
|
||||
QHash<int, QByteArray> m_roles;
|
||||
};
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
|
||||
class TestModel : public QAbstractListModel {
|
||||
|
|
Loading…
Reference in New Issue