1 // Formatting library for C++ - color tests
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
10 #include <iterator> // std::back_inserter
12 #include "gtest-extra.h" // EXPECT_WRITE
14 TEST(color_test
, format
) {
15 EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
16 "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
17 EXPECT_EQ(fmt::format(fg(fmt::color::blue
), "blue"),
18 "\x1b[38;2;000;000;255mblue\x1b[0m");
20 fmt::format(fg(fmt::color::blue
) | bg(fmt::color::red
), "two color"),
21 "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m");
22 EXPECT_EQ(fmt::format(fmt::emphasis::bold
, "bold"), "\x1b[1mbold\x1b[0m");
23 EXPECT_EQ(fmt::format(fmt::emphasis::faint
, "faint"), "\x1b[2mfaint\x1b[0m");
24 EXPECT_EQ(fmt::format(fmt::emphasis::italic
, "italic"),
25 "\x1b[3mitalic\x1b[0m");
26 EXPECT_EQ(fmt::format(fmt::emphasis::underline
, "underline"),
27 "\x1b[4munderline\x1b[0m");
28 EXPECT_EQ(fmt::format(fmt::emphasis::blink
, "blink"), "\x1b[5mblink\x1b[0m");
29 EXPECT_EQ(fmt::format(fmt::emphasis::reverse
, "reverse"),
30 "\x1b[7mreverse\x1b[0m");
31 EXPECT_EQ(fmt::format(fmt::emphasis::conceal
, "conceal"),
32 "\x1b[8mconceal\x1b[0m");
33 EXPECT_EQ(fmt::format(fmt::emphasis::strikethrough
, "strikethrough"),
34 "\x1b[9mstrikethrough\x1b[0m");
36 fmt::format(fg(fmt::color::blue
) | fmt::emphasis::bold
, "blue/bold"),
37 "\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m");
38 EXPECT_EQ(fmt::format(fmt::emphasis::bold
, "bold error"),
39 "\x1b[1mbold error\x1b[0m");
40 EXPECT_EQ(fmt::format(fg(fmt::color::blue
), "blue log"),
41 "\x1b[38;2;000;000;255mblue log\x1b[0m");
42 EXPECT_EQ(fmt::format(fmt::text_style(), "hi"), "hi");
43 EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red
), "tred"),
44 "\x1b[31mtred\x1b[0m");
45 EXPECT_EQ(fmt::format(bg(fmt::terminal_color::cyan
), "tcyan"),
46 "\x1b[46mtcyan\x1b[0m");
47 EXPECT_EQ(fmt::format(fg(fmt::terminal_color::bright_green
), "tbgreen"),
48 "\x1b[92mtbgreen\x1b[0m");
49 EXPECT_EQ(fmt::format(bg(fmt::terminal_color::bright_magenta
), "tbmagenta"),
50 "\x1b[105mtbmagenta\x1b[0m");
51 EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red
), "{}", "foo"),
52 "\x1b[31mfoo\x1b[0m");
53 EXPECT_EQ(fmt::format("{}{}", fmt::styled("red", fg(fmt::color::red
)),
54 fmt::styled("bold", fmt::emphasis::bold
)),
55 "\x1b[38;2;255;000;000mred\x1b[0m\x1b[1mbold\x1b[0m");
56 EXPECT_EQ(fmt::format("{}", fmt::styled("bar", fg(fmt::color::blue
) |
57 fmt::emphasis::underline
)),
58 "\x1b[4m\x1b[38;2;000;000;255mbar\x1b[0m");
61 TEST(color_test
, format_to
) {
62 auto out
= std::string();
63 fmt::format_to(std::back_inserter(out
), fg(fmt::rgb(255, 20, 30)),
64 "rgb(255,20,30){}{}{}", 1, 2, 3);
65 EXPECT_EQ(fmt::to_string(out
),
66 "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m");
69 TEST(color_test
, print
) {
70 EXPECT_WRITE(stdout
, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
71 "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");