Add ToggleTest.

This commit is contained in:
ArthurSonzogni 2020-03-26 23:12:42 +01:00 committed by Arthur Sonzogni
parent f48bfcff10
commit 6600e4fc03
2 changed files with 77 additions and 1 deletions

View File

@ -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}
)

View File

@ -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);
}