umbrello: unittests/testbase.h Source File

umbrello 2.31.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 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)
82
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 "uml.h"
128 #include "umldoc.h"
129
133 template <class T, typename N>
134 class TestUML : public T
135 {
136 public:
137 TestUML<T,N>() : T() {}
138 TestUML<T,N>(N name) : T(name) {}
139 TestUML<T,N>(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
140 QDomDocument testSave1();
141 bool testLoad1(QDomDocument &qDoc);
142 void testDump(const QString &title = QString());
143 UMLObject *secondary() const;
144 };
145
146 template <class T, typename N>
147 QDomDocument TestUML<T,N>::testSave1()
148 {
149 QDomDocument qDoc;
150 QDomElement root = qDoc.createElement("unittest");
151 qDoc.appendChild(root);
152 T::saveToXMI1(qDoc, root);
153 return qDoc;
154 }
155
156 template <class T, typename N>
157 bool TestUML<T,N>::testLoad1(QDomDocument &qDoc)
158 {
159 QDomElement root = qDoc.childNodes().at(0).toElement();
160 QDomElement e = root.childNodes().at(0).toElement();
161 bool result = T::loadFromXMI1(e);
162 if (result) {
163 const SignalBlocker sb(UMLApp::app()->document());
164 result = T::resolveRef();
165 }
166 return result;
167 }
168
169 template <class T, typename N>
170 void TestUML<T,N>::testDump(const QString &title)
171 {
172 QDomDocument doc = testSave1();
173 QString xml = doc.toString();
174 qDebug() << title << doc.toString();
175 }
176
177 // used by resolveRef() tests
178 template <class T, typename N>
179 UMLObject *TestUML<T,N>::secondary() const
180 {
181 return T::m_pSecondary.data();
182 }
183
187 template <class T, typename N>
188 class TestWidget : public T
189 {
190 public:
191 TestWidget<T,N>(UMLScene *scene, N w) : T(scene, w) {}
192 QDomDocument testSave1();
193 bool testLoad1(QDomDocument &qDoc);
194 void testDump(const QString &title = QString());
195 };
196
197 template <class T, typename N>
198 QDomDocument TestWidget<T,N>::testSave1()
199 {
200 QDomDocument qDoc;
201 QDomElement root = qDoc.createElement("unittest");
202 qDoc.appendChild(root);
203 T::saveToXMI1(qDoc, root);
204 return qDoc;
205 }
206
207 template <class T, typename N>
208 bool TestWidget<T,N>::testLoad1(QDomDocument &qDoc)
209 {
210 QDomElement root = qDoc.childNodes().at(0).toElement();
211 QDomElement e = root.childNodes().at(0).toElement();
212 bool result = T::loadFromXMI1(e);
213 if (result) {
214 const SignalBlocker sb(UMLApp::app()->document());
215 result = T::activate(nullptr);
216 }
217 return result;
218 }
219
220 template <class T, typename N>
221 void TestWidget<T,N>::testDump(const QString &title)
222 {
223 QDomDocument doc = testSave1();
224 QString xml = doc.toString();
225 qDebug() << title << doc.toString();
226 }
227
228 #endif // TESTBASE_H
SetLoading::_state
bool _state
Definition: testbase.h:123
TestUML
Definition: testbase.h:134
TestUML::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:170
TestBase::cleanupTestCase
virtual void cleanupTestCase()
Definition: testbase.cpp:55
TestWidget::testLoad1
bool testLoad1(QDomDocument &qDoc)
Definition: testbase.h:208
TestCodeGeneratorBase::temporaryPath
QString temporaryPath()
Definition: testbase.cpp:86
UMLApp::commonPolicy
CodeGenerationPolicy * commonPolicy() const
Definition: uml.cpp:2274
TestWidget::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:221
TestUML::testLoad1
bool testLoad1(QDomDocument &qDoc)
Definition: testbase.h:157
TestBase::TestBase
TestBase(QObject *parent=0)
Definition: testbase.cpp:43
TestCodeGeneratorBase
Definition: testbase.h:70
TestBase::cleanupOnExit
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:63
SignalBlocker
QSignalBlocker SignalBlocker
Definition: testbase.h:111
TestCodeGeneratorBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:68
UMLObject
The base class for UML objects.
Definition: umlobject.h:73
testbase.h
UMLDoc::loading
bool loading() const
Definition: umldoc.cpp:1570
TestWidget
Definition: testbase.h:188
TestUML::testSave1
QDomDocument testSave1()
Definition: testbase.h:147
UMLApp
Definition: uml.h:99
UMLDoc::setLoading
void setLoading(bool state=true)
Definition: umldoc.cpp:1579
TestWidget::testSave1
QDomDocument testSave1()
Definition: testbase.h:198
codegenerationpolicy.h
UMLApp::setActiveLanguage
void setActiveLanguage(Uml::ProgrammingLanguage::Enum pl)
Definition: uml.cpp:2500
Uml::ProgrammingLanguage::Cpp
@ Cpp
Definition: basictypes.h:291
TestBase::m_objectsToDelete
QList< QPointer< QObject > > m_objectsToDelete
Definition: testbase.h:59
UMLApp::app
static UMLApp * app()
Definition: uml.cpp:269
uml.h
CodeGenerationPolicy::setOutputDirectory
void setOutputDirectory(QDir new_var)
Definition: codegenerationpolicy.cpp:201
SetLoading::SetLoading
SetLoading()
Definition: testbase.cpp:91
TestCodeGeneratorBase::m_tempPath
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:77
UMLScene
Definition: umlscene.h:68
SetLoading
Definition: testbase.h:117
TestBase
Definition: testbase.h:47
TestUML::secondary
UMLObject * secondary() const
Definition: testbase.h:179
TestBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:48
UMLApp::document
UMLDoc * document() const
Definition: uml.cpp:1055
umldoc.h
SetLoading::~SetLoading
~SetLoading()
Definition: testbase.cpp:97