FTXUI/src/ftxui/component/input_test.cpp

183 lines
5.4 KiB
C++
Raw Normal View History

#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
2021-05-10 02:32:27 +08:00
#include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
#include "ftxui/component/input.hpp"
#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;
Component input = Input(&content, &placeholder);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
EXPECT_EQ(InputBase::From(input)->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;
Component input = Input(&content, &placeholder);
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");
EXPECT_EQ(InputBase::From(input)->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");
EXPECT_EQ(InputBase::From(input)->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;
Component input = Input(&content, &placeholder);
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
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
2021-03-29 00:14:43 +08:00
2021-05-10 02:32:27 +08:00
input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->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;
Component input = Input(&content, &placeholder);
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-05-10 02:32:27 +08:00
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
input->OnEvent(Event::Home);
EXPECT_EQ(InputBase::From(input)->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;
Component input = Input(&content, &placeholder);
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-05-10 02:32:27 +08:00
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
input->OnEvent(Event::End);
EXPECT_EQ(InputBase::From(input)->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;
Component input = Input(&content, &placeholder);
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");
EXPECT_EQ(InputBase::From(input)->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");
EXPECT_EQ(InputBase::From(input)->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");
EXPECT_EQ(InputBase::From(input)->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;
Component input = Input(&content, &placeholder);
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");
EXPECT_EQ(InputBase::From(input)->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");
EXPECT_EQ(InputBase::From(input)->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");
EXPECT_EQ(InputBase::From(input)->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");
EXPECT_EQ(InputBase::From(input)->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.