aboutsummaryrefslogtreecommitdiff
path: root/ttwhy/scanners/terminal_scanner.tests.cpp
blob: f4ea6ad84d49116d7d6abe3898c0bea2cde77032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <catch2/catch_test_macros.hpp>

#include <string_view>
#include <variant>
#include <vector>

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<ttwhy::scanners::character_event>(&event);
  return data != nullptr && data->value == expected;
}

[[nodiscard]] constexpr auto static is_control(ttwhy::scanners::input_event & event,
                                               ttwhy::scanners::control_key expected) -> bool
{
  auto const * data = std::get_if<ttwhy::scanners::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::scanners::input_event>{};
    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"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::scanners::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::scanners::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::scanners::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::scanners::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::scanners::control_key::enter));
      }
    }
  }
}