edbee - Qt Editor Library v0.11.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test.h
Go to the documentation of this file.
1// edbee - Copyright (c) 2012-2025 by Rick Blommers and contributors
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include "edbee/exports.h"
7
8#include <QObject>
9#include <QString>
10#include <QSharedPointer>
11#include <QTextStream>
12
13
15
16//=============================================================================
17// MACROS
18//=============================================================================
19
20#define TEST_CONCATENATE_DETAIL(x, y) x##y
21#define TEST_CONCATENATE(x, y) TEST_CONCATENATE_DETAIL(x, y)
22#define TEST_MAKE_UNIQUE(x) TEST_CONCATENATE(x, __COUNTER__)
23
24#define DECLARE_TEST(className) static edbee::test::Test<className> TEST_MAKE_UNIQUE(t)(#className)
25#define DECLARE_NAMED_TEST(name,className) static edbee::test::Test<className> TEST_MAKE_UNIQUE(name)(#className)
26
27
28namespace edbee { namespace test {
29
30
31class OutputHandler;
32class TestEngine;
33class TestCase;
34
35//=============================================================================
36// Test Results
37//=============================================================================
38
39template <class T>
40QString toQString( const T& obj )
41{
42 return QStringLiteral("%1").arg(obj);
43}
44
45
48public:
52
53 explicit TestResult( TestCase* testCase, const QString& methodName, const QString& description, const char* file, int lineNumber );
54 virtual ~TestResult() {}
55
56 virtual void setBooleanResult( bool result, const char *statement );
57 //virtual void setCompareResult( const QString& actualValue, const QString& expectedValue, const char* actualStatement, const char* expectedStatement );
58
59 virtual void setCompareResult( bool result, const QString& actualValue, const QString& expectedValue, const char* actualStatement, const char* expectedStatement );
60
61// template <class T>
62// void setCompareResult( const T& actualValue, const T& expectedValue, const char* actualStatement, const char* expectedStatement )
63// {
64// setCompareResult( actualValue == expectedValue, toQString(actualValue), toQString(expectedValue), actualStatement, expectedStatement );
65// }
66
67 virtual void setSkip();
68
69 virtual TestCase* testCae() { return testCaseRef_; }
70 virtual QString methodName() { return methodName_; }
71 virtual QString description() { return description_; }
72 virtual const char* fileName() { return fileNameRef_; }
73 virtual int lineNumber() { return lineNumber_; }
74
75 virtual bool compareStatement() { return compareStatement_; }
76 virtual const char* actualStatement() { return actualStatementRef_; }
77 virtual const char* statement() { return actualStatementRef_; }
78 virtual const char* expectedStatement() { return expectedStatementRef_; }
79 virtual QString actualValue() { return actualValue_; }
80 virtual QString expectedValue() { return expectedValue_; }
81
82 virtual Status status() { return status_; }
83
84
85private:
86
87 // method / line defionition
88 TestCase* testCaseRef_;
89 QString methodName_;
90 QString description_;
91 const char* fileNameRef_;
92 int lineNumber_;
93
94 // code definition
95 bool compareStatement_;
96 const char* actualStatementRef_;
97 const char* expectedStatementRef_;
98 QString actualValue_;
99 QString expectedValue_;
100
101 // the result
102 Status status_;
103};
104
105
106
107
108//=============================================================================
109// Test Unit Class
110//=============================================================================
111
112#define GET_3TH_ARG(arg1, arg2, arg3, ...) arg3
113#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
114
115
116#define testTrue_1(statement) testTrueImpl( ( statement ) ? true : false, #statement, "", __FILE__, __LINE__)
117#define testTrue_2(statement, message) testTrueImpl( ( statement ) ? true : false, #statement, (message), __FILE__, __LINE__ )
118#define testTrue_MACRO_CHOOSER(...) \
119 GET_3TH_ARG(__VA_ARGS__, testTrue_2, testTrue_1, )
120#define testTrue(...) testTrue_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
121
122
123#define testFalse(statement) testTrue( !statement )
124
125
126#define testEqual_1(actual,expected) do { \
127 QString actualStr = edbee::test::toQString(actual); \
128 QString expectedStr = edbee::test::toQString(expected); \
129 testEqualImpl( ( actualStr ) == ( expectedStr ), actualStr, expectedStr, #actual, #expected, "", __FILE__, __LINE__ ); \
130} while( false )
131
132#define testEqual_2(actual,expected, message) do \
133 QString actualStr = edbee::test::toQString(actual); \
134 QString expectedStr = edbee::test::toQString(expected); \
135 testEqualImpl( ( actualStr ) == ( expectedStr ), actualStr, expectedStr, #actual, #expected, (message), __FILE__, __LINE__ ); \
136} while( false )
137
138
139#define testEqual_MACRO_CHOOSER(...) \
140 GET_4TH_ARG(__VA_ARGS__, testEqual_2, testEqual_1, )
141#define testEqual(...) testEqual_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
142
143
144#define testSkip(msg) testSkipImpl( msg, __FILE__, __LINE__)
145
146
147
148
149
157
160class EDBEE_EXPORT TestCase : public QObject
161{
162 Q_OBJECT
163public:
164 explicit TestCase();
165
167 virtual TestEngine* engine() { return engineRef_; }
168 virtual void setEngine( TestEngine *engine ) { engineRef_ = engine; }
169
170 virtual OutputHandler* out();
171
172
173 virtual void testTrueImpl( bool condition, const char* statement, const QString& description, const char* file, int line );
174
175
176 virtual void testEqualImpl( bool result, const QString& actual, const QString& expected,
177 const char* actualStatement, const char* expectedStatement,
178 const QString& description, const char* file, int line );
179
180 virtual void testSkipImpl( const QString& description, const char* file, int line );
181
182
183 virtual TestCase* currentTest(); // usually returns this :P
184 virtual QString currentMethodName();
185 virtual void giveTestResultToEngine( TestResult* result );
186
187
188signals:
189
190public slots:
191
192private slots:
193
194private:
195
196 TestEngine *engineRef_;
197};
198
199
200//=============================================================================
201// Output Handling
202//=============================================================================
203
207public:
208
209 explicit OutputHandler();
210 virtual ~OutputHandler();
211
212 // writes down the start of a multiple test run
213 virtual void startTestRun( TestEngine* engine );
214 virtual void endTestRun( TestEngine* engine );
215
216
217 // writes down the 'category' of the test
218 virtual void startTestCase( TestEngine* engine );
219 virtual void endTestCase( TestEngine* engine );
220
221
222 // method output of the test case
223 virtual void startTestMethod( TestEngine* engine );
224 virtual void endTestMethod( TestEngine* engine );
225
226 virtual void testResultAdded( TestEngine* engine, TestResult* testResult );
227
228
229
230private:
231
232 QString buffer_;
233 QString failBuffer_;
234 QString outBuffer_;
235
236 // this method notifies that a value is wrong
237// void writeSuccess( TestCase & testCase, const char *methodName );
238
239 // fails with the given messages
240 //void fail( const QString & message, const QString & details = "" ) ;
241};
242
243
244
245
246//=============================================================================
247// Test Engine
248//=============================================================================
249
251class EDBEE_EXPORT TestEngine : public QObject
252{
253 Q_OBJECT
254public:
255
256 explicit TestEngine();
257 virtual ~TestEngine();
258
259 virtual bool hasTest(TestCase* object);
260 virtual void addTest(TestCase* object);
261 virtual int runAll();
262 virtual int run( TestCase* object );
263 virtual int run( const QString& name );
264 virtual void startRun();
265 virtual void endRun();
266
267 virtual OutputHandler* outputHandler() { return outputHandlerRef_; }
268 virtual void setOutputHandler( OutputHandler* handler ) { outputHandlerRef_ = handler; }
269
271 virtual TestCase* currentTest() { return currentTestRef_; }
272
274 virtual const char* currentClassName() { return currentTestRef_->metaObject()->className(); }
275
277 virtual QString currentMethodName() { return currentMethodName_; }
278
279 virtual void giveTestResult( TestResult* testResult );
280
282 virtual QList<TestResult*> testResultList() { return testResultList_; }
283
284private:
285
286 QList<TestCase*> testRefList_;
287 OutputHandler* outputHandlerRef_;
288
289 // the current state
290 TestCase* currentTestRef_;
291 QString currentMethodName_;
292// const char* currentMethodNameRef_; ///< A reference to the current method
293
294 // test results
295 QList<TestResult*> testResultList_;
296};
297
298
299//=============================================================================
300// Appending Tests via a Macro
301//=============================================================================
302
303// this method returns the test engine
305{
306 static TestEngine engine;
307 return engine;
308}
309
310
311template <class T>
313public:
314 QSharedPointer<T> child;
315
316 Test(const QString& name) : child(new T)
317 {
318 child->setObjectName(name);
319 engine().addTest(child.data());
320 }
321};
322
323
324}} // edbee::test
This is the basic outputhandler. The basic outputhandler simply executes a qDebug with the given info...
Definition test.h:206
virtual void startTestRun(TestEngine *engine)
This method is called if the test run is started.
Definition test.cpp:177
OutputHandler()
The output handler constructor of this test.
Definition test.cpp:164
virtual void startTestMethod(TestEngine *engine)
This method is called if a test method is started.
Definition test.cpp:234
virtual void endTestCase(TestEngine *engine)
This method is called if a testcase is ended.
Definition test.cpp:224
virtual void endTestRun(TestEngine *engine)
This method is called if the test run is completed.
Definition test.cpp:187
virtual void startTestCase(TestEngine *engine)
This method is called if a testcase is started.
Definition test.cpp:216
virtual void endTestMethod(TestEngine *engine)
This method is called if a test method is ended.
Definition test.cpp:244
virtual void testResultAdded(TestEngine *engine, TestResult *testResult)
This method is called if a test result is added.
Definition test.cpp:261
I really really hate the QTestLib output on my Mac. It's a very ugly xwindows console result.
Definition test.h:161
TestCase()
Constructs the testcase.
Definition test.cpp:80
virtual TestEngine * engine()
This method returns the current engine.
Definition test.h:167
virtual void setEngine(TestEngine *engine)
Definition test.h:168
This is the main test engine.
Definition test.h:252
virtual void endRun()
This method should be called when your done with running single test When using runAll you should not...
Definition test.cpp:447
virtual int run(TestCase *object)
This class executes all tests on the given test object (all private Slots)
Definition test.cpp:363
virtual QList< TestResult * > testResultList()
This method returns a list of test results.
Definition test.h:282
virtual int runAll()
This method runs all tests.
Definition test.cpp:345
virtual void setOutputHandler(OutputHandler *handler)
Definition test.h:268
virtual OutputHandler * outputHandler()
Definition test.h:267
virtual QString currentMethodName()
This method returns the current method name.
Definition test.h:277
virtual void startRun()
should be called before the first run when manually running single unit tests When using runAll you s...
Definition test.cpp:439
virtual void addTest(TestCase *object)
this metohd adds a test
Definition test.cpp:337
virtual TestCase * currentTest()
This method returns the current test.
Definition test.h:271
TestEngine()
The test engine constructor.
Definition test.cpp:305
virtual const char * currentClassName()
This method returns the current class name.
Definition test.h:274
virtual bool hasTest(TestCase *object)
this method returns true if the given object is found
Definition test.cpp:324
QSharedPointer< T > child
Definition test.h:314
Test(const QString &name)
Definition test.h:316
This method represents a test result.
Definition test.h:47
TestResult(TestCase *testCase, const QString &methodName, const QString &description, const char *file, int lineNumber)
A single test result.
Definition test.cpp:23
virtual const char * actualStatement()
Definition test.h:76
Status
Definition test.h:49
@ Passed
Definition test.h:50
@ Skipped
Definition test.h:50
@ Failed
Definition test.h:50
virtual const char * statement()
Definition test.h:77
virtual const char * expectedStatement()
Definition test.h:78
virtual Status status()
Definition test.h:82
virtual QString expectedValue()
Definition test.h:80
virtual ~TestResult()
Definition test.h:54
virtual QString description()
Definition test.h:71
virtual QString methodName()
Definition test.h:70
virtual bool compareStatement()
Definition test.h:75
virtual int lineNumber()
Definition test.h:73
virtual QString actualValue()
Definition test.h:79
virtual TestCase * testCae()
Definition test.h:69
virtual const char * fileName()
Definition test.h:72
#define EDBEE_EXPORT
Definition exports.h:15
QString toQString(const T &obj)
Definition test.h:40
TestEngine & engine()
Definition test.h:304
QT Acessibility has an issue with reporting blank lines between elements lines. defining 'WINDOWS_EMP...
Definition commentcommand.cpp:20