Add coverage for menu and resizable_split.

This commit is contained in:
ArthurSonzogni 2022-04-27 23:00:29 +02:00
parent d0890f94d1
commit c33e805a76
4 changed files with 194 additions and 1 deletions

View File

@ -30,6 +30,7 @@ add_executable(tests
src/ftxui/component/input_test.cpp
src/ftxui/component/menu_test.cpp
src/ftxui/component/radiobox_test.cpp
src/ftxui/component/resizable_split_test.cpp
src/ftxui/component/receiver_test.cpp
src/ftxui/component/screen_interactive_test.cpp
src/ftxui/component/terminal_input_parser_test.cpp

View File

@ -14,6 +14,8 @@
namespace ftxui {
using namespace std::chrono_literals;
TEST(MenuTest, RemoveEntries) {
int focused_entry = 0;
int selected = 0;
@ -90,6 +92,84 @@ TEST(MenuTest, Directions) {
}
}
TEST(MenuTest, AnimationsHorizontal) {
int selected = 0;
std::vector<std::string> entries = {"1", "2", "3"};
auto option = MenuOption::HorizontalAnimated();
auto menu = Menu(&entries, &selected, &option);
{
Screen screen(4, 3);
Render(screen, menu->Render());
EXPECT_EQ(
screen.ToString(),
"\x1B[1m\x1B[7m1\x1B[22m\x1B[27m \x1B[2m2\x1B[22m "
"\r\n\x1B[97m\x1B[49m\xE2\x94\x80\x1B[90m\x1B["
"49m\xE2\x95\xB6\xE2\x94\x80\xE2\x94\x80\x1B[39m\x1B[49m\r\n ");
}
selected = 1;
{
Screen screen(4, 3);
Render(screen, menu->Render());
EXPECT_EQ(
screen.ToString(),
"\x1B[7m1\x1B[27m \x1B[1m2\x1B[22m "
"\r\n\x1B[97m\x1B[49m\xE2\x94\x80\x1B[90m\x1B["
"49m\xE2\x95\xB6\xE2\x94\x80\xE2\x94\x80\x1B[39m\x1B[49m\r\n ");
}
animation::Params params(2s);
menu->OnAnimation(params);
{
Screen screen(4, 3);
Render(screen, menu->Render());
EXPECT_EQ(
screen.ToString(),
"\x1B[7m1\x1B[27m \x1B[1m2\x1B[22m "
"\r\n\x1B[90m\x1B[49m\xE2\x94\x80\xE2\x95\xB4\x1B[97m\x1B["
"49m\xE2\x94\x80\x1B[90m\x1B[49m\xE2\x95\xB6\x1B[39m\x1B[49m\r\n ");
}
}
TEST(MenuTest, AnimationsVertical) {
int selected = 0;
std::vector<std::string> entries = {"1", "2", "3"};
auto option = MenuOption::VerticalAnimated();
auto menu = Menu(&entries, &selected, &option);
{
Screen screen(10, 3);
Render(screen, menu->Render());
EXPECT_EQ(
screen.ToString(),
"\x1B[90m\x1B[49m\xE2\x94\x82\x1B[1m\x1B[7m\x1B[39m\x1B[49m1\x1B["
"22m\x1B[27m "
"\r\n\x1B[97m\x1B[49m\xE2\x95\xB7\x1B[2m\x1B[39m\x1B[49m2\x1B[22m "
" \r\n\x1B[97m\x1B[49m\xE2\x94\x82\x1B[2m\x1B[39m\x1B[49m3\x1B[22m "
" ");
}
selected = 1;
{
Screen screen(10, 3);
Render(screen, menu->Render());
EXPECT_EQ(
screen.ToString(),
"\x1B[90m\x1B[49m\xE2\x94\x82\x1B[7m\x1B[39m\x1B[49m1\x1B[27m "
"\r\n\x1B[97m\x1B[49m\xE2\x95\xB7\x1B[1m\x1B[39m\x1B[49m2\x1B[22m "
" \r\n\x1B[97m\x1B[49m\xE2\x94\x82\x1B[2m\x1B[39m\x1B[49m3\x1B[22m "
" ");
}
animation::Params params(2s);
menu->OnAnimation(params);
{
Screen screen(10, 3);
Render(screen, menu->Render());
EXPECT_EQ(
screen.ToString(),
"\x1B[97m\x1B[49m\xE2\x95\xB5\x1B[7m\x1B[39m\x1B[49m1\x1B[27m "
"\r\n\x1B[90m\x1B[49m\xE2\x94\x82\x1B[1m\x1B[39m\x1B[49m2\x1B[22m "
" \r\n\x1B[97m\x1B[49m\xE2\x95\xB7\x1B[2m\x1B[39m\x1B[49m3\x1B[22m "
" ");
}
}
} // namespace ftxui
// Copyright 2022 Arthur Sonzogni. All rights reserved.

View File

@ -0,0 +1,111 @@
#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 string, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Radiobox
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for RadioboxOption
#include "ftxui/component/event.hpp" // for Event, Event::Return, Event::ArrowDown, Event::ArrowUp, Event::Tab, Event::TabReverse
#include "ftxui/util/ref.hpp" // for Ref
#include "gtest/gtest_pred_impl.h" // for EXPECT_EQ, Test, TEST
namespace ftxui {
namespace {
Component BasicComponent() {
return Renderer([] { return text(""); });
}
Event MousePressed(int x, int y) {
Mouse mouse;
mouse.button = Mouse::Left;
mouse.motion = Mouse::Pressed;
mouse.shift = false;
mouse.meta = false;
mouse.control = false;
mouse.x = x;
mouse.y = y;
return Event::Mouse("jjj", mouse);
}
Event MouseReleased(int x, int y) {
Mouse mouse;
mouse.button = Mouse::Left;
mouse.motion = Mouse::Released;
mouse.shift = false;
mouse.meta = false;
mouse.control = false;
mouse.x = x;
mouse.y = y;
return Event::Mouse("jjj", mouse);
}
} // namespace
TEST(ResizableSplit, BasicLeft) {
int position = 3;
auto component =
ResizableSplitLeft(BasicComponent(), BasicComponent(), &position);
auto screen = Screen(20, 20);
Render(screen, component->Render());
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(3, 1)));
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
EXPECT_EQ(position, 10);
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
EXPECT_EQ(position, 10);
}
TEST(ResizableSplit, BasicRight) {
int position = 3;
auto component =
ResizableSplitRight(BasicComponent(), BasicComponent(), &position);
auto screen = Screen(20, 20);
Render(screen, component->Render());
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(16, 1)));
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
EXPECT_EQ(position, 9);
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
EXPECT_EQ(position, 9);
}
TEST(ResizableSplit, BasicTop) {
int position = 3;
auto component =
ResizableSplitTop(BasicComponent(), BasicComponent(), &position);
auto screen = Screen(20, 20);
Render(screen, component->Render());
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(1, 3)));
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(1, 10)));
EXPECT_EQ(position, 10);
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 10)));
EXPECT_EQ(position, 10);
}
TEST(ResizableSplit, BasicBottom) {
int position = 3;
auto component =
ResizableSplitBottom(BasicComponent(), BasicComponent(), &position);
auto screen = Screen(20, 20);
Render(screen, component->Render());
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(1, 16)));
EXPECT_EQ(position, 3);
EXPECT_TRUE(component->OnEvent(MousePressed(1, 10)));
EXPECT_EQ(position, 9);
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 10)));
EXPECT_EQ(position, 9);
}
} // namespace ftxui
// Copyright 2022 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

View File

@ -8,6 +8,7 @@ rm * -rf
echo $CMAKE_CXX_INCLUDE_WHAT_YOU_USE
cmake .. -DFTXUI_BUILD_TESTS=ON -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="include-what-you-use;-Xiwyu;--cxx17ns;-Xiwyu;--mapping_file=${mapping_dir}/iwyu.imp;-Xiwyu;--verbose=3"
make -j 2>out
fix_includes.py --comments < out
#fix_includes.py --comments < out
fix_include --comments < out
../tools/format.sh