From d05b9343f69a0841043963088e0003368e205bd9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 22 Sep 2026 22:47:33 +0200 Subject: lib: restructure into vertical slices --- ttwhy/terminal/scanner.tests.cpp | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 ttwhy/terminal/scanner.tests.cpp (limited to 'ttwhy/terminal/scanner.tests.cpp') diff --git a/ttwhy/terminal/scanner.tests.cpp b/ttwhy/terminal/scanner.tests.cpp new file mode 100644 index 0000000..64acb2d --- /dev/null +++ b/ttwhy/terminal/scanner.tests.cpp @@ -0,0 +1,101 @@ +#include + +#include +#include +#include + +import ttwhy.core; +import ttwhy.terminal; + +using namespace std::string_view_literals; + +[[nodiscard]] constexpr auto static is_character(ttwhy::terminal::input_event & event, char expected) -> bool +{ + auto const * data = std::get_if(&event); + return data != nullptr && data->value == expected; +} + +[[nodiscard]] constexpr auto static is_control(ttwhy::terminal::input_event & event, + ttwhy::terminal::control_key expected) -> bool +{ + auto const * data = std::get_if(&event); + return data != nullptr && data->key == 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::terminal::ansi{sink}; + + WHEN("Processing a standard printable character") + { + scanner.process("A"sv); + + THEN("It yields a single character event") + { + REQUIRE(queue.size() == 1); + CHECK(is_character(queue.at(0), 'A')); + } + } + + WHEN("Processing a BS byte (\\x08)") + { + scanner.process("\x08"sv); + + THEN("It yields a backspace control event") + { + REQUIRE(queue.size() == 1); + CHECK(is_control(queue.at(0), ttwhy::terminal::control_key::backspace)); + } + } + + WHEN("Processing a DEL byte (\\x7f as BS)") + { + scanner.process("\x7f"sv); + + THEN("It yields a backspace control event") + { + REQUIRE(queue.size() == 1); + CHECK(is_control(queue.at(0), ttwhy::terminal::control_key::backspace)); + } + } + + WHEN("Processing a TAB byte") + { + scanner.process("\x09"sv); + + THEN("It yields a tab control event") + { + REQUIRE(queue.size() == 1); + CHECK(is_control(queue.at(0), ttwhy::terminal::control_key::tab)); + } + } + + WHEN("Processing an LF byte") + { + scanner.process("\x0a"sv); + + THEN("It yields an enter control event") + { + REQUIRE(queue.size() == 1); + CHECK(is_control(queue.at(0), ttwhy::terminal::control_key::enter)); + } + } + + WHEN("Processing an CR byte") + { + scanner.process("\x0d"sv); + + THEN("It yields an enter control event") + { + REQUIRE(queue.size() == 1); + CHECK(is_control(queue.at(0), ttwhy::terminal::control_key::enter)); + } + } + } +} -- cgit v1.2.3