umbrello: unittests/testbase.h Source File

umbrello 2.32.1 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 Copyright 2015, 2019 Ralf Habacker <ralf.habacker@freenet.de>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License or (at your option) version 3 or any later version
8 accepted by the membership of KDE e.V. (or its successor approved
9 by the membership of KDE e.V.), which shall act as a proxy
10 defined in Section 14 of version 3 of the license.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef TESTBASE_H
22 #define TESTBASE_H
23
24 // qt includes
25 #include <QObject>
26 #include <QtTest>
27
28 #ifdef RUN_ALL
29 #undef QCOMPARE
30 #define QCOMPARE(actual, expected) \
31 QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)
32 #undef QVERIFY
33 #define QVERIFY(statement) \
34 QTest::qVerify((statement), #statement, "", __FILE__, __LINE__)
35 #endif
36
37 #define IS_NOT_IMPL() QSKIP("not implemented yet", SkipSingle)
38
47 class TestBase : public QObject
48 {
49 Q_OBJECT
50 public:
51 explicit TestBase(QObject *parent = 0);
52
53 protected slots:
54 virtual void initTestCase();
55 virtual void cleanupTestCase();
56 virtual void cleanupOnExit(QObject *p);
57
58 protected:
59 QList<QPointer<QObject>> m_objectsToDelete;
60 };
61
70 class TestCodeGeneratorBase : public TestBase
71 {
72 Q_OBJECT
73 private slots:
74 virtual void initTestCase();
75
76 protected:
77 QString m_tempPath;
78 QString temporaryPath();
79 };
80
81 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
85 class SignalBlocker
86 {
87 public:
88 explicit SignalBlocker(QObject *o)
89 : _o(o)
90 {
91 _state = _o->blockSignals(true);
92 }
93
94 SignalBlocker(QObject &o)
95 : _o(&o)
96 {
97 _state = _o->blockSignals(true);
98 }
99
100 ~SignalBlocker()
101 {
102 _o->blockSignals(_state);
103 }
104
105 protected:
106 QObject *_o;
107 bool _state;
108 };
109 #else
110 #include <QSignalBlocker>
111 typedef QSignalBlocker SignalBlocker;
112 #endif
113
117 class SetLoading
118 {
119 public:
120 SetLoading();
121 ~SetLoading();
122 protected:
123 bool _state;
124 };
125
126 #include <QDomDocument>
127 #include <QXmlStreamWriter>
128 #include "uml.h"
129 #include "umldoc.h"
130
134 template <class T, typename N>
135 class TestUML : public T
136 {
137 public:
138 TestUML<T,N>() : T() {}
139 TestUML<T,N>(N name) : T(name) {}
140 TestUML<T,N>(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
141 QString testSave1();
142 bool testLoad1(const QString& xml);
143 void testDump(const QString &title = QString());
144 UMLObject *secondary() const;
145 };
146
147 template <class T, typename N>
148 QString TestUML<T,N>::testSave1()
149 {
150 QString xml;
151 QXmlStreamWriter stream(&xml);
152 stream.writeStartElement("unittest");
153 T::saveToXMI1(stream);
154 stream.writeEndElement();
155 return xml;
156 }
157
158 template <class T, typename N>
159 bool TestUML<T,N>::testLoad1(const QString& xml)
160 {
161 QDomDocument qDoc;
162 QString error;
163 int line;
164 if (!qDoc.setContent(xml, &error, &line))
165 return false;
166 QDomElement root = qDoc.childNodes().at(0).toElement();
167 QDomElement e = root.childNodes().at(0).toElement();
168 bool result = T::loadFromXMI1(e);
169 if (result) {
170 const SignalBlocker sb(UMLApp::app()->document());
171 result = T::resolveRef();
172 }
173 return result;
174 }
175
176 template <class T, typename N>
177 void TestUML<T,N>::testDump(const QString &title)
178 {
179 QString xml = testSave1();
180 qDebug() << title << xml;
181 }
182
183 // used by resolveRef() tests
184 template <class T, typename N>
185 UMLObject *TestUML<T,N>::secondary() const
186 {
187 return T::m_pSecondary.data();
188 }
189
193 template <class T, typename N>
194 class TestWidget : public T
195 {
196 public:
197 TestWidget<T,N>(UMLScene *scene, N w) : T(scene, w) {}
198 QString testSave1();
199 bool testLoad1(const QString& xml);
200 void testDump(const QString &title = QString());
201 };
202
203 template <class T, typename N>
204 QString TestWidget<T,N>::testSave1()
205 {
206 QString xml;
207 QXmlStreamWriter stream(&xml);
208 stream.writeStartElement("unittest");
209 T::saveToXMI1(stream);
210 stream.writeEndElement();
211 return xml;
212 }
213
214 template <class T, typename N>
215 bool TestWidget<T,N>::testLoad1(const QString& xml)
216 {
217 QDomDocument qDoc;
218 QString error;
219 int line;
220 if (!qDoc.setContent(xml, &error, &line))
221 return false;
222 QDomElement root = qDoc.childNodes().at(0).toElement();
223 QDomElement e = root.childNodes().at(0).toElement();
224 bool result = T::loadFromXMI1(e);
225 if (result) {
226 const SignalBlocker sb(UMLApp::app()->document());
227 result = T::activate(nullptr);
228 }
229 return result;
230 }
231
232 template <class T, typename N>
233 void TestWidget<T,N>::testDump(const QString &title)
234 {
235 QString xml = testSave1();
236 qDebug() << title << xml;
237 }
238
239 #endif // TESTBASE_H
SetLoading
Definition: testbase.h:118
SetLoading::SetLoading
SetLoading()
Definition: testbase.cpp:91
SetLoading::~SetLoading
~SetLoading()
Definition: testbase.cpp:97
SetLoading::_state
bool _state
Definition: testbase.h:123
TestBase
Definition: testbase.h:48
TestBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:48
TestBase::cleanupOnExit
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:63
TestBase::TestBase
TestBase(QObject *parent=0)
Definition: testbase.cpp:43
TestBase::cleanupTestCase
virtual void cleanupTestCase()
Definition: testbase.cpp:55
TestBase::m_objectsToDelete
QList< QPointer< QObject > > m_objectsToDelete
Definition: testbase.h:59
TestCodeGeneratorBase
Definition: testbase.h:71
TestCodeGeneratorBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:68
TestCodeGeneratorBase::temporaryPath
QString temporaryPath()
Definition: testbase.cpp:86
TestCodeGeneratorBase::m_tempPath
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:77
TestUML
Definition: testbase.h:136
TestUML::testSave1
QString testSave1()
Definition: testbase.h:148
TestUML::secondary
UMLObject * secondary() const
Definition: testbase.h:185
TestUML::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:177
TestUML::testLoad1
bool testLoad1(const QString &xml)
Definition: testbase.h:159
TestWidget
Definition: testbase.h:195
TestWidget::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:233
TestWidget::testLoad1
bool testLoad1(const QString &xml)
Definition: testbase.h:215
TestWidget::testSave1
QString testSave1()
Definition: testbase.h:204
UMLApp::app
static UMLApp * app()
Definition: uml.cpp:280
UMLObject
The base class for UML objects.
Definition: umlobject.h:75
UMLScene
Definition: umlscene.h:70
SignalBlocker
QSignalBlocker SignalBlocker
Definition: testbase.h:111
uml.h
umldoc.h