Line data Source code
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/qtest_global.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 :
58 : QT_BEGIN_NAMESPACE
59 :
60 : class QRegularExpression;
61 :
62 : #define QVERIFY(statement) \
63 : do {\
64 : if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))\
65 : return;\
66 : } while (false)
67 :
68 : #define QFAIL(message) \
69 : do {\
70 : QTest::qFail(message, __FILE__, __LINE__);\
71 : return;\
72 : } while (false)
73 :
74 : #define QVERIFY2(statement, description) \
75 : do {\
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) \
86 : do {\
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) \
160 : do { \
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) \
169 : do { \
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) \
178 : do { \
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) \
186 : do {\
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)\
202 : do {\
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)\
214 : do {\
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 :
233 : class QObject;
234 : class QTestData;
235 :
236 : #define QTEST_COMPARE_DECL(KLASS)\
237 : template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
238 :
239 : namespace 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 Q_NULLPTR;
254 : }
255 :
256 : } // namespace Internal
257 :
258 : template<typename T>
259 : inline char *toString(const T &t)
260 : {
261 : return Internal::toString(t);
262 : }
263 :
264 : template <typename T1, typename T2>
265 : inline char *toString(const QPair<T1, T2> &pair);
266 :
267 : template <typename T1, typename T2>
268 : inline char *toString(const std::pair<T1, T2> &pair);
269 :
270 : Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
271 : Q_TESTLIB_EXPORT char *toPrettyCString(const char *unicode, int length);
272 : Q_TESTLIB_EXPORT char *toPrettyUnicode(QStringView string);
273 : Q_TESTLIB_EXPORT char *toString(const char *);
274 : Q_TESTLIB_EXPORT char *toString(const void *);
275 :
276 : Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc = 0, char **argv = Q_NULLPTR);
277 : Q_TESTLIB_EXPORT int qRun();
278 : Q_TESTLIB_EXPORT void qCleanup();
279 :
280 : Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = Q_NULLPTR);
281 : Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
282 :
283 : Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = Q_NULLPTR);
284 :
285 : Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
286 : const char *file, int line);
287 : Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
288 : Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
289 : Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
290 : const char *file, int line);
291 : Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = Q_NULLPTR, int line = 0);
292 : Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
293 : #ifndef QT_NO_REGULAREXPRESSION
294 : Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
295 : #endif
296 :
297 : #if QT_CONFIG(temporaryfile)
298 : Q_TESTLIB_EXPORT QSharedPointer<QTemporaryDir> qExtractTestData(const QString &dirName);
299 : #endif
300 : Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = Q_NULLPTR, int line = 0, const char* builddir = Q_NULLPTR);
301 : Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = Q_NULLPTR, int line = 0, const char* builddir = Q_NULLPTR);
302 :
303 : Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
304 : Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
305 : Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
306 : Q_TESTLIB_EXPORT QObject *testObject();
307 :
308 : Q_TESTLIB_EXPORT const char *currentAppName();
309 :
310 : Q_TESTLIB_EXPORT const char *currentTestFunction();
311 : Q_TESTLIB_EXPORT const char *currentDataTag();
312 : Q_TESTLIB_EXPORT bool currentTestFailed();
313 :
314 : Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
315 : Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
316 :
317 : Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
318 : char *val1, char *val2,
319 : const char *actual, const char *expected,
320 : const char *file, int line);
321 : Q_TESTLIB_EXPORT void qSleep(int ms);
322 : Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
323 :
324 : template <typename T>
325 : inline void addColumn(const char *name, T * = nullptr)
326 : {
327 : typedef std::is_same<T, const char*> QIsSameTConstChar;
328 : Q_STATIC_ASSERT_X(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
329 : addColumnInternal(qMetaTypeId<T>(), name);
330 : }
331 : Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
332 : Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
333 :
334 : #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
335 : // kept after adding implementation of <T1, T2> out of paranoia:
336 : template <typename T>
337 22 : inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
338 : const char *file, int line)
339 : {
340 22 : return compare_helper(t1 == t2, "Compared values are not the same",
341 22 : toString(t1), toString(t2), actual, expected, file, line);
342 : }
343 : #endif
344 :
345 : Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
346 : const char *actual, const char *expected, const char *file, int line);
347 :
348 : Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
349 : const char *actual, const char *expected, const char *file, int line);
350 :
351 : inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual,
352 : const char *expected, const char *file, int line)
353 : {
354 : return compare_helper(t1 == t2, "Compared pointers are not the same",
355 : toString(t1), toString(t2), actual, expected, file, line);
356 : }
357 :
358 : inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
359 : const char *expected, const char *file, int line)
360 : {
361 : return compare_helper(t1 == nullptr, "Compared pointers are not the same",
362 : toString(t1), toString(nullptr), actual, expected, file, line);
363 : }
364 :
365 : inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual,
366 : const char *expected, const char *file, int line)
367 : {
368 : return compare_helper(nullptr == t2, "Compared pointers are not the same",
369 : toString(nullptr), toString(t2), actual, expected, file, line);
370 : }
371 :
372 : Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
373 : const char *expected, const char *file, int line);
374 :
375 : #ifndef Q_QDOC
376 : QTEST_COMPARE_DECL(short)
377 : QTEST_COMPARE_DECL(ushort)
378 : QTEST_COMPARE_DECL(int)
379 : QTEST_COMPARE_DECL(uint)
380 : QTEST_COMPARE_DECL(long)
381 : QTEST_COMPARE_DECL(ulong)
382 : QTEST_COMPARE_DECL(qint64)
383 : QTEST_COMPARE_DECL(quint64)
384 :
385 : QTEST_COMPARE_DECL(float)
386 : QTEST_COMPARE_DECL(double)
387 : QTEST_COMPARE_DECL(char)
388 : QTEST_COMPARE_DECL(signed char)
389 : QTEST_COMPARE_DECL(unsigned char)
390 : QTEST_COMPARE_DECL(bool)
391 : #endif
392 :
393 : template <typename T1, typename T2>
394 : inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected,
395 : const char *file, int line)
396 : {
397 : return compare_helper(t1 == t2, "Compared values are not the same",
398 : toString(t1), toString(t2), actual, expected, file, line);
399 : }
400 :
401 : inline bool qCompare(double const &t1, float const &t2, const char *actual,
402 : const char *expected, const char *file, int line)
403 : {
404 : return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
405 : }
406 :
407 : inline bool qCompare(float const &t1, double const &t2, const char *actual,
408 : const char *expected, const char *file, int line)
409 : {
410 : return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
411 : }
412 :
413 : template <typename T>
414 : inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
415 : const char *file, int line)
416 : {
417 : return compare_ptr_helper(t1, t2, actual, expected, file, line);
418 : }
419 : template <typename T>
420 : inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
421 : const char *file, int line)
422 : {
423 : return compare_ptr_helper(t1, t2, actual, expected, file, line);
424 : }
425 :
426 : template <typename T>
427 : inline bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected,
428 : const char *file, int line)
429 : {
430 : return compare_ptr_helper(t1, nullptr, actual, expected, file, line);
431 : }
432 : template <typename T>
433 : inline bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected,
434 : const char *file, int line)
435 : {
436 : return compare_ptr_helper(nullptr, t2, actual, expected, file, line);
437 : }
438 :
439 : template <typename T1, typename T2>
440 : inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
441 : const char *file, int line)
442 : {
443 : return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
444 : }
445 : template <typename T1, typename T2>
446 : inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
447 : const char *file, int line)
448 : {
449 : return compare_ptr_helper(const_cast<const T1 *>(t1),
450 : static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
451 : }
452 : inline bool qCompare(const char *t1, const char *t2, const char *actual,
453 : const char *expected, const char *file, int line)
454 : {
455 : return compare_string_helper(t1, t2, actual, expected, file, line);
456 : }
457 : inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
458 : const char *file, int line)
459 : {
460 : return compare_string_helper(t1, t2, actual, expected, file, line);
461 : }
462 :
463 : /* The next two overloads are for MSVC that shows problems with implicit
464 : conversions
465 : */
466 : inline bool qCompare(char *t1, const char *t2, const char *actual,
467 : const char *expected, const char *file, int line)
468 : {
469 : return compare_string_helper(t1, t2, actual, expected, file, line);
470 : }
471 : inline bool qCompare(const char *t1, char *t2, const char *actual,
472 : const char *expected, const char *file, int line)
473 : {
474 : return compare_string_helper(t1, t2, actual, expected, file, line);
475 : }
476 :
477 : template <class T>
478 : inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
479 : const char *expected, const char *file, int line)
480 : {
481 : return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
482 : qMetaTypeId<T>())), actualStr, expected, file, line);
483 : }
484 : }
485 :
486 : #undef QTEST_COMPARE_DECL
487 :
488 : QT_END_NAMESPACE
489 :
490 : #endif
|