FTXUI/src/ftxui/component/input_test.cpp

193 lines
5.5 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 wstring
2021-03-29 00:14:43 +08:00
2021-07-10 19:20:43 +08:00
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Input, Component
#include "ftxui/component/component_base.hpp" // for ComponentBase
#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-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) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
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) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
Component 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'));
EXPECT_EQ(content, L"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, L"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, Arrow) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
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) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
Component input = Input(&content, &placeholder);
input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c'));
EXPECT_EQ(content, L"abc");
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, L"a-bc");
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, L"a--bc");
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, L"-a--bc");
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, Home) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
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, L"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, L"-abc");
2021-03-29 00:14:43 +08:00
}
TEST(InputTest, End) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
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) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
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
2021-05-10 02:32:27 +08:00
EXPECT_EQ(content, L"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, L"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, L"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) {
2021-05-10 02:32:27 +08:00
std::wstring content;
std::wstring placeholder;
2021-07-10 18:29:39 +08:00
auto option = InputOption();
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
2021-05-10 02:32:27 +08:00
EXPECT_EQ(content, L"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, L"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, L"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, L"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.