color-test.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "fmt/color.h"
  8. #include <iterator> // std::back_inserter
  9. #include "gtest-extra.h" // EXPECT_WRITE
  10. TEST(color_test, format) {
  11. EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
  12. "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
  13. EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue"),
  14. "\x1b[38;2;000;000;255mblue\x1b[0m");
  15. EXPECT_EQ(
  16. fmt::format(fg(fmt::color::blue) | bg(fmt::color::red), "two color"),
  17. "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m");
  18. EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold"), "\x1b[1mbold\x1b[0m");
  19. EXPECT_EQ(fmt::format(fmt::emphasis::faint, "faint"), "\x1b[2mfaint\x1b[0m");
  20. EXPECT_EQ(fmt::format(fmt::emphasis::italic, "italic"),
  21. "\x1b[3mitalic\x1b[0m");
  22. EXPECT_EQ(fmt::format(fmt::emphasis::underline, "underline"),
  23. "\x1b[4munderline\x1b[0m");
  24. EXPECT_EQ(fmt::format(fmt::emphasis::blink, "blink"), "\x1b[5mblink\x1b[0m");
  25. EXPECT_EQ(fmt::format(fmt::emphasis::reverse, "reverse"),
  26. "\x1b[7mreverse\x1b[0m");
  27. EXPECT_EQ(fmt::format(fmt::emphasis::conceal, "conceal"),
  28. "\x1b[8mconceal\x1b[0m");
  29. EXPECT_EQ(fmt::format(fmt::emphasis::strikethrough, "strikethrough"),
  30. "\x1b[9mstrikethrough\x1b[0m");
  31. EXPECT_EQ(
  32. fmt::format(fg(fmt::color::blue) | fmt::emphasis::bold, "blue/bold"),
  33. "\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m");
  34. EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold error"),
  35. "\x1b[1mbold error\x1b[0m");
  36. EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue log"),
  37. "\x1b[38;2;000;000;255mblue log\x1b[0m");
  38. EXPECT_EQ(fmt::format(fmt::text_style(), "hi"), "hi");
  39. EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "tred"),
  40. "\x1b[31mtred\x1b[0m");
  41. EXPECT_EQ(fmt::format(bg(fmt::terminal_color::cyan), "tcyan"),
  42. "\x1b[46mtcyan\x1b[0m");
  43. EXPECT_EQ(fmt::format(fg(fmt::terminal_color::bright_green), "tbgreen"),
  44. "\x1b[92mtbgreen\x1b[0m");
  45. EXPECT_EQ(fmt::format(bg(fmt::terminal_color::bright_magenta), "tbmagenta"),
  46. "\x1b[105mtbmagenta\x1b[0m");
  47. EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "{}", "foo"),
  48. "\x1b[31mfoo\x1b[0m");
  49. }
  50. TEST(color_test, format_to) {
  51. auto out = std::string();
  52. fmt::format_to(std::back_inserter(out), fg(fmt::rgb(255, 20, 30)),
  53. "rgb(255,20,30){}{}{}", 1, 2, 3);
  54. EXPECT_EQ(fmt::to_string(out),
  55. "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m");
  56. }
  57. TEST(color_test, print) {
  58. EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
  59. "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
  60. }