Skip to content

Commit

Permalink
fix: Update cmake Test Settings (#4)
Browse files Browse the repository at this point in the history
* updated directory settings and target flags
  • Loading branch information
johnpatek authored Dec 13, 2024
1 parent 349a179 commit 65d5e7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
15 changes: 8 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
add_executable(
unit
unit.cpp
${CMAKE_SOURCE_DIR}/src/sigfn.c
${CMAKE_SOURCE_DIR}/src/sigfn.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/../src/sigfn.c
${CMAKE_CURRENT_SOURCE_DIR}/../src/sigfn.cpp)

target_include_directories(unit PRIVATE ${SIGFN_INCLUDE})

if(SIGFN_COVER)
if(WIN32)
message("skipping code coverage for windows")
else()
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -g -O0")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -g -O0")
target_compile_options(unit PRIVATE -fprofile-arcs -ftest-coverage -g -O0)
target_link_libraries(unit PRIVATE gcov "--coverage")
add_custom_target(
cover
DEPENDS unit)
add_custom_command(
TARGET cover
COMMAND gcovr -r ${CMAKE_SOURCE_DIR} -e ${CMAKE_SOURCE_DIR}/tests)
endif()
COMMAND gcovr -r ${CMAKE_CURRENT_SOURCE_DIR}/.. -e ${CMAKE_CURRENT_SOURCE_DIR})
endif()
endif()

add_test(NAME test_c_handle COMMAND unit HANDLE_C)
add_test(NAME test_c_ignore COMMAND unit IGNORE_C)
add_test(NAME test_c_reset COMMAND unit RESET_C)
add_test(NAME test_cpp_handle COMMAND unit HANDLE_CPP)
add_test(NAME test_cpp_ignore COMMAND unit IGNORE_CPP)
add_test(NAME test_cpp_reset COMMAND unit RESET_CPP)
add_test(NAME test_cpp_reset COMMAND unit RESET_CPP)
add_test(NAME test_cpp_exception COMMAND unit EXCEPTION_CPP)
28 changes: 28 additions & 0 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static void test_c_reset();
static void test_cpp_handle();
static void test_cpp_ignore();
static void test_cpp_reset();
static void test_cpp_exception();

static void echo_signum(int signum, void *userdata);
// GCOV_EXCL_START
Expand All @@ -68,6 +69,7 @@ int main(int argc, const char **argv)
{"HANDLE_CPP", test_cpp_handle},
{"IGNORE_CPP", test_cpp_ignore},
{"RESET_CPP", test_cpp_reset},
{"EXCEPTION_CPP", test_cpp_exception},
};
int result;
result = PASS;
Expand Down Expand Up @@ -238,6 +240,32 @@ void test_cpp_reset()
try_catch_assert(SIGINT, false);
}

void test_cpp_exception()
{
const sigfn::handler_function valid_handler = [](int signum)
{
};
const std::function<void(int, sigfn::handler_function)> try_catch_assert(
[](
int signum,
const sigfn::handler_function& handler)
{
bool has_error;

has_error = false;
try
{
sigfn::handle(signum, handler);
}
catch (const std::exception &e)
{
has_error = (e.what() != nullptr);
}
TEST_ASSERT(has_error);
});
try_catch_assert(-1, valid_handler);
}

void echo_signum(int signum, void *userdata)
{
*(int *)userdata = signum;
Expand Down

0 comments on commit 65d5e7e

Please sign in to comment.