353bd895a Add FMT_EXPORT on ranges.h customization points (#4476) 953cffa70 Replace memset with constexpr fill_n in bigint::align (#4471) 571c02d47 Add xchar support for std::byte formatter (#4480) f4345467f Fix compilation on clang-21 / libc++-21 (#4477) 1ef834807 Properly constrain `detail::copy` optimization (#4474) a5dccffa5 Add double and float support to scan test 4a149f513 Test non-SSO constexpr string formatting 067bc479b Avoid redundant work when processing UTF-8 strings (#4475) 730fd4d9a Remove redundant tests 5860688d7 Enable constexpr support for fmt::format (fmtlib#3403) (#4456) 46be88bc1 Cleanup FP formatting cc8891490 Export fmt::dynamic_format_arg_store in fmt module (#4459) fc0c76a07 Handle large precision 6332a3852 Bump ossf/scorecard-action from 2.4.0 to 2.4.2 (#4462) 02de29e00 Remove a reference to a compromised account 6d51c78c1 Cleanup FP formatting 0f4e9d0bd Cleanup FP formatting d9d50495a Optimize the default FP formatting befbc5fdb Fix ADL lookup for memory_buffer 8aa1d6a9f Minor cleanup 6d79757a3 Interpret precision as display width (#4443) 1ff0b7f5e Cleanup warning suppression ea985e84f Remove some implicit conversions (#4447) f7033da09 Avoid include locale inline if C++20 modules are enabled (#4451) b723c021d Give useful error when misusing fmt::ptr. (#4453) 3ba3c390f Clarify that formatting of pointers is disallowed ab161a71c Fix some typos in comments (#4448) b5266fd3b Remove some redundant `const`s (#4445) 9b0ebd443 Cleanup base-test 7af94e559 Remove old gcc workaround 2924fcf8f Cleanup base-test 102752ad4 Update docs a6cd72c9e Cleanup base-test 07885271a Minor cleanup 4999416e5 Fix reference_wrapper ambiguity with format_as (#4434) 55a8f6a4b Change component prefix for NSIS compatibility (#4442) eb9a95d42 Clarify that formatting of pointers is disallowed d5c33e4f4 Make template parameter order consistent a2225f288 Remove unused include b43b2f953 Cleanup standard formatters 1312b4a16 Cleanup standard formatters 4404dc05d Consolidate implementation details 7bb6fcb32 Bump version 59259a5fd Make a doc directory if it doesn't exist 542ea7c40 Clarify that Formatter parameter is deprecated 40626af88 Update version 7fdd6846b Bump version 6caff7ed9 Cleanup test 71a548387 Update changelog 448929d49 Update and apply clang-format 26d87edab Bump github/codeql-action from 3.28.13 to 3.28.16 (#4432) 505ee058f Update changelog ccab41719 Update changelog ec1349d34 Update changelog 0ed2a65a8 Clarify why we use __builtin_strlen instead of strlen e22c94307 Update changelog b252bad3c Update changelog 268083123 Cleanun string_view 8978ab09b Avoiding __builtin_strlen (#4429) c936e2e44 Implement debug format for error_code a7d7b894c Implement the s specifier for error_code e98155a6f Remove redundant specializations 41b3bed4d Clarify why we don't use qualified names 67d9e4932 Update changelog 9db5e4df2 Don't specialize std::is_floating_point 906eaf2dd Make specifier order consistent 9f6c12c3d Remove deprecated localtime from docs 2d0518b5f Fix cmake error in pedantic mode (#4426) c81cbed2b Simplify test c7925241c Remove `core.h` from README (#4422) c70913835 Add support for incomplete types db405954c Remove `fmt/core.h` from docs (#4421) 0a917ee2f Minor comment tweak 969d4aef6 Update doc image 8061c7c8c Cleanup duration formatter 7b59df411 Remove redundant member b8192d233 Fix build error with MSVC v141 (#4413) e814b5fab Reduce template parametrization ed0d216f7 Fix localization and formatting of timezone names bd9554a29 Fix formatting of timezone names f086dc0d2 Fix timezone handling in tm f10b6dd81 Improve chrono formatting f470b9c56 Cleanup chrono tests and set consistent TZ b28214487 Fix handling of %Z 6d69f0c5f Improve chorno tests da776c9a6 Test timezone 64db979e3 Added a missing FMT_STRING in fmt::println() (#4407) 5f2e61fdd Cleanup chrono detail b3d45e1d3 Remove fmt_detail 5f6fb96df Bump github/codeql-action from 3.28.8 to 3.28.13 (#4403) 5199e0f88 Fix a flush issue on libstdc++ 2f5843057 Move buffering tests to os-test d5d32c1e8 Bazel support: Update platforms to 0.0.11 (#4400) 204661287 Improve local_time test e1ab38336 Report an error when timezone is not available b9e0e94a0 Enable more chrono tests on Windows a81842428 Update changelog f53055efe Revert "Workaround an ABI issue in spdlog" b2dfcb2b8 Fix local_time test 7ac97cbd1 Enable some local_time tests and make them deterministic 17898794a Use fmt::local_time 443a8ef34 Deprecate fmt::localtime 3607e92dc Bump version 43e31614c Test ambiguous time 989826ce5 Update changelog 9d6e24c64 Fix handling of long with FMT_BUILTIN_TYPES=0 0843317e0 Update changelog 784eac839 Workaround an ABI issue in spdlog 6fdf225a3 Always inline value ctors in optimized gcc mode only 332da79bf Always inline value ctors 7b273fbb5 Minor cleanup 191c504b1 Cleanup build config d13fb6092 Cleanup build config dd780fde4 Add clang-3.4 37e647471 Fix dynamic named arg format spec handling (#4361) 77c0fc07d Switch to supported ubuntu image 9212ff6ca Apply coding conventions and use constexpr 864bdf963 Report error on duplicate named arg names (#4367) b776cf66f Optimize `text_style` using bit packing (#4363) bdbf957b9 Bump msys2/setup-msys2 from 2.25.0 to 2.27.0 577fd3be8 Fix TU-local entity exposition error in GCC 15 faac8b1fa Remove exports in std.h 123913715 Update version 8c1059b92 Update changelog 4e5aafbf4 Bump version db30fb3b8 Update changelog 3401ce2be Fix ABI compatibility 7f7695524 Fix conflict with std::ignore (#4356) 251320fcb Add .vs folder to .gitignore (#4355) 94ab51cb8 Simplify implementation of `operator""_cf` (#4349) 0ca42e836 Workaround an MSVC v140 bug ed27df576 Replace forward slashes by backslashes in BMI path for MSVC. (#4344) d42a068db Apply coding conventions f2cec917d Move is_compiled_string to public API (#4342) d5b866e24 fix gcc 8.3 compile errors (#4336) 5676e408f Bump github/codeql-action from 3.27.0 to 3.28.8 (#4337) 71d24b564 Bump actions/upload-artifact from 4.4.0 to 4.6.0 (#4339) c9267da4d Fix typo in `FMT_HAS_BUILTIN` check 373855c1b Clarify difference in FP representation 52eeeb52a Make exponent threshold depend on representation (#3649) 9cf9f38ed Update version 4946bdb72 Update changelog 01a5b56f0 Fix error of unitialized variable FMT_HEADERS cb6fdf219 Restore constraint on map formatter (#4326) f841ae61e Fix #4303: avoid instantiating formatter<const T> (#4325) a3d05d70c Silence a constexpr warning when compiling with MSVC and /W4 (#4322) 41539c29f Workaround a bug in gcc 6 (#4318) aabe63910 Tweak changelog f90090be2 Update changelog 9ff9c695d Bump version 06ad1224e Update changelog 5f0572acd Workaround a compilation error on gcc 9.4 898d43857 Fix formatting into std::ostreambuf_iterator using a compiled format (#4312) 937b7c5c1 Add args() accessor back to fmt::format_context (#4310) 01914f038 Reduce size of basic_specs c43da3570 Workaround an ICE when using modules with gcc 14.2 and earlier 8303d140a Update version b0b3dc5ff Bump version 586ea06f0 Rename set_fill to copy_fill_from 5750f434f Update changelog bfbdc2be9 Add parameter to the fallback to_sys function. 87e007267 Update changelog d57040f94 Prefix components 21aa0956d Restore ABI compatibility 3f864a450 Address MSVC C4127 warning when formatting non unicode `tm` (#4299) git-subtree-dir: external/fmt git-subtree-split: 353bd895a2bf9d0b1bc5977dc002fb6e0cdb0960
444 lines
15 KiB
C++
444 lines
15 KiB
C++
// 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"
|
||
|
||
#include <bitset>
|
||
#include <stdexcept>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "fmt/os.h" // fmt::system_category
|
||
#include "gtest-extra.h" // StartsWith
|
||
|
||
#ifdef __cpp_lib_filesystem
|
||
TEST(std_test, path) {
|
||
using std::filesystem::path;
|
||
EXPECT_EQ(fmt::format("{}", path("/usr/bin")), "/usr/bin");
|
||
|
||
// see #4303
|
||
const path p = "/usr/bin";
|
||
EXPECT_EQ(fmt::format("{}", p), "/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\"");
|
||
|
||
EXPECT_EQ(fmt::format("{:g}", path("/usr/bin")), "/usr/bin");
|
||
# ifdef _WIN32
|
||
EXPECT_EQ(fmt::format("{}", path("C:\\foo")), "C:\\foo");
|
||
EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo");
|
||
|
||
EXPECT_EQ(fmt::format("{}", path(L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
|
||
L"\x0447\x044B\x043D\x0430")),
|
||
"Шчучыншчына");
|
||
EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "<EFBFBD>");
|
||
EXPECT_EQ(fmt::format("{}", path(L"HEAD \xd800 TAIL")), "HEAD <20> TAIL");
|
||
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");
|
||
EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\"");
|
||
# endif
|
||
}
|
||
|
||
// Intentionally delayed include to test #4303
|
||
# include "fmt/ranges.h"
|
||
|
||
// Test ambiguity problem described in #2954.
|
||
TEST(ranges_std_test, format_vector_path) {
|
||
auto p = std::filesystem::path("foo/bar.txt");
|
||
auto c = std::vector<std::string>{"abc", "def"};
|
||
EXPECT_EQ(fmt::format("path={}, range={}", p, c),
|
||
"path=foo/bar.txt, range=[\"abc\", \"def\"]");
|
||
}
|
||
|
||
// Test that path is not escaped twice in the debug mode.
|
||
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
|
||
}
|
||
#endif
|
||
|
||
TEST(std_test, thread_id) {
|
||
EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
|
||
}
|
||
|
||
TEST(std_test, complex) {
|
||
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)");
|
||
|
||
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>(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>(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)");
|
||
|
||
EXPECT_EQ(fmt::format("{:8}", std::complex<double>(1, 2)), "(1+2i) ");
|
||
EXPECT_EQ(fmt::format("{:-<8}", std::complex<double>(1, 2)), "(1+2i)--");
|
||
|
||
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("{:<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) ");
|
||
}
|
||
|
||
#ifdef __cpp_lib_source_location
|
||
TEST(std_test, source_location) {
|
||
std::source_location loc = std::source_location::current();
|
||
EXPECT_EQ(fmt::format("{}", loc),
|
||
fmt::format("{}:{}:{}: {}", loc.file_name(), loc.line(),
|
||
loc.column(), loc.function_name()));
|
||
}
|
||
#endif
|
||
|
||
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
|
||
}
|
||
|
||
TEST(std_test, expected) {
|
||
#ifdef __cpp_lib_expected
|
||
EXPECT_EQ(fmt::format("{}", std::expected<void, int>{}), "expected()");
|
||
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{1}), "expected(1)");
|
||
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\")");
|
||
EXPECT_EQ(fmt::format("{}", std::expected<char, int>{'a'}), "expected('a')");
|
||
EXPECT_EQ(fmt::format("{}", std::expected<int, char>{std::unexpected('a')}),
|
||
"unexpected('a')");
|
||
|
||
struct unformattable1 {};
|
||
struct unformattable2 {};
|
||
EXPECT_FALSE((fmt::is_formattable<unformattable1>::value));
|
||
EXPECT_FALSE((fmt::is_formattable<unformattable2>::value));
|
||
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));
|
||
EXPECT_TRUE((fmt::is_formattable<std::expected<int, int>>::value));
|
||
EXPECT_TRUE((fmt::is_formattable<std::expected<void, int>>::value));
|
||
#endif
|
||
}
|
||
|
||
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
|
||
}
|
||
|
||
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
|
||
|
||
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\")");
|
||
|
||
volatile int i = 42; // Test compile error before GCC 11 described in #3068.
|
||
EXPECT_EQ(fmt::format("{}", i), "42");
|
||
|
||
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)");
|
||
|
||
#endif
|
||
}
|
||
|
||
TEST(std_test, error_code) {
|
||
auto& generic = std::generic_category();
|
||
EXPECT_EQ(fmt::format("{}", std::error_code(42, generic)), "generic:42");
|
||
EXPECT_EQ(fmt::format("{:>12}", std::error_code(42, generic)),
|
||
" generic:42");
|
||
EXPECT_EQ(fmt::format("{:12}", std::error_code(42, generic)), "generic:42 ");
|
||
EXPECT_EQ(fmt::format("{}", std::error_code(42, fmt::system_category())),
|
||
"system:42");
|
||
EXPECT_EQ(fmt::format("{}", std::error_code(-42, fmt::system_category())),
|
||
"system:-42");
|
||
auto ec = std::make_error_code(std::errc::value_too_large);
|
||
EXPECT_EQ(fmt::format("{:s}", ec), ec.message());
|
||
EXPECT_EQ(fmt::format("{:?}", std::error_code(42, generic)),
|
||
"\"generic:42\"");
|
||
}
|
||
|
||
template <typename Catch> void exception_test() {
|
||
try {
|
||
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));
|
||
}
|
||
}
|
||
|
||
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) {
|
||
using testing::StartsWith;
|
||
exception_test<std::exception>();
|
||
exception_test<std::runtime_error>();
|
||
|
||
try {
|
||
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));
|
||
}
|
||
|
||
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: "));
|
||
}
|
||
|
||
#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
|
||
}
|
||
|
||
#if FMT_USE_RTTI
|
||
TEST(std_test, type_info) {
|
||
EXPECT_EQ(fmt::format("{}", typeid(std::runtime_error)),
|
||
"std::runtime_error");
|
||
}
|
||
#endif
|
||
|
||
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");
|
||
}
|
||
|
||
TEST(std_test, format_bitset) {
|
||
auto bs = std::bitset<6>(42);
|
||
EXPECT_EQ(fmt::format("{}", bs), "101010");
|
||
EXPECT_EQ(fmt::format("{:0>8}", bs), "00101010");
|
||
EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
|
||
}
|
||
|
||
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");
|
||
}
|
||
|
||
#ifdef __cpp_lib_atomic_flag_test
|
||
TEST(std_test, format_atomic_flag) {
|
||
std::atomic_flag f;
|
||
(void)f.test_and_set();
|
||
EXPECT_EQ(fmt::format("{}", f), "true");
|
||
|
||
f.clear();
|
||
const std::atomic_flag& cf = f;
|
||
EXPECT_EQ(fmt::format("{}", cf), "false");
|
||
}
|
||
#endif // __cpp_lib_atomic_flag_test
|
||
|
||
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)));
|
||
}
|
||
|
||
TEST(std_test, format_reference_wrapper) {
|
||
int num = 35;
|
||
EXPECT_EQ(fmt::to_string(std::cref(num)), "35");
|
||
EXPECT_EQ(fmt::to_string(std::ref(num)), "35");
|
||
EXPECT_EQ(fmt::format("{}", std::cref(num)), "35");
|
||
EXPECT_EQ(fmt::format("{}", std::ref(num)), "35");
|
||
}
|
||
|
||
// Regression test for https://github.com/fmtlib/fmt/issues/4424.
|
||
struct type_with_format_as {};
|
||
int format_as(type_with_format_as) { return 20; }
|
||
|
||
TEST(std_test, format_reference_wrapper_with_format_as) {
|
||
type_with_format_as t;
|
||
EXPECT_EQ(fmt::to_string(std::cref(t)), "20");
|
||
EXPECT_EQ(fmt::to_string(std::ref(t)), "20");
|
||
EXPECT_EQ(fmt::format("{}", std::cref(t)), "20");
|
||
EXPECT_EQ(fmt::format("{}", std::ref(t)), "20");
|
||
}
|
||
|
||
struct type_with_format_as_string {};
|
||
std::string format_as(type_with_format_as_string) { return "foo"; }
|
||
|
||
TEST(std_test, format_reference_wrapper_with_format_as_string) {
|
||
type_with_format_as_string t;
|
||
EXPECT_EQ(fmt::to_string(std::cref(t)), "foo");
|
||
EXPECT_EQ(fmt::to_string(std::ref(t)), "foo");
|
||
EXPECT_EQ(fmt::format("{}", std::cref(t)), "foo");
|
||
EXPECT_EQ(fmt::format("{}", std::ref(t)), "foo");
|
||
}
|