Skip to content

Commit

Permalink
feat: migrate unit tests to doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperPaul123 committed Sep 17, 2024
1 parent c4646be commit 2d06fab
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 59 deletions.
25 changes: 7 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -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 (~)
Expand Down
21 changes: 6 additions & 15 deletions eventbus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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()
51 changes: 26 additions & 25 deletions eventbus/test/event_bus_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <gtest/gtest.h>
#include <doctest/doctest.h>

#include <atomic>
#include <eventbus/event_bus.hpp>
#include <iostream>
#include <thread>

struct test_event_type {
Expand All @@ -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 =
Expand All @@ -39,41 +40,41 @@ TEST(EventBus, LambdaRegistrationAndDeregistration) {
evt_bus.register_handler<test_event_type>([]() { std::cout << "Lambda 1\n"; });
const auto lambda_two_reg =
evt_bus.register_handler<test_event_type>([&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>(
[](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 =
Expand Down Expand Up @@ -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_;

Expand Down Expand Up @@ -158,22 +159,22 @@ 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;
{
auto registration = evt_bus.register_handler<test_event_type>(
&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);
}
CHECK_EQ(counter.get_count(), 0);
}
2 changes: 2 additions & 0 deletions eventbus/test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

0 comments on commit 2d06fab

Please sign in to comment.