Skip to content

Commit 533013c

Browse files
hodlinatorl0rincMarcoFalkeryanofsky
committed
test: Prove+document ConstevalFormatString/tinyformat parity
Co-Authored-By: Lőrinc <pap.lorinc@gmail.com> Co-Authored-By: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Co-Authored-By: Ryan Ofsky <ryan@ofsky.org>
1 parent b81a465 commit 533013c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/test/util_string_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ using util::detail::CheckNumFormatSpecifiers;
1212

1313
BOOST_AUTO_TEST_SUITE(util_string_tests)
1414

15+
template <unsigned NumArgs>
16+
void TfmFormatZeroes(const std::string& fmt)
17+
{
18+
std::apply([&](auto... args) {
19+
(void)tfm::format(fmt, args...);
20+
}, std::array<int, NumArgs>{});
21+
}
22+
1523
// Helper to allow compile-time sanity checks while providing the number of
1624
// args directly. Normally PassFmt<sizeof...(Args)> would be used.
1725
template <unsigned NumArgs>
1826
void PassFmt(ConstevalFormatString<NumArgs> fmt)
1927
{
2028
// Execute compile-time check again at run-time to get code coverage stats
2129
BOOST_CHECK_NO_THROW(CheckNumFormatSpecifiers<NumArgs>(fmt.fmt));
30+
31+
// If ConstevalFormatString didn't throw above, make sure tinyformat doesn't
32+
// throw either for the same format string and parameter count combination.
33+
// Proves that we have some extent of protection from runtime errors
34+
// (tinyformat may still throw for some type mismatches).
35+
BOOST_CHECK_NO_THROW(TfmFormatZeroes<NumArgs>(fmt.fmt));
2236
}
2337
template <unsigned WrongNumArgs>
2438
void FailFmtWithError(const char* wrong_fmt, std::string_view error)
@@ -111,6 +125,15 @@ BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
111125
FailFmtWithError<2>("%1$*2$", err_term);
112126
FailFmtWithError<2>("%1$.*2$", err_term);
113127
FailFmtWithError<2>("%1$9.*2$", err_term);
128+
129+
// Ensure that tinyformat throws if format string contains wrong number
130+
// of specifiers. PassFmt relies on this to verify tinyformat successfully
131+
// formats the strings, and will need to be updated if tinyformat is changed
132+
// not to throw on failure.
133+
BOOST_CHECK_EXCEPTION(TfmFormatZeroes<2>("%s"), tfm::format_error,
134+
HasReason{"tinyformat: Not enough conversion specifiers in format string"});
135+
BOOST_CHECK_EXCEPTION(TfmFormatZeroes<1>("%s %s"), tfm::format_error,
136+
HasReason{"tinyformat: Too many conversion specifiers in format string"});
114137
}
115138

116139
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)