FTXUI/src/ftxui/component/input_test.cpp

232 lines
6.7 KiB
C++
Raw Normal View History

#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
2021-07-10 19:20:43 +08:00
#include <memory> // for __shared_ptr_access, shared_ptr, allocator
#include <string> // for string
2021-03-29 00:14:43 +08:00
2021-08-07 02:32:33 +08:00
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Input
2021-08-07 02:32:33 +08:00
#include "ftxui/component/component_base.hpp" // for ComponentBase, Component
2021-07-10 19:20:43 +08:00
#include "ftxui/component/component_options.hpp" // for InputOption
2021-05-10 02:32:27 +08:00
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
2021-08-07 02:32:33 +08:00
#include "ftxui/dom/elements.hpp" // for Fit
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/screen.hpp" // for Screen, Pixel
2021-07-10 19:20:43 +08:00
#include "ftxui/util/ref.hpp" // for Ref
2021-07-10 18:29:39 +08:00
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
2021-03-29 00:14:43 +08:00
using namespace ftxui;
TEST(InputTest, Init) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
Component input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 0);
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Type) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
Component input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
input->OnEvent(Event::Character("a"));
EXPECT_EQ(content, "a");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 1u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('b'));
EXPECT_EQ(content, "ab");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
auto document = input->Render();
auto screen = Screen::Create(Dimension::Fit(document));
Render(screen, document);
EXPECT_EQ(screen.PixelAt(0, 0).character, "a");
EXPECT_EQ(screen.PixelAt(1, 0).character, "b");
}
TEST(InputTest, TypePassword) {
std::string content;
std::string placeholder;
auto option = InputOption();
option.cursor_position = 0;
option.password = true;
Component input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a'));
EXPECT_EQ(content, "a");
EXPECT_EQ(option.cursor_position(), 1u);
input->OnEvent(Event::Character('b'));
EXPECT_EQ(content, "ab");
EXPECT_EQ(option.cursor_position(), 2u);
auto document = input->Render();
auto screen = Screen::Create(Dimension::Fit(document));
Render(screen, document);
EXPECT_EQ(screen.PixelAt(0, 0).character, "");
EXPECT_EQ(screen.PixelAt(1, 0).character, "");
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Arrow) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
auto input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
2021-03-29 00:14:43 +08:00
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 3u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 1u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 0u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 0u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 1u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 3u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 3u);
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Insert) {
std::string content;
std::string placeholder;
2021-05-10 02:32:27 +08:00
Component input = Input(&content, &placeholder);
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
EXPECT_EQ(content, "abc");
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, "a-bc");
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, "a--bc");
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, "-a--bc");
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Home) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
auto input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
EXPECT_EQ(content, "abc");
2021-03-29 00:14:43 +08:00
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 3u);
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Home);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 0u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, "-abc");
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, End) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
auto input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
2021-03-29 00:14:43 +08:00
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 1u);
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::End);
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 3u);
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Delete) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
auto input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
input->OnEvent(Event::ArrowLeft);
2021-03-29 00:14:43 +08:00
EXPECT_EQ(content, "abc");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Delete);
EXPECT_EQ(content, "ab");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Delete);
EXPECT_EQ(content, "ab");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Backspace) {
std::string content;
std::string placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
option.cursor_position = 0;
2021-07-10 18:29:39 +08:00
auto input = Input(&content, &placeholder, &option);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
input->OnEvent(Event::ArrowLeft);
2021-03-29 00:14:43 +08:00
EXPECT_EQ(content, "abc");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Backspace);
EXPECT_EQ(content, "ac");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 1u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Backspace);
EXPECT_EQ(content, "c");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 0u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::Backspace);
EXPECT_EQ(content, "c");
2021-07-10 18:29:39 +08:00
EXPECT_EQ(option.cursor_position(), 0u);
2021-03-29 00:14:43 +08:00
}
2021-05-10 02:32:27 +08:00
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.