From 06ed8567b87d9afcfef76d48996353cb03cbfa91 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Tue, 26 Apr 2022 18:00:05 +0200 Subject: [PATCH] Add coverage for colors (extended) (#385) --- .github/workflows/build.yaml | 4 +-- cmake/ftxui_test.cmake | 1 + src/ftxui/dom/border_test.cpp | 10 ++++++ src/ftxui/dom/canvas_test.cpp | 56 +++++++++++++++++++++++++++++++++ src/ftxui/screen/color.cpp | 11 ++----- src/ftxui/screen/color_test.cpp | 42 +++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 src/ftxui/dom/canvas_test.cpp diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 18b5514..04f69c9 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -77,7 +77,7 @@ jobs: if: runner.os != 'Windows' working-directory: ./build run: > - ctest -C Debug; + ctest -C Debug --rerun-failed --output-on-failure; gcovr -j ${{env.nproc}} --delete @@ -99,7 +99,7 @@ jobs: --export_type cobertura:coverage.xml --cover_children -- - ctest -C Debug + ctest -C Debug --rerun-failed --output-on-failure; - name: Publish to codecov uses: codecov/codecov-action@v2 diff --git a/cmake/ftxui_test.cmake b/cmake/ftxui_test.cmake index fd1dd79..4dafb7c 100644 --- a/cmake/ftxui_test.cmake +++ b/cmake/ftxui_test.cmake @@ -35,6 +35,7 @@ add_executable(tests src/ftxui/dom/blink_test.cpp 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/dim_test.cpp diff --git a/src/ftxui/dom/border_test.cpp b/src/ftxui/dom/border_test.cpp index 153de6b..9f1706d 100644 --- a/src/ftxui/dom/border_test.cpp +++ b/src/ftxui/dom/border_test.cpp @@ -89,6 +89,16 @@ TEST(BorderTest, WithPixel) { "ooooo"); } +TEST(BorderTest, Window) { + auto element = window(text("title"), text("content")); + Screen screen(10, 3); + Render(screen, element); + EXPECT_EQ(screen.ToString(), + "╭title───╮\r\n" + "│content │\r\n" + "╰────────╯"); +} + } // namespace ftxui // Copyright 2022 Arthur Sonzogni. All rights reserved. diff --git a/src/ftxui/dom/canvas_test.cpp b/src/ftxui/dom/canvas_test.cpp new file mode 100644 index 0000000..c31c6d8 --- /dev/null +++ b/src/ftxui/dom/canvas_test.cpp @@ -0,0 +1,56 @@ +#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 { +int Hash(const std::string s) { + int hash = 0; + for (auto c : s) { + hash += c; + } + return hash; +} +} + +TEST(BorderTest, GoldPoint) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + auto element = canvas([](Canvas& c) { // + c.DrawPoint(3, 3, 1, Color::Red); + c.DrawPointToggle(2, 8); + c.DrawPointLine(3, 7, 10, 19, Color::Blue); + c.DrawPointCircle(10, 5, 3, Color::Yellow); + c.DrawPointCircleFilled(20, 5, 3, Color::Green); + c.DrawPointEllipse(10, 10, 5, 2, Color::Blue); + c.DrawPointEllipseFilled(10, 20, 5, 2, Color::DarkGreen); + }); + Screen screen(30, 10); + Render(screen, element); + EXPECT_EQ(Hash(screen.ToString()), 17651); +} + +TEST(BorderTest, GoldBlock) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + auto element = canvas([](Canvas& c) { // + c.DrawBlock(3, 3, 1, Color::Red); + c.DrawBlockToggle(2, 8); + c.DrawBlockLine(3, 7, 10, 19, Color::Blue); + c.DrawBlockCircle(10, 5, 3, Color::Yellow); + c.DrawBlockCircleFilled(20, 5, 3, Color::Green); + c.DrawBlockEllipse(10, 10, 5, 2, Color::Blue); + c.DrawBlockEllipseFilled(10, 20, 5, 2, Color::DarkGreen); + }); + Screen screen(30, 10); + Render(screen, element); + EXPECT_EQ(Hash(screen.ToString()), 14383); +} + +} // 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/screen/color.cpp b/src/ftxui/screen/color.cpp index 33e1d07..2d50125 100644 --- a/src/ftxui/screen/color.cpp +++ b/src/ftxui/screen/color.cpp @@ -170,7 +170,8 @@ Color Color::HSV(uint8_t h, uint8_t s, uint8_t v) { // static Color Color::Interpolate(float t, const Color& a, const Color& b) { - if (a.type_ == ColorType::Palette1) { + if (a.type_ == ColorType::Palette1 || // + b.type_ == ColorType::Palette1) { if (t < 0.5F) { // NOLINT return a; } else { @@ -178,14 +179,6 @@ Color Color::Interpolate(float t, const Color& a, const Color& b) { } } - if (b.type_ == ColorType::Palette1) { - if (t > 0.5F) { // NOLINT - return a; - } else { - return b; - } - } - auto get_color = [](const Color& color, // uint8_t* red, uint8_t* green, uint8_t* blue) { switch (color.type_) { diff --git a/src/ftxui/screen/color_test.cpp b/src/ftxui/screen/color_test.cpp index 07c771d..bcceeec 100644 --- a/src/ftxui/screen/color_test.cpp +++ b/src/ftxui/screen/color_test.cpp @@ -40,6 +40,48 @@ TEST(ColorTest, FallbackTo16) { EXPECT_EQ(Color::RGB(1,2,3).Print(false), "30"); } +TEST(ColorTest, Litterals) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + using namespace ftxui::literals; + EXPECT_EQ(Color(0xABCDEF_rgb).Print(false), "38;2;171;205;239"); +} + +TEST(ColorTest, Interpolate) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + + EXPECT_EQ(Color::Interpolate(0.3f, Color(), Color()).Print(false), "39"); + EXPECT_EQ(Color::Interpolate(0.3f, Color::Red, Color()).Print(false), "31"); + EXPECT_EQ(Color::Interpolate(0.7f, Color::Red, Color()).Print(false), "39"); + EXPECT_EQ(Color::Interpolate(0.3f, Color(), Color::Red).Print(false), "39"); + EXPECT_EQ(Color::Interpolate(0.7f, Color(), Color::Red).Print(false), "31"); + + EXPECT_EQ(Color::Interpolate(0.3f, // + Color::RGB(1, 2, 3), // + Color::RGB(244, 244, 123)) // + .Print(false), + "38;2;73;74;39"); + EXPECT_EQ(Color::Interpolate(0.7f, // + Color::RGB(1, 2, 3), // + Color::RGB(244, 244, 123)) // + .Print(false), + "38;2;171;171;87"); + EXPECT_EQ(Color::Interpolate(0.7f, // + Color(Color::Red), // + Color::RGB(244, 244, 123)) // + .Print(false), + "38;2;209;170;86"); + EXPECT_EQ(Color::Interpolate(0.7f, // + Color::RGB(244, 244, 123), // + Color(Color::Plum1)) // + .Print(false), + "38;2;251;195;215"); +} + +TEST(ColorTest, HSV) { + Terminal::SetColorSupport(Terminal::Color::TrueColor); + EXPECT_EQ(Color::HSV(0, 255, 255).Print(false), "38;2;255;0;0"); +} + } // namespace ftxui // Copyright 2022 Arthur Sonzogni. All rights reserved.