From 7a44c11378bf4920728f117a586dfa9c589992aa Mon Sep 17 00:00:00 2001 From: Michel Hermier Date: Wed, 15 Dec 2021 00:47:43 +0100 Subject: [PATCH] LibTest: Add `EXPECT_NO_CRASH` --- Tests/CMakeLists.txt | 1 + Tests/LibTest/CMakeLists.txt | 7 +++++++ Tests/LibTest/TestNoCrash.cpp | 14 ++++++++++++++ Userland/Libraries/LibTest/CrashTest.cpp | 2 ++ Userland/Libraries/LibTest/Macros.h | 7 +++++++ 5 files changed, 31 insertions(+) create mode 100644 Tests/LibTest/CMakeLists.txt create mode 100644 Tests/LibTest/TestNoCrash.cpp diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 0a2f7c9fd5d..920f64749fc 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -14,6 +14,7 @@ add_subdirectory(LibMarkdown) add_subdirectory(LibPthread) add_subdirectory(LibRegex) add_subdirectory(LibSQL) +add_subdirectory(LibTest) add_subdirectory(LibThreading) add_subdirectory(LibUnicode) add_subdirectory(LibWasm) diff --git a/Tests/LibTest/CMakeLists.txt b/Tests/LibTest/CMakeLists.txt new file mode 100644 index 00000000000..1efe46d0c45 --- /dev/null +++ b/Tests/LibTest/CMakeLists.txt @@ -0,0 +1,7 @@ +set(TEST_SOURCES + TestNoCrash.cpp +) + +foreach(source IN LISTS TEST_SOURCES) + serenity_test("${source}" LibSQL LIBS LibSQL LibIPC) +endforeach() diff --git a/Tests/LibTest/TestNoCrash.cpp b/Tests/LibTest/TestNoCrash.cpp new file mode 100644 index 00000000000..d29fad6db74 --- /dev/null +++ b/Tests/LibTest/TestNoCrash.cpp @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2021, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +TEST_CASE(raise) +{ + EXPECT_NO_CRASH("This should never crash", [] { + return Test::Crash::Failure::DidNotCrash; + }); +} diff --git a/Userland/Libraries/LibTest/CrashTest.cpp b/Userland/Libraries/LibTest/CrashTest.cpp index ac8433d9bec..419867342ab 100644 --- a/Userland/Libraries/LibTest/CrashTest.cpp +++ b/Userland/Libraries/LibTest/CrashTest.cpp @@ -64,6 +64,8 @@ bool Crash::do_report(Report report) bool pass = false; if (m_crash_signal == ANY_SIGNAL) { pass = report.has(); + } else if (m_crash_signal == 0) { + pass = report.has() && report.get() == Failure::DidNotCrash; } else if (m_crash_signal > 0) { pass = report.has() && report.get() == m_crash_signal; } else { diff --git a/Userland/Libraries/LibTest/Macros.h b/Userland/Libraries/LibTest/Macros.h index d0dca516aae..6f32e0ae132 100644 --- a/Userland/Libraries/LibTest/Macros.h +++ b/Userland/Libraries/LibTest/Macros.h @@ -134,3 +134,10 @@ void current_test_case_did_fail(); if (!crash.run()) \ ::Test::current_test_case_did_fail(); \ } while (false) + +#define EXPECT_NO_CRASH(test_message, test_func) \ + do { \ + Test::Crash crash(test_message, test_func, 0); \ + if (!crash.run()) \ + ::Test::current_test_case_did_fail(); \ + } while (false)