umbrello: unittests/testbase.h Source File

umbrello 25.08.0 Umbrello UML Modeller is a Unified Modelling Language (UML) diagram program based on KDE Technology

testbase.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2015, 2019 Ralf Habacker <ralf.habacker@freenet.de>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6
7#ifndef TESTBASE_H
8#define TESTBASE_H
9
10// qt includes
11#include <QObject>
12#include <QtTest>
13
14#ifdef RUN_ALL
15#undef QCOMPARE
16#define QCOMPARE(actual, expected) \
17 QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)
18#undef QVERIFY
19#define QVERIFY(statement) \
20 QTest::qVerify((statement), #statement, "", __FILE__, __LINE__)
21#endif
22
23#define IS_NOT_IMPL() QSKIP("not implemented yet", SkipSingle)
24
33 class TestBase : public QObject
34{
35 Q_OBJECT
36public:
37 explicit TestBase(QObject *parent = nullptr);
38
39protected Q_SLOTS:
40 virtual void initTestCase();
41 virtual void cleanupTestCase();
42 virtual void cleanupOnExit(QObject *p);
43
44protected:
45 QList<QPointer<QObject>> m_objectsToDelete;
46};
47
56 class TestCodeGeneratorBase : public TestBase
57{
58 Q_OBJECT
59private Q_SLOTS:
60 virtual void initTestCase();
61
62protected:
63 QString m_tempPath;
64 QString temporaryPath();
65};
66
67#include <QSignalBlocker>
68 typedef QSignalBlocker SignalBlocker;
69
73 class SetLoading
74{
75public:
76 SetLoading();
77 ~SetLoading();
78protected:
79 bool _state;
80};
81
82#include <QDomDocument>
83#include <QXmlStreamWriter>
84#include "uml.h"
85#include "umldoc.h"
86
90template <class T, typename N>
91 class TestUML : public T
92{
93public:
94 TestUML<T,N>() : T() {}
95 TestUML<T,N>(N name) : T(name) {}
96 TestUML<T,N>(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
97 QString testSave1();
98 bool testLoad1(const QString& xml);
99 void testDump(const QString &title = QString());
100 UMLObject *secondary() const;
101};
102
103template <class T, typename N>
104QString TestUML<T,N>::testSave1()
105{
106 QString xml;
107 QXmlStreamWriter stream(&xml);
108 stream.writeStartElement("unittest");
109 T::saveToXMI(stream);
110 stream.writeEndElement();
111 return xml;
112}
113
114template <class T, typename N>
115 bool TestUML<T,N>::testLoad1(const QString& xml)
116{
117 QDomDocument qDoc;
118 QString error;
119 int line;
120 if (!qDoc.setContent(xml, &error, &line))
121 return false;
122 QDomElement root = qDoc.childNodes().at(0).toElement();
123 QDomElement e = root.childNodes().at(0).toElement();
124 bool result = T::loadFromXMI(e);
125 if (result) {
126 const SignalBlocker sb(UMLApp::app()->document());
127 result = T::resolveRef();
128 }
129 return result;
130}
131
132template <class T, typename N>
133 void TestUML<T,N>::testDump(const QString &title)
134{
135 QString xml = testSave1();
136 qDebug() << title << xml;
137}
138
139// used by resolveRef() tests
140template <class T, typename N>
141 UMLObject *TestUML<T,N>::secondary() const
142{
143 return T::m_pSecondary.data();
144}
145
149template <class T, typename N>
150 class TestWidget : public T
151{
152public:
153 TestWidget<T,N>(UMLScene *scene, N w) : T(scene, w) {}
154 QString testSave1();
155 bool testLoad1(const QString& xml);
156 void testDump(const QString &title = QString());
157};
158
159template <class T, typename N>
160QString TestWidget<T,N>::testSave1()
161{
162 QString xml;
163 QXmlStreamWriter stream(&xml);
164 stream.writeStartElement("unittest");
165 T::saveToXMI(stream);
166 stream.writeEndElement();
167 return xml;
168}
169
170template <class T, typename N>
171 bool TestWidget<T,N>::testLoad1(const QString& xml)
172{
173 QDomDocument qDoc;
174 QString error;
175 int line;
176 if (!qDoc.setContent(xml, &error, &line))
177 return false;
178 QDomElement root = qDoc.childNodes().at(0).toElement();
179 QDomElement e = root.childNodes().at(0).toElement();
180 bool result = T::loadFromXMI(e);
181 if (result) {
182 const SignalBlocker sb(UMLApp::app()->document());
183 result = T::activate(nullptr);
184 }
185 return result;
186}
187
188template <class T, typename N>
189 void TestWidget<T,N>::testDump(const QString &title)
190{
191 QString xml = testSave1();
192 qDebug() << title << xml;
193}
194
195#endif // TESTBASE_H
SetLoading::SetLoading
SetLoading()
Definition testbase.cpp:65
SetLoading::~SetLoading
~SetLoading()
Definition testbase.cpp:71
SetLoading::_state
bool _state
Definition testbase.h:79
TestBase::initTestCase
virtual void initTestCase()
Definition testbase.cpp:28
TestBase::cleanupOnExit
virtual void cleanupOnExit(QObject *p)
Definition testbase.cpp:42
TestBase::TestBase
TestBase(QObject *parent=nullptr)
Definition testbase.cpp:23
TestBase::cleanupTestCase
virtual void cleanupTestCase()
Definition testbase.cpp:36
TestBase::m_objectsToDelete
QList< QPointer< QObject > > m_objectsToDelete
Definition testbase.h:45
TestCodeGeneratorBase
Definition testbase.h:57
TestCodeGeneratorBase::initTestCase
virtual void initTestCase()
Definition testbase.cpp:47
TestCodeGeneratorBase::temporaryPath
QString temporaryPath()
Definition testbase.cpp:60
TestCodeGeneratorBase::m_tempPath
QString m_tempPath
holds path to temporary directory
Definition testbase.h:63
TestUML::testSave1
QString testSave1()
Definition testbase.h:104
TestUML::TestUML
TestUML()
Definition testbase.h:94
TestUML::secondary
UMLObject * secondary() const
Definition testbase.h:141
TestUML::testDump
void testDump(const QString &title=QString())
Definition testbase.h:133
TestUML::testLoad1
bool testLoad1(const QString &xml)
Definition testbase.h:115
TestWidget::TestWidget
TestWidget(UMLScene *scene, N w)
Definition testbase.h:153
TestWidget::testDump
void testDump(const QString &title=QString())
Definition testbase.h:189
TestWidget::testLoad1
bool testLoad1(const QString &xml)
Definition testbase.h:171
TestWidget::testSave1
QString testSave1()
Definition testbase.h:160
UMLApp::app
static UMLApp * app()
Definition uml.cpp:304
UMLObject
The base class for UML objects.
Definition umlobject.h:70
UMLScene
Definition umlscene.h:65
SignalBlocker
QSignalBlocker SignalBlocker
Definition testbase.h:68
uml.h
umldoc.h