@@ -12,13 +12,27 @@ using util::detail::CheckNumFormatSpecifiers;
12
12
13
13
BOOST_AUTO_TEST_SUITE (util_string_tests)
14
14
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
+
15
23
// Helper to allow compile-time sanity checks while providing the number of
16
24
// args directly. Normally PassFmt<sizeof...(Args)> would be used.
17
25
template <unsigned NumArgs>
18
26
void PassFmt (ConstevalFormatString<NumArgs> fmt)
19
27
{
20
28
// Execute compile-time check again at run-time to get code coverage stats
21
29
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 ));
22
36
}
23
37
template <unsigned WrongNumArgs>
24
38
void FailFmtWithError (const char * wrong_fmt, std::string_view error)
@@ -111,6 +125,15 @@ BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
111
125
FailFmtWithError<2 >(" %1$*2$" , err_term);
112
126
FailFmtWithError<2 >(" %1$.*2$" , err_term);
113
127
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" });
114
137
}
115
138
116
139
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments