From 84d6e6b3dd25dbd22bde5d13d20d037a66e33c6f Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Wed, 27 Apr 2022 14:00:46 +0200 Subject: [PATCH] Add additional coverage tests. --- cmake/ftxui_test.cmake | 5 +- src/ftxui/dom/border.cpp | 14 +-- src/ftxui/dom/canvas_test.cpp | 48 +++++++++-- src/ftxui/dom/dbox_test.cpp | 40 +++++++++ src/ftxui/dom/scroll_indicator_test.cpp | 109 ++++++++++++++++++++++++ src/ftxui/dom/separator.cpp | 14 +-- src/ftxui/dom/spinner_test.cpp | 42 +++++++++ 7 files changed, 249 insertions(+), 23 deletions(-) create mode 100644 src/ftxui/dom/dbox_test.cpp create mode 100644 src/ftxui/dom/scroll_indicator_test.cpp create mode 100644 src/ftxui/dom/spinner_test.cpp diff --git a/cmake/ftxui_test.cmake b/cmake/ftxui_test.cmake index 78a5336..3ed3bfa 100644 --- a/cmake/ftxui_test.cmake +++ b/cmake/ftxui_test.cmake @@ -38,14 +38,17 @@ add_executable(tests src/ftxui/dom/bold_test.cpp src/ftxui/dom/border_test.cpp src/ftxui/dom/canvas_test.cpp - src/ftxui/dom/separator_test.cpp src/ftxui/dom/color_test.cpp + src/ftxui/dom/dbox_test.cpp src/ftxui/dom/dim_test.cpp src/ftxui/dom/flexbox_helper_test.cpp src/ftxui/dom/flexbox_test.cpp src/ftxui/dom/gauge_test.cpp src/ftxui/dom/gridbox_test.cpp src/ftxui/dom/hbox_test.cpp + src/ftxui/dom/scroll_indicator_test.cpp + src/ftxui/dom/separator_test.cpp + src/ftxui/dom/spinner_test.cpp src/ftxui/dom/table_test.cpp src/ftxui/dom/text_test.cpp src/ftxui/dom/underlined_test.cpp diff --git a/src/ftxui/dom/border.cpp b/src/ftxui/dom/border.cpp index 1c8555d..0466dd9 100644 --- a/src/ftxui/dom/border.cpp +++ b/src/ftxui/dom/border.cpp @@ -15,13 +15,13 @@ namespace ftxui { using Charset = std::array; // NOLINT using Charsets = std::array; // NOLINT -static Charsets simple_border_charset = // NOLINT - { - Charset{"┌", "┐", "└", "┘", "─", "│"}, - Charset{"┏", "┓", "┗", "┛", "━", "┃"}, - Charset{"╔", "╗", "╚", "╝", "═", "║"}, - Charset{"╭", "╮", "╰", "╯", "─", "│"}, - Charset{" ", " ", " ", " ", " ", " "}, +// NOLINTNEXTLINE +static Charsets simple_border_charset = { + Charset{"┌", "┐", "└", "┘", "─", "│"}, + Charset{"┏", "┓", "┗", "┛", "━", "┃"}, + Charset{"╔", "╗", "╚", "╝", "═", "║"}, + Charset{"╭", "╮", "╰", "╯", "─", "│"}, + Charset{" ", " ", " ", " ", " ", " "}, }; // For reference, here is the charset for normal border: diff --git a/src/ftxui/dom/canvas_test.cpp b/src/ftxui/dom/canvas_test.cpp index 274bdb9..ec790c1 100644 --- a/src/ftxui/dom/canvas_test.cpp +++ b/src/ftxui/dom/canvas_test.cpp @@ -8,16 +8,17 @@ namespace ftxui { namespace { -int Hash(const std::string s) { - int hash = 0; +uint32_t Hash(const std::string s) { + uint32_t hash = 0; for (auto c : s) { hash += c; + hash *= 7; } return hash; } } -TEST(BorderTest, GoldPoint) { +TEST(CanvasTest, GoldPoint) { Terminal::SetColorSupport(Terminal::Color::TrueColor); auto element = canvas([](Canvas& c) { // c.DrawPoint(3, 3, 1); @@ -32,10 +33,25 @@ TEST(BorderTest, GoldPoint) { }); Screen screen(30, 10); Render(screen, element); - EXPECT_EQ(Hash(screen.ToString()), 1069); + EXPECT_EQ(Hash(screen.ToString()), -1195891837); } -TEST(BorderTest, GoldBlock) { +TEST(CanvasTest, GoldPointColor) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + auto element = canvas([](Canvas& c) { // + c.DrawPoint(3, 3, 1, Color::Red); + c.DrawPointLine(3, 7, 10, 19,Color::Blue); + c.DrawPointCircle(10, 5, 3, Color::Yellow); + c.DrawPointCircleFilled(20, 5, 3, Color::White); + c.DrawPointEllipse(10, 10, 5, 2, Color::Black); + c.DrawPointEllipseFilled(10, 20, 5, 2, Color::Cyan); + }); + Screen screen(30, 10); + Render(screen, element); + EXPECT_EQ(Hash(screen.ToString()), 1109533029); +} + +TEST(CanvasTest, GoldBlock) { Terminal::SetColorSupport(Terminal::Color::TrueColor); auto element = canvas([](Canvas& c) { // c.DrawBlock(3, 3, 1); @@ -50,10 +66,26 @@ TEST(BorderTest, GoldBlock) { }); Screen screen(30, 10); Render(screen, element); - EXPECT_EQ(Hash(screen.ToString()), 472); + EXPECT_EQ(Hash(screen.ToString()), 817159424); } -TEST(BorderTest, GoldText) { +TEST(CanvasTest, GoldBlockColor) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + auto element = canvas([](Canvas& c) { // + c.DrawBlock(3, 3, 1, Color::Red); + c.DrawBlockLine(3, 7, 10, 19, Color::Green); + c.DrawBlockCircle(10, 5, 3, Color::Blue); + c.DrawBlockCircleFilled(20, 5, 3, Color::Yellow); + c.DrawBlockEllipse(10, 10, 5, 2, Color::White); + c.DrawBlockEllipseFilled(10, 20, 5, 2, Color::Black); + }); + Screen screen(30, 10); + Render(screen, element); + EXPECT_EQ(Hash(screen.ToString()), 2869205941); +} + + +TEST(CanvasTest, GoldText) { Terminal::SetColorSupport(Terminal::Color::TrueColor); Canvas c(10, 10); c.DrawText(0, 0, "test"); @@ -62,7 +94,7 @@ TEST(BorderTest, GoldText) { auto element = canvas(c); Screen screen(30, 10); Render(screen, element); - EXPECT_EQ(Hash(screen.ToString()), 10447); + EXPECT_EQ(Hash(screen.ToString()), 1074960375); } } // namespace ftxui diff --git a/src/ftxui/dom/dbox_test.cpp b/src/ftxui/dom/dbox_test.cpp new file mode 100644 index 0000000..159ea31 --- /dev/null +++ b/src/ftxui/dom/dbox_test.cpp @@ -0,0 +1,40 @@ +#include // for Message +#include // for SuiteApiResolver, TestFactoryImpl, TestPartResult +#include // for remove +#include // for allocator, basic_string, string +#include // for vector + +#include "ftxui/dom/elements.hpp" // for vtext, operator|, Element, flex_grow, flex_shrink, vbox +#include "ftxui/dom/node.hpp" // for Render +#include "ftxui/screen/color.hpp" // for ftxui +#include "ftxui/screen/screen.hpp" // for Screen +#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST + +namespace ftxui { + +TEST(DBoxTest, Basic) { + auto root = dbox({ + hbox({ + text("test") | border, + filler(), + }), + vbox({ + text("test") | border, + filler(), + }), + }); + + Screen screen(8, 4); + Render(screen, root); + EXPECT_EQ(screen.ToString(), + "╭────┬─╮\r\n" + "│test│ │\r\n" + "╰────┴─╯\r\n" + "╰────╯ "); +} + +} // namespace ftxui + +// Copyright 2020 Arthur Sonzogni. All rights reserved. +// Use of this source code is governed by the MIT license that can be found in +// the LICENSE file. diff --git a/src/ftxui/dom/scroll_indicator_test.cpp b/src/ftxui/dom/scroll_indicator_test.cpp new file mode 100644 index 0000000..171d9f6 --- /dev/null +++ b/src/ftxui/dom/scroll_indicator_test.cpp @@ -0,0 +1,109 @@ +#include // for Message +#include // for SuiteApiResolver, TestFactoryImpl, TestPartResult +#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST +#include // for allocator +#include "ftxui/dom/elements.hpp" // for text, flexbox +#include "ftxui/screen/screen.hpp" // for Screen + +namespace ftxui { + +namespace { +Element MakeList(int focused_index, int n) { + Elements list; + for (int i = 0; i < n; ++i) { + auto element = text(std::to_string(i)); + if (i == focused_index) { + element |= focus; + } + list.push_back(element); + } + return vbox(std::move(list)) | vscroll_indicator | frame | border; +} + +std::string Print(int focused_index, int n) { + auto element = MakeList(focused_index, n); + Screen screen(6, 6); + Render(screen, element); + return screen.ToString(); +} + +} // namespace + +TEST(ScrollIndicator, Basic) { + EXPECT_EQ(Print(0, 10), + "╭────╮\r\n" + "│0 ┃│\r\n" + "│1 ┃│\r\n" + "│2 │\r\n" + "│3 │\r\n" + "╰────╯"); + EXPECT_EQ(Print(1, 10), + "╭────╮\r\n" + "│0 ┃│\r\n" + "│1 ┃│\r\n" + "│2 │\r\n" + "│3 │\r\n" + "╰────╯"); + EXPECT_EQ(Print(2, 10), + "╭────╮\r\n" + "│1 ┃│\r\n" + "│2 ┃│\r\n" + "│3 │\r\n" + "│4 │\r\n" + "╰────╯"); + EXPECT_EQ(Print(3, 10), + "╭────╮\r\n" + "│2 ╻│\r\n" + "│3 ┃│\r\n" + "│4 ╹│\r\n" + "│5 │\r\n" + "╰────╯"); + EXPECT_EQ(Print(4, 10), + "╭────╮\r\n" + "│3 │\r\n" + "│4 ┃│\r\n" + "│5 ┃│\r\n" + "│6 │\r\n" + "╰────╯"); + EXPECT_EQ(Print(5, 10), + "╭────╮\r\n" + "│4 │\r\n" + "│5 ╻│\r\n" + "│6 ┃│\r\n" + "│7 ╹│\r\n" + "╰────╯"); + EXPECT_EQ(Print(6, 10), + "╭────╮\r\n" + "│5 │\r\n" + "│6 │\r\n" + "│7 ┃│\r\n" + "│8 ┃│\r\n" + "╰────╯"); + EXPECT_EQ(Print(7, 10), + "╭────╮\r\n" + "│6 │\r\n" + "│7 │\r\n" + "│8 ┃│\r\n" + "│9 ┃│\r\n" + "╰────╯"); + EXPECT_EQ(Print(8, 10), + "╭────╮\r\n" + "│6 │\r\n" + "│7 │\r\n" + "│8 ┃│\r\n" + "│9 ┃│\r\n" + "╰────╯"); + EXPECT_EQ(Print(9, 10), + "╭────╮\r\n" + "│6 │\r\n" + "│7 │\r\n" + "│8 ┃│\r\n" + "│9 ┃│\r\n" + "╰────╯"); +} + +} // 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. diff --git a/src/ftxui/dom/separator.cpp b/src/ftxui/dom/separator.cpp index a6b986d..7a10dcb 100644 --- a/src/ftxui/dom/separator.cpp +++ b/src/ftxui/dom/separator.cpp @@ -17,13 +17,13 @@ using ftxui::Screen; using Charset = std::array; // NOLINT using Charsets = std::array; // NOLINT -const Charsets charsets = // NOLINT - { - Charset{"│", "─"}, // - Charset{"┃", "━"}, // - Charset{"║", "═"}, // - Charset{"│", "─"}, // - Charset{" ", " "}, // +// NOLINTNEXTLINE +const Charsets charsets = { + Charset{"│", "─"}, // + Charset{"┃", "━"}, // + Charset{"║", "═"}, // + Charset{"│", "─"}, // + Charset{" ", " "}, // }; } // namespace diff --git a/src/ftxui/dom/spinner_test.cpp b/src/ftxui/dom/spinner_test.cpp new file mode 100644 index 0000000..d15ba16 --- /dev/null +++ b/src/ftxui/dom/spinner_test.cpp @@ -0,0 +1,42 @@ +#include // for Message +#include // for SuiteApiResolver, TestFactoryImpl, TestPartResult +#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST +#include // for allocator +#include "ftxui/dom/elements.hpp" // for text, flexbox +#include "ftxui/screen/screen.hpp" // for Screen + +namespace ftxui { + +TEST(SpinnerTest, Spinner1) { + auto element = spinner(1, 0); + Screen screen(4, 1); + Render(screen, element); + EXPECT_EQ(screen.ToString(), ". "); +} + +TEST(SpinnerTest, Spinner2) { + auto element = spinner(1, 1); + Screen screen(4, 1); + Render(screen, element); + EXPECT_EQ(screen.ToString(), ".. "); +} + +TEST(SpinnerTest, Spinner3) { + auto element = spinner(1, 2); + Screen screen(4, 1); + Render(screen, element); + EXPECT_EQ(screen.ToString(), "... "); +} + +TEST(SpinnerTest, Spinner4) { + auto element = spinner(1, 3); + Screen screen(4, 1); + Render(screen, element); + EXPECT_EQ(screen.ToString(), ". "); +} + +} // 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.