umbrello: unittests/testbase.h Source File

umbrello 2.32.3 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 = 0);
38
39protected 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 slots:
60 virtual void initTestCase();
61
62protected:
63 QString m_tempPath;
64 QString temporaryPath();
65};
66
67#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
71class SignalBlocker
72{
73public:
74 explicit SignalBlocker(QObject *o)
75 : _o(o)
76 {
77 _state = _o->blockSignals(true);
78 }
79
80 SignalBlocker(QObject &o)
81 : _o(&o)
82 {
83 _state = _o->blockSignals(true);
84 }
85
86 ~SignalBlocker()
87 {
88 _o->blockSignals(_state);
89 }
90
91protected:
92 QObject *_o;
93 bool _state;
94};
95#else
96#include <QSignalBlocker>
97 typedef QSignalBlocker SignalBlocker;
98#endif
99
103 class SetLoading
104{
105public:
106 SetLoading();
107 ~SetLoading();
108protected:
109 bool _state;
110};
111
112#include <QDomDocument>
113#include <QXmlStreamWriter>
114#include "uml.h"
115#include "umldoc.h"
116
120template <class T, typename N>
121 class TestUML : public T
122{
123public:
124 TestUML<T,N>() : T() {}
125 TestUML<T,N>(N name) : T(name) {}
126 TestUML<T,N>(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
127 QString testSave1();
128 bool testLoad1(const QString& xml);
129 void testDump(const QString &title = QString());
130 UMLObject *secondary() const;
131};
132
133template <class T, typename N>
134QString TestUML<T,N>::testSave1()
135{
136 QString xml;
137 QXmlStreamWriter stream(&xml);
138 stream.writeStartElement("unittest");
139 T::saveToXMI1(stream);
140 stream.writeEndElement();
141 return xml;
142}
143
144template <class T, typename N>
145 bool TestUML<T,N>::testLoad1(const QString& xml)
146{
147 QDomDocument qDoc;
148 QString error;
149 int line;
150 if (!qDoc.setContent(xml, &error, &line))
151 return false;
152 QDomElement root = qDoc.childNodes().at(0).toElement();
153 QDomElement e = root.childNodes().at(0).toElement();
154 bool result = T::loadFromXMI1(e);
155 if (result) {
156 const SignalBlocker sb(UMLApp::app()->document());
157 result = T::resolveRef();
158 }
159 return result;
160}
161
162template <class T, typename N>
163 void TestUML<T,N>::testDump(const QString &title)
164{
165 QString xml = testSave1();
166 qDebug() << title << xml;
167}
168
169// used by resolveRef() tests
170template <class T, typename N>
171 UMLObject *TestUML<T,N>::secondary() const
172{
173 return T::m_pSecondary.data();
174}
175
179template <class T, typename N>
180 class TestWidget : public T
181{
182public:
183 TestWidget<T,N>(UMLScene *scene, N w) : T(scene, w) {}
184 QString testSave1();
185 bool testLoad1(const QString& xml);
186 void testDump(const QString &title = QString());
187};
188
189template <class T, typename N>
190QString TestWidget<T,N>::testSave1()
191{
192 QString xml;
193 QXmlStreamWriter stream(&xml);
194 stream.writeStartElement("unittest");
195 T::saveToXMI1(stream);
196 stream.writeEndElement();
197 return xml;
198}
199
200template <class T, typename N>
201 bool TestWidget<T,N>::testLoad1(const QString& xml)
202{
203 QDomDocument qDoc;
204 QString error;
205 int line;
206 if (!qDoc.setContent(xml, &error, &line))
207 return false;
208 QDomElement root = qDoc.childNodes().at(0).toElement();
209 QDomElement e = root.childNodes().at(0).toElement();
210 bool result = T::loadFromXMI1(e);
211 if (result) {
212 const SignalBlocker sb(UMLApp::app()->document());
213 result = T::activate(nullptr);
214 }
215 return result;
216}
217
218template <class T, typename N>
219 void TestWidget<T,N>::testDump(const QString &title)
220{
221 QString xml = testSave1();
222 qDebug() << title << xml;
223}
224
225#endif // TESTBASE_H
SetLoading
Definition: testbase.h:104
SetLoading::SetLoading
SetLoading()
Definition: testbase.cpp:77
SetLoading::~SetLoading
~SetLoading()
Definition: testbase.cpp:83
SetLoading::_state
bool _state
Definition: testbase.h:109
TestBase
Definition: testbase.h:34
TestBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:34
TestBase::cleanupOnExit
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:49
TestBase::TestBase
TestBase(QObject *parent=0)
Definition: testbase.cpp:29
TestBase::cleanupTestCase
virtual void cleanupTestCase()
Definition: testbase.cpp:41
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:54
TestCodeGeneratorBase::temporaryPath
QString temporaryPath()
Definition: testbase.cpp:72
TestCodeGeneratorBase::m_tempPath
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:63
TestUML
Definition: testbase.h:122
TestUML::testSave1
QString testSave1()
Definition: testbase.h:134
TestUML::secondary
UMLObject * secondary() const
Definition: testbase.h:171
TestUML::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:163
TestUML::testLoad1
bool testLoad1(const QString &xml)
Definition: testbase.h:145
TestWidget
Definition: testbase.h:181
TestWidget::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:219
TestWidget::testLoad1
bool testLoad1(const QString &xml)
Definition: testbase.h:201
TestWidget::testSave1
QString testSave1()
Definition: testbase.h:190
UMLApp::app
static UMLApp * app()
Definition: uml.cpp:275
UMLObject
The base class for UML objects.
Definition: umlobject.h:70
UMLScene
Definition: umlscene.h:65
SignalBlocker
QSignalBlocker SignalBlocker
Definition: testbase.h:97
uml.h
umldoc.h