LibTest: Add a TRY_OR_FAIL macro

This macro has the usual `TRY` semantics, but instead of returning the
error, it will let the test fail with the formatted error as the fail
message.
This commit is contained in:
Tim Schumacher 2023-04-05 20:23:26 +02:00 committed by Jelle Raaijmakers
parent bfffe88de5
commit 5f806ec53a
Notes: sideshowbarker 2024-07-17 04:49:48 +09:00

View File

@ -141,3 +141,17 @@ void current_test_case_did_fail();
if (!crash.run()) \
::Test::current_test_case_did_fail(); \
} while (false)
#define TRY_OR_FAIL(expression) \
({ \
/* Ignore -Wshadow to allow nesting the macro. */ \
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
auto&& _temporary_result = (expression)); \
static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
"Do not return a reference from a fallible expression"); \
if (_temporary_result.is_error()) [[unlikely]] { \
FAIL(_temporary_result.release_error()); \
return; \
} \
_temporary_result.release_value(); \
})