Skip to content

Commit

Permalink
EditableTreeModel: Add a test
Browse files Browse the repository at this point in the history
Add a test running QAbstractItemModelTester on the model.

Pick-to: 6.6
Change-Id: I40c141c7e754ca05234da611534bd65e456be2fb
Reviewed-by: Kai Köhne <[email protected]>
  • Loading branch information
FriedemannKleint committed Dec 6, 2023
1 parent d50b56b commit 283cdcd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
15 changes: 15 additions & 0 deletions examples/widgets/doc/src/editabletreemodel.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,19 @@
\target TreeModel::data
\target TreeModel::setupModelData

\section1 Testing the model

Correctly implementing an item model can be challenging. The class
\l QAbstractItemModelTester from the \l{Qt Test} module checks for model
consistency, like the model index creation and parent-child relationships.

You can test your model by just passing a model instance to the class
constructor, for instance as part of a Qt unit test:

\snippet itemviews/editabletreemodel/test.cpp 1

To create a test which can be run using the \c ctest executable,
\c add_test() is used:

\snippet itemviews/editabletreemodel/CMakeLists.txt 1
*/
28 changes: 27 additions & 1 deletion examples/widgets/itemviews/editabletreemodel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()

set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/editabletreemodel")

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)

qt_standard_project_setup()

Expand Down Expand Up @@ -49,3 +49,29 @@ install(TARGETS editabletreemodel
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

#! [1]
# Unit Test

include(CTest)

qt_add_executable(editabletreemodel_tester
test.cpp
treeitem.cpp treeitem.h
treemodel.cpp treemodel.h)

target_link_libraries(editabletreemodel_tester PRIVATE
Qt6::Core
Qt6::Test
)

qt_add_resources(editabletreemodel_tester "editabletreemodel"
PREFIX
"/"
FILES
${editabletreemodel_resource_files}
)

add_test(NAME editabletreemodel_tester
COMMAND editabletreemodel_tester)
#! [1]
37 changes: 37 additions & 0 deletions examples/widgets/itemviews/editabletreemodel/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "treemodel.h"

#include <QAbstractItemModelTester>
#include <QObject>
#include <QTest>

using namespace Qt::StringLiterals;

//! [1]
class TestEditableTreeModel : public QObject
{
Q_OBJECT

private slots:
void testTreeModel();
};

void TestEditableTreeModel::testTreeModel()
{
constexpr auto fileName = ":/default.txt"_L1;
QFile file(fileName);
QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text),
qPrintable(fileName + " cannot be opened: "_L1 + file.errorString()));

const QStringList headers{"column1"_L1, "column2"_L1};
TreeModel model(headers, QString::fromUtf8(file.readAll()));

QAbstractItemModelTester tester(&model);
}

QTEST_APPLESS_MAIN(TestEditableTreeModel)

#include "test.moc"
//! [1]

0 comments on commit 283cdcd

Please sign in to comment.