diff options
author | Michel Hermier <michel.hermier@gmail.com> | 2021-12-17 18:12:47 +0100 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-12-19 14:22:06 -0800 |
commit | 4c6e826c055e23493a8b3d72f5f9e5af7a5432ad (patch) | |
tree | 35ab5b7cdfcfa8a85d66f7d658b6cdd163d285b4 /Userland/Libraries | |
parent | b1b94692e69c6c4f22f0fbace1b9a80d5d3bd304 (diff) | |
download | serenity-4c6e826c055e23493a8b3d72f5f9e5af7a5432ad.zip |
LibTest: Add `EXPECT_CRASH_WITH_SIGNAL`
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibTest/CrashTest.cpp | 32 | ||||
-rw-r--r-- | Userland/Libraries/LibTest/CrashTest.h | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibTest/Macros.h | 7 |
3 files changed, 37 insertions, 7 deletions
diff --git a/Userland/Libraries/LibTest/CrashTest.cpp b/Userland/Libraries/LibTest/CrashTest.cpp index c002bec148..ac8433d9be 100644 --- a/Userland/Libraries/LibTest/CrashTest.cpp +++ b/Userland/Libraries/LibTest/CrashTest.cpp @@ -6,6 +6,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/Assertions.h> #include <AK/Platform.h> #include <LibTest/CrashTest.h> #include <sys/wait.h> @@ -17,9 +18,10 @@ namespace Test { -Crash::Crash(String test_type, Function<Crash::Failure()> crash_function) +Crash::Crash(String test_type, Function<Crash::Failure()> crash_function, int crash_signal) : m_type(move(test_type)) , m_crash_function(move(crash_function)) + , m_crash_signal(crash_signal) { } @@ -49,7 +51,9 @@ bool Crash::run(RunType run_type) return do_report(Failure(WEXITSTATUS(status))); } if (WIFSIGNALED(status)) { - return do_report(WTERMSIG(status)); + int signal = WTERMSIG(status); + VERIFY(signal > 0); + return do_report(signal); } VERIFY_NOT_REACHED(); } @@ -57,7 +61,14 @@ bool Crash::run(RunType run_type) bool Crash::do_report(Report report) { - const bool pass = report.has<int>(); + bool pass = false; + if (m_crash_signal == ANY_SIGNAL) { + pass = report.has<int>(); + } else if (m_crash_signal > 0) { + pass = report.has<int>() && report.get<int>() == m_crash_signal; + } else { + VERIFY_NOT_REACHED(); + } if (pass) out("\x1B[32mPASS\x1B[0m: "); @@ -68,19 +79,28 @@ bool Crash::do_report(Report report) [&](const Failure& failure) { switch (failure) { case Failure::DidNotCrash: - outln("Did not crash!"); + out("Did not crash"); break; case Failure::UnexpectedError: - outln("Unexpected error!"); + out("Unexpected error"); break; default: VERIFY_NOT_REACHED(); } }, [&](const int& signal) { - outln("Terminated with signal {}", signal); + out("Terminated with signal {}", signal); }); + if (!pass) { + if (m_crash_signal == ANY_SIGNAL) { + out(" while expecting any signal"); + } else if (m_crash_signal > 0) { + out(" while expecting signal {}", m_crash_signal); + } + } + outln(); + return pass; } diff --git a/Userland/Libraries/LibTest/CrashTest.h b/Userland/Libraries/LibTest/CrashTest.h index 994b4060e4..eda8cc03b8 100644 --- a/Userland/Libraries/LibTest/CrashTest.h +++ b/Userland/Libraries/LibTest/CrashTest.h @@ -26,7 +26,9 @@ public: UnexpectedError, }; - Crash(String test_type, Function<Crash::Failure()> crash_function); + static constexpr int ANY_SIGNAL = -1; + + Crash(String test_type, Function<Crash::Failure()> crash_function, int crash_signal = ANY_SIGNAL); bool run(RunType run_type = RunType::UsingChildProcess); @@ -36,6 +38,7 @@ private: String m_type; Function<Crash::Failure()> m_crash_function; + int m_crash_signal; }; } diff --git a/Userland/Libraries/LibTest/Macros.h b/Userland/Libraries/LibTest/Macros.h index d883da655f..d0dca516aa 100644 --- a/Userland/Libraries/LibTest/Macros.h +++ b/Userland/Libraries/LibTest/Macros.h @@ -127,3 +127,10 @@ void current_test_case_did_fail(); if (!crash.run()) \ ::Test::current_test_case_did_fail(); \ } while (false) + +#define EXPECT_CRASH_WITH_SIGNAL(test_message, signal, test_func) \ + do { \ + Test::Crash crash(test_message, test_func, (signal)); \ + if (!crash.run()) \ + ::Test::current_test_case_did_fail(); \ + } while (false) |