From 2502e5a2cbc55adb43fc149aa40abc860d00d1b3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 10 Jan 2024 11:36:36 +0100 Subject: [PATCH] QTestLib: prepare QVERIFY_THROWS_EXCEPTION for FAILED_ACTION = throw MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can't have a QTEST_FAILED_ACTION in a try-block if we intend to allow QTEST_FAILED_ACTION to be a throw statement. We could, of course, add a whitelisting catch-block for the eventual TestFailedException, but as of now, the intent it to give users full control over the definition of QTEST_FAILED_ACTION, and they may have their own idea of what to throw. So add a bool variable to record whether no exception was thrown and use it to drag QTEST_FAILED_ACTION out of the try block. It's ok to be in catch blocks, no action needed there. Change-Id: I0b004e43b1db82cd8b5b12f900ed985e58a56807 Reviewed-by: Tor Arne Vestbø Reviewed-by: Edward Welbourne Reviewed-by: Friedemann Kleint --- src/testlib/qtestcase.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 47ffaca95a2..62eb5799865 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -116,17 +116,20 @@ inline void useVerifyThrowsException() {} # define QVERIFY_THROWS_EXCEPTION(exceptiontype, ...) \ do {\ + bool qverify_throws_exception_did_not_throw = false; \ QT_TRY {\ __VA_ARGS__; \ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ " but no exception caught", __FILE__, __LINE__); \ - QTEST_FAIL_ACTION; \ + qverify_throws_exception_did_not_throw = true; \ } QT_CATCH (const exceptiontype &) { \ /* success */ \ } QT_CATCH (...) {\ QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \ QTEST_FAIL_ACTION; \ }\ + if (qverify_throws_exception_did_not_throw) \ + QTEST_FAIL_ACTION; \ } while (false) #else // QT_NO_EXCEPTIONS