From 2d06fab5afeecacd7a2ccd7663435d775cddd2d1 Mon Sep 17 00:00:00 2001 From: Paul Tsouchlos Date: Mon, 16 Sep 2024 20:37:29 -0600 Subject: [PATCH] feat: migrate unit tests to doctest --- CMakeLists.txt | 25 +++++---------- cmake/CPM.cmake | 2 +- eventbus/CMakeLists.txt | 21 ++++--------- eventbus/test/event_bus_tests.cpp | 51 ++++++++++++++++--------------- eventbus/test/main.cpp | 2 ++ 5 files changed, 42 insertions(+), 59 deletions(-) create mode 100644 eventbus/test/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f5b17da..86d52b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,35 +6,24 @@ set(CXX_STANDARD 17) set(CXX_STANDARD_REQUIRED ON) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) - +include(CPM) option(EVENTBUS_BUILD_TESTS "Build unit tests." ON) +# set up warnings interface project to re-use include(CompilerWarnings) add_library(project_warnings INTERFACE) set_project_warnings(project_warnings) +# set up options interface project to re-use add_library(project_options INTERFACE) +# this project requires C++17 target_compile_features(project_options INTERFACE cxx_std_17) if(EVENTBUS_BUILD_TESTS) - set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake") - set(CPM_VERSION 0.27.3) - - if(NOT EXISTS ${CPM_DOWNLOAD_LOCATION}) - message(STATUS "Downloading CPM.cmake") - file(DOWNLOAD https://raw.githubusercontent.com/TheLartians/CPM/v${CPM_VERSION}/cmake/CPM.cmake ${CPM_DOWNLOAD_LOCATION}) - endif(NOT EXISTS ${CPM_DOWNLOAD_LOCATION}) - - include(${CPM_DOWNLOAD_LOCATION}) - CPMAddPackage( - NAME googletest - GITHUB_REPOSITORY google/googletest - GIT_TAG release-1.8.1 - VERSION 1.8.1 - OPTIONS - "INSTALL_GTEST OFF" - "gtest_force_shared_crt ON" + NAME doctest + GITHUB_REPOSITORY onqtam/doctest + VERSION 2.4.11 ) # this needs to be in the top level directory diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index f5af4d1..7ad5407 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -1,4 +1,4 @@ -set(CPM_DOWNLOAD_VERSION 0.36.0) +set(CPM_DOWNLOAD_VERSION 0.40.2) if(CPM_SOURCE_CACHE) # Expand relative path. This is important if the provided path contains a tilde (~) diff --git a/eventbus/CMakeLists.txt b/eventbus/CMakeLists.txt index ec8c2eb..cf55380 100644 --- a/eventbus/CMakeLists.txt +++ b/eventbus/CMakeLists.txt @@ -11,6 +11,7 @@ set(project_headers # Required for multithreading testing find_package(Threads REQUIRED) +# create library and aliases add_library(${PROJECT_NAME} INTERFACE) add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) add_library(dp::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) @@ -19,27 +20,17 @@ target_include_directories(${PROJECT_NAME} INTERFACE include) target_link_libraries(${PROJECT_NAME} INTERFACE project_options Threads::Threads) -if(MSVC_IDE) - add_custom_target(${PROJECT_NAME}.headers SOURCES ${project_headers}) -endif() - if(EVENTBUS_BUILD_TESTS) - set(project_test_sources - test/event_bus_tests.cpp - ) + file(GLOB_RECURSE project_test_sources CONFIGURE_DEPENDS test/*.cpp) set(project_test_name ${PROJECT_NAME}.tests) add_executable(${project_test_name} ${project_test_sources}) target_link_libraries(${project_test_name} PUBLIC - gtest - gtest_main - gmock + doctest::doctest ${PROJECT_NAME} ) - include(GoogleTest) - gtest_add_tests( - TARGET ${project_test_name} - SOURCES ${project_test_sources} - ) + # Note: doctest and similar testing frameworks can automatically configure CMake tests. For other + include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake) + doctest_discover_tests(${project_test_name}) endif() diff --git a/eventbus/test/event_bus_tests.cpp b/eventbus/test/event_bus_tests.cpp index 4710e4f..f00f327 100644 --- a/eventbus/test/event_bus_tests.cpp +++ b/eventbus/test/event_bus_tests.cpp @@ -1,7 +1,8 @@ -#include +#include #include #include +#include #include struct test_event_type { @@ -28,7 +29,7 @@ void free_function_callback(const test_event_type& type_event) { std::cout << "Free function callback : " << type_event << "\n"; } -TEST(EventBus, LambdaRegistrationAndDeregistration) { +TEST_CASE("lambda registration and de-registration") { dp::event_bus evt_bus; event_handler_counter counter; auto registration = @@ -39,41 +40,41 @@ TEST(EventBus, LambdaRegistrationAndDeregistration) { evt_bus.register_handler([]() { std::cout << "Lambda 1\n"; }); const auto lambda_two_reg = evt_bus.register_handler([&test_event](const test_event_type& evt) { - EXPECT_EQ(evt.id, test_event.id); - EXPECT_EQ(evt.event_message, test_event.event_message); - EXPECT_EQ(evt.data_value, test_event.data_value); + CHECK_EQ(evt.id, test_event.id); + CHECK_EQ(evt.event_message, test_event.event_message); + CHECK_EQ(evt.data_value, test_event.data_value); }); const auto lambda_three_reg = evt_bus.register_handler( [](test_event_type) { std::cout << "Lambda 3 take by copy.\n"; }); // should be 4 because we register a handler in the test fixture SetUp - ASSERT_EQ(evt_bus.handler_count(), 4); + REQUIRE_EQ(evt_bus.handler_count(), 4); evt_bus.fire_event(test_event); - EXPECT_EQ(counter.get_count(), 1); + CHECK_EQ(counter.get_count(), 1); evt_bus.fire_event(test_event); - EXPECT_EQ(counter.get_count(), 2); + CHECK_EQ(counter.get_count(), 2); evt_bus.remove_handler(lambda_one_reg); evt_bus.fire_event(test_event); - EXPECT_EQ(counter.get_count(), 3); - EXPECT_EQ(evt_bus.handler_count(), 3); + CHECK_EQ(counter.get_count(), 3); + CHECK_EQ(evt_bus.handler_count(), 3); evt_bus.remove_handler(lambda_two_reg); evt_bus.fire_event(test_event); - EXPECT_EQ(counter.get_count(), 4); - EXPECT_EQ(evt_bus.handler_count(), 2); + CHECK_EQ(counter.get_count(), 4); + CHECK_EQ(evt_bus.handler_count(), 2); evt_bus.remove_handler(lambda_three_reg); evt_bus.fire_event(test_event); - EXPECT_EQ(counter.get_count(), 5); - EXPECT_EQ(evt_bus.handler_count(), 1); + CHECK_EQ(counter.get_count(), 5); + CHECK_EQ(evt_bus.handler_count(), 1); } -TEST(EventBus, DeregisterWhileDispatching) { +TEST_CASE("deregister while dispatching") { dp::event_bus evt_bus; event_handler_counter counter; auto registration = @@ -106,18 +107,18 @@ TEST(EventBus, DeregisterWhileDispatching) { for (auto i = 0; i < 40; ++i) { evt_bus.fire_event(test_event_type{3, "test event", 3.4}); // add 1 because of the test fixture. - EXPECT_EQ(evt_bus.handler_count(), listeners.size() + 1); + CHECK_EQ(evt_bus.handler_count(), listeners.size() + 1); } // remove all the registrations for (auto& reg : registrations) { - EXPECT_TRUE(evt_bus.remove_handler(reg)); + CHECK(evt_bus.remove_handler(reg)); } - EXPECT_EQ(evt_bus.handler_count(), 1); + CHECK_EQ(evt_bus.handler_count(), 1); } -TEST(EventBus, MultiThreaded) { +TEST_CASE("multi-threaded event dispatch") { class simple_listener { int index_; @@ -158,12 +159,12 @@ TEST(EventBus, MultiThreaded) { thread_two.join(); // include the event counter - EXPECT_EQ(evt_bus.handler_count(), 3); + CHECK_EQ(evt_bus.handler_count(), 3); - EXPECT_EQ(event_counter.get_count(), 10); + CHECK_EQ(event_counter.get_count(), 10); } -TEST(EventBus, AutoDeregisterInDtor) { +TEST_CASE("auto de-register in destructor") { dp::event_bus evt_bus; event_handler_counter counter; { @@ -171,9 +172,9 @@ TEST(EventBus, AutoDeregisterInDtor) { &counter, &event_handler_counter::on_test_event); } - EXPECT_EQ(evt_bus.handler_count(), 0); + CHECK_EQ(evt_bus.handler_count(), 0); evt_bus.fire_event(test_event_type{}); evt_bus.fire_event(test_event_type{}); evt_bus.fire_event(test_event_type{}); - EXPECT_EQ(counter.get_count(), 0); -} \ No newline at end of file + CHECK_EQ(counter.get_count(), 0); +} diff --git a/eventbus/test/main.cpp b/eventbus/test/main.cpp new file mode 100644 index 0000000..0a3f254 --- /dev/null +++ b/eventbus/test/main.cpp @@ -0,0 +1,2 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include