Allow delayed type checking

This commit is contained in:
Victor Zverovich
2017-09-03 08:28:30 -07:00
parent b0867f3fa0
commit 80505995d0
2 changed files with 231 additions and 71 deletions

View File

@@ -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");
}