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 #include "gtest-extra.h" 10 TEST(ColorsTest,ColorsPrint)11 TEST(ColorsTest, ColorsPrint) { 12 EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), 13 "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); 14 EXPECT_WRITE(stdout, fmt::print(fg(fmt::color::blue), "blue"), 15 "\x1b[38;2;000;000;255mblue\x1b[0m"); 16 EXPECT_WRITE( 17 stdout, 18 fmt::print(fg(fmt::color::blue) | bg(fmt::color::red), "two color"), 19 "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m"); 20 EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::bold, "bold"), 21 "\x1b[1mbold\x1b[0m"); 22 EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::italic, "italic"), 23 "\x1b[3mitalic\x1b[0m"); 24 EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::underline, "underline"), 25 "\x1b[4munderline\x1b[0m"); 26 EXPECT_WRITE(stdout, 27 fmt::print(fmt::emphasis::strikethrough, "strikethrough"), 28 "\x1b[9mstrikethrough\x1b[0m"); 29 EXPECT_WRITE( 30 stdout, 31 fmt::print(fg(fmt::color::blue) | fmt::emphasis::bold, "blue/bold"), 32 "\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m"); 33 EXPECT_WRITE(stderr, fmt::print(stderr, fmt::emphasis::bold, "bold error"), 34 "\x1b[1mbold error\x1b[0m"); 35 EXPECT_WRITE(stderr, fmt::print(stderr, fg(fmt::color::blue), "blue log"), 36 "\x1b[38;2;000;000;255mblue log\x1b[0m"); 37 EXPECT_WRITE(stdout, fmt::print(fmt::text_style(), "hi"), "hi"); 38 EXPECT_WRITE(stdout, fmt::print(fg(fmt::terminal_color::red), "tred"), 39 "\x1b[31mtred\x1b[0m"); 40 EXPECT_WRITE(stdout, fmt::print(bg(fmt::terminal_color::cyan), "tcyan"), 41 "\x1b[46mtcyan\x1b[0m"); 42 EXPECT_WRITE(stdout, 43 fmt::print(fg(fmt::terminal_color::bright_green), "tbgreen"), 44 "\x1b[92mtbgreen\x1b[0m"); 45 EXPECT_WRITE(stdout, 46 fmt::print(bg(fmt::terminal_color::bright_magenta), "tbmagenta"), 47 "\x1b[105mtbmagenta\x1b[0m"); 48 } 49 TEST(ColorsTest,Format)50 TEST(ColorsTest, Format) { 51 EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), 52 "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); 53 EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue"), 54 "\x1b[38;2;000;000;255mblue\x1b[0m"); 55 EXPECT_EQ( 56 fmt::format(fg(fmt::color::blue) | bg(fmt::color::red), "two color"), 57 "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m"); 58 EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold"), "\x1b[1mbold\x1b[0m"); 59 EXPECT_EQ(fmt::format(fmt::emphasis::italic, "italic"), 60 "\x1b[3mitalic\x1b[0m"); 61 EXPECT_EQ(fmt::format(fmt::emphasis::underline, "underline"), 62 "\x1b[4munderline\x1b[0m"); 63 EXPECT_EQ(fmt::format(fmt::emphasis::strikethrough, "strikethrough"), 64 "\x1b[9mstrikethrough\x1b[0m"); 65 EXPECT_EQ( 66 fmt::format(fg(fmt::color::blue) | fmt::emphasis::bold, "blue/bold"), 67 "\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m"); 68 EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold error"), 69 "\x1b[1mbold error\x1b[0m"); 70 EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue log"), 71 "\x1b[38;2;000;000;255mblue log\x1b[0m"); 72 EXPECT_EQ(fmt::format(fmt::text_style(), "hi"), "hi"); 73 EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "tred"), 74 "\x1b[31mtred\x1b[0m"); 75 EXPECT_EQ(fmt::format(bg(fmt::terminal_color::cyan), "tcyan"), 76 "\x1b[46mtcyan\x1b[0m"); 77 EXPECT_EQ(fmt::format(fg(fmt::terminal_color::bright_green), "tbgreen"), 78 "\x1b[92mtbgreen\x1b[0m"); 79 EXPECT_EQ(fmt::format(bg(fmt::terminal_color::bright_magenta), "tbmagenta"), 80 "\x1b[105mtbmagenta\x1b[0m"); 81 EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "{}", "foo"), 82 "\x1b[31mfoo\x1b[0m"); 83 } 84