aboutsummaryrefslogtreecommitdiff
path: root/ttwhy/terminal/scanner.tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ttwhy/terminal/scanner.tests.cpp')
-rw-r--r--ttwhy/terminal/scanner.tests.cpp101
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));
+ }
+ }
+ }
+}