SimpleTreeModel: Add a test
Add a test running QAbstractItemModelTester on the model. Change-Id: I6ea5d34308357409b9a13eb0271392c8d171addd Reviewed-by: Kai Köhne <kai.koehne@qt.io> (cherry picked from commit d9c71b834ccd815b85e9589f21eba1f51c48614a) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
7e28513c28
commit
b1039a405a
@ -319,4 +319,20 @@
|
||||
|
||||
To ensure that the model works correctly, it is only necessary to
|
||||
create instances of \c TreeItem with the correct data and parent item.
|
||||
|
||||
\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/simpletreemodel/test.cpp 1
|
||||
|
||||
To create a test which can be run using the \c ctest executable,
|
||||
\c add_test() is used:
|
||||
|
||||
\snippet itemviews/simpletreemodel/CMakeLists.txt 1
|
||||
*/
|
||||
|
@ -10,7 +10,7 @@ endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/simpletreemodel")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
@ -48,3 +48,29 @@ install(TARGETS simpletreemodel
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
||||
|
||||
#! [1]
|
||||
# Unit Test
|
||||
|
||||
include(CTest)
|
||||
|
||||
qt_add_executable(simpletreemodel_tester
|
||||
test.cpp
|
||||
treeitem.cpp treeitem.h
|
||||
treemodel.cpp treemodel.h)
|
||||
|
||||
target_link_libraries(simpletreemodel_tester PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Test
|
||||
)
|
||||
|
||||
qt_add_resources(simpletreemodel_tester "simpletreemodel"
|
||||
PREFIX
|
||||
"/"
|
||||
FILES
|
||||
${simpletreemodel_resource_files}
|
||||
)
|
||||
|
||||
add_test(NAME simpletreemodel_tester
|
||||
COMMAND simpletreemodel_tester)
|
||||
#! [1]
|
||||
|
35
examples/widgets/itemviews/simpletreemodel/test.cpp
Normal file
35
examples/widgets/itemviews/simpletreemodel/test.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright (C) 2023 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "treemodel.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractItemModelTester>
|
||||
#include <QTest>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
//! [1]
|
||||
class TestSimpleTreeModel : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void testTreeModel();
|
||||
};
|
||||
|
||||
void TestSimpleTreeModel::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()));
|
||||
TreeModel model(QString::fromUtf8(file.readAll()));
|
||||
|
||||
QAbstractItemModelTester tester(&model);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(TestSimpleTreeModel)
|
||||
|
||||
#include "test.moc"
|
||||
//! [1]
|
Loading…
x
Reference in New Issue
Block a user