std-test.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Formatting library for C++ - tests of formatters for standard library types
  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/std.h"
  8. #include "fmt/ranges.h"
  9. #include <string>
  10. #include <vector>
  11. #include "gtest/gtest.h"
  12. TEST(std_test, path) {
  13. #ifdef __cpp_lib_filesystem
  14. EXPECT_EQ(fmt::format("{:8}", std::filesystem::path("foo")), "\"foo\" ");
  15. EXPECT_EQ(fmt::format("{}", std::filesystem::path("foo\"bar.txt")),
  16. "\"foo\\\"bar.txt\"");
  17. # ifdef _WIN32
  18. // File.txt in Russian.
  19. const wchar_t unicode_path[] = {0x424, 0x430, 0x439, 0x43b, 0x2e,
  20. 0x74, 0x78, 0x74, 0};
  21. const char unicode_u8path[] = {'"', char(0xd0), char(0xa4), char(0xd0),
  22. char(0xb0), char(0xd0), char(0xb9), char(0xd0),
  23. char(0xbb), '.', 't', 'x',
  24. 't', '"', '\0'};
  25. EXPECT_EQ(fmt::format("{}", std::filesystem::path(unicode_path)),
  26. unicode_u8path);
  27. # endif
  28. #endif
  29. }
  30. TEST(ranges_std_test, format_vector_path) {
  31. // Test ambiguity problem described in #2954.
  32. #ifdef __cpp_lib_filesystem
  33. auto p = std::filesystem::path("foo/bar.txt");
  34. auto c = std::vector<std::string>{"abc", "def"};
  35. EXPECT_EQ(fmt::format("path={}, range={}", p, c),
  36. "path=\"foo/bar.txt\", range=[\"abc\", \"def\"]");
  37. #endif
  38. }
  39. TEST(std_test, thread_id) {
  40. EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
  41. }
  42. TEST(std_test, variant) {
  43. #ifdef __cpp_lib_variant
  44. EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
  45. using V0 = std::variant<int, float, std::string, char>;
  46. V0 v0(42);
  47. V0 v1(1.5f);
  48. V0 v2("hello");
  49. V0 v3('i');
  50. EXPECT_EQ(fmt::format("{}", v0), "variant(42)");
  51. EXPECT_EQ(fmt::format("{}", v1), "variant(1.5)");
  52. EXPECT_EQ(fmt::format("{}", v2), "variant(\"hello\")");
  53. EXPECT_EQ(fmt::format("{}", v3), "variant('i')");
  54. struct unformattable {};
  55. EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
  56. EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable>>::value));
  57. EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable, int>>::value));
  58. EXPECT_FALSE((fmt::is_formattable<std::variant<int, unformattable>>::value));
  59. EXPECT_FALSE(
  60. (fmt::is_formattable<std::variant<unformattable, unformattable>>::value));
  61. EXPECT_TRUE((fmt::is_formattable<std::variant<int, float>>::value));
  62. using V1 = std::variant<std::monostate, std::string, std::string>;
  63. V1 v4{};
  64. V1 v5{std::in_place_index<1>, "yes, this is variant"};
  65. EXPECT_EQ(fmt::format("{}", v4), "variant(monostate)");
  66. EXPECT_EQ(fmt::format("{}", v5), "variant(\"yes, this is variant\")");
  67. #endif
  68. }