Support named arguments

This commit is contained in:
jamboree
2015-06-10 09:32:59 +08:00
parent ed2dfe5124
commit 7487bde587
5 changed files with 239 additions and 16 deletions

View File

@@ -562,7 +562,7 @@ TEST(FormatterTest, ArgsInDifferentPositions) {
TEST(FormatterTest, ArgErrors) {
EXPECT_THROW_MSG(format("{"), FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{x}"), FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{?}"), FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{0"), FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{0}"), FormatError, "argument index out of range");
@@ -609,6 +609,23 @@ TEST(FormatterTest, ManyArgs) {
}
#endif
TEST(FormatterTest, NamedArg) {
char a = 'A', b = 'B', c = 'C';
EXPECT_EQ("BBAACC", format("{1}{b}{0}{a}{2}{c}", FMT_CAPTURE(a, b, c)));
EXPECT_EQ(" A", format("{a:>2}", FMT_CAPTURE(a)));
EXPECT_THROW_MSG(format("{a+}", FMT_CAPTURE(a)), FormatError, "missing '}' in format string");
EXPECT_THROW_MSG(format("{a}"), FormatError, "argument not found");
EXPECT_THROW_MSG(format("{d}", FMT_CAPTURE(a, b, c)), FormatError, "argument not found");
EXPECT_THROW_MSG(format("{a}{}", FMT_CAPTURE(a)),
FormatError, "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG(format("{}{a}", FMT_CAPTURE(a)),
FormatError, "cannot switch from automatic to manual argument indexing");
EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4)));
EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2)));
int n = 100;
EXPECT_EQ(L"n=100", format(L"n={n}", FMT_CAPTURE_W(n)));
}
TEST(FormatterTest, AutoArgIndex) {
EXPECT_EQ("abc", format("{}{}{}", 'a', 'b', 'c'));
EXPECT_THROW_MSG(format("{0}{}", 'a', 'b'),
@@ -920,7 +937,7 @@ TEST(FormatterTest, RuntimeWidth) {
FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{0:{}", 0),
FormatError, "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG(format("{0:{x}}", 0),
EXPECT_THROW_MSG(format("{0:{?}}", 0),
FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{0:{1}}", 0),
FormatError, "argument index out of range");
@@ -1037,7 +1054,7 @@ TEST(FormatterTest, RuntimePrecision) {
FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{0:.{}", 0),
FormatError, "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG(format("{0:.{x}}", 0),
EXPECT_THROW_MSG(format("{0:.{?}}", 0),
FormatError, "invalid format string");
EXPECT_THROW_MSG(format("{0:.{1}", 0, 0),
FormatError, "precision not allowed in integer format specifier");