QTest: replace naked returns with QTEST_{FAIL,SKIP}_ACTION macros

... 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ø <tor.arne.vestbo@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2024-01-10 10:32:33 +01:00
parent 711def1290
commit 557275301e
2 changed files with 24 additions and 16 deletions

View File

@ -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 If the exception inherits std::exception, its what() message is logged and
this function returns normally. The caller of this function must then 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 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 rethrows the exception, skipping the \c{QTEST_FAIL_ACTION}
this function call in the caller. that follows this function call in the caller.
*/ */
void QTest::qCaught(const char *expected, const char *file, int line) 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); qCaught(expected, nullptr, file, line);
throw; throw;
} }
// caller shall invoke `return` if control reached here // caller shall invoke `QTEST_FAIL_ACTION` if control reached here
} }

View File

@ -23,36 +23,44 @@
QT_BEGIN_NAMESPACE 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 qfloat16;
class QRegularExpression; class QRegularExpression;
#define QVERIFY(statement) \ #define QVERIFY(statement) \
do {\ do {\
if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))\ if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))\
return;\ QTEST_FAIL_ACTION; \
} while (false) } while (false)
#define QFAIL(message) \ #define QFAIL(message) \
do {\ do {\
QTest::qFail(static_cast<const char *>(message), __FILE__, __LINE__);\ QTest::qFail(static_cast<const char *>(message), __FILE__, __LINE__);\
return;\ QTEST_FAIL_ACTION; \
} while (false) } while (false)
#define QVERIFY2(statement, description) \ #define QVERIFY2(statement, description) \
do {\ do {\
if (statement) {\ if (statement) {\
if (!QTest::qVerify(true, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\ if (!QTest::qVerify(true, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
return;\ QTEST_FAIL_ACTION; \
} else {\ } else {\
if (!QTest::qVerify(false, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\ if (!QTest::qVerify(false, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
return;\ QTEST_FAIL_ACTION; \
}\ }\
} while (false) } while (false)
#define QCOMPARE(actual, expected) \ #define QCOMPARE(actual, expected) \
do {\ do {\
if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
return;\ QTEST_FAIL_ACTION; \
} while (false) } while (false)
// A wrapper lambda is introduced to extend the lifetime of lhs and rhs in // A wrapper lambda is introduced to extend the lifetime of lhs and rhs in
@ -71,7 +79,7 @@ do { \
#lhs, #rhs, QTest::ComparisonOperation::opId, \ #lhs, #rhs, QTest::ComparisonOperation::opId, \
__FILE__, __LINE__); \ __FILE__, __LINE__); \
}(lhs, rhs)) { \ }(lhs, rhs)) { \
return; \ QTEST_FAIL_ACTION; \
} \ } \
} while (false) } while (false)
@ -91,7 +99,7 @@ do { \
/* success */ \ /* success */ \
} QT_CATCH (...) { \ } QT_CATCH (...) { \
QTest::qCaught(nullptr, __FILE__, __LINE__); \ QTest::qCaught(nullptr, __FILE__, __LINE__); \
return; \ QTEST_FAIL_ACTION; \
} \ } \
} while (false) \ } while (false) \
/* end */ /* end */
@ -112,12 +120,12 @@ inline void useVerifyThrowsException() {}
__VA_ARGS__; \ __VA_ARGS__; \
QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
" but no exception caught", __FILE__, __LINE__); \ " but no exception caught", __FILE__, __LINE__); \
return; \ QTEST_FAIL_ACTION; \
} QT_CATCH (const exceptiontype &) { \ } QT_CATCH (const exceptiontype &) { \
/* success */ \ /* success */ \
} QT_CATCH (...) {\ } QT_CATCH (...) {\
QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \ QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \
return; \ QTEST_FAIL_ACTION; \
}\ }\
} while (false) } while (false)
@ -240,7 +248,7 @@ do { \
#define QSKIP_INTERNAL(statement) \ #define QSKIP_INTERNAL(statement) \
do {\ do {\
QTest::qSkip(static_cast<const char *>(statement), __FILE__, __LINE__);\ QTest::qSkip(static_cast<const char *>(statement), __FILE__, __LINE__);\
return;\ QTEST_SKIP_ACTION; \
} while (false) } while (false)
#define QSKIP(statement, ...) QSKIP_INTERNAL(statement) #define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
@ -248,7 +256,7 @@ do {\
#define QEXPECT_FAIL(dataIndex, comment, mode)\ #define QEXPECT_FAIL(dataIndex, comment, mode)\
do {\ do {\
if (!QTest::qExpectFail(dataIndex, static_cast<const char *>(comment), QTest::mode, __FILE__, __LINE__))\ if (!QTest::qExpectFail(dataIndex, static_cast<const char *>(comment), QTest::mode, __FILE__, __LINE__))\
return;\ QTEST_FAIL_ACTION; \
} while (false) } while (false)
#define QFETCH(Type, name)\ #define QFETCH(Type, name)\
@ -260,7 +268,7 @@ do {\
#define QTEST(actual, testElement)\ #define QTEST(actual, testElement)\
do {\ do {\
if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\ if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
return;\ QTEST_FAIL_ACTION; \
} while (false) } while (false)
#ifdef QT_TESTCASE_BUILDDIR #ifdef QT_TESTCASE_BUILDDIR