From 52ca3cd0960f27a7889585aa09b89c9a3416fb8f Mon Sep 17 00:00:00 2001 From: Sven Mikael Persson Date: Mon, 17 Jul 2023 21:19:14 -0400 Subject: [PATCH] Added format_string forwarding tests to color-test, Fixed #3536 --- include/fmt/core.h | 6 ++++++ test/color-test.cc | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/fmt/core.h b/include/fmt/core.h index 370b2f1e..802fcd54 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -536,6 +536,12 @@ constexpr auto to_string_view(const S& s) -> basic_string_view { return basic_string_view(s); } +// Catch basic_format_string for any char type. +template ::value)> +constexpr auto to_string_view(const S& s) + -> decltype(s.get()) { + return s.get(); +} void to_string_view(...); // Specifies whether S is a string type convertible to fmt::basic_string_view. diff --git a/test/color-test.cc b/test/color-test.cc index c2ba13a9..d9291b89 100644 --- a/test/color-test.cc +++ b/test/color-test.cc @@ -58,6 +58,13 @@ TEST(color_test, format) { "\x1b[4m\x1b[38;2;000;000;255mbar\x1b[0m"); } +TEST(color_test, format_forwarding) { + const fmt::format_string format_str_int = "rgb(255,20,30){}{}{}"; + auto sv = fmt::detail::to_string_view(format_str_int); + EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), format_str_int, 1, 2, 3), + "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); +} + TEST(color_test, format_to) { auto out = std::string(); fmt::format_to(std::back_inserter(out), fg(fmt::rgb(255, 20, 30)), @@ -66,7 +73,24 @@ TEST(color_test, format_to) { "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); } +TEST(color_test, format_to_forwarding) { + auto out = std::string(); + const fmt::format_string format_str_int = "rgb(255,20,30){}{}{}"; + fmt::format_to(std::back_inserter(out), fg(fmt::rgb(255, 20, 30)), + format_str_int, 1, 2, 3); + EXPECT_EQ(fmt::to_string(out), + "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); +} + TEST(color_test, print) { EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); } + +TEST(color_test, print_forwarding) { + const fmt::format_string format_str_int = "rgb(255,20,30){}{}{}"; + EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), format_str_int, 1, 2, 3), + "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); + EXPECT_WRITE(stdout, fmt::print(stdout, fg(fmt::rgb(255, 20, 30)), format_str_int, 1, 2, 3), + "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); +}