Skip to content

Commit

Permalink
Replace boost testing framework with doctest (#7)
Browse files Browse the repository at this point in the history
* Add doctest 2.4.11

* Switch from boost to doctest
  • Loading branch information
sizeofvoid authored Sep 13, 2024
1 parent fda73b5 commit 5b1a0fa
Show file tree
Hide file tree
Showing 7 changed files with 7,197 additions and 92 deletions.
17 changes: 7 additions & 10 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
project(InotifyUnitTest)

find_package(Threads REQUIRED)
find_package(Boost REQUIRED COMPONENTS system unit_test_framework)

if (NOT DEFINED Boost_INCLUDE_DIRS OR NOT Boost_LIBRARIES)
message(FATAL_ERROR "Missing boost feature")
endif()

add_executable(event_handler_unit_test main.cpp event_handler_test.cpp)
target_link_libraries(
event_handler_unit_test
PUBLIC notify-cpp-shared stdc++fs Threads::Threads ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
PUBLIC notify-cpp-shared stdc++fs Threads::Threads ${CMAKE_THREAD_LIBS_INIT}
)
target_compile_definitions(event_handler_unit_test PRIVATE DOCTEST_CONFIG_DOUBLE_STRINGIFY=1)
target_include_directories(event_handler_unit_test PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/../../include/"
"${Boost_INCLUDE_DIRS}")

add_executable(inotify_unit_test main.cpp inotify_controller_test.cpp)
target_link_libraries(
inotify_unit_test
PUBLIC notify-cpp-shared stdc++fs Threads::Threads ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
PUBLIC notify-cpp-shared stdc++fs Threads::Threads ${CMAKE_THREAD_LIBS_INIT}
)
target_compile_definitions(inotify_unit_test PRIVATE DOCTEST_CONFIG_DOUBLE_STRINGIFY=1)
target_include_directories(inotify_unit_test PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/../../include/"
"${Boost_INCLUDE_DIRS}")
"${CMAKE_CURRENT_SOURCE_DIR}/../../include/")

add_executable(fanotify_unit_test main.cpp fanotify_controller_test.cpp)
target_link_libraries(
fanotify_unit_test
PUBLIC notify-cpp-shared stdc++fs Threads::Threads ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
PUBLIC notify-cpp-shared stdc++fs Threads::Threads ${CMAKE_THREAD_LIBS_INIT}
)
target_compile_definitions(fanotify_unit_test PRIVATE DOCTEST_CONFIG_DOUBLE_STRINGIFY=1)
target_include_directories(fanotify_unit_test PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/../../include/"
"${Boost_INCLUDE_DIRS}")
Expand Down
7,106 changes: 7,106 additions & 0 deletions test/unit/doctest.h

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions test/unit/event_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@
*/
#include <notify-cpp/event.h>

#include <boost/test/unit_test.hpp>
#include "doctest.h"

using namespace notifycpp;

BOOST_AUTO_TEST_CASE(EventOperatorTest)
TEST_CASE("EventOperatorTest")
{
BOOST_CHECK_EQUAL((Event::all & Event::close_write), Event::close_write);
BOOST_CHECK_EQUAL((Event::close & Event::close_write), Event::close_write);
BOOST_CHECK_EQUAL((Event::all & Event::close), Event::close);
BOOST_CHECK_EQUAL((Event::all & Event::access | Event::modify), Event::access | Event::modify);
BOOST_CHECK_EQUAL((Event::all & Event::moved_from), Event::moved_from);
BOOST_CHECK_EQUAL((Event::move & Event::moved_from), Event::moved_from);
BOOST_CHECK(!((Event::move & Event::open) == Event::open));
CHECK_EQ((Event::all & Event::close_write), Event::close_write);
CHECK_EQ((Event::close & Event::close_write), Event::close_write);
CHECK_EQ((Event::all & Event::close), Event::close);
CHECK_EQ((Event::all & Event::access | Event::modify), Event::access | Event::modify);
CHECK_EQ((Event::all & Event::moved_from), Event::moved_from);
CHECK_EQ((Event::move & Event::moved_from), Event::moved_from);
CHECK_FALSE((Event::move & Event::open) == Event::open);
}

