diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dac375..02bf2c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,7 @@ export(TARGETS screen dom component NAMESPACE ftxui:: find_package(GTest) if (FTXUI_BUILD_TESTS AND GTEST_FOUND AND THREADS_FOUND) add_executable(tests + src/ftxui/component/toggle_test.cpp src/ftxui/dom/gauge_test.cpp src/ftxui/dom/hbox_test.cpp src/ftxui/dom/text_test.cpp @@ -161,7 +162,7 @@ if (FTXUI_BUILD_TESTS AND GTEST_FOUND AND THREADS_FOUND) ) target_link_libraries(tests - PRIVATE dom + PRIVATE component PRIVATE Threads::Threads PRIVATE ${GTEST_BOTH_LIBRARIES} ) diff --git a/src/ftxui/component/toggle_test.cpp b/src/ftxui/component/toggle_test.cpp new file mode 100644 index 0000000..090e5e0 --- /dev/null +++ b/src/ftxui/component/toggle_test.cpp @@ -0,0 +1,75 @@ +#include "ftxui/component/toggle.hpp" + +#include "gtest/gtest.h" + +using namespace ftxui; +using namespace ftxui; + +TEST(ToggleTest, leftRightArrow) { + Toggle toggle; + + // With arrow key. + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::ArrowRight); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::ArrowRight); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::ArrowLeft); + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::ArrowLeft); + EXPECT_EQ(toggle.selected, 0); + + // With vim like characters. + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::Character('l')); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::Character('l')); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::Character('h')); + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::Character('h')); + EXPECT_EQ(toggle.selected, 0); + + // With more entries + toggle.entries = {L"1", L"2", L"3"}; + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::ArrowRight); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::ArrowRight); + EXPECT_EQ(toggle.selected, 2); + toggle.OnEvent(Event::ArrowRight); + EXPECT_EQ(toggle.selected, 2); + toggle.OnEvent(Event::ArrowLeft); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::ArrowLeft); + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::ArrowLeft); + EXPECT_EQ(toggle.selected, 0); +} + +TEST(ToggleTest, Tab) { + Toggle toggle; + toggle.entries = {L"1", L"2", L"3"}; + + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::Tab); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::Tab); + EXPECT_EQ(toggle.selected, 2); + toggle.OnEvent(Event::Tab); + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::Tab); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::Tab); + EXPECT_EQ(toggle.selected, 2); + toggle.OnEvent(Event::TabReverse); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::TabReverse); + EXPECT_EQ(toggle.selected, 0); + toggle.OnEvent(Event::TabReverse); + EXPECT_EQ(toggle.selected, 2); + toggle.OnEvent(Event::TabReverse); + EXPECT_EQ(toggle.selected, 1); + toggle.OnEvent(Event::TabReverse); + +}