1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTESTCASE_H
41#define QTESTCASE_H
42
43#include <QtTest/qttestglobal.h>
44
45#include <QtCore/qstring.h>
46#include <QtCore/qnamespace.h>
47#include <QtCore/qmetatype.h>
48#include <QtCore/qmetaobject.h>
49#include <QtCore/qsharedpointer.h>
50#include <QtCore/qtemporarydir.h>
51
52#include <string.h>
53
54#ifndef QT_NO_EXCEPTIONS
55# include <exception>
56#endif // QT_NO_EXCEPTIONS
57
58QT_BEGIN_NAMESPACE
59
60class QRegularExpression;
61
62#define QVERIFY(statement) \
63do {\
64 if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))\
65 return;\
66} while (false)
67
68#define QFAIL(message) \
69do {\
70 QTest::qFail(message, __FILE__, __LINE__);\
71 return;\
72} while (false)
73
74#define QVERIFY2(statement, description) \
75do {\
76 if (statement) {\
77 if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
78 return;\
79 } else {\
80 if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
81 return;\
82 }\
83} while (false)
84
85#define QCOMPARE(actual, expected) \
86do {\
87 if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
88 return;\
89} while (false)
90
91
92#ifndef QT_NO_EXCEPTIONS
93
94# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
95 do {\
96 QT_TRY {\
97 QT_TRY {\
98 expression;\
99 QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
100 " but no exception caught", __FILE__, __LINE__);\
101 return;\
102 } QT_CATCH (const exceptiontype &) {\
103 }\
104 } QT_CATCH (const std::exception &e) {\
105 QByteArray msg = QByteArray() + "Expected exception of type " #exceptiontype \
106 " to be thrown but std::exception caught with message: " + e.what(); \
107 QTest::qFail(msg.constData(), __FILE__, __LINE__);\
108 return;\
109 } QT_CATCH (...) {\
110 QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
111 " but unknown exception caught", __FILE__, __LINE__);\
112 return;\
113 }\
114 } while (false)
115
116#else // QT_NO_EXCEPTIONS
117
118/*
119 * The expression passed to the macro should throw an exception and we can't
120 * catch it because Qt has been compiled without exception support. We can't
121 * skip the expression because it may have side effects and must be executed.
122 * So, users must use Qt with exception support enabled if they use exceptions
123 * in their code.
124 */
125# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
126 Q_STATIC_ASSERT_X(false, "Support of exceptions is disabled")
127
128#endif // !QT_NO_EXCEPTIONS
129
130
131#define QTRY_LOOP_IMPL(expr, timeoutValue, step) \
132 if (!(expr)) { \
133 QTest::qWait(0); \
134 } \
135 int qt_test_i = 0; \
136 for (; qt_test_i < timeoutValue && !(expr); qt_test_i += step) { \
137 QTest::qWait(step); \
138 }
139
140#define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step)\
141 if (!(expr)) { \
142 QTRY_LOOP_IMPL((expr), (2 * timeoutValue), step);\
143 if (expr) { \
144 QString msg = QString::fromUtf8("QTestLib: This test case check (\"%1\") failed because the requested timeout (%2 ms) was too short, %3 ms would have been sufficient this time."); \
145 msg = msg.arg(QString::fromUtf8(#expr)).arg(timeoutValue).arg(timeoutValue + qt_test_i); \
146 QFAIL(qPrintable(msg)); \
147 } \
148 }
149
150// Ideally we'd use qWaitFor instead of QTRY_LOOP_IMPL, but due
151// to a compiler bug on MSVC < 2017 we can't (see QTBUG-59096)
152#define QTRY_IMPL(expr, timeout)\
153 const int qt_test_step = 50; \
154 const int qt_test_timeoutValue = timeout; \
155 QTRY_LOOP_IMPL((expr), qt_test_timeoutValue, qt_test_step); \
156 QTRY_TIMEOUT_DEBUG_IMPL((expr), qt_test_timeoutValue, qt_test_step)\
157
158// Will try to wait for the expression to become true while allowing event processing
159#define QTRY_VERIFY_WITH_TIMEOUT(expr, timeout) \
160do { \
161 QTRY_IMPL((expr), timeout);\
162 QVERIFY(expr); \
163} while (false)
164
165#define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT((expr), 5000)
166
167// Will try to wait for the expression to become true while allowing event processing
168#define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout) \
169do { \
170 QTRY_IMPL((expr), timeout);\
171 QVERIFY2(expr, messageExpression); \
172} while (false)
173
174#define QTRY_VERIFY2(expr, messageExpression) QTRY_VERIFY2_WITH_TIMEOUT((expr), (messageExpression), 5000)
175
176// Will try to wait for the comparison to become successful while allowing event processing
177#define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout) \
178do { \
179 QTRY_IMPL(((expr) == (expected)), timeout);\
180 QCOMPARE((expr), expected); \
181} while (false)
182
183#define QTRY_COMPARE(expr, expected) QTRY_COMPARE_WITH_TIMEOUT((expr), expected, 5000)
184
185#define QSKIP_INTERNAL(statement) \
186do {\
187 QTest::qSkip(statement, __FILE__, __LINE__);\
188 return;\
189} while (false)
190
191#ifdef Q_COMPILER_VARIADIC_MACROS
192
193#define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
194
195#else
196
197#define QSKIP(statement) QSKIP_INTERNAL(statement)
198
199#endif
200
201#define QEXPECT_FAIL(dataIndex, comment, mode)\
202do {\
203 if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
204 return;\
205} while (false)
206
207#define QFETCH(Type, name)\
208 Type name = *static_cast<Type *>(QTest::qData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
209
210#define QFETCH_GLOBAL(Type, name)\
211 Type name = *static_cast<Type *>(QTest::qGlobalData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
212
213#define QTEST(actual, testElement)\
214do {\
215 if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
216 return;\
217} while (false)
218
219#define QWARN(msg)\
220 QTest::qWarn(msg, __FILE__, __LINE__)
221
222#ifdef QT_TESTCASE_BUILDDIR
223# define QFINDTESTDATA(basepath)\
224 QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR)
225#else
226# define QFINDTESTDATA(basepath)\
227 QTest::qFindTestData(basepath, __FILE__, __LINE__)
228#endif
229
230# define QEXTRACTTESTDATA(resourcePath) \
231 QTest::qExtractTestData(resourcePath)
232
233class QObject;
234class QTestData;
235
236#define QTEST_COMPARE_DECL(KLASS)\
237 template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
238
239namespace QTest
240{
241 namespace Internal {
242
243 template<typename T> // Output registered enums
244 inline typename std::enable_if<QtPrivate::IsQEnumHelper<T>::Value, char*>::type toString(T e)
245 {
246 QMetaEnum me = QMetaEnum::fromType<T>();
247 return qstrdup(me.valueToKey(int(e))); // int cast is necessary to support enum classes
248 }
249
250 template <typename T> // Fallback
251 inline typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value, char*>::type toString(const T &)
252 {
253 return nullptr;
254 }
255
256 template<typename F> // Output QFlags of registered enumerations
257 inline typename std::enable_if<QtPrivate::IsQEnumHelper<F>::Value, char*>::type toString(QFlags<F> f)
258 {
259 const QMetaEnum me = QMetaEnum::fromType<F>();
260 return qstrdup(me.valueToKeys(int(f)).constData());
261 }
262
263 template <typename F> // Fallback: Output hex value
264 inline typename std::enable_if<!QtPrivate::IsQEnumHelper<F>::Value, char*>::type toString(QFlags<F> f)
265 {
266 const size_t space = 3 + 2 * sizeof(unsigned); // 2 for 0x, two hex digits per byte, 1 for '\0'
267 char *msg = new char[space];
268 qsnprintf(msg, space, "0x%x", unsigned(f));
269 return msg;
270 }
271
272 } // namespace Internal
273
274 template<typename T>
275 inline char *toString(const T &t)
276 {
277 return Internal::toString(t);
278 }
279
280 template <typename T1, typename T2>
281 inline char *toString(const QPair<T1, T2> &pair);
282
283 template <typename T1, typename T2>
284 inline char *toString(const std::pair<T1, T2> &pair);
285
286 Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
287 Q_TESTLIB_EXPORT char *toPrettyCString(const char *unicode, int length);
288 Q_TESTLIB_EXPORT char *toPrettyUnicode(QStringView string);
289 Q_TESTLIB_EXPORT char *toString(const char *);
290 Q_TESTLIB_EXPORT char *toString(const void *);
291
292 Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc = 0, char **argv = nullptr);
293 Q_TESTLIB_EXPORT int qRun();
294 Q_TESTLIB_EXPORT void qCleanup();
295
296 Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = nullptr);
297 Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
298
299 Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = nullptr);
300
301 Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
302 const char *file, int line);
303 Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
304 Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
305 Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
306 const char *file, int line);
307 Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = nullptr, int line = 0);
308 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
309#if QT_CONFIG(regularexpression)
310 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
311#endif
312
313#if QT_CONFIG(temporaryfile)
314 Q_TESTLIB_EXPORT QSharedPointer<QTemporaryDir> qExtractTestData(const QString &dirName);
315#endif
316 Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr);
317 Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr);
318
319 Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
320 Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
321 Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
322 Q_TESTLIB_EXPORT QObject *testObject();
323
324 Q_TESTLIB_EXPORT const char *currentAppName();
325
326 Q_TESTLIB_EXPORT const char *currentTestFunction();
327 Q_TESTLIB_EXPORT const char *currentDataTag();
328 Q_TESTLIB_EXPORT bool currentTestFailed();
329
330 Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
331 Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
332
333 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
334 char *val1, char *val2,
335 const char *actual, const char *expected,
336 const char *file, int line);
337 Q_TESTLIB_EXPORT void qSleep(int ms);
338 Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
339
340 template <typename T>
341 inline void addColumn(const char *name, T * = nullptr)
342 {
343 typedef std::is_same<T, const char*> QIsSameTConstChar;
344 Q_STATIC_ASSERT_X(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
345 addColumnInternal(qMetaTypeId<T>(), name);
346 }
347 Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
348 Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
349
350#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
351 // kept after adding implementation of <T1, T2> out of paranoia:
352 template <typename T>
353 inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
354 const char *file, int line)
355 {
356 return compare_helper(t1 == t2, "Compared values are not the same",
357 toString(t1), toString(t2), actual, expected, file, line);
358 }
359#endif
360
361 Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
362 const char *actual, const char *expected, const char *file, int line);
363
364 Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
365 const char *actual, const char *expected, const char *file, int line);
366
367 inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual,
368 const char *expected, const char *file, int line)
369 {
370 return compare_helper(t1 == t2, "Compared pointers are not the same",
371 toString(t1), toString(t2), actual, expected, file, line);
372 }
373
374 inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
375 const char *expected, const char *file, int line)
376 {
377 return compare_helper(t1 == nullptr, "Compared pointers are not the same",
378 toString(t1), toString(nullptr), actual, expected, file, line);
379 }
380
381 inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual,
382 const char *expected, const char *file, int line)
383 {
384 return compare_helper(nullptr == t2, "Compared pointers are not the same",
385 toString(nullptr), toString(t2), actual, expected, file, line);
386 }
387
388 Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
389 const char *expected, const char *file, int line);
390
391#ifndef Q_QDOC
392 QTEST_COMPARE_DECL(short)
393 QTEST_COMPARE_DECL(ushort)
394 QTEST_COMPARE_DECL(int)
395 QTEST_COMPARE_DECL(uint)
396 QTEST_COMPARE_DECL(long)
397 QTEST_COMPARE_DECL(ulong)
398 QTEST_COMPARE_DECL(qint64)
399 QTEST_COMPARE_DECL(quint64)
400
401 QTEST_COMPARE_DECL(float)
402 QTEST_COMPARE_DECL(double)
403 QTEST_COMPARE_DECL(char)
404 QTEST_COMPARE_DECL(signed char)
405 QTEST_COMPARE_DECL(unsigned char)
406 QTEST_COMPARE_DECL(bool)
407#endif
408
409 template <typename T1, typename T2>
410 inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected,
411 const char *file, int line)
412 {
413 return compare_helper(t1 == t2, "Compared values are not the same",
414 toString(t1), toString(t2), actual, expected, file, line);
415 }
416
417 inline bool qCompare(double const &t1, float const &t2, const char *actual,
418 const char *expected, const char *file, int line)
419 {
420 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
421 }
422
423 inline bool qCompare(float const &t1, double const &t2, const char *actual,
424 const char *expected, const char *file, int line)
425 {
426 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
427 }
428
429 template <typename T>
430 inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
431 const char *file, int line)
432 {
433 return compare_ptr_helper(t1, t2, actual, expected, file, line);
434 }
435 template <typename T>
436 inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
437 const char *file, int line)
438 {
439 return compare_ptr_helper(t1, t2, actual, expected, file, line);
440 }
441
442 template <typename T>
443 inline bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected,
444 const char *file, int line)
445 {
446 return compare_ptr_helper(t1, nullptr, actual, expected, file, line);
447 }
448 template <typename T>
449 inline bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected,
450 const char *file, int line)
451 {
452 return compare_ptr_helper(nullptr, t2, actual, expected, file, line);
453 }
454
455 template <typename T1, typename T2>
456 inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
457 const char *file, int line)
458 {
459 return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
460 }
461 template <typename T1, typename T2>
462 inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
463 const char *file, int line)
464 {
465 return compare_ptr_helper(const_cast<const T1 *>(t1),
466 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
467 }
468 inline bool qCompare(const char *t1, const char *t2, const char *actual,
469 const char *expected, const char *file, int line)
470 {
471 return compare_string_helper(t1, t2, actual, expected, file, line);
472 }
473 inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
474 const char *file, int line)
475 {
476 return compare_string_helper(t1, t2, actual, expected, file, line);
477 }
478
479 /* The next two overloads are for MSVC that shows problems with implicit
480 conversions
481 */
482 inline bool qCompare(char *t1, const char *t2, const char *actual,
483 const char *expected, const char *file, int line)
484 {
485 return compare_string_helper(t1, t2, actual, expected, file, line);
486 }
487 inline bool qCompare(const char *t1, char *t2, const char *actual,
488 const char *expected, const char *file, int line)
489 {
490 return compare_string_helper(t1, t2, actual, expected, file, line);
491 }
492
493 template <class T>
494 inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
495 const char *expected, const char *file, int line)
496 {
497 return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
498 qMetaTypeId<T>())), actualStr, expected, file, line);
499 }
500}
501
502#undef QTEST_COMPARE_DECL
503
504QT_END_NAMESPACE
505
506#endif
507