BOOST_AUTO_TEST_CASE(EventToStringTest)
TEST_CASE("EventToStringTest")
{
BOOST_CHECK_EQUAL(toString(Event::all), std::string("access,modify,attrib,close_write,close_nowrite,open,moved_from,moved_to,create,delete,delete_self,move_self,close,move,all"));
CHECK_EQ(toString(Event::all), std::string("access,modify,attrib,close_write,close_nowrite,open,moved_from,moved_to,create,delete,delete_self,move_self,close,move,all"));

BOOST_CHECK_EQUAL(toString(Event::access), std::string("access"));
BOOST_CHECK_EQUAL(toString(Event::access | Event::close_nowrite), std::string("access,close_nowrite"));
BOOST_CHECK_EQUAL(toString(Event::close_nowrite| Event::access), std::string("access,close_nowrite"));
CHECK_EQ(toString(Event::access), std::string("access"));
CHECK_EQ(toString(Event::access | Event::close_nowrite), std::string("access,close_nowrite"));
CHECK_EQ(toString(Event::close_nowrite| Event::access), std::string("access,close_nowrite"));
}
64 changes: 33 additions & 31 deletions test/unit/fanotify_controller_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
* SOFTWARE.
*/
#include <notify-cpp/fanotify.h>
#include <notify-cpp/event.h>
#include <notify-cpp/notify_controller.h>

#include "filesystem_event_helper.hpp"

#include <boost/test/unit_test.hpp>
#include "doctest.h"

#include <thread>
#include <chrono>
#include <filesystem>
#include <fstream>
Expand All @@ -35,7 +37,7 @@
using namespace notifycpp;


BOOST_FIXTURE_TEST_CASE(shouldNotifyOnMultipleEvents, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldNotifyOnMultipleEvents")
{
FanotifyController notifier = FanotifyController();

Expand Down Expand Up @@ -63,29 +65,29 @@ BOOST_FIXTURE_TEST_CASE(shouldNotifyOnMultipleEvents, FilesystemEventHelper)

auto futureOpen = promisedOpen_.get_future();
auto futureCloseNoWrite = promisedCloseNoWrite_.get_future();
BOOST_CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready);
BOOST_CHECK(futureOpen.get().getEvent() == Event::open);
BOOST_CHECK(futureCloseNoWrite.wait_for(timeout_) == std::future_status::ready);
BOOST_CHECK(futureCloseNoWrite.get().getEvent() == Event::close_write);
CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready);
CHECK_EQ(futureOpen.get().getEvent(), Event::open);
CHECK(futureCloseNoWrite.wait_for(timeout_) == std::future_status::ready);
CHECK(futureCloseNoWrite.get().getEvent() == Event::close_write);
thread.join();
}

BOOST_AUTO_TEST_CASE(EventOperatorTest)
TEST_CASE("EventOperatorTest")
{
BOOST_CHECK((Event::all & Event::close_write) == Event::close_write);
BOOST_CHECK((Event::all & Event::moved_from) == Event::moved_from);
BOOST_CHECK((Event::move & Event::moved_from) == Event::moved_from);
BOOST_CHECK(!((Event::move & Event::open) == Event::open));
BOOST_CHECK(toString(Event::access) == std::string("access"));
CHECK_EQ((Event::all & Event::close_write), Event::close_write);
CHECK_EQ((Event::all & Event::moved_from), Event::moved_from);
CHECK_EQ((Event::move & Event::moved_from), Event::moved_from);
CHECK(!((Event::move & Event::open) != Event::open));
CHECK_EQ(toString(Event::access), std::string("access"));
}

