Test and correct handling of the width specifier.
This commit is contained in:
+57
-23
@@ -66,12 +66,12 @@ class TestString {
|
||||
}
|
||||
};
|
||||
|
||||
TEST(FormatterTest, FormatNoArgs) {
|
||||
TEST(FormatterTest, NoArgs) {
|
||||
EXPECT_EQ("abracadabra", str(Format("{0}{1}{0}") << "abra" << "cad"));
|
||||
EXPECT_EQ("test", str(Format("test")));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatArgs) {
|
||||
TEST(FormatterTest, Args) {
|
||||
EXPECT_EQ("42", str(Format("{0}") << 42));
|
||||
EXPECT_EQ("before 42", str(Format("before {0}") << 42));
|
||||
EXPECT_EQ("42 after", str(Format("{0} after") << 42));
|
||||
@@ -81,7 +81,7 @@ TEST(FormatterTest, FormatArgs) {
|
||||
EXPECT_EQ("abracadabra", str(Format("{0}{1}{0}") << "abra" << "cad"));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatArgErrors) {
|
||||
TEST(FormatterTest, ArgErrors) {
|
||||
EXPECT_THROW_MSG(Format("{"), FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{}"), FormatError,
|
||||
"missing argument index in format string");
|
||||
@@ -109,57 +109,87 @@ TEST(FormatterTest, FormatArgErrors) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatPlusFlag) {
|
||||
TEST(FormatterTest, EmptySpecs) {
|
||||
EXPECT_EQ("42", str(Format("{0:}") << 42));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, PlusFlag) {
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42));
|
||||
EXPECT_EQ("-42", str(Format("{0:+}") << -42));
|
||||
EXPECT_THROW_MSG(Format("{0:+") << 'c',
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:+}") << 'c',
|
||||
FormatError, "format specifier '+' used with non-numeric type");
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42));
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42u));
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42l));
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42ul));
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42.0));
|
||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42.0l));
|
||||
EXPECT_EQ("+0x42",
|
||||
str(Format("{0:+}") << reinterpret_cast<void*>(0x42)));
|
||||
EXPECT_THROW_MSG(Format("{0:+") << 'c',
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:+}") << 'c',
|
||||
FormatError, "format specifier '+' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:+}") << "abc",
|
||||
FormatError, "format specifier '+' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:+}") << L"abc",
|
||||
FormatError, "format specifier '+' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:+}") << static_cast<const void*>("abc"),
|
||||
FormatError, "format specifier '+' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:+}") << TestString(),
|
||||
FormatError, "format specifier '+' used with non-numeric type");
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatZeroFlag) {
|
||||
TEST(FormatterTest, ZeroFlag) {
|
||||
EXPECT_EQ("42", str(Format("{0:0}") << 42));
|
||||
EXPECT_THROW_MSG(Format("{0:0") << 'c',
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:05}") << 'c',
|
||||
FormatError, "format specifier '0' used with non-numeric type");
|
||||
EXPECT_EQ("-0042", str(Format("{0:05}") << -42));
|
||||
EXPECT_EQ("00042", str(Format("{0:05}") << 42u));
|
||||
EXPECT_EQ("-0042", str(Format("{0:05}") << -42l));
|
||||
EXPECT_EQ("00042", str(Format("{0:05}") << 42ul));
|
||||
EXPECT_EQ("-0042", str(Format("{0:05}") << -42.0));
|
||||
EXPECT_EQ("-0042", str(Format("{0:05}") << -42.0l));
|
||||
EXPECT_EQ("0x0042",
|
||||
str(Format("{0:06}") << reinterpret_cast<void*>(0x42)));
|
||||
EXPECT_THROW_MSG(Format("{0:0") << 'c',
|
||||
FormatError, "unmatched '{' in format");
|
||||
EXPECT_THROW_MSG(Format("{0:05}") << 'c',
|
||||
FormatError, "format specifier '0' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:05}") << "abc",
|
||||
FormatError, "format specifier '0' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:05}") << L"abc",
|
||||
FormatError, "format specifier '0' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:05}") << static_cast<const void*>("abc"),
|
||||
FormatError, "format specifier '0' used with non-numeric type");
|
||||
EXPECT_THROW_MSG(Format("{0:05}") << TestString(),
|
||||
FormatError, "format specifier '0' used with non-numeric type");
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatWidth) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatChar) {
|
||||
EXPECT_EQ("a*b", str(Format("{0}{1}{2}") << 'a' << '*' << 'b'));
|
||||
TEST(FormatterTest, Width) {
|
||||
char format[256];
|
||||
if (ULONG_MAX > UINT_MAX) {
|
||||
std::sprintf(format, "{0:%lu", INT_MAX + 1l);
|
||||
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:%lu}", UINT_MAX + 1l);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "number is too big in format");
|
||||
} else {
|
||||
std::sprintf(format, "{0:%u0", UINT_MAX);
|
||||
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:%u0}", UINT_MAX);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "number is too big in format");
|
||||
}
|
||||
std::sprintf(format, "{0:%u", INT_MAX + 1u);
|
||||
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
|
||||
std::sprintf(format, "{0:%u}", INT_MAX + 1u);
|
||||
EXPECT_THROW_MSG(Format(format) << 0,
|
||||
FormatError, "number is too big in format");
|
||||
EXPECT_EQ(" -42", str(Format("{0:4}") << -42));
|
||||
EXPECT_EQ(" 42", str(Format("{0:5}") << 42u));
|
||||
EXPECT_EQ(" -42", str(Format("{0:6}") << -42l));
|
||||
EXPECT_EQ(" 42", str(Format("{0:7}") << 42lu));
|
||||
EXPECT_EQ(" -1.23", str(Format("{0:8}") << -1.23));
|
||||
EXPECT_EQ(" -1.23", str(Format("{0:9}") << -1.23l));
|
||||
EXPECT_EQ(" 0xcafe",
|
||||
str(Format("{0:10}") << reinterpret_cast<void*>(0xcafe)));
|
||||
EXPECT_EQ("x ", str(Format("{0:11}") << 'x'));
|
||||
EXPECT_EQ("narrow ", str(Format("{0:12}") << "narrow"));
|
||||
EXPECT_EQ("wide ", str(Format("{0:13}") << L"wide"));
|
||||
EXPECT_EQ("test ", str(Format("{0:14}") << TestString("test")));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatInt) {
|
||||
@@ -173,6 +203,10 @@ TEST(FormatterTest, FormatInt) {
|
||||
EXPECT_EQ(os.str(), str(Format("{0}") << INT_MAX));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatChar) {
|
||||
EXPECT_EQ("a*b", str(Format("{0}{1}{2}") << 'a' << '*' << 'b'));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatString) {
|
||||
EXPECT_EQ("test", str(Format("{0}") << std::string("test")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user