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

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
36 public:
37 explicit TestBase(QObject *parent = nullptr);
38
39 protected Q_SLOTS:
40 virtual void initTestCase();
41 virtual void cleanupTestCase();
42 virtual void cleanupOnExit(QObject *p);
43
44 protected:
45 QList<QPointer<QObject>> m_objectsToDelete;
46 QWidget *m_mainWidget = nullptr;
47};
48
57 class TestCodeGeneratorBase : public TestBase
58{
59 Q_OBJECT
60 private Q_SLOTS:
61 virtual void initTestCase();
62
63 protected:
64 QString m_tempPath;
65 QString temporaryPath();
66};
67
68#include <QSignalBlocker>
69 typedef QSignalBlocker SignalBlocker;
70
74 class SetLoading
75{
76 public:
77 SetLoading();
78 ~SetLoading();
79 protected:
80 bool _state;
81};
82
83#include <QDomDocument>
84#include <QXmlStreamWriter>
85#include "umlapp.h"
86#include "umldoc.h"
87
91 template <class T, typename N>
92 class TestUML : public T
93{
94 public:
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
104 template <class T, typename N>
105QString TestUML<T,N>::testSave1()
106{
107 QString xml;
108 QXmlStreamWriter stream(&xml);
109 stream.writeStartElement("unittest");
110 T::saveToXMI(stream);
111 stream.writeEndElement();
112 return xml;
113}
114
115 template <class T, typename N>
116 bool 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
133 template <class T, typename N>
134 void TestUML<T,N>::testDump(const QString &title)
135{
136 QString xml = testSave1();
137 qDebug() << title << xml;
138}
139
140// used by resolveRef() tests
141 template <class T, typename N>
142 UMLObject *TestUML<T,N>::secondary() const
143{
144 return T::m_pSecondary.data();
145}
146
150 template <class T, typename N>
151 class TestWidget : public T
152{
153 public:
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
160 template <class T, typename N>
161QString TestWidget<T,N>::testSave1()
162{
163 QString xml;
164 QXmlStreamWriter stream(&xml);
165 stream.writeStartElement("unittest");
166 T::saveToXMI(stream);
167 stream.writeEndElement();
168 return xml;
169}
170
171 template <class T, typename N>
172 bool 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
189 template <class T, typename N>
190 void TestWidget<T,N>::testDump(const QString &title)
191{
192 QString xml = testSave1();
193 qDebug() << title << xml;
194}
195
196#endif // TESTBASE_H
SetLoading
Definition: testbase.h:75
SetLoading::SetLoading
SetLoading()
Definition: testbase.cpp:93
SetLoading::~SetLoading
~SetLoading()
Definition: testbase.cpp:99
SetLoading::_state
bool _state
Definition: testbase.h:80
TestBase
Definition: testbase.h:34
TestBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:40
TestBase::cleanupOnExit
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:70
TestBase::cleanupTestCase
virtual void cleanupTestCase()
Definition: testbase.cpp:59
TestBase::m_mainWidget
QWidget * m_mainWidget
Definition: testbase.h:46
TestBase::m_objectsToDelete
QList< QPointer< QObject > > m_objectsToDelete
Definition: testbase.h:45
TestCodeGeneratorBase
Definition: testbase.h:58
TestCodeGeneratorBase::initTestCase
virtual void initTestCase()
Definition: testbase.cpp:75
TestCodeGeneratorBase::temporaryPath
QString temporaryPath()
Definition: testbase.cpp:88
TestCodeGeneratorBase::m_tempPath
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:64
TestUML
Definition: testbase.h:93
TestUML::TestUML
TestUML(N name)
Definition: testbase.h:96
TestUML::testSave1
QString testSave1()
Definition: testbase.h:105
TestUML::TestUML
TestUML()
Definition: testbase.h:95
TestUML::secondary
UMLObject * secondary() const
Definition: testbase.h:142
TestUML::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:134
TestUML::testLoad1
bool testLoad1(const QString &xml)
Definition: testbase.h:116
TestUML::TestUML
TestUML(N p1, UMLObject *p2, UMLObject *p3)
Definition: testbase.h:97
TestWidget
Definition: testbase.h:152
TestWidget::TestWidget
TestWidget(UMLScene *scene, N w)
Definition: testbase.h:154
TestWidget::testDump
void testDump(const QString &title=QString())
Definition: testbase.h:190
TestWidget::testLoad1
bool testLoad1(const QString &xml)
Definition: testbase.h:172
TestWidget::testSave1
QString testSave1()
Definition: testbase.h:161
UMLApp::app
static UMLApp * app()
Definition: umlapp.cpp:284
UMLObject
The base class for UML objects.
Definition: umlobject.h:70
UMLScene
Definition: umlscene.h:66
SignalBlocker
QSignalBlocker SignalBlocker
Definition: testbase.h:69
umlapp.h
umldoc.h