umbrello 26.03.70-f7b1fd3
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
33class 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 QWidget *m_mainWidget = nullptr;
47};
48
58{
59 Q_OBJECT
60private Q_SLOTS:
61 virtual void initTestCase();
62
63protected:
64 QString m_tempPath;
65 QString temporaryPath();
66};
67
68#include <QSignalBlocker>
69typedef QSignalBlocker SignalBlocker;
70
75{
76public:
77 SetLoading();
79protected:
80 bool _state;
81};
82
83#include <QDomDocument>
84#include <QXmlStreamWriter>
85#include "umlapp.h"
86#include "umldoc.h"
87
91template <class T, typename N>
92class TestUML : public T
93{
94public:
95 TestUML() : T() {}
96 TestUML(N name) : T(name) {}
97 TestUML(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
98 QString testSave1();
99 bool testLoad1(const QString& xml);
100 void testDump(const QString &title = QString());
101 UMLObject *secondary() const;
102};
103
104template <class T, typename N>
106{
107 QString xml;
108 QXmlStreamWriter stream(&xml);
109 stream.writeStartElement("unittest");
110 T::saveToXMI(stream);
111 stream.writeEndElement();
112 return xml;
113}
114
115template <class T, typename N>
116bool TestUML<T,N>::testLoad1(const QString& xml)
117{
118 QDomDocument qDoc;
119 QString error;
120 int line;
121 if (!qDoc.setContent(xml, &error, &line))
122 return false;
123 QDomElement root = qDoc.childNodes().at(0).toElement();
124 QDomElement e = root.childNodes().at(0).toElement();
125 bool result = T::loadFromXMI(e);
126 if (result) {
127 const SignalBlocker sb(UMLApp::app()->document());
128 result = T::resolveRef();
129 }
130 return result;
131}
132
133template <class T, typename N>
134void TestUML<T,N>::testDump(const QString &title)
135{
136 QString xml = testSave1();
137 qDebug() << title << xml;
138}
139
140// used by resolveRef() tests
141template <class T, typename N>
143{
144 return T::m_pSecondary.data();
145}
146
150template <class T, typename N>
151class TestWidget : public T
152{
153public:
154 TestWidget(UMLScene *scene, N w) : T(scene, w) {}
155 QString testSave1();
156 bool testLoad1(const QString& xml);
157 void testDump(const QString &title = QString());
158};
159
160template <class T, typename N>
162{
163 QString xml;
164 QXmlStreamWriter stream(&xml);
165 stream.writeStartElement("unittest");
166 T::saveToXMI(stream);
167 stream.writeEndElement();
168 return xml;
169}
170
171template <class T, typename N>
172bool TestWidget<T,N>::testLoad1(const QString& xml)
173{
174 QDomDocument qDoc;
175 QString error;
176 int line;
177 if (!qDoc.setContent(xml, &error, &line))
178 return false;
179 QDomElement root = qDoc.childNodes().at(0).toElement();
180 QDomElement e = root.childNodes().at(0).toElement();
181 bool result = T::loadFromXMI(e);
182 if (result) {
183 const SignalBlocker sb(UMLApp::app()->document());
184 result = T::activate(nullptr);
185 }
186 return result;
187}
188
189template <class T, typename N>
190void TestWidget<T,N>::testDump(const QString &title)
191{
192 QString xml = testSave1();
193 qDebug() << title << xml;
194}
195
196#endif // TESTBASE_H
Definition: testbase.h:75
SetLoading()
Definition: testbase.cpp:93
~SetLoading()
Definition: testbase.cpp:99
bool _state
Definition: testbase.h:80
Definition: testbase.h:34
virtual void initTestCase()
Definition: testbase.cpp:40
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:70
virtual void cleanupTestCase()
Definition: testbase.cpp:59
QWidget * m_mainWidget
Definition: testbase.h:46
QList< QPointer< QObject > > m_objectsToDelete
Definition: testbase.h:45
Definition: testbase.h:58
virtual void initTestCase()
Definition: testbase.cpp:75
QString temporaryPath()
Definition: testbase.cpp:88
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:64
Definition: testbase.h:93
TestUML(N name)
Definition: testbase.h:96
QString testSave1()
Definition: testbase.h:105
TestUML()
Definition: testbase.h:95
UMLObject * secondary() const
Definition: testbase.h:142
void testDump(const QString &title=QString())
Definition: testbase.h:134
bool testLoad1(const QString &xml)
Definition: testbase.h:116
TestUML(N p1, UMLObject *p2, UMLObject *p3)
Definition: testbase.h:97
Definition: testbase.h:152
TestWidget(UMLScene *scene, N w)
Definition: testbase.h:154
void testDump(const QString &title=QString())
Definition: testbase.h:190
bool testLoad1(const QString &xml)
Definition: testbase.h:172
QString testSave1()
Definition: testbase.h:161
static UMLApp * app()
Definition: umlapp.cpp:284
The base class for UML objects.
Definition: umlobject.h:70
Definition: umlscene.h:66
QSignalBlocker SignalBlocker
Definition: testbase.h:69