LCOV - code coverage report
Current view: top level - usr/include/x86_64-linux-gnu/qt5/QtTest - qtestcase.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 3 100.0 %
Date: 2017-03-02 17:11:10 Functions: 1 1 100.0 %

          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/qtypetraits.h>
      50             : #include <QtCore/qsharedpointer.h>
      51             : #include <QtCore/qtemporarydir.h>
      52             : 
      53             : #include <string.h>
      54             : 
      55             : #ifndef QT_NO_EXCEPTIONS
      56             : #  include <exception>
      57             : #endif // QT_NO_EXCEPTIONS
      58             : 
      59             : 
      60             : QT_BEGIN_NAMESPACE
      61             : 
      62             : class QRegularExpression;
      63             : 
      64             : #define QVERIFY(statement) \
      65             : do {\
      66             :     if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
      67             :         return;\
      68             : } while (0)
      69             : 
      70             : #define QFAIL(message) \
      71             : do {\
      72             :     QTest::qFail(message, __FILE__, __LINE__);\
      73             :     return;\
      74             : } while (0)
      75             : 
      76             : #define QVERIFY2(statement, description) \
      77             : do {\
      78             :     if (statement) {\
      79             :         if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
      80             :             return;\
      81             :     } else {\
      82             :         if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
      83             :             return;\
      84             :     }\
      85             : } while (0)
      86             : 
      87             : #define QCOMPARE(actual, expected) \
      88             : do {\
      89             :     if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
      90             :         return;\
      91             : } while (0)
      92             : 
      93             : 
      94             : #ifndef QT_NO_EXCEPTIONS
      95             : 
      96             : #  define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
      97             :     do {\
      98             :         QT_TRY {\
      99             :             QT_TRY {\
     100             :                 expression;\
     101             :                 QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
     102             :                              " but no exception caught", __FILE__, __LINE__);\
     103             :                 return;\
     104             :             } QT_CATCH (const exceptiontype &) {\
     105             :             }\
     106             :         } QT_CATCH (const std::exception &e) {\
     107             :             QByteArray msg = QByteArray() + "Expected exception of type " #exceptiontype \
     108             :                              " to be thrown but std::exception caught with message: " + e.what(); \
     109             :             QTest::qFail(msg.constData(), __FILE__, __LINE__);\
     110             :             return;\
     111             :         } QT_CATCH (...) {\
     112             :             QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
     113             :                          " but unknown exception caught", __FILE__, __LINE__);\
     114             :             return;\
     115             :         }\
     116             :     } while (0)
     117             : 
     118             : #else // QT_NO_EXCEPTIONS
     119             : 
     120             : /*
     121             :  * The expression passed to the macro should throw an exception and we can't
     122             :  * catch it because Qt has been compiled without exception support. We can't
     123             :  * skip the expression because it may have side effects and must be executed.
     124             :  * So, users must use Qt with exception support enabled if they use exceptions
     125             :  * in their code.
     126             :  */
     127             : #  define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
     128             :     Q_STATIC_ASSERT_X(false, "Support of exceptions is disabled")
     129             : 
     130             : #endif // !QT_NO_EXCEPTIONS
     131             : 
     132             : 
     133             : #define QTRY_LOOP_IMPL(expr, timeoutValue, step) \
     134             :     if (!(expr)) { \
     135             :         QTest::qWait(0); \
     136             :     } \
     137             :     int qt_test_i = 0; \
     138             :     for (; qt_test_i < timeoutValue && !(expr); qt_test_i += step) { \
     139             :         QTest::qWait(step); \
     140             :     }
     141             : 
     142             : #define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step)\
     143             :     if (!(expr)) { \
     144             :         QTRY_LOOP_IMPL((expr), (2 * timeoutValue), step);\
     145             :         if (expr) { \
     146             :             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."); \
     147             :             msg = msg.arg(QString::fromUtf8(#expr)).arg(timeoutValue).arg(timeoutValue + qt_test_i); \
     148             :             QFAIL(qPrintable(msg)); \
     149             :         } \
     150             :     }
     151             : 
     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 (0)
     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 (0)
     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 (0)
     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 (0)
     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 (0)
     206             : 
     207             : #define QFETCH(type, name)\
     208             :     type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
     209             : 
     210             : #define QFETCH_GLOBAL(type, name)\
     211             :     type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
     212             : 
     213             : #define QTEST(actual, testElement)\
     214             : do {\
     215             :     if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
     216             :         return;\
     217             : } while (0)
     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 QtPrivate::QEnableIf<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 QtPrivate::QEnableIf<!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             :     Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
     265             :     Q_TESTLIB_EXPORT char *toPrettyCString(const char *unicode, int length);
     266             :     Q_TESTLIB_EXPORT char *toPrettyUnicode(const ushort *unicode, int length);
     267             :     Q_TESTLIB_EXPORT char *toString(const char *);
     268             :     Q_TESTLIB_EXPORT char *toString(const void *);
     269             : 
     270             :     Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = Q_NULLPTR);
     271             :     Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
     272             : 
     273             :     Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = Q_NULLPTR);
     274             : 
     275             :     Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
     276             :                                  const char *file, int line);
     277             :     Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
     278             :     Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
     279             :     Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
     280             :                            const char *file, int line);
     281             :     Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = Q_NULLPTR, int line = 0);
     282             :     Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
     283             : #ifndef QT_NO_REGULAREXPRESSION
     284             :     Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
     285             : #endif
     286             : 
     287             :     Q_TESTLIB_EXPORT QSharedPointer<QTemporaryDir> qExtractTestData(const QString &dirName);
     288             :     Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = Q_NULLPTR, int line = 0, const char* builddir = Q_NULLPTR);
     289             :     Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = Q_NULLPTR, int line = 0, const char* builddir = Q_NULLPTR);
     290             : 
     291             :     Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
     292             :     Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
     293             :     Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
     294             :     Q_TESTLIB_EXPORT QObject *testObject();
     295             : 
     296             :     Q_TESTLIB_EXPORT const char *currentAppName();
     297             : 
     298             :     Q_TESTLIB_EXPORT const char *currentTestFunction();
     299             :     Q_TESTLIB_EXPORT const char *currentDataTag();
     300             :     Q_TESTLIB_EXPORT bool currentTestFailed();
     301             : 
     302             :     Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
     303             :     Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
     304             : 
     305             :     Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
     306             :                                          char *val1, char *val2,
     307             :                                          const char *actual, const char *expected,
     308             :                                          const char *file, int line);
     309             :     Q_TESTLIB_EXPORT void qSleep(int ms);
     310             :     Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
     311             : 
     312             :     template <typename T>
     313             :     inline void addColumn(const char *name, T * = 0)
     314             :     {
     315             :         typedef QtPrivate::is_same<T, const char*> QIsSameTConstChar;
     316             :         Q_STATIC_ASSERT_X(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
     317             :         addColumnInternal(qMetaTypeId<T>(), name);
     318             :     }
     319             :     Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
     320             : 
     321             :     template <typename T>
     322          20 :     inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
     323             :                         const char *file, int line)
     324             :     {
     325          20 :         return compare_helper(t1 == t2, "Compared values are not the same",
     326          20 :                               toString(t1), toString(t2), actual, expected, file, line);
     327             :     }
     328             : 
     329             :     Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
     330             :                     const char *actual, const char *expected, const char *file, int line);
     331             : 
     332             :     Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
     333             :                     const char *actual, const char *expected, const char *file, int line);
     334             : 
     335             :     inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
     336             :                                    const char *expected, const char *file, int line)
     337             :     {
     338             :         return compare_helper(t1 == t2, "Compared pointers are not the same",
     339             :                               toString(t1), toString(t2), actual, expected, file, line);
     340             :     }
     341             : 
     342             :     Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
     343             :                                       const char *expected, const char *file, int line);
     344             : 
     345             : #ifndef Q_QDOC
     346             :     QTEST_COMPARE_DECL(short)
     347             :     QTEST_COMPARE_DECL(ushort)
     348             :     QTEST_COMPARE_DECL(int)
     349             :     QTEST_COMPARE_DECL(uint)
     350             :     QTEST_COMPARE_DECL(long)
     351             :     QTEST_COMPARE_DECL(ulong)
     352             :     QTEST_COMPARE_DECL(qint64)
     353             :     QTEST_COMPARE_DECL(quint64)
     354             : 
     355             :     QTEST_COMPARE_DECL(float)
     356             :     QTEST_COMPARE_DECL(double)
     357             :     QTEST_COMPARE_DECL(char)
     358             :     QTEST_COMPARE_DECL(signed char)
     359             :     QTEST_COMPARE_DECL(unsigned char)
     360             :     QTEST_COMPARE_DECL(bool)
     361             : #endif
     362             : 
     363             :     template <typename T1, typename T2>
     364             :     bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
     365             : 
     366             :     inline bool qCompare(double const &t1, float const &t2, const char *actual,
     367             :                                  const char *expected, const char *file, int line)
     368             :     {
     369             :         return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
     370             :     }
     371             : 
     372             :     inline bool qCompare(float const &t1, double const &t2, const char *actual,
     373             :                                  const char *expected, const char *file, int line)
     374             :     {
     375             :         return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
     376             :     }
     377             : 
     378             :     template <typename T>
     379             :     inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
     380             :                         const char *file, int line)
     381             :     {
     382             :         return compare_ptr_helper(t1, t2, actual, expected, file, line);
     383             :     }
     384             :     template <typename T>
     385             :     inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
     386             :                         const char *file, int line)
     387             :     {
     388             :         return compare_ptr_helper(t1, t2, actual, expected, file, line);
     389             :     }
     390             : 
     391             :     template <typename T1, typename T2>
     392             :     inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
     393             :                         const char *file, int line)
     394             :     {
     395             :         return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
     396             :     }
     397             :     template <typename T1, typename T2>
     398             :     inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
     399             :                         const char *file, int line)
     400             :     {
     401             :         return compare_ptr_helper(const_cast<const T1 *>(t1),
     402             :                 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
     403             :     }
     404             :     inline bool qCompare(const char *t1, const char *t2, const char *actual,
     405             :                                        const char *expected, const char *file, int line)
     406             :     {
     407             :         return compare_string_helper(t1, t2, actual, expected, file, line);
     408             :     }
     409             :     inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
     410             :                         const char *file, int line)
     411             :     {
     412             :         return compare_string_helper(t1, t2, actual, expected, file, line);
     413             :     }
     414             : 
     415             :     /* The next two overloads are for MSVC that shows problems with implicit
     416             :        conversions
     417             :      */
     418             :     inline bool qCompare(char *t1, const char *t2, const char *actual,
     419             :                          const char *expected, const char *file, int line)
     420             :     {
     421             :         return compare_string_helper(t1, t2, actual, expected, file, line);
     422             :     }
     423             :     inline bool qCompare(const char *t1, char *t2, const char *actual,
     424             :                          const char *expected, const char *file, int line)
     425             :     {
     426             :         return compare_string_helper(t1, t2, actual, expected, file, line);
     427             :     }
     428             : 
     429             :     template <class T>
     430             :     inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
     431             :                      const char *expected, const char *file, int line)
     432             :     {
     433             :         return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
     434             :                        qMetaTypeId<T>())), actualStr, expected, file, line);
     435             :     }
     436             : }
     437             : 
     438             : #undef QTEST_COMPARE_DECL
     439             : 
     440             : QT_END_NAMESPACE
     441             : 
     442             : #endif

Generated by: LCOV version 1.13