Add tests for the input component.

This commit is contained in:
ArthurSonzogni 2021-03-28 18:14:43 +02:00 committed by Arthur Sonzogni
parent 160b1c8bbc
commit 386a0f9eac
2 changed files with 160 additions and 1 deletions

View File

@ -215,9 +215,10 @@ if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4")
add_executable(tests
src/ftxui/component/container_test.cpp
src/ftxui/component/terminal_input_parser_test.cpp
src/ftxui/component/input_test.cpp
src/ftxui/component/radiobox_test.cpp
src/ftxui/component/receiver_test.cpp
src/ftxui/component/terminal_input_parser_test.cpp
src/ftxui/component/toggle_test.cpp
src/ftxui/dom/gauge_test.cpp
src/ftxui/dom/hbox_test.cpp

View File

@ -0,0 +1,158 @@
#include "ftxui/component/input.hpp"
#include "gtest/gtest.h"
using namespace ftxui;
TEST(InputTest, Init) {
Input input;
EXPECT_EQ(input.content, L"");
EXPECT_EQ(input.placeholder, L"");
EXPECT_EQ(input.cursor_position, 0);
}
TEST(InputTest, Type) {
Input input;
input.OnEvent(Event::Character('a'));
EXPECT_EQ(input.content, L"a");
EXPECT_EQ(input.cursor_position, 1u);
input.OnEvent(Event::Character('b'));
EXPECT_EQ(input.content, L"ab");
EXPECT_EQ(input.cursor_position, 2u);
}
TEST(InputTest, Arrow) {
Input input;
input.OnEvent(Event::Character('a'));
input.OnEvent(Event::Character('b'));
input.OnEvent(Event::Character('c'));
EXPECT_EQ(input.cursor_position, 3u);
input.OnEvent(Event::ArrowLeft);
EXPECT_EQ(input.cursor_position, 2u);
input.OnEvent(Event::ArrowLeft);
EXPECT_EQ(input.cursor_position, 1u);
input.OnEvent(Event::ArrowLeft);
EXPECT_EQ(input.cursor_position, 0u);
input.OnEvent(Event::ArrowLeft);
EXPECT_EQ(input.cursor_position, 0u);
input.OnEvent(Event::ArrowRight);
EXPECT_EQ(input.cursor_position, 1u);
input.OnEvent(Event::ArrowRight);
EXPECT_EQ(input.cursor_position, 2u);
input.OnEvent(Event::ArrowRight);
EXPECT_EQ(input.cursor_position, 3u);
input.OnEvent(Event::ArrowRight);
EXPECT_EQ(input.cursor_position, 3u);
}
TEST(InputTest, Insert) {
Input input;
input.OnEvent(Event::Character('a'));
input.OnEvent(Event::Character('b'));
input.OnEvent(Event::Character('c'));
EXPECT_EQ(input.content, L"abc");
input.OnEvent(Event::ArrowLeft);
input.OnEvent(Event::ArrowLeft);
input.OnEvent(Event::Character('-'));
EXPECT_EQ(input.content, L"a-bc");
input.OnEvent(Event::ArrowLeft);
input.OnEvent(Event::Character('-'));
EXPECT_EQ(input.content, L"a--bc");
input.OnEvent(Event::ArrowLeft);
input.OnEvent(Event::ArrowLeft);
input.OnEvent(Event::ArrowLeft);
input.OnEvent(Event::Character('-'));
EXPECT_EQ(input.content, L"-a--bc");
}
TEST(InputTest, Home) {
Input input;
input.OnEvent(Event::Character('a'));
input.OnEvent(Event::Character('b'));
input.OnEvent(Event::Character('c'));
EXPECT_EQ(input.content, L"abc");
EXPECT_EQ(input.cursor_position, 3u);
input.OnEvent(Event::Home);
EXPECT_EQ(input.cursor_position, 0u);
input.OnEvent(Event::Character('-'));
EXPECT_EQ(input.content, L"-abc");
}
TEST(InputTest, End) {
Input input;
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(input.cursor_position, 1u);
input.OnEvent(Event::End);
EXPECT_EQ(input.cursor_position, 3u);
}
TEST(InputTest, Delete) {
Input input;
input.OnEvent(Event::Character('a'));
input.OnEvent(Event::Character('b'));
input.OnEvent(Event::Character('c'));
input.OnEvent(Event::ArrowLeft);
EXPECT_EQ(input.content, L"abc");
EXPECT_EQ(input.cursor_position, 2u);
input.OnEvent(Event::Delete);
EXPECT_EQ(input.content, L"ab");
EXPECT_EQ(input.cursor_position, 2u);
input.OnEvent(Event::Delete);
EXPECT_EQ(input.content, L"ab");
EXPECT_EQ(input.cursor_position, 2u);
}
TEST(InputTest, Backspace) {
Input input;
input.OnEvent(Event::Character('a'));
input.OnEvent(Event::Character('b'));
input.OnEvent(Event::Character('c'));
input.OnEvent(Event::ArrowLeft);
EXPECT_EQ(input.content, L"abc");
EXPECT_EQ(input.cursor_position, 2u);
input.OnEvent(Event::Backspace);
EXPECT_EQ(input.content, L"ac");
EXPECT_EQ(input.cursor_position, 1u);
input.OnEvent(Event::Backspace);
EXPECT_EQ(input.content, L"c");
EXPECT_EQ(input.cursor_position, 0u);
input.OnEvent(Event::Backspace);
EXPECT_EQ(input.content, L"c");
EXPECT_EQ(input.cursor_position, 0u);
}