BOOST_FIXTURE_TEST_CASE(shouldNotAcceptNotExistingPaths, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldNotAcceptNotExistingPaths")
{
BOOST_CHECK_THROW(FanotifyController().watchPathRecursively(std::filesystem::path("/not/existing/path/")), std::invalid_argument);
BOOST_CHECK_THROW(FanotifyController().watchFile(std::filesystem::path("/not/existing/file")), std::invalid_argument);
CHECK_THROWS_AS(FanotifyController().watchPathRecursively(std::filesystem::path("/not/existing/path/")), std::invalid_argument);
CHECK_THROWS_AS(FanotifyController().watchFile(std::filesystem::path("/not/existing/file")), std::invalid_argument);
}

BOOST_FIXTURE_TEST_CASE(shouldNotifyOnOpenEvent, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldNotifyOnOpenEvent")
{
NotifyController notifier = FanotifyController().watchFile({testFileOne_, Event::close}).onEvent(Event::close, [&](Notification notification) {
promisedOpen_.set_value(notification);
Expand All @@ -96,16 +98,16 @@ BOOST_FIXTURE_TEST_CASE(shouldNotifyOnOpenEvent, FilesystemEventHelper)
openFile(testFileOne_);

auto futureOpenEvent = promisedOpen_.get_future();
BOOST_CHECK(futureOpenEvent.wait_for(timeout_) == std::future_status::ready);
CHECK(futureOpenEvent.wait_for(timeout_) == std::future_status::ready);
const auto notify = futureOpenEvent.get();
BOOST_CHECK_EQUAL(notify.getEvent(), Event::close);
CHECK_EQ(notify.getEvent(), Event::close);
auto fullpath = std::filesystem::current_path();
fullpath /= testFileOne_;
BOOST_CHECK_EQUAL(notify.getPath(), fullpath);
CHECK_EQ(notify.getPath(), fullpath);
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldStopRunOnce, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldStopRunOnce")
{
NotifyController notifier = FanotifyController().watchFile(testFileOne_);

Expand All @@ -116,7 +118,7 @@ BOOST_FIXTURE_TEST_CASE(shouldStopRunOnce, FilesystemEventHelper)
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldStopRun, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldStopRun")
{
FanotifyController notifier = FanotifyController();
notifier.watchFile(testFileOne_);
Expand All @@ -128,7 +130,7 @@ BOOST_FIXTURE_TEST_CASE(shouldStopRun, FilesystemEventHelper)
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldIgnoreFile, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldIgnoreFile")
{
NotifyController notifier = FanotifyController().ignore(testFileOne_).watchFile({testFileOne_, Event::close}).onEvent(Event::close, [&](Notification notification) {
promisedOpen_.set_value(notification);
Expand All @@ -139,12 +141,12 @@ BOOST_FIXTURE_TEST_CASE(shouldIgnoreFile, FilesystemEventHelper)
openFile(testFileOne_);

auto futureOpenEvent = promisedOpen_.get_future();
BOOST_CHECK(futureOpenEvent.wait_for(timeout_) == std::future_status::timeout);
CHECK(futureOpenEvent.wait_for(timeout_) == std::future_status::timeout);
notifier.stop();
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldUnwatchPath, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldUnwatchPath")
{
std::promise<Notification> timeoutObserved;
std::chrono::milliseconds timeout(100);
Expand All @@ -155,12 +157,12 @@ BOOST_FIXTURE_TEST_CASE(shouldUnwatchPath, FilesystemEventHelper)
std::thread thread([&notifier]() { notifier.runOnce(); });

openFile(testFileOne_);
BOOST_CHECK(promisedOpen_.get_future().wait_for(timeout_) != std::future_status::ready);
CHECK(promisedOpen_.get_future().wait_for(timeout_) != std::future_status::ready);
notifier.stop();
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldCallUserDefinedUnexpectedExceptionObserver, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldCallUserDefinedUnexpectedExceptionObserver")
{
std::promise<void> observerCalled;

Expand All @@ -173,11 +175,11 @@ BOOST_FIXTURE_TEST_CASE(shouldCallUserDefinedUnexpectedExceptionObserver, Filesy

openFile(testFileOne_);

BOOST_CHECK(observerCalled.get_future().wait_for(timeout_) == std::future_status::ready);
CHECK(observerCalled.get_future().wait_for(timeout_) == std::future_status::ready);
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldWatchPathRecursively, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldWatchPathRecursively")
{
FanotifyController notifier = FanotifyController();
notifier.watchPathRecursively(testDirectory_)
Expand All @@ -195,13 +197,13 @@ BOOST_FIXTURE_TEST_CASE(shouldWatchPathRecursively, FilesystemEventHelper)
openFile(testFileOne_);

auto futureOpen = promisedOpen_.get_future();
BOOST_CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready);
CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready);

notifier.stop();
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldIgnoreFileOnce, FilesystemEventHelper)
TEST_CASE_FIXTURE(FilesystemEventHelper, "shouldIgnoreFileOnce")
{
size_t counter = 0;
FanotifyController notifier = FanotifyController();
Expand All @@ -220,7 +222,7 @@ BOOST_FIXTURE_TEST_CASE(shouldIgnoreFileOnce, FilesystemEventHelper)
openFile(testFileOne_);

auto futureOpen = _promisedCounter.get_future();
BOOST_CHECK(futureOpen.wait_for(std::chrono::seconds(1)) == std::future_status::ready);
CHECK(futureOpen.wait_for(std::chrono::seconds(1)) == std::future_status::ready);
notifier.stop();
thread.join();
}
4 changes: 2 additions & 2 deletions test/unit/filesystem_event_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <notify-cpp/notify_controller.h>

#include <boost/test/unit_test.hpp>
#include "doctest.h"

#include <chrono>
#include <filesystem>
Expand All @@ -36,7 +36,7 @@ void openFile(const std::filesystem::path& file)
{
std::ofstream stream;
stream.open(file.string(), std::ifstream::out);
BOOST_CHECK(stream.is_open());
CHECK(stream.is_open());
stream << "Writing this to a file.\n";
stream.close();
}
Expand Down
Loading

0 comments on commit 5b1a0fa

Please sign in to comment.