Commit 80505995 authored by Victor Zverovich's avatar Victor Zverovich

Allow delayed type checking

parent b0867f3f
This diff is collapsed.
......@@ -1536,3 +1536,42 @@ TEST(FormatTest, CustomArgFormatter) {
TEST(FormatTest, NonNullTerminatedFormatString) {
EXPECT_EQ("42", format(string_view("{}foo", 2), 42));
}
struct variant {
enum {INT, STRING} type;
explicit variant(int) : type(INT) {}
explicit variant(const char *) : type(STRING) {}
};
namespace fmt {
template <>
struct formatter<variant> : dynamic_formatter<> {
void format(buffer& buf, variant value, context& ctx) {
if (value.type == variant::INT)
dynamic_formatter::format(buf, 42, ctx);
else
dynamic_formatter::format(buf, "foo", ctx);
}
};
}
TEST(FormatTest, DynamicFormatter) {
auto num = variant(42);
auto str = variant("foo");
EXPECT_EQ("42", format("{:d}", num));
EXPECT_EQ("foo", format("{:s}", str));
EXPECT_THROW_MSG(format("{:=}", str),
format_error, "format specifier '=' requires numeric argument");
EXPECT_THROW_MSG(format("{:+}", str),
format_error, "format specifier '+' requires numeric argument");
EXPECT_THROW_MSG(format("{:-}", str),
format_error, "format specifier '-' requires numeric argument");
EXPECT_THROW_MSG(format("{: }", str),
format_error, "format specifier ' ' requires numeric argument");
EXPECT_THROW_MSG(format("{:#}", str),
format_error, "format specifier '#' requires numeric argument");
EXPECT_THROW_MSG(format("{:0}", str),
format_error, "format specifier '=' requires numeric argument");
EXPECT_THROW_MSG(format("{:.2}", num),
format_error, "precision not allowed in integer format specifier");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment