diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2026-09-22 22:47:33 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2026-09-22 22:47:33 +0200 |
| commit | d05b9343f69a0841043963088e0003368e205bd9 (patch) | |
| tree | 8cde07ff86d27cbc808a0a149b89d0024b4a586c /ttwhy/terminal/scanner.tests.cpp | |
| parent | 22f6534081acfabdab4956d337604a21e7da5b64 (diff) | |
| download | ttwhy-d05b9343f69a0841043963088e0003368e205bd9.tar.xz ttwhy-d05b9343f69a0841043963088e0003368e205bd9.zip | |
Diffstat (limited to 'ttwhy/terminal/scanner.tests.cpp')
| -rw-r--r-- | ttwhy/terminal/scanner.tests.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
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 <catch2/catch_test_macros.hpp> + +#include <string_view> +#include <variant> +#include <vector> + +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<ttwhy::terminal::character_event>(&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<ttwhy::terminal::control_event>(&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<ttwhy::terminal::input_event>{}; + 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)); + } + } + } +} |
