1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-26 12:12:08 +00:00

LibTest: Expose new EXPECT_CRASH(..) macro for unit test assertions

Utilize the Crash type we imported into LibTest to support testing
of crash scenarios when testing all other components.
This commit is contained in:
Brian Gianforcaro 2021-05-07 04:38:56 -07:00 committed by Linus Groh
parent 7e28ecc305
commit 7f51754780
3 changed files with 22 additions and 6 deletions

View file

@ -13,16 +13,16 @@
namespace Test { namespace Test {
Crash::Crash(String test_type, Function<Crash::Failure()> crash_function) Crash::Crash(String test_type, Function<Crash::Failure()> crash_function)
: m_type(test_type) : m_type(move(test_type))
, m_crash_function(move(crash_function)) , m_crash_function(move(crash_function))
{ {
} }
void Crash::run(RunType run_type = RunType::UsingChildProcess) bool Crash::run(RunType run_type)
{ {
printf("\x1B[33mTesting\x1B[0m: \"%s\"\n", m_type.characters()); printf("\x1B[33mTesting\x1B[0m: \"%s\"\n", m_type.characters());
auto run_crash_and_print_if_error = [this]() { auto run_crash_and_print_if_error = [this]() -> bool {
auto failure = m_crash_function(); auto failure = m_crash_function();
// If we got here something went wrong // If we got here something went wrong
@ -37,10 +37,11 @@ void Crash::run(RunType run_type = RunType::UsingChildProcess)
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
return false;
}; };
if (run_type == RunType::UsingCurrentProcess) { if (run_type == RunType::UsingCurrentProcess) {
run_crash_and_print_if_error(); return run_crash_and_print_if_error();
} else { } else {
// Run the test in a child process so that we do not crash the crash program :^) // Run the test in a child process so that we do not crash the crash program :^)
@ -55,8 +56,11 @@ void Crash::run(RunType run_type = RunType::UsingChildProcess)
int status; int status;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (WIFSIGNALED(status)) if (WIFSIGNALED(status)) {
printf("\x1B[32mPASS\x1B[0m: Terminated with signal %d\n", WTERMSIG(status)); printf("\x1B[32mPASS\x1B[0m: Terminated with signal %d\n", WTERMSIG(status));
return true;
}
return false;
} }
} }

View file

@ -27,7 +27,7 @@ public:
Crash(String test_type, Function<Crash::Failure()> crash_function); Crash(String test_type, Function<Crash::Failure()> crash_function);
void run(RunType run_type); bool run(RunType run_type = RunType::UsingChildProcess);
private: private:
String m_type; String m_type;

View file

@ -9,6 +9,7 @@
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/CheckedFormatString.h> #include <AK/CheckedFormatString.h>
#include <LibTest/CrashTest.h>
namespace AK { namespace AK {
template<typename... Parameters> template<typename... Parameters>
@ -101,3 +102,14 @@ void current_test_case_did_fail();
::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: {}", __FILE__, __LINE__, message); \ ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: {}", __FILE__, __LINE__, message); \
::Test::current_test_case_did_fail(); \ ::Test::current_test_case_did_fail(); \
} while (false) } while (false)
// To use, specify the lambda to execute in a sub process and verify it exits:
// EXPECT_CRASH("This should fail", []{
// return Test::Crash::Failure::DidNotCrash;
// });
#define EXPECT_CRASH(test_message, test_func) \
do { \
Test::Crash crash(test_message, test_func); \
if (!crash.run()) \
::Test::current_test_case_did_fail(); \
} while (false)