• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Formatting library for C++ - color tests
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #include "fmt/color.h"
9 
10 #include <iterator>
11 #include <string>
12 #include <utility>
13 
14 #include "gtest-extra.h"
15 
TEST(ColorsTest,ColorsPrint)16 TEST(ColorsTest, ColorsPrint) {
17   EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
18                "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
19   EXPECT_WRITE(stdout, fmt::print(fg(fmt::color::blue), "blue"),
20                "\x1b[38;2;000;000;255mblue\x1b[0m");
21   EXPECT_WRITE(
22       stdout,
23       fmt::print(fg(fmt::color::blue) | bg(fmt::color::red), "two color"),
24       "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m");
25   EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::bold, "bold"),
26                "\x1b[1mbold\x1b[0m");
27   EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::italic, "italic"),
28                "\x1b[3mitalic\x1b[0m");
29   EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::underline, "underline"),
30                "\x1b[4munderline\x1b[0m");
31   EXPECT_WRITE(stdout,
32                fmt::print(fmt::emphasis::strikethrough, "strikethrough"),
33                "\x1b[9mstrikethrough\x1b[0m");
34   EXPECT_WRITE(
35       stdout,
36       fmt::print(fg(fmt::color::blue) | fmt::emphasis::bold, "blue/bold"),
37       "\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m");
38   EXPECT_WRITE(stderr, fmt::print(stderr, fmt::emphasis::bold, "bold error"),
39                "\x1b[1mbold error\x1b[0m");
40   EXPECT_WRITE(stderr, fmt::print(stderr, fg(fmt::color::blue), "blue log"),
41                "\x1b[38;2;000;000;255mblue log\x1b[0m");
42   EXPECT_WRITE(stdout, fmt::print(fmt::text_style(), "hi"), "hi");
43   EXPECT_WRITE(stdout, fmt::print(fg(fmt::terminal_color::red), "tred"),
44                "\x1b[31mtred\x1b[0m");
45   EXPECT_WRITE(stdout, fmt::print(bg(fmt::terminal_color::cyan), "tcyan"),
46                "\x1b[46mtcyan\x1b[0m");
47   EXPECT_WRITE(stdout,
48                fmt::print(fg(fmt::terminal_color::bright_green), "tbgreen"),
49                "\x1b[92mtbgreen\x1b[0m");
50   EXPECT_WRITE(stdout,
51                fmt::print(bg(fmt::terminal_color::bright_magenta), "tbmagenta"),
52                "\x1b[105mtbmagenta\x1b[0m");
53 }
54 
TEST(ColorsTest,Format)55 TEST(ColorsTest, Format) {
56   EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
57             "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
58   EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"),
59             L"\x1b[38;2;255;020;030mrgb(255,20,30) wide\x1b[0m");
60   EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue"),
61             "\x1b[38;2;000;000;255mblue\x1b[0m");
62   EXPECT_EQ(
63       fmt::format(fg(fmt::color::blue) | bg(fmt::color::red), "two color"),
64       "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m");
65   EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold"), "\x1b[1mbold\x1b[0m");
66   EXPECT_EQ(fmt::format(fmt::emphasis::italic, "italic"),
67             "\x1b[3mitalic\x1b[0m");
68   EXPECT_EQ(fmt::format(fmt::emphasis::underline, "underline"),
69             "\x1b[4munderline\x1b[0m");
70   EXPECT_EQ(fmt::format(fmt::emphasis::strikethrough, "strikethrough"),
71             "\x1b[9mstrikethrough\x1b[0m");
72   EXPECT_EQ(
73       fmt::format(fg(fmt::color::blue) | fmt::emphasis::bold, "blue/bold"),
74       "\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m");
75   EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold error"),
76             "\x1b[1mbold error\x1b[0m");
77   EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue log"),
78             "\x1b[38;2;000;000;255mblue log\x1b[0m");
79   EXPECT_EQ(fmt::format(fmt::text_style(), "hi"), "hi");
80   EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "tred"),
81             "\x1b[31mtred\x1b[0m");
82   EXPECT_EQ(fmt::format(bg(fmt::terminal_color::cyan), "tcyan"),
83             "\x1b[46mtcyan\x1b[0m");
84   EXPECT_EQ(fmt::format(fg(fmt::terminal_color::bright_green), "tbgreen"),
85             "\x1b[92mtbgreen\x1b[0m");
86   EXPECT_EQ(fmt::format(bg(fmt::terminal_color::bright_magenta), "tbmagenta"),
87             "\x1b[105mtbmagenta\x1b[0m");
88   EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "{}", "foo"),
89             "\x1b[31mfoo\x1b[0m");
90 }
91 
TEST(ColorsTest,FormatToOutAcceptsTextStyle)92 TEST(ColorsTest, FormatToOutAcceptsTextStyle) {
93   fmt::text_style ts = fg(fmt::rgb(255, 20, 30));
94   std::string out;
95   fmt::format_to(std::back_inserter(out), ts, "rgb(255,20,30){}{}{}", 1, 2, 3);
96 
97   EXPECT_EQ(fmt::to_string(out),
98             "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m");
99 }
100