qtestcase.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #ifndef QTESTCASE_H
  40. #define QTESTCASE_H
  41. #include <QtTest/qtest_global.h>
  42. #include <QtCore/qstring.h>
  43. #include <QtCore/qnamespace.h>
  44. #include <QtCore/qmetatype.h>
  45. #include <QtCore/qmetaobject.h>
  46. #include <QtCore/qtypetraits.h>
  47. #include <QtCore/qsharedpointer.h>
  48. #include <QtCore/qtemporarydir.h>
  49. #include <string.h>
  50. #ifndef QT_NO_EXCEPTIONS
  51. # include <exception>
  52. #endif // QT_NO_EXCEPTIONS
  53. QT_BEGIN_NAMESPACE
  54. class QRegularExpression;
  55. #define QVERIFY(statement) \
  56. do {\
  57. if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
  58. return;\
  59. } while (0)
  60. #define QFAIL(message) \
  61. do {\
  62. QTest::qFail(message, __FILE__, __LINE__);\
  63. return;\
  64. } while (0)
  65. #define QVERIFY2(statement, description) \
  66. do {\
  67. if (statement) {\
  68. if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
  69. return;\
  70. } else {\
  71. if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
  72. return;\
  73. }\
  74. } while (0)
  75. #define QCOMPARE(actual, expected) \
  76. do {\
  77. if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
  78. return;\
  79. } while (0)
  80. #ifndef QT_NO_EXCEPTIONS
  81. # define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
  82. do {\
  83. QT_TRY {\
  84. QT_TRY {\
  85. expression;\
  86. QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
  87. " but no exception caught", __FILE__, __LINE__);\
  88. return;\
  89. } QT_CATCH (const exceptiontype &) {\
  90. }\
  91. } QT_CATCH (const std::exception &e) {\
  92. QByteArray msg = QByteArray() + "Expected exception of type " #exceptiontype \
  93. " to be thrown but std::exception caught with message: " + e.what(); \
  94. QTest::qFail(msg.constData(), __FILE__, __LINE__);\
  95. return;\
  96. } QT_CATCH (...) {\
  97. QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
  98. " but unknown exception caught", __FILE__, __LINE__);\
  99. return;\
  100. }\
  101. } while (0)
  102. #else // QT_NO_EXCEPTIONS
  103. /*
  104. * The expression passed to the macro should throw an exception and we can't
  105. * catch it because Qt has been compiled without exception support. We can't
  106. * skip the expression because it may have side effects and must be executed.
  107. * So, users must use Qt with exception support enabled if they use exceptions
  108. * in their code.
  109. */
  110. # define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
  111. Q_STATIC_ASSERT_X(false, "Support of exceptions is disabled")
  112. #endif // !QT_NO_EXCEPTIONS
  113. #define QTRY_LOOP_IMPL(expr, timeoutValue, step) \
  114. if (!(expr)) { \
  115. QTest::qWait(0); \
  116. } \
  117. int qt_test_i = 0; \
  118. for (; qt_test_i < timeoutValue && !(expr); qt_test_i += step) { \
  119. QTest::qWait(step); \
  120. }
  121. #define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step)\
  122. if (!(expr)) { \
  123. QTRY_LOOP_IMPL((expr), (2 * timeoutValue), step);\
  124. if (expr) { \
  125. 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."); \
  126. msg = msg.arg(QString::fromUtf8(#expr)).arg(timeoutValue).arg(timeoutValue + qt_test_i); \
  127. QFAIL(qPrintable(msg)); \
  128. } \
  129. }
  130. #define QTRY_IMPL(expr, timeout)\
  131. const int qt_test_step = 50; \
  132. const int qt_test_timeoutValue = timeout; \
  133. QTRY_LOOP_IMPL((expr), qt_test_timeoutValue, qt_test_step); \
  134. QTRY_TIMEOUT_DEBUG_IMPL((expr), qt_test_timeoutValue, qt_test_step)\
  135. // Will try to wait for the expression to become true while allowing event processing
  136. #define QTRY_VERIFY_WITH_TIMEOUT(expr, timeout) \
  137. do { \
  138. QTRY_IMPL((expr), timeout);\
  139. QVERIFY(expr); \
  140. } while (0)
  141. #define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT((expr), 5000)
  142. // Will try to wait for the expression to become true while allowing event processing
  143. #define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout) \
  144. do { \
  145. QTRY_IMPL((expr), timeout);\
  146. QVERIFY2(expr, messageExpression); \
  147. } while (0)
  148. #define QTRY_VERIFY2(expr, messageExpression) QTRY_VERIFY2_WITH_TIMEOUT((expr), (messageExpression), 5000)
  149. // Will try to wait for the comparison to become successful while allowing event processing
  150. #define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout) \
  151. do { \
  152. QTRY_IMPL(((expr) == (expected)), timeout);\
  153. QCOMPARE((expr), expected); \
  154. } while (0)
  155. #define QTRY_COMPARE(expr, expected) QTRY_COMPARE_WITH_TIMEOUT((expr), expected, 5000)
  156. #define QSKIP_INTERNAL(statement) \
  157. do {\
  158. QTest::qSkip(statement, __FILE__, __LINE__);\
  159. return;\
  160. } while (0)
  161. #ifdef Q_COMPILER_VARIADIC_MACROS
  162. #define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
  163. #else
  164. #define QSKIP(statement) QSKIP_INTERNAL(statement)
  165. #endif
  166. #define QEXPECT_FAIL(dataIndex, comment, mode)\
  167. do {\
  168. if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
  169. return;\
  170. } while (0)
  171. #define QFETCH(type, name)\
  172. type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
  173. #define QFETCH_GLOBAL(type, name)\
  174. type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
  175. #define QTEST(actual, testElement)\
  176. do {\
  177. if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
  178. return;\
  179. } while (0)
  180. #define QWARN(msg)\
  181. QTest::qWarn(msg, __FILE__, __LINE__)
  182. #ifdef QT_TESTCASE_BUILDDIR
  183. # define QFINDTESTDATA(basepath)\
  184. QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR)
  185. #else
  186. # define QFINDTESTDATA(basepath)\
  187. QTest::qFindTestData(basepath, __FILE__, __LINE__)
  188. #endif
  189. # define QEXTRACTTESTDATA(resourcePath) \
  190. QTest::qExtractTestData(resourcePath)
  191. class QObject;
  192. class QTestData;
  193. #define QTEST_COMPARE_DECL(KLASS)\
  194. template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
  195. namespace QTest
  196. {
  197. namespace Internal {
  198. template<typename T> // Output registered enums
  199. inline typename QtPrivate::QEnableIf<QtPrivate::IsQEnumHelper<T>::Value, char*>::Type toString(T e)
  200. {
  201. QMetaEnum me = QMetaEnum::fromType<T>();
  202. return qstrdup(me.valueToKey(int(e))); // int cast is necessary to support enum classes
  203. }
  204. template <typename T> // Fallback
  205. inline typename QtPrivate::QEnableIf<!QtPrivate::IsQEnumHelper<T>::Value, char*>::Type toString(const T &)
  206. {
  207. return Q_NULLPTR;
  208. }
  209. } // namespace Internal
  210. template<typename T>
  211. inline char *toString(const T &t)
  212. {
  213. return Internal::toString(t);
  214. }
  215. Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
  216. Q_TESTLIB_EXPORT char *toPrettyCString(const char *unicode, int length);
  217. Q_TESTLIB_EXPORT char *toPrettyUnicode(const ushort *unicode, int length);
  218. Q_TESTLIB_EXPORT char *toString(const char *);
  219. Q_TESTLIB_EXPORT char *toString(const void *);
  220. Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = Q_NULLPTR);
  221. Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
  222. Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = Q_NULLPTR);
  223. Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
  224. const char *file, int line);
  225. Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
  226. Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
  227. Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
  228. const char *file, int line);
  229. Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = Q_NULLPTR, int line = 0);
  230. Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
  231. #ifndef QT_NO_REGULAREXPRESSION
  232. Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
  233. #endif
  234. Q_TESTLIB_EXPORT QSharedPointer<QTemporaryDir> qExtractTestData(const QString &dirName);
  235. Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = Q_NULLPTR, int line = 0, const char* builddir = Q_NULLPTR);
  236. Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = Q_NULLPTR, int line = 0, const char* builddir = Q_NULLPTR);
  237. Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
  238. Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
  239. Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
  240. Q_TESTLIB_EXPORT QObject *testObject();
  241. Q_TESTLIB_EXPORT const char *currentAppName();
  242. Q_TESTLIB_EXPORT const char *currentTestFunction();
  243. Q_TESTLIB_EXPORT const char *currentDataTag();
  244. Q_TESTLIB_EXPORT bool currentTestFailed();
  245. Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
  246. Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
  247. Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
  248. char *val1, char *val2,
  249. const char *actual, const char *expected,
  250. const char *file, int line);
  251. Q_TESTLIB_EXPORT void qSleep(int ms);
  252. Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
  253. template <typename T>
  254. inline void addColumn(const char *name, T * = 0)
  255. {
  256. typedef QtPrivate::is_same<T, const char*> QIsSameTConstChar;
  257. Q_STATIC_ASSERT_X(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
  258. addColumnInternal(qMetaTypeId<T>(), name);
  259. }
  260. Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
  261. template <typename T>
  262. inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
  263. const char *file, int line)
  264. {
  265. return compare_helper(t1 == t2, "Compared values are not the same",
  266. toString(t1), toString(t2), actual, expected, file, line);
  267. }
  268. Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
  269. const char *actual, const char *expected, const char *file, int line);
  270. Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
  271. const char *actual, const char *expected, const char *file, int line);
  272. inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
  273. const char *expected, const char *file, int line)
  274. {
  275. return compare_helper(t1 == t2, "Compared pointers are not the same",
  276. toString(t1), toString(t2), actual, expected, file, line);
  277. }
  278. Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
  279. const char *expected, const char *file, int line);
  280. #ifndef Q_QDOC
  281. QTEST_COMPARE_DECL(short)
  282. QTEST_COMPARE_DECL(ushort)
  283. QTEST_COMPARE_DECL(int)
  284. QTEST_COMPARE_DECL(uint)
  285. QTEST_COMPARE_DECL(long)
  286. QTEST_COMPARE_DECL(ulong)
  287. QTEST_COMPARE_DECL(qint64)
  288. QTEST_COMPARE_DECL(quint64)
  289. QTEST_COMPARE_DECL(float)
  290. QTEST_COMPARE_DECL(double)
  291. QTEST_COMPARE_DECL(char)
  292. QTEST_COMPARE_DECL(signed char)
  293. QTEST_COMPARE_DECL(unsigned char)
  294. QTEST_COMPARE_DECL(bool)
  295. #endif
  296. template <typename T1, typename T2>
  297. bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
  298. inline bool qCompare(double const &t1, float const &t2, const char *actual,
  299. const char *expected, const char *file, int line)
  300. {
  301. return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
  302. }
  303. inline bool qCompare(float const &t1, double const &t2, const char *actual,
  304. const char *expected, const char *file, int line)
  305. {
  306. return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
  307. }
  308. template <typename T>
  309. inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
  310. const char *file, int line)
  311. {
  312. return compare_ptr_helper(t1, t2, actual, expected, file, line);
  313. }
  314. template <typename T>
  315. inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
  316. const char *file, int line)
  317. {
  318. return compare_ptr_helper(t1, t2, actual, expected, file, line);
  319. }
  320. template <typename T1, typename T2>
  321. inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
  322. const char *file, int line)
  323. {
  324. return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
  325. }
  326. template <typename T1, typename T2>
  327. inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
  328. const char *file, int line)
  329. {
  330. return compare_ptr_helper(const_cast<const T1 *>(t1),
  331. static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
  332. }
  333. inline bool qCompare(const char *t1, const char *t2, const char *actual,
  334. const char *expected, const char *file, int line)
  335. {
  336. return compare_string_helper(t1, t2, actual, expected, file, line);
  337. }
  338. inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
  339. const char *file, int line)
  340. {
  341. return compare_string_helper(t1, t2, actual, expected, file, line);
  342. }
  343. /* The next two overloads are for MSVC that shows problems with implicit
  344. conversions
  345. */
  346. inline bool qCompare(char *t1, const char *t2, const char *actual,
  347. const char *expected, const char *file, int line)
  348. {
  349. return compare_string_helper(t1, t2, actual, expected, file, line);
  350. }
  351. inline bool qCompare(const char *t1, char *t2, const char *actual,
  352. const char *expected, const char *file, int line)
  353. {
  354. return compare_string_helper(t1, t2, actual, expected, file, line);
  355. }
  356. template <class T>
  357. inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
  358. const char *expected, const char *file, int line)
  359. {
  360. return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
  361. qMetaTypeId<T>())), actualStr, expected, file, line);
  362. }
  363. }
  364. #undef QTEST_COMPARE_DECL
  365. QT_END_NAMESPACE
  366. #endif