Skip to content

Commit

Permalink
Check that emscripten death tests actually die. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrahams authored Aug 15, 2024
1 parent 1548fa4 commit 0597967
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
26 changes: 25 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ function(adobe_contract_checking_add_test base_name)
adobe-contract-checking
GTest::gtest_main)
gtest_discover_tests("${base_name}")

if(EMSCRIPTEN)
handle_emscripten_death_tests("${base_name}")
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
add_custom_command(
TARGET "${base_name}"
Expand All @@ -48,5 +50,27 @@ function(adobe_contract_checking_add_test base_name)
endif()
endfunction()

# Turns all tests discovered in <test_executable> by
# gtest_discover_tests that are named with the "DeathTest" convention
# into CMake WILL_FAIL tests.
#
# It is expected that under emscripten, these tests are actually
# compiled as non-death tests; i.e. they simply execute the code that
# is expected to die. There is no support for checking that the exit
# code or output meets expectations.
function(handle_emscripten_death_tests test_executable)
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${test_executable}_death.cmake")
file(WRITE "${ctest_include_file}"
"set(death_tests \"\${${test_executable}_TESTS}}\")\n"
"list(FILTER death_tests INCLUDE REGEX \"DeathTest\")\n"
"set_tests_properties(\${death_tests} PROPERTIES WILL_FAIL YES)\n"
)
set_property(DIRECTORY
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
)
endfunction()

adobe_contract_checking_add_test(precondition_tests)
adobe_contract_checking_add_test(throwing_tests)

# adobe_contract_checking_add_test(wrapper_header_tests)
30 changes: 28 additions & 2 deletions test/precondition_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,32 @@

ADOBE_DEFAULT_CONTRACT_VIOLATION_HANDLER()

#if defined(GTEST_OS_WINDOWS) || defined(__EMSCRIPTEN__)
#if defined(__EMSCRIPTEN__)

// GoogleTest doesn't support death tests under emscripten, so instead
// we handle death by setting the test's WILL_FAIL property in CMake.
// Therefore the test simply executes the code that aborts with no
// wrapper. There's currently no facility for checking test output.

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define EXPECT_ABORT(code, expected_output_regex) code

#elif defined(GTEST_OS_WINDOWS)
// GoogleTest doesn't support checking for the abort signal on
// Windows, so we use an auxilliary file, win32_abort_detection.cpp,
// to ensure that an unusual string is printed, which we can check for
// with the EXPECT_DEATH macro.

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define EXPECT_ABORT(code, expected_output_regex) \
EXPECT_DEATH_IF_SUPPORTED(code, expected_output_regex ".*\n*##ABORTED##");
EXPECT_DEATH(code, expected_output_regex ".*\n*##ABORTED##");

#else

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define EXPECT_ABORT(code, expected_output_regex) \
EXPECT_EXIT(code, testing::KilledBySignal(SIGABRT), expected_output_regex);

#endif

TEST(PreconditionDeathTest, PreconditionFailureAborts)
Expand All @@ -34,3 +52,11 @@ TEST(PreconditionDeathTest, PreconditionFailureWithMessageOutput)
EXPECT_ABORT(ADOBE_PRECONDITION(false, "expected message"),
"precondition_tests.cpp:999.: Precondition violated \\(false\\)\\. expected message");
}

// ****** This should be the last test in the file. *********
//
// For unknown reasons if the last test is a death test, under
// emscripten, the test fails even if the executable aborts.
#if defined(__EMSCRIPTEN__)
TEST(Precondition, EmscriptenDummy) {}
#endif

0 comments on commit 0597967

Please sign in to comment.