From c5f8cd91186adbe9c68835675a019449cc275c6d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 20 Jun 2026 09:29:57 +0200 Subject: lib: add basic test setup --- CMakeLists.txt | 27 +++++++++++++++++++++++++++ ttwhy/scanners/ansi.tests.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 ttwhy/scanners/ansi.tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bb68832..390afbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,11 @@ FetchContent_Declare("asio" GIT_TAG "asio-1-38-0" ) +FetchContent_Declare("catch2" + GIT_REPOSITORY "https://github.com/catchorg/Catch2.git" + GIT_TAG "v3.15.1" +) + FetchContent_Declare("sml" GIT_REPOSITORY "https://github.com/boost-ext/sml" GIT_TAG "v1.2.0" @@ -21,6 +26,7 @@ FetchContent_Declare("sml" FetchContent_MakeAvailable( "asio" + "catch2" "sml" ) @@ -94,3 +100,24 @@ target_link_libraries("ttwhy" PRIVATE "ttwhy::lib" ) +### Tests + +include("CTest") + +if(BUILD_TESTING) + include("Catch") + + add_executable("ttwhy-tests") + add_executable("ttwhy::tests" ALIAS "ttwhy-tests") + + target_sources("ttwhy-tests" PRIVATE + "ttwhy/scanners/ansi.tests.cpp" + ) + + target_link_libraries("ttwhy-tests" PRIVATE + "ttwhy::lib" + "Catch2::Catch2WithMain" + ) + + catch_discover_tests("ttwhy-tests") +endif() diff --git a/ttwhy/scanners/ansi.tests.cpp b/ttwhy/scanners/ansi.tests.cpp new file mode 100644 index 0000000..6fdc8af --- /dev/null +++ b/ttwhy/scanners/ansi.tests.cpp @@ -0,0 +1,38 @@ +#include + +#include // IWYU pragma: keep +#include +#include + +import ttwhy.scanners; + +using namespace std::string_view_literals; + +[[nodiscard]] constexpr auto static is_character(ttwhy::scanners::input_event & event, char expected) -> bool +{ + auto const * data = std::get_if(&event); + return data != nullptr && data->value == expected; +} + +SCENARIO("The ANSI scanner processes printable ASCII and standard C0 control characters", "[scanner][ansi]") +{ + GIVEN("An initialized scanner and event sink") + { + auto queue = std::vector{}; + auto sink = [&queue](auto const & event) { + queue.push_back(event); + }; + auto scanner = ttwhy::scanners::ansi{sink}; + + WHEN("Processing a standard printable character") + { + scanner.process("A"); + + THEN("It yields a single character event") + { + REQUIRE(queue.size() == 1); + CHECK(is_character(queue.at(0), 'A')); + } + } + } +} -- cgit v1.2.3