http://macieira.org/~thiago/qt-5.3/QtTest.diff
diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index ac1d6cc..7c9a7b2 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -66,14 +66,14 @@ QT_BEGIN_NAMESPACE namespace QTest { -template<> inline char *toString(const QLatin1String &str) +template<> inline char *toString(const QString &str) { - return qstrdup(qPrintable(QString(str))); + return QTest::toPrettyUnicode(reinterpret_cast<const ushort *>(str.constData()), str.length()); } -template<> inline char *toString(const QString &str) +template<> inline char *toString(const QLatin1String &str) { - return qstrdup(qPrintable(str)); + return toString(QString(str)); } template<> inline char *toString(const QByteArray &ba) @@ -195,15 +195,15 @@ inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const int expectedSize = t2.count(); if (actualSize != expectedSize) { qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n" - " Actual (%s) size: '%d'\n" - " Expected (%s) size: '%d'", actual, actualSize, expected, expectedSize); + " Actual (%s) size: %d\n" + " Expected (%s) size: %d", actual, actualSize, expected, expectedSize); isOk = false; } for (int i = 0; isOk && i < actualSize; ++i) { if (!(t1.at(i) == t2.at(i))) { qsnprintf(msg, sizeof(msg), "Compared lists differ at index %d.\n" - " Actual (%s): '%s'\n" - " Expected (%s): '%s'", i, actual, toString(t1.at(i)), + " Actual (%s): %s\n" + " Expected (%s): %s", i, actual, toString(t1.at(i)), expected, toString(t2.at(i))); isOk = false; } diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index ba727b5..d9c8a43 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -51,8 +51,14 @@ #include <string.h> +#ifndef QT_NO_EXCEPTIONS +# include <exception> +#endif // QT_NO_EXCEPTIONS + + QT_BEGIN_NAMESPACE +class QRegularExpression; #define QVERIFY(statement) \ do {\ @@ -83,38 +89,88 @@ do {\ return;\ } while (0) -// Will try to wait for the expression to become true while allowing event processing -#define QTRY_VERIFY_WITH_TIMEOUT(__expr, __timeout) \ + +#ifndef QT_NO_EXCEPTIONS + +# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \ do {\ - const int __step = 50; \ - const int __timeoutValue = __timeout; \ + QT_TRY {\ + QT_TRY {\ + expression;\ + QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ + " but no exception caught", __FILE__, __LINE__);\ + return;\ + } QT_CATCH (const exceptiontype &) {\ + }\ + } QT_CATCH (const std::exception &e) {\ + QByteArray msg = QByteArray() + "Expected exception of type " #exceptiontype \ + " to be thrown but std::exception caught with message: " + e.what(); \ + QTest::qFail(msg.constData(), __FILE__, __LINE__);\ + return;\ + } QT_CATCH (...) {\ + QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ + " but unknown exception caught", __FILE__, __LINE__);\ + return;\ + }\ + } while (0) + +#else // QT_NO_EXCEPTIONS + +/* + * The expression passed to the macro should throw an exception and we can't + * catch it because Qt has been compiled without exception support. We can't + * skip the expression because it may have side effects and must be executed. + * So, users must use Qt with exception support enabled if they use exceptions + * in their code. + */ +# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \ + Q_STATIC_ASSERT_X(false, "Support of exceptions is disabled") + +#endif // !QT_NO_EXCEPTIONS + + +#define QTRY_LOOP_IMPL(__expr, __timeoutValue, __step) \ if (!(__expr)) { \ QTest::qWait(0); \ } \ - for (int __i = 0; __i < __timeoutValue && !(__expr); __i+=__step) { \ + int __i = 0; \ + for (; __i < __timeoutValue && !(__expr); __i += __step) { \ QTest::qWait(__step); \ + } + +#define QTRY_TIMEOUT_DEBUG_IMPL(__expr, __timeoutValue, __step)\ + if (!(__expr)) { \ + QTRY_LOOP_IMPL((__expr), (2 * __timeoutValue), __step);\ + if (__expr) { \ + 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."); \ + msg = msg.arg(QString::fromUtf8(#__expr)).arg(__timeoutValue).arg(__timeoutValue + __i); \ + QFAIL(qPrintable(msg)); \ } \ + } + +#define QTRY_IMPL(__expr, __timeout)\ + const int __step = 50; \ + const int __timeoutValue = __timeout; \ + QTRY_LOOP_IMPL((__expr), __timeoutValue, __step); \ + QTRY_TIMEOUT_DEBUG_IMPL((__expr), __timeoutValue, __step)\ + +// Will try to wait for the expression to become true while allowing event processing +#define QTRY_VERIFY_WITH_TIMEOUT(__expr, __timeout) \ +do { \ + QTRY_IMPL((__expr), __timeout);\ QVERIFY(__expr); \ } while (0) -#define QTRY_VERIFY(__expr) QTRY_VERIFY_WITH_TIMEOUT(__expr, 5000) +#define QTRY_VERIFY(__expr) QTRY_VERIFY_WITH_TIMEOUT((__expr), 5000) // Will try to wait for the comparison to become successful while allowing event processing - #define QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, __timeout) \ do { \ - const int __step = 50; \ - const int __timeoutValue = __timeout; \ - if ((__expr) != (__expected)) { \ - QTest::qWait(0); \ - } \ - for (int __i = 0; __i < __timeoutValue && ((__expr) != (__expected)); __i+=__step) { \ - QTest::qWait(__step); \ - } \ - QCOMPARE(__expr, __expected); \ + QTRY_IMPL(((__expr) == (__expected)), __timeout);\ + QCOMPARE((__expr), __expected); \ } while (0) -#define QTRY_COMPARE(__expr, __expected) QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, 5000) +#define QTRY_COMPARE(__expr, __expected) QTRY_COMPARE_WITH_TIMEOUT((__expr), __expected, 5000) #define QSKIP_INTERNAL(statement) \ do {\ @@ -177,6 +233,7 @@ namespace QTest Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length); + Q_TESTLIB_EXPORT char *toPrettyUnicode(const ushort *unicode, int length); Q_TESTLIB_EXPORT char *toString(const char *); Q_TESTLIB_EXPORT char *toString(const void *); @@ -191,6 +248,7 @@ namespace QTest const char *file, int line); Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = 0, int line = 0); Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message); + Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern); Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = 0, int line = 0, const char* builddir = 0); Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = 0, int line = 0, const char* builddir = 0); diff --git a/src/testlib/qtestkeyboard.h b/src/testlib/qtestkeyboard.h index 8788067..d693d66 100644 --- a/src/testlib/qtestkeyboard.h +++ b/src/testlib/qtestkeyboard.h @@ -151,17 +151,17 @@ namespace QTest Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { sendKeyEvent(action, window, key, keyToAscii(key), modifier, delay); } - inline static void keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) + Q_DECL_UNUSED inline static void keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { keyEvent(Click, window, key, modifier, delay); } - inline static void keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) + Q_DECL_UNUSED inline static void keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { keyEvent(Click, window, key, modifier, delay); } - inline static void keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) + Q_DECL_UNUSED inline static void keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { keyEvent(Release, window, key, modifier, delay); } - inline static void keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) + Q_DECL_UNUSED inline static void keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { keyEvent(Release, window, key, modifier, delay); } - inline static void keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) + Q_DECL_UNUSED inline static void keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { keyEvent(Press, window, key, modifier, delay); } - inline static void keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) + Q_DECL_UNUSED inline static void keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) { keyEvent(Press, window, key, modifier, delay); } #ifdef QT_WIDGETS_LIB diff --git a/src/testlib/qtestsystem.h b/src/testlib/qtestsystem.h index 2a719e9..f859ff16 100644 --- a/src/testlib/qtestsystem.h +++ b/src/testlib/qtestsystem.h @@ -56,7 +56,7 @@ QT_BEGIN_NAMESPACE namespace QTest { - inline static void qWait(int ms) + Q_DECL_UNUSED inline static void qWait(int ms) { Q_ASSERT(QCoreApplication::instance());
_______________________________________________ Development mailing list Development@qt-project.org http://lists.qt-project.org/mailman/listinfo/development