diff --git a/CMakeLists.txt b/CMakeLists.txt index ba4671d..0831fbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/ftxui/component/input_test.cpp b/src/ftxui/component/input_test.cpp new file mode 100644 index 0000000..4bfc6b9 --- /dev/null +++ b/src/ftxui/component/input_test.cpp @@ -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); +}