aboutsummaryrefslogtreecommitdiff
path: root/ttwhy/routers/echo.tests.cpp
blob: da2db40a8b80a72128c40aa9fce96b047a998573 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <asio.hpp>

#include <catch2/catch_test_macros.hpp>

#include <string>
#include <string_view>
#include <utility>

import ttwhy.routers;
import ttwhy.scanners;

using namespace std::string_view_literals;

namespace
{
  struct mock_stream
  {
    using executor_type = asio::any_io_executor;

    asio::any_io_executor executor;
    std::string written{};

    explicit mock_stream(asio::any_io_executor exec)
        : executor{std::move(exec)}
    {}

    auto get_executor() const noexcept -> executor_type
    {
      return executor;
    }

    template<typename ConstBufferSequence, typename Token>
    auto async_write_some(ConstBufferSequence const & buffers, Token && token)
    {
      return asio::async_initiate<Token, void(asio::error_code, std::size_t)>(
          [this](auto handler, ConstBufferSequence bufs) {
            auto const total = asio::buffer_size(bufs);
            auto const offset = written.size();
            written.resize(offset + total);
            asio::buffer_copy(asio::buffer(written.data() + offset, total), bufs);
            asio::post(executor, [handler = std::move(handler), total]() mutable {
              std::move(handler)(asio::error_code{}, total);
            });
          },
          token, buffers);
    }
  };

  struct incomplete_router_no_process
  {
  };

  struct incomplete_router_wrong_return
  {
    auto process(ttwhy::scanners::input_event) -> void;
  };

  struct incomplete_router_wrong_event
  {
    auto process(int) -> asio::awaitable<void>;
  };
}  // namespace

TEST_CASE("The router concept validates router candidate interfaces", "[router][concept]")
{
  STATIC_REQUIRE(ttwhy::router<ttwhy::routers::echo<mock_stream>, ttwhy::scanners::input_event>);
  STATIC_REQUIRE_FALSE(ttwhy::router<incomplete_router_no_process, ttwhy::scanners::input_event>);
  STATIC_REQUIRE_FALSE(ttwhy::router<incomplete_router_wrong_return, ttwhy::scanners::input_event>);
  STATIC_REQUIRE_FALSE(ttwhy::router<incomplete_router_wrong_event, ttwhy::scanners::input_event>);
}

SCENARIO("The echo router handles terminal navigation within the valid printed area", "[router][echo]")
{
  GIVEN("An initialized echo router with a mock stream")
  {
    auto context = asio::io_context{};
    auto stream = mock_stream{context.get_executor()};
    auto router = ttwhy::routers::echo{stream};

    auto run_coro = [&](asio::awaitable<void> coro) {
      asio::co_spawn(context, std::move(coro), asio::detached);
      context.restart();
      context.run();
    };

    WHEN("Typing characters")
    {
      run_coro(router.process(ttwhy::scanners::character_event{'A'}));
      run_coro(router.process(ttwhy::scanners::character_event{'B'}));

      THEN("Characters are echoed and positions advance")
      {
        CHECK(stream.written == "AB");
        CHECK(router.cursor_position() == 2);
        CHECK(router.max_position() == 2);
      }

      AND_WHEN("Navigating left")
      {
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::left}));

        THEN("The cursor moves left")
        {
          CHECK(stream.written == "\x1b[D");
          CHECK(router.cursor_position() == 1);
          CHECK(router.max_position() == 2);
        }

        AND_WHEN("Navigating left again to position 0")
        {
          stream.written.clear();
          run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::left}));

          THEN("The cursor reaches 0")
          {
            CHECK(stream.written == "\x1b[D");
            CHECK(router.cursor_position() == 0);
            CHECK(router.max_position() == 2);
          }

          AND_WHEN("Attempting to navigate left beyond 0")
          {
            stream.written.clear();
            run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::left}));

            THEN("Navigation is prevented and nothing is emitted")
            {
              CHECK(stream.written.empty());
              CHECK(router.cursor_position() == 0);
              CHECK(router.max_position() == 2);
            }
          }
        }
      }

      AND_WHEN("Attempting to navigate right beyond what was printed")
      {
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::right}));

        THEN("Navigation is prevented and nothing is emitted")
        {
          CHECK(stream.written.empty());
          CHECK(router.cursor_position() == 2);
          CHECK(router.max_position() == 2);
        }
      }

      AND_WHEN("Navigating left and then overwriting with new character")
      {
        run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::left}));
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::character_event{'X'}));

        THEN("The new character is written and max position is preserved")
        {
          CHECK(stream.written == "X");
          CHECK(router.cursor_position() == 2);
          CHECK(router.max_position() == 2);
        }
      }

      AND_WHEN("Using Home and End keys")
      {
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::home}));

        THEN("Home navigates to 0")
        {
          CHECK(stream.written == "\x1b[2D");
          CHECK(router.cursor_position() == 0);
          CHECK(router.max_position() == 2);
        }

        AND_WHEN("Pressing Home when already at 0")
        {
          stream.written.clear();
          run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::home}));

          THEN("Nothing is emitted")
          {
            CHECK(stream.written.empty());
            CHECK(router.cursor_position() == 0);
          }
        }

        AND_WHEN("Pressing End from 0")
        {
          stream.written.clear();
          run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::end}));

          THEN("End moves to max position")
          {
            CHECK(stream.written == "\x1b[2C");
            CHECK(router.cursor_position() == 2);
            CHECK(router.max_position() == 2);
          }
        }
      }

      AND_WHEN("Pressing Enter")
      {
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::control_event{ttwhy::scanners::control_key::enter}));

        THEN("Newline is emitted and positions reset")
        {
          CHECK(stream.written == "\r\n");
          CHECK(router.cursor_position() == 0);
          CHECK(router.max_position() == 0);
        }
      }

      AND_WHEN("Pressing Backspace at position 0 after reset")
      {
        run_coro(router.process(ttwhy::scanners::control_event{ttwhy::scanners::control_key::enter}));
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::control_event{ttwhy::scanners::control_key::backspace}));

        THEN("Backspace does nothing at position 0")
        {
          CHECK(stream.written.empty());
          CHECK(router.cursor_position() == 0);
          CHECK(router.max_position() == 0);
        }
      }

      AND_WHEN("Using Delete key")
      {
        run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::left}));
        stream.written.clear();
        run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::delete_key}));

        THEN("Delete character sequence is emitted and max position shrinks")
        {
          CHECK(stream.written == "\x1b[P");
          CHECK(router.cursor_position() == 1);
          CHECK(router.max_position() == 1);
        }

        AND_WHEN("Pressing Delete when cursor is at max position")
        {
          stream.written.clear();
          run_coro(router.process(ttwhy::scanners::navigation_event{ttwhy::scanners::navigation_key::delete_key}));

          THEN("Nothing is emitted")
          {
            CHECK(stream.written.empty());
            CHECK(router.cursor_position() == 1);
            CHECK(router.max_position() == 1);
          }
        }
      }
    }
  }
}