diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index fd9d08b267..ae83432eb8 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -247,7 +247,7 @@ static void stackTrace() \note This macro can only be used in a test function that is invoked by the test framework. - \sa QTRY_VERIFY(), QVERIFY(), QCOMPARE(), QTRY_COMPARE() + \sa QTRY_VERIFY(), QTRY_VERIFY2_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE() */ @@ -261,7 +261,47 @@ static void stackTrace() \note This macro can only be used in a test function that is invoked by the test framework. - \sa QTRY_VERIFY_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE() + \sa QTRY_VERIFY_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), QTRY_COMPARE() +*/ + +/*! \macro QTRY_VERIFY2_WITH_TIMEOUT(condition, message, timeout) + \since 5.6 + + \relates QTest + + The QTRY_VERIFY2_WITH_TIMEOUT macro is similar to QTRY_VERIFY_WITH_TIMEOUT() + except that it outputs a verbose \a message when \a condition is still false + after the specified timeout. The \a message is a plain C string. + + Example: + \code + QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10000); + \endcode + + \note This macro can only be used in a test function that is invoked + by the test framework. + + \sa QTRY_VERIFY(), QTRY_VERIFY_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE() +*/ + +/*! \macro QTRY_VERIFY2(condition, message) + \since 5.6 + + \relates QTest + + Checks the \a condition by invoking QTRY_VERIFY2_WITH_TIMEOUT() with a timeout + of five seconds. If \a condition is then still false, \a message is output. + The \a message is a plain C string. + + Example: + \code + QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData()); + \endcode + + \note This macro can only be used in a test function that is invoked + by the test framework. + + \sa QTRY_VERIFY2_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), QTRY_COMPARE() */ /*! \macro QTRY_COMPARE_WITH_TIMEOUT(actual, expected, timeout) diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 9f3c461cf0..7d2fd2e701 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -158,6 +158,15 @@ do { \ #define QTRY_VERIFY(__expr) QTRY_VERIFY_WITH_TIMEOUT((__expr), 5000) +// Will try to wait for the expression to become true while allowing event processing +#define QTRY_VERIFY2_WITH_TIMEOUT(__expr, __messageExpression, __timeout) \ +do { \ + QTRY_IMPL((__expr), __timeout);\ + QVERIFY2(__expr, __messageExpression); \ +} while (0) + +#define QTRY_VERIFY2(__expr, __messageExpression) QTRY_VERIFY2_WITH_TIMEOUT((__expr), (__messageExpression), 5000) + // Will try to wait for the comparison to become successful while allowing event processing #define QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, __timeout) \ do { \ diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp index 1ee78960ab..6446fec510 100644 --- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp @@ -140,6 +140,10 @@ private slots: void compareQImages(); void compareQImages_data(); #endif + void verify(); + void verify2(); + void tryVerify(); + void tryVerify2(); }; enum MyUnregisteredEnum { MyUnregisteredEnumValue1, MyUnregisteredEnumValue2 }; @@ -387,7 +391,36 @@ void tst_Cmptest::compareQImages() QCOMPARE(opA, opB); } -#endif +#endif // QT_GUI_LIB + +static int opaqueFunc() +{ + return 42; +} + +void tst_Cmptest::verify() +{ + QVERIFY(opaqueFunc() > 2); + QVERIFY(opaqueFunc() < 2); +} + +void tst_Cmptest::verify2() +{ + QVERIFY2(opaqueFunc() > 2, QByteArray::number(opaqueFunc()).constData()); + QVERIFY2(opaqueFunc() < 2, QByteArray::number(opaqueFunc()).constData()); +} + +void tst_Cmptest::tryVerify() +{ + QTRY_VERIFY(opaqueFunc() > 2); + QTRY_VERIFY_WITH_TIMEOUT(opaqueFunc() < 2, 1); +} + +void tst_Cmptest::tryVerify2() +{ + QTRY_VERIFY2(opaqueFunc() > 2, QByteArray::number(opaqueFunc()).constData()); + QTRY_VERIFY2_WITH_TIMEOUT(opaqueFunc() < 2, QByteArray::number(opaqueFunc()).constData(), 1); +} QTEST_MAIN(tst_Cmptest) #include "tst_cmptest.moc" diff --git a/tests/auto/testlib/selftests/expected_cmptest.lightxml b/tests/auto/testlib/selftests/expected_cmptest.lightxml index c8d2cff964..36929cec6b 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.lightxml +++ b/tests/auto/testlib/selftests/expected_cmptest.lightxml @@ -8,13 +8,13 @@ - + - + @@ -30,7 +30,7 @@ - + - + - + - + ) @@ -66,31 +66,31 @@ - + - + - + - + - + - + @@ -107,7 +107,7 @@ - + @@ -118,13 +118,13 @@ - + - + - + - + @@ -149,13 +149,13 @@ - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/expected_cmptest.txt b/tests/auto/testlib/selftests/expected_cmptest.txt index 4b12e08750..70c54704f9 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.txt +++ b/tests/auto/testlib/selftests/expected_cmptest.txt @@ -2,96 +2,104 @@ Config: Using QtTest library PASS : tst_Cmptest::initTestCase() FAIL! : tst_Cmptest::compare_unregistered_enums() Compared values are not the same - Loc: [tst_cmptest.cpp(150)] + Loc: [tst_cmptest.cpp(154)] FAIL! : tst_Cmptest::compare_registered_enums() Compared values are not the same Actual (Qt::ArrowCursor): ArrowCursor Expected (Qt::BusyCursor) : BusyCursor - Loc: [tst_cmptest.cpp(156)] + Loc: [tst_cmptest.cpp(160)] PASS : tst_Cmptest::compare_boolfuncs() PASS : tst_Cmptest::compare_pointerfuncs() FAIL! : tst_Cmptest::compare_tostring(int, string) Compared values are not the same Actual (actual) : QVariant(int,123) Expected (expected): QVariant(QString,hi) - Loc: [tst_cmptest.cpp(227)] + Loc: [tst_cmptest.cpp(231)] PASS : tst_Cmptest::compare_tostring(both invalid) FAIL! : tst_Cmptest::compare_tostring(null hash, invalid) Compared values are not the same Actual (actual) : QVariant(QVariantHash) Expected (expected): QVariant() - Loc: [tst_cmptest.cpp(227)] + Loc: [tst_cmptest.cpp(231)] FAIL! : tst_Cmptest::compare_tostring(string, null user type) Compared values are not the same Actual (actual) : QVariant(QString,A simple string) Expected (expected): QVariant(PhonyClass) - Loc: [tst_cmptest.cpp(227)] + Loc: [tst_cmptest.cpp(231)] FAIL! : tst_Cmptest::compare_tostring(both non-null user type) Compared values are not the same Actual (actual) : QVariant(PhonyClass,) Expected (expected): QVariant(PhonyClass,) - Loc: [tst_cmptest.cpp(227)] + Loc: [tst_cmptest.cpp(231)] PASS : tst_Cmptest::compareQStringLists(empty lists) PASS : tst_Cmptest::compareQStringLists(equal lists) FAIL! : tst_Cmptest::compareQStringLists(last item different) Compared lists differ at index 2. Actual (opA): "string3" Expected (opB): "DIFFERS" - Loc: [tst_cmptest.cpp(321)] + Loc: [tst_cmptest.cpp(325)] FAIL! : tst_Cmptest::compareQStringLists(second-last item different) Compared lists differ at index 2. Actual (opA): "string3" Expected (opB): "DIFFERS" - Loc: [tst_cmptest.cpp(321)] + Loc: [tst_cmptest.cpp(325)] FAIL! : tst_Cmptest::compareQStringLists(prefix) Compared lists have different sizes. Actual (opA) size: 2 Expected (opB) size: 1 - Loc: [tst_cmptest.cpp(321)] + Loc: [tst_cmptest.cpp(325)] FAIL! : tst_Cmptest::compareQStringLists(short list second) Compared lists have different sizes. Actual (opA) size: 12 Expected (opB) size: 1 - Loc: [tst_cmptest.cpp(321)] + Loc: [tst_cmptest.cpp(325)] FAIL! : tst_Cmptest::compareQStringLists(short list first) Compared lists have different sizes. Actual (opA) size: 1 Expected (opB) size: 12 - Loc: [tst_cmptest.cpp(321)] + Loc: [tst_cmptest.cpp(325)] FAIL! : tst_Cmptest::compareQListInt() Compared lists differ at index 2. Actual (int1): 3 Expected (int2): 4 - Loc: [tst_cmptest.cpp(328)] + Loc: [tst_cmptest.cpp(332)] FAIL! : tst_Cmptest::compareQListDouble() Compared lists differ at index 0. Actual (double1): 1.5 Expected (double2): 1 - Loc: [tst_cmptest.cpp(335)] + Loc: [tst_cmptest.cpp(339)] PASS : tst_Cmptest::compareQPixmaps(both null) FAIL! : tst_Cmptest::compareQPixmaps(one null) Compared QPixmaps differ. Actual (opA).isNull(): 1 Expected (opB).isNull(): 0 - Loc: [tst_cmptest.cpp(361)] + Loc: [tst_cmptest.cpp(365)] FAIL! : tst_Cmptest::compareQPixmaps(other null) Compared QPixmaps differ. Actual (opA).isNull(): 0 Expected (opB).isNull(): 1 - Loc: [tst_cmptest.cpp(361)] + Loc: [tst_cmptest.cpp(365)] PASS : tst_Cmptest::compareQPixmaps(equal) FAIL! : tst_Cmptest::compareQPixmaps(different size) Compared QPixmaps differ in size. Actual (opA): 11x20 Expected (opB): 20x20 - Loc: [tst_cmptest.cpp(361)] + Loc: [tst_cmptest.cpp(365)] FAIL! : tst_Cmptest::compareQPixmaps(different pixels) Compared values are not the same - Loc: [tst_cmptest.cpp(361)] + Loc: [tst_cmptest.cpp(365)] PASS : tst_Cmptest::compareQImages(both null) FAIL! : tst_Cmptest::compareQImages(one null) Compared QImages differ. Actual (opA).isNull(): 1 Expected (opB).isNull(): 0 - Loc: [tst_cmptest.cpp(388)] + Loc: [tst_cmptest.cpp(392)] FAIL! : tst_Cmptest::compareQImages(other null) Compared QImages differ. Actual (opA).isNull(): 0 Expected (opB).isNull(): 1 - Loc: [tst_cmptest.cpp(388)] + Loc: [tst_cmptest.cpp(392)] PASS : tst_Cmptest::compareQImages(equal) FAIL! : tst_Cmptest::compareQImages(different size) Compared QImages differ in size. Actual (opA): 11x20 Expected (opB): 20x20 - Loc: [tst_cmptest.cpp(388)] + Loc: [tst_cmptest.cpp(392)] FAIL! : tst_Cmptest::compareQImages(different format) Compared QImages differ in format. Actual (opA): 6 Expected (opB): 3 - Loc: [tst_cmptest.cpp(388)] + Loc: [tst_cmptest.cpp(392)] FAIL! : tst_Cmptest::compareQImages(different pixels) Compared values are not the same - Loc: [tst_cmptest.cpp(388)] + Loc: [tst_cmptest.cpp(392)] +FAIL! : tst_Cmptest::verify() 'opaqueFunc() < 2' returned FALSE. () + Loc: [tst_cmptest.cpp(404)] +FAIL! : tst_Cmptest::verify2() 'opaqueFunc() < 2' returned FALSE. (42) + Loc: [tst_cmptest.cpp(410)] +FAIL! : tst_Cmptest::tryVerify() 'opaqueFunc() < 2' returned FALSE. () + Loc: [tst_cmptest.cpp(416)] +FAIL! : tst_Cmptest::tryVerify2() 'opaqueFunc() < 2' returned FALSE. (42) + Loc: [tst_cmptest.cpp(422)] PASS : tst_Cmptest::cleanupTestCase() -Totals: 11 passed, 22 failed, 0 skipped, 0 blacklisted +Totals: 11 passed, 26 failed, 0 skipped, 0 blacklisted ********* Finished testing of tst_Cmptest ********* diff --git a/tests/auto/testlib/selftests/expected_cmptest.xml b/tests/auto/testlib/selftests/expected_cmptest.xml index 1a5c772da5..9437e8e4b7 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xml +++ b/tests/auto/testlib/selftests/expected_cmptest.xml @@ -10,13 +10,13 @@ - + - + @@ -32,7 +32,7 @@ - + - + - + - + ) @@ -68,31 +68,31 @@ - + - + - + - + - + - + @@ -109,7 +109,7 @@ - + @@ -120,13 +120,13 @@ - + - + - + - + @@ -151,13 +151,13 @@ - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/expected_cmptest.xunitxml b/tests/auto/testlib/selftests/expected_cmptest.xunitxml index bb1ef22577..fa970d4172 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xunitxml +++ b/tests/auto/testlib/selftests/expected_cmptest.xunitxml @@ -1,5 +1,5 @@ - + @@ -84,6 +84,18 @@ Expected (opB): 3" result="fail"/> + + + + + + + + + + + +