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
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
}

View File

@ -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<bool>(statement), #statement, "", __FILE__, __LINE__))\
return;\
QTEST_FAIL_ACTION; \
} while (false)
#define QFAIL(message) \
do {\
QTest::qFail(static_cast<const char *>(message), __FILE__, __LINE__);\
return;\
QTEST_FAIL_ACTION; \
} while (false)
#define QVERIFY2(statement, description) \
do {\
if (statement) {\
if (!QTest::qVerify(true, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
return;\
QTEST_FAIL_ACTION; \
} else {\
if (!QTest::qVerify(false, #statement, static_cast<const char *>(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<const char *>(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<const char *>(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