From 557275301edfc23a6d2f97a44e6b142790d9059c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 10 Jan 2024 10:32:33 +0100 Subject: [PATCH] QTest: replace naked returns with QTEST_{FAIL,SKIP}_ACTION macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... defaulting to "return". This allows customizing these actions, incl. to eventually make them throwing exceptions instead (but that won't work for QVERIFY_THROWS_EXCEPTION, yet). Change-Id: I078a4ce48135bda2cf98fce78318a12d757d7aa5 Reviewed-by: Tor Arne Vestbø Reviewed-by: Friedemann Kleint Reviewed-by: Edward Welbourne --- src/testlib/qtestcase.cpp | 8 ++++---- src/testlib/qtestcase.h | 32 ++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index fd47db3f553..9a5c088a6e1 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2627,11 +2627,11 @@ void QTest::qCaught(const char *expected, const char *what, const char *file, in If the exception inherits std::exception, its what() message is logged and this function returns normally. The caller of this function must then - execute a \c{return} to exit from the test function. + execute a \c{QTEST_FAIL_ACTION} to exit from the test function. Otherwise, a message saying an unknown exception was caught is logged and - this function rethrows the exception, skipping the \c{return} that follows - this function call in the caller. + this function rethrows the exception, skipping the \c{QTEST_FAIL_ACTION} + that follows this function call in the caller. */ void QTest::qCaught(const char *expected, const char *file, int line) { @@ -2644,7 +2644,7 @@ void QTest::qCaught(const char *expected, const char *file, int line) qCaught(expected, nullptr, file, line); throw; } - // caller shall invoke `return` if control reached here + // caller shall invoke `QTEST_FAIL_ACTION` if control reached here } diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index fcd9ad8cfcf..47ffaca95a2 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -23,36 +23,44 @@ QT_BEGIN_NAMESPACE +#ifndef QTEST_FAIL_ACTION +# define QTEST_FAIL_ACTION return +#endif + +#ifndef QTEST_SKIP_ACTION +# define QTEST_SKIP_ACTION return +#endif + class qfloat16; class QRegularExpression; #define QVERIFY(statement) \ do {\ if (!QTest::qVerify(static_cast(statement), #statement, "", __FILE__, __LINE__))\ - return;\ + QTEST_FAIL_ACTION; \ } while (false) #define QFAIL(message) \ do {\ QTest::qFail(static_cast(message), __FILE__, __LINE__);\ - return;\ + QTEST_FAIL_ACTION; \ } while (false) #define QVERIFY2(statement, description) \ do {\ if (statement) {\ if (!QTest::qVerify(true, #statement, static_cast(description), __FILE__, __LINE__))\ - return;\ + QTEST_FAIL_ACTION; \ } else {\ if (!QTest::qVerify(false, #statement, static_cast(description), __FILE__, __LINE__))\ - return;\ + QTEST_FAIL_ACTION; \ }\ } while (false) #define QCOMPARE(actual, expected) \ do {\ if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ - return;\ + QTEST_FAIL_ACTION; \ } while (false) // A wrapper lambda is introduced to extend the lifetime of lhs and rhs in @@ -71,7 +79,7 @@ do { \ #lhs, #rhs, QTest::ComparisonOperation::opId, \ __FILE__, __LINE__); \ }(lhs, rhs)) { \ - return; \ + QTEST_FAIL_ACTION; \ } \ } while (false) @@ -91,7 +99,7 @@ do { \ /* success */ \ } QT_CATCH (...) { \ QTest::qCaught(nullptr, __FILE__, __LINE__); \ - return; \ + QTEST_FAIL_ACTION; \ } \ } while (false) \ /* end */ @@ -112,12 +120,12 @@ inline void useVerifyThrowsException() {} __VA_ARGS__; \ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ " but no exception caught", __FILE__, __LINE__); \ - return; \ + QTEST_FAIL_ACTION; \ } QT_CATCH (const exceptiontype &) { \ /* success */ \ } QT_CATCH (...) {\ QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \ - return; \ + QTEST_FAIL_ACTION; \ }\ } while (false) @@ -240,7 +248,7 @@ do { \ #define QSKIP_INTERNAL(statement) \ do {\ QTest::qSkip(static_cast(statement), __FILE__, __LINE__);\ - return;\ + QTEST_SKIP_ACTION; \ } while (false) #define QSKIP(statement, ...) QSKIP_INTERNAL(statement) @@ -248,7 +256,7 @@ do {\ #define QEXPECT_FAIL(dataIndex, comment, mode)\ do {\ if (!QTest::qExpectFail(dataIndex, static_cast(comment), QTest::mode, __FILE__, __LINE__))\ - return;\ + QTEST_FAIL_ACTION; \ } while (false) #define QFETCH(Type, name)\ @@ -260,7 +268,7 @@ do {\ #define QTEST(actual, testElement)\ do {\ if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\ - return;\ + QTEST_FAIL_ACTION; \ } while (false) #ifdef QT_TESTCASE_BUILDDIR