mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-05-06 07:25:48 +08:00

In the past, FTXUI switched from std::string to std::wstring to support fullwidth characters. The reasons was that fullwidth characters can be stored inside a single wchar_t. Then FTXUI added support for combining characters. A single glygh doesn't even fit a wchar_t. Instead, a glyph can be arbitrary large. The usage of wstring doesn't really fit the new model and have several drawbacks: 1. It doesn't simplify the implementation of FTXUI, because of combining characters. 2. It reduces drawing performance by 2x. 3. It increase Screen's memory allocation by 2x. This patch converts FTXUI to use std::string internally. It now exposes std::string based API. The std::wstring API remains, but is now deprecated. Tests and examples haven't been update to show the breakage is limited. They will be updated in a second set of patches. Bug: https://github.com/ArthurSonzogni/FTXUI/issues/153 Co-authored-by: Tushar Maheshwari <tushar27192@gmail.com>
224 lines
6.5 KiB
C++
224 lines
6.5 KiB
C++
#include <gtest/gtest-message.h> // for Message
|
|
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
|
#include <memory> // for __shared_ptr_access, shared_ptr, allocator
|
|
#include <string> // for wstring
|
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase, Component
|
|
#include "ftxui/component/component_options.hpp" // for InputOption
|
|
#include "ftxui/component/deprecated.hpp" // for Input
|
|
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
|
|
#include "ftxui/dom/elements.hpp" // for Fit
|
|
#include "ftxui/dom/node.hpp" // for Render
|
|
#include "ftxui/screen/screen.hpp" // for Screen, Pixel
|
|
#include "ftxui/util/ref.hpp" // for Ref
|
|
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
|
|
|
|
using namespace ftxui;
|
|
|
|
TEST(InputTest, Init) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
Component input = Input(&content, &placeholder, &option);
|
|
|
|
EXPECT_EQ(option.cursor_position(), 0);
|
|
}
|
|
|
|
TEST(InputTest, Type) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
Component input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
EXPECT_EQ(content, L"a");
|
|
EXPECT_EQ(option.cursor_position(), 1u);
|
|
|
|
input->OnEvent(Event::Character('b'));
|
|
EXPECT_EQ(content, L"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, "a");
|
|
EXPECT_EQ(screen.PixelAt(1, 0).character, "b");
|
|
}
|
|
|
|
TEST(InputTest, TypePassword) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
option.password = true;
|
|
Component input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
EXPECT_EQ(content, L"a");
|
|
EXPECT_EQ(option.cursor_position(), 1u);
|
|
|
|
input->OnEvent(Event::Character('b'));
|
|
EXPECT_EQ(content, L"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, "•");
|
|
}
|
|
|
|
TEST(InputTest, Arrow) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
auto input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
input->OnEvent(Event::Character('b'));
|
|
input->OnEvent(Event::Character('c'));
|
|
|
|
EXPECT_EQ(option.cursor_position(), 3u);
|
|
|
|
input->OnEvent(Event::ArrowLeft);
|
|
EXPECT_EQ(option.cursor_position(), 2u);
|
|
|
|
input->OnEvent(Event::ArrowLeft);
|
|
EXPECT_EQ(option.cursor_position(), 1u);
|
|
|
|
input->OnEvent(Event::ArrowLeft);
|
|
EXPECT_EQ(option.cursor_position(), 0u);
|
|
|
|
input->OnEvent(Event::ArrowLeft);
|
|
EXPECT_EQ(option.cursor_position(), 0u);
|
|
|
|
input->OnEvent(Event::ArrowRight);
|
|
EXPECT_EQ(option.cursor_position(), 1u);
|
|
|
|
input->OnEvent(Event::ArrowRight);
|
|
EXPECT_EQ(option.cursor_position(), 2u);
|
|
|
|
input->OnEvent(Event::ArrowRight);
|
|
EXPECT_EQ(option.cursor_position(), 3u);
|
|
|
|
input->OnEvent(Event::ArrowRight);
|
|
EXPECT_EQ(option.cursor_position(), 3u);
|
|
}
|
|
|
|
TEST(InputTest, Insert) {
|
|
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");
|
|
}
|
|
|
|
TEST(InputTest, Home) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
auto input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
input->OnEvent(Event::Character('b'));
|
|
input->OnEvent(Event::Character('c'));
|
|
EXPECT_EQ(content, L"abc");
|
|
|
|
EXPECT_EQ(option.cursor_position(), 3u);
|
|
input->OnEvent(Event::Home);
|
|
EXPECT_EQ(option.cursor_position(), 0u);
|
|
|
|
input->OnEvent(Event::Character('-'));
|
|
EXPECT_EQ(content, L"-abc");
|
|
}
|
|
|
|
TEST(InputTest, End) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
auto input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
input->OnEvent(Event::Character('b'));
|
|
input->OnEvent(Event::Character('c'));
|
|
|
|
input->OnEvent(Event::ArrowLeft);
|
|
input->OnEvent(Event::ArrowLeft);
|
|
|
|
EXPECT_EQ(option.cursor_position(), 1u);
|
|
input->OnEvent(Event::End);
|
|
EXPECT_EQ(option.cursor_position(), 3u);
|
|
}
|
|
|
|
TEST(InputTest, Delete) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
auto input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
input->OnEvent(Event::Character('b'));
|
|
input->OnEvent(Event::Character('c'));
|
|
input->OnEvent(Event::ArrowLeft);
|
|
|
|
EXPECT_EQ(content, L"abc");
|
|
EXPECT_EQ(option.cursor_position(), 2u);
|
|
|
|
input->OnEvent(Event::Delete);
|
|
EXPECT_EQ(content, L"ab");
|
|
EXPECT_EQ(option.cursor_position(), 2u);
|
|
|
|
input->OnEvent(Event::Delete);
|
|
EXPECT_EQ(content, L"ab");
|
|
EXPECT_EQ(option.cursor_position(), 2u);
|
|
}
|
|
|
|
TEST(InputTest, Backspace) {
|
|
std::wstring content;
|
|
std::wstring placeholder;
|
|
auto option = InputOption();
|
|
auto input = Input(&content, &placeholder, &option);
|
|
|
|
input->OnEvent(Event::Character('a'));
|
|
input->OnEvent(Event::Character('b'));
|
|
input->OnEvent(Event::Character('c'));
|
|
input->OnEvent(Event::ArrowLeft);
|
|
|
|
EXPECT_EQ(content, L"abc");
|
|
EXPECT_EQ(option.cursor_position(), 2u);
|
|
|
|
input->OnEvent(Event::Backspace);
|
|
EXPECT_EQ(content, L"ac");
|
|
EXPECT_EQ(option.cursor_position(), 1u);
|
|
|
|
input->OnEvent(Event::Backspace);
|
|
EXPECT_EQ(content, L"c");
|
|
EXPECT_EQ(option.cursor_position(), 0u);
|
|
|
|
input->OnEvent(Event::Backspace);
|
|
EXPECT_EQ(content, L"c");
|
|
EXPECT_EQ(option.cursor_position(), 0u);
|
|
}
|
|
|
|
// 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.
|