2022-05-17 00:32:25 +00:00
|
|
|
|
// Formatting library for C++ - tests of formatters for standard library types
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
|
// All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
|
|
|
|
|
|
#include "fmt/std.h"
|
|
|
|
|
|
2023-08-06 18:36:30 +00:00
|
|
|
|
#include <bitset>
|
2023-03-18 14:07:06 +00:00
|
|
|
|
#include <stdexcept>
|
2022-06-26 14:28:01 +00:00
|
|
|
|
#include <string>
|
2022-07-03 18:06:54 +00:00
|
|
|
|
#include <vector>
|
2022-06-26 14:28:01 +00:00
|
|
|
|
|
2022-10-02 00:46:09 +00:00
|
|
|
|
#include "fmt/os.h" // fmt::system_category
|
2022-09-02 15:33:37 +00:00
|
|
|
|
#include "fmt/ranges.h"
|
2022-09-29 16:52:38 +00:00
|
|
|
|
#include "gtest-extra.h" // StartsWith
|
|
|
|
|
|
2022-07-10 13:26:23 +00:00
|
|
|
|
#ifdef __cpp_lib_filesystem
|
2023-04-30 16:09:35 +00:00
|
|
|
|
TEST(std_test, path) {
|
2023-07-22 13:56:52 +00:00
|
|
|
|
using std::filesystem::path;
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", path("/usr/bin")), "/usr/bin");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:?}", path("/usr/bin")), "\"/usr/bin\"");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:8}", path("foo")), "foo ");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
|
2023-11-30 15:34:26 +00:00
|
|
|
|
|
2023-11-29 22:49:36 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{:g}", path("/usr/bin")), "/usr/bin");
|
2023-11-30 15:34:26 +00:00
|
|
|
|
# ifdef _WIN32
|
2023-12-28 15:27:33 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", path("C:\\foo")), "C:\\foo");
|
2023-11-29 22:49:36 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo");
|
2022-05-31 14:58:49 +00:00
|
|
|
|
|
2023-11-30 15:34:26 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", path(L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
|
|
|
|
|
L"\x0447\x044B\x043D\x0430")),
|
2023-07-21 00:18:03 +00:00
|
|
|
|
"Шчучыншчына");
|
2023-07-22 13:56:52 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "<EFBFBD>");
|
2024-07-30 14:27:11 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", path(L"HEAD \xd800 TAIL")), "HEAD <20> TAIL");
|
2024-08-25 16:20:40 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", path(L"HEAD \xD83D\xDE00 TAIL")),
|
|
|
|
|
"HEAD \xF0\x9F\x98\x80 TAIL");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", path(L"HEAD \xD83D\xD83D\xDE00 TAIL")),
|
|
|
|
|
"HEAD <20>\xF0\x9F\x98\x80 TAIL");
|
2023-07-22 13:56:52 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\"");
|
2023-11-30 15:34:26 +00:00
|
|
|
|
# endif
|
2022-05-17 00:32:25 +00:00
|
|
|
|
}
|
2022-05-17 23:31:06 +00:00
|
|
|
|
|
2022-07-10 13:26:23 +00:00
|
|
|
|
// Test ambiguity problem described in #2954.
|
2023-04-30 16:09:35 +00:00
|
|
|
|
TEST(ranges_std_test, format_vector_path) {
|
2022-07-03 18:06:54 +00:00
|
|
|
|
auto p = std::filesystem::path("foo/bar.txt");
|
|
|
|
|
auto c = std::vector<std::string>{"abc", "def"};
|
|
|
|
|
EXPECT_EQ(fmt::format("path={}, range={}", p, c),
|
2023-07-21 00:18:03 +00:00
|
|
|
|
"path=foo/bar.txt, range=[\"abc\", \"def\"]");
|
2022-07-03 18:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 16:09:35 +00:00
|
|
|
|
// Test that path is not escaped twice in the debug mode.
|
2023-03-05 16:01:06 +00:00
|
|
|
|
TEST(ranges_std_test, format_quote_path) {
|
|
|
|
|
auto vec =
|
|
|
|
|
std::vector<std::filesystem::path>{"path1/file1.txt", "path2/file2.txt"};
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec),
|
|
|
|
|
"[\"path1/file1.txt\", \"path2/file2.txt\"]");
|
|
|
|
|
# ifdef __cpp_lib_optional
|
|
|
|
|
auto o = std::optional<std::filesystem::path>("path/file.txt");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", o), "optional(\"path/file.txt\")");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:?}", o), "optional(\"path/file.txt\")");
|
|
|
|
|
# endif
|
|
|
|
|
}
|
2023-04-30 16:09:35 +00:00
|
|
|
|
#endif
|
2023-03-05 16:01:06 +00:00
|
|
|
|
|
2022-05-17 23:31:06 +00:00
|
|
|
|
TEST(std_test, thread_id) {
|
|
|
|
|
EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
|
|
|
|
|
}
|
2022-06-26 14:28:01 +00:00
|
|
|
|
|
2024-03-09 07:27:50 +00:00
|
|
|
|
TEST(std_test, complex) {
|
2024-07-05 00:24:01 +00:00
|
|
|
|
using limits = std::numeric_limits<double>;
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, limits::quiet_NaN())),
|
|
|
|
|
"(1+nan i)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -limits::infinity())),
|
|
|
|
|
"(1-inf i)");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<int>(1, 2)), "(1+2i)");
|
|
|
|
|
|
2024-03-18 22:55:45 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, 2.2)), "(1+2.2i)");
|
2024-07-05 00:24:01 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -2.2)), "(1-2.2i)");
|
2024-03-20 20:31:32 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, 2.2)), "2.2i");
|
2024-07-05 00:24:01 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, -2.2)), "-2.2i");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, 2.2)), "+2.2i");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, -2.2)), "-2.2i");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, -2.2)), "(+1-2.2i)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, 2.2)), "(+1+2.2i)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)");
|
|
|
|
|
|
2024-03-09 07:27:50 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
|
2024-03-18 22:55:45 +00:00
|
|
|
|
" (1.00+2.20i)");
|
2024-07-05 00:24:01 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)),
|
|
|
|
|
"(1.00+2.20i) ");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, -2.2)),
|
|
|
|
|
"(1.00-2.20i) ");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:<{}.{}f}", std::complex<double>(1, -2.2), 20, 2),
|
|
|
|
|
"(1.00-2.20i) ");
|
2024-03-09 07:27:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 18:05:56 +00:00
|
|
|
|
#ifdef __cpp_lib_source_location
|
|
|
|
|
TEST(std_test, source_location) {
|
|
|
|
|
std::source_location loc = std::source_location::current();
|
2023-12-01 16:52:42 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", loc),
|
|
|
|
|
fmt::format("{}:{}:{}: {}", loc.file_name(), loc.line(),
|
|
|
|
|
loc.column(), loc.function_name()));
|
2023-11-30 18:05:56 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-02-25 14:45:56 +00:00
|
|
|
|
TEST(std_test, optional) {
|
|
|
|
|
#ifdef __cpp_lib_optional
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional<int>{}), "none");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::pair{1, "second"}), "(1, \"second\")");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::vector{std::optional{1}, std::optional{2},
|
|
|
|
|
std::optional{3}}),
|
|
|
|
|
"[optional(1), optional(2), optional(3)]");
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
fmt::format("{}", std::optional<std::optional<const char*>>{{"nested"}}),
|
|
|
|
|
"optional(optional(\"nested\"))");
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
fmt::format("{:<{}}", std::optional{std::string{"left aligned"}}, 30),
|
|
|
|
|
"optional(\"left aligned\" )");
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
fmt::format("{::d}", std::optional{std::vector{'h', 'e', 'l', 'l', 'o'}}),
|
|
|
|
|
"optional([104, 101, 108, 108, 111])");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional{std::string{"string"}}),
|
|
|
|
|
"optional(\"string\")");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional{'C'}), "optional(\'C\')");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:.{}f}", std::optional{3.14}, 1), "optional(3.1)");
|
|
|
|
|
|
|
|
|
|
struct unformattable {};
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::optional<unformattable>>::value));
|
|
|
|
|
EXPECT_TRUE((fmt::is_formattable<std::optional<int>>::value));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-03 17:06:15 +00:00
|
|
|
|
TEST(std_test, expected) {
|
|
|
|
|
#ifdef __cpp_lib_expected
|
2024-09-03 16:44:10 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::expected<void, int>{}), "expected()");
|
2024-02-03 17:06:15 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{1}), "expected(1)");
|
2024-03-13 08:40:03 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{std::unexpected(1)}),
|
|
|
|
|
"unexpected(1)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::expected<std::string, int>{"test"}),
|
|
|
|
|
"expected(\"test\")");
|
|
|
|
|
EXPECT_EQ(fmt::format(
|
|
|
|
|
"{}", std::expected<int, std::string>{std::unexpected("test")}),
|
|
|
|
|
"unexpected(\"test\")");
|
2024-02-03 17:06:15 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::expected<char, int>{'a'}), "expected('a')");
|
2024-03-13 08:40:03 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::expected<int, char>{std::unexpected('a')}),
|
|
|
|
|
"unexpected('a')");
|
2024-02-03 17:06:15 +00:00
|
|
|
|
|
|
|
|
|
struct unformattable1 {};
|
|
|
|
|
struct unformattable2 {};
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<unformattable1>::value));
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<unformattable2>::value));
|
2024-03-13 08:40:03 +00:00
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<
|
|
|
|
|
std::expected<unformattable1, unformattable2>>::value));
|
|
|
|
|
EXPECT_FALSE(
|
|
|
|
|
(fmt::is_formattable<std::expected<unformattable1, int>>::value));
|
|
|
|
|
EXPECT_FALSE(
|
|
|
|
|
(fmt::is_formattable<std::expected<int, unformattable2>>::value));
|
2024-02-03 17:06:15 +00:00
|
|
|
|
EXPECT_TRUE((fmt::is_formattable<std::expected<int, int>>::value));
|
2024-09-03 16:44:10 +00:00
|
|
|
|
EXPECT_TRUE((fmt::is_formattable<std::expected<void, int>>::value));
|
2024-02-03 17:06:15 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 23:02:33 +00:00
|
|
|
|
namespace my_nso {
|
|
|
|
|
enum class my_number {
|
|
|
|
|
one,
|
|
|
|
|
two,
|
|
|
|
|
};
|
|
|
|
|
auto format_as(my_number number) -> fmt::string_view {
|
|
|
|
|
return number == my_number::one ? "first" : "second";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class my_class {
|
|
|
|
|
public:
|
|
|
|
|
int av;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend auto format_as(const my_class& elm) -> std::string {
|
|
|
|
|
return fmt::to_string(elm.av);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} // namespace my_nso
|
|
|
|
|
TEST(std_test, optional_format_as) {
|
|
|
|
|
#ifdef __cpp_lib_optional
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional<my_nso::my_number>{}), "none");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional{my_nso::my_number::one}),
|
|
|
|
|
"optional(\"first\")");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional<my_nso::my_class>{}), "none");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::optional{my_nso::my_class{7}}),
|
|
|
|
|
"optional(\"7\")");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 14:07:06 +00:00
|
|
|
|
struct throws_on_move {
|
|
|
|
|
throws_on_move() = default;
|
|
|
|
|
|
|
|
|
|
[[noreturn]] throws_on_move(throws_on_move&&) {
|
|
|
|
|
throw std::runtime_error("Thrown by throws_on_move");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throws_on_move(const throws_on_move&) = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace fmt {
|
|
|
|
|
template <> struct formatter<throws_on_move> : formatter<string_view> {
|
|
|
|
|
auto format(const throws_on_move&, format_context& ctx) const
|
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
|
string_view str("<throws_on_move>");
|
|
|
|
|
return formatter<string_view>::format(str, ctx);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} // namespace fmt
|
|
|
|
|
|
2022-06-26 14:28:01 +00:00
|
|
|
|
TEST(std_test, variant) {
|
|
|
|
|
#ifdef __cpp_lib_variant
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
|
|
|
|
|
using V0 = std::variant<int, float, std::string, char>;
|
|
|
|
|
V0 v0(42);
|
|
|
|
|
V0 v1(1.5f);
|
|
|
|
|
V0 v2("hello");
|
|
|
|
|
V0 v3('i');
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v0), "variant(42)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v1), "variant(1.5)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v2), "variant(\"hello\")");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v3), "variant('i')");
|
|
|
|
|
|
|
|
|
|
struct unformattable {};
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable>>::value));
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable, int>>::value));
|
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::variant<int, unformattable>>::value));
|
|
|
|
|
EXPECT_FALSE(
|
|
|
|
|
(fmt::is_formattable<std::variant<unformattable, unformattable>>::value));
|
|
|
|
|
EXPECT_TRUE((fmt::is_formattable<std::variant<int, float>>::value));
|
|
|
|
|
|
|
|
|
|
using V1 = std::variant<std::monostate, std::string, std::string>;
|
|
|
|
|
V1 v4{};
|
|
|
|
|
V1 v5{std::in_place_index<1>, "yes, this is variant"};
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v4), "variant(monostate)");
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v5), "variant(\"yes, this is variant\")");
|
2022-09-03 04:08:07 +00:00
|
|
|
|
|
|
|
|
|
volatile int i = 42; // Test compile error before GCC 11 described in #3068.
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", i), "42");
|
2023-03-18 14:07:06 +00:00
|
|
|
|
|
|
|
|
|
std::variant<std::monostate, throws_on_move> v6;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
throws_on_move thrower;
|
|
|
|
|
v6.emplace<throws_on_move>(std::move(thrower));
|
|
|
|
|
} catch (const std::runtime_error&) {
|
|
|
|
|
}
|
|
|
|
|
// v6 is now valueless by exception
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", v6), "variant(valueless by exception)");
|
|
|
|
|
|
2022-06-26 14:28:01 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2022-09-02 15:33:37 +00:00
|
|
|
|
|
2022-10-02 00:46:09 +00:00
|
|
|
|
TEST(std_test, error_code) {
|
2024-09-21 14:39:24 +00:00
|
|
|
|
auto& generic = std::generic_category();
|
2022-10-02 00:46:09 +00:00
|
|
|
|
EXPECT_EQ("generic:42",
|
2024-09-21 14:39:24 +00:00
|
|
|
|
fmt::format(FMT_STRING("{0}"), std::error_code(42, generic)));
|
|
|
|
|
EXPECT_EQ(" generic:42",
|
|
|
|
|
fmt::format(FMT_STRING("{:>12}"), std::error_code(42, generic)));
|
|
|
|
|
EXPECT_EQ("generic:42 ",
|
|
|
|
|
fmt::format(FMT_STRING("{:12}"), std::error_code(42, generic)));
|
2022-10-02 00:46:09 +00:00
|
|
|
|
EXPECT_EQ("system:42",
|
|
|
|
|
fmt::format(FMT_STRING("{0}"),
|
|
|
|
|
std::error_code(42, fmt::system_category())));
|
|
|
|
|
EXPECT_EQ("system:-42",
|
|
|
|
|
fmt::format(FMT_STRING("{0}"),
|
|
|
|
|
std::error_code(-42, fmt::system_category())));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-10 15:04:00 +00:00
|
|
|
|
template <typename Catch> void exception_test() {
|
2022-09-02 15:33:37 +00:00
|
|
|
|
try {
|
2022-09-10 15:04:00 +00:00
|
|
|
|
throw std::runtime_error("Test Exception");
|
|
|
|
|
} catch (const Catch& ex) {
|
|
|
|
|
EXPECT_EQ("Test Exception", fmt::format("{}", ex));
|
|
|
|
|
EXPECT_EQ("std::runtime_error: Test Exception", fmt::format("{:t}", ex));
|
2022-09-02 15:33:37 +00:00
|
|
|
|
}
|
2022-09-10 15:04:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace my_ns1 {
|
|
|
|
|
namespace my_ns2 {
|
|
|
|
|
struct my_exception : public std::exception {
|
|
|
|
|
private:
|
|
|
|
|
std::string msg;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
my_exception(const std::string& s) : msg(s) {}
|
|
|
|
|
const char* what() const noexcept override;
|
|
|
|
|
};
|
|
|
|
|
const char* my_exception::what() const noexcept { return msg.c_str(); }
|
|
|
|
|
} // namespace my_ns2
|
|
|
|
|
} // namespace my_ns1
|
|
|
|
|
|
|
|
|
|
TEST(std_test, exception) {
|
2023-07-22 13:56:52 +00:00
|
|
|
|
using testing::StartsWith;
|
2022-09-10 15:04:00 +00:00
|
|
|
|
exception_test<std::exception>();
|
|
|
|
|
exception_test<std::runtime_error>();
|
2022-09-02 15:33:37 +00:00
|
|
|
|
|
|
|
|
|
try {
|
2022-09-10 15:04:00 +00:00
|
|
|
|
using namespace my_ns1::my_ns2;
|
|
|
|
|
throw my_exception("My Exception");
|
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
EXPECT_EQ("my_ns1::my_ns2::my_exception: My Exception",
|
|
|
|
|
fmt::format("{:t}", ex));
|
|
|
|
|
EXPECT_EQ("My Exception", fmt::format("{:}", ex));
|
2022-09-02 15:33:37 +00:00
|
|
|
|
}
|
2022-09-29 16:52:38 +00:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
throw std::system_error(std::error_code(), "message");
|
|
|
|
|
} catch (const std::system_error& ex) {
|
|
|
|
|
EXPECT_THAT(fmt::format("{:t}", ex), StartsWith("std::system_error: "));
|
|
|
|
|
}
|
2023-05-06 14:49:46 +00:00
|
|
|
|
|
|
|
|
|
#ifdef __cpp_lib_filesystem
|
|
|
|
|
// Tests that the inline namespace is stripped out, e.g.
|
|
|
|
|
// std::filesystem::__cxx11::* -> std::filesystem::*.
|
|
|
|
|
try {
|
|
|
|
|
throw std::filesystem::filesystem_error("message", std::error_code());
|
|
|
|
|
} catch (const std::filesystem::filesystem_error& ex) {
|
|
|
|
|
EXPECT_THAT(fmt::format("{:t}", ex),
|
|
|
|
|
StartsWith("std::filesystem::filesystem_error: "));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-09-02 15:33:37 +00:00
|
|
|
|
}
|
2023-08-06 18:36:30 +00:00
|
|
|
|
|
2024-05-28 17:57:08 +00:00
|
|
|
|
#if FMT_USE_RTTI
|
|
|
|
|
TEST(std_test, type_info) {
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", typeid(std::runtime_error)),
|
|
|
|
|
"std::runtime_error");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-08-06 18:36:30 +00:00
|
|
|
|
TEST(std_test, format_bit_reference) {
|
|
|
|
|
std::bitset<2> bs(1);
|
|
|
|
|
EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
|
|
|
|
|
std::vector<bool> v = {true, false};
|
|
|
|
|
EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(std_test, format_const_bit_reference) {
|
|
|
|
|
const std::bitset<2> bs(1);
|
|
|
|
|
EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
|
|
|
|
|
const std::vector<bool> v = {true, false};
|
|
|
|
|
EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
|
|
|
|
|
}
|
2023-08-14 15:34:31 +00:00
|
|
|
|
|
2023-10-03 16:53:47 +00:00
|
|
|
|
TEST(std_test, format_bitset) {
|
2023-10-08 14:30:08 +00:00
|
|
|
|
auto bs = std::bitset<6>(42);
|
2023-10-03 16:53:47 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", bs), "101010");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:0>8}", bs), "00101010");
|
|
|
|
|
EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 15:34:31 +00:00
|
|
|
|
TEST(std_test, format_atomic) {
|
|
|
|
|
std::atomic<bool> b(false);
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", b), "false");
|
|
|
|
|
|
|
|
|
|
const std::atomic<bool> cb(true);
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", cb), "true");
|
|
|
|
|
}
|
2023-08-25 15:06:10 +00:00
|
|
|
|
|
|
|
|
|
#ifdef __cpp_lib_atomic_flag_test
|
|
|
|
|
TEST(std_test, format_atomic_flag) {
|
|
|
|
|
std::atomic_flag f = ATOMIC_FLAG_INIT;
|
2023-11-30 15:34:26 +00:00
|
|
|
|
(void)f.test_and_set();
|
2023-08-25 15:06:10 +00:00
|
|
|
|
EXPECT_EQ(fmt::format("{}", f), "true");
|
|
|
|
|
|
|
|
|
|
const std::atomic_flag cf = ATOMIC_FLAG_INIT;
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", cf), "false");
|
|
|
|
|
}
|
2023-11-30 15:34:26 +00:00
|
|
|
|
#endif // __cpp_lib_atomic_flag_test
|
2024-01-11 03:34:31 +00:00
|
|
|
|
|
|
|
|
|
TEST(std_test, format_unique_ptr) {
|
|
|
|
|
std::unique_ptr<int> up(new int(1));
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::ptr(up.get())),
|
|
|
|
|
fmt::format("{}", fmt::ptr(up)));
|
|
|
|
|
struct custom_deleter {
|
|
|
|
|
void operator()(int* p) const { delete p; }
|
|
|
|
|
};
|
|
|
|
|
std::unique_ptr<int, custom_deleter> upcd(new int(1));
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::ptr(upcd.get())),
|
|
|
|
|
fmt::format("{}", fmt::ptr(upcd)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(std_test, format_shared_ptr) {
|
|
|
|
|
std::shared_ptr<int> sp(new int(1));
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())),
|
|
|
|
|
fmt::format("{}", fmt::ptr(sp)));
|
|
|
|
|
}
|
2024-09-16 18:37:19 +00:00
|
|
|
|
|
|
|
|
|
TEST(std_test, format_reference_wrapper) {
|
|
|
|
|
int num = 35;
|
|
|
|
|
EXPECT_EQ("35", fmt::to_string(std::cref(num)));
|
|
|
|
|
}
|