Disable range formatter if value type is not formattable (#1885)

This commit is contained in:
Victor Zverovich
2020-09-20 07:53:13 -07:00
parent c46a8de4e1
commit 2f7e08856b
2 changed files with 25 additions and 6 deletions

View File

@@ -141,13 +141,16 @@ TEST(RangesTest, FormatStringLike) {
#endif // FMT_USE_STRING_VIEW
struct zstring_sentinel {};
bool operator==(const char* p, zstring_sentinel) { return *p == '\0'; }
bool operator!=(const char* p, zstring_sentinel) { return *p != '\0'; }
struct zstring {
const char* p;
const char* begin() const { return p; }
zstring_sentinel end() const { return {}; }
};
TEST(RangesTest, JoinSentinel) {
zstring hello{"hello"};
EXPECT_EQ("{'h', 'e', 'l', 'l', 'o'}", fmt::format("{}", hello));
@@ -189,3 +192,12 @@ TEST(RangesTest, JoinRange) {
const std::vector<int> z(3u, 0);
EXPECT_EQ("0,0,0", fmt::format("{}", fmt::join(z, ",")));
}
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
struct unformattable {};
TEST(RangesTest, UnformattableRange) {
EXPECT_FALSE((fmt::has_formatter<std::vector<unformattable>,
fmt::format_context>::value));
}
#endif