8#include <boost/test/unit_test.hpp>
15BOOST_AUTO_TEST_SUITE(util_string_tests)
17template <
unsigned NumArgs>
20 std::apply([&](
auto...
args) {
22 }, std::array<int, NumArgs>{});
27template <
unsigned NumArgs>
39template <
unsigned WrongNumArgs>
42 BOOST_CHECK_EXCEPTION(CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt),
const char*,
HasReason{error});
57 PassFmt<1>(
"%1$s%1$s");
59 PassFmt<2>(
"%2$s 4$s %2$s");
60 PassFmt<129>(
"%129$s 999$s %2$s");
81 PassFmt<3>(
"%2$*3$d");
82 PassFmt<3>(
"%2$*3$.9d");
83 PassFmt<3>(
"%2$.*3$d");
84 PassFmt<3>(
"%2$9.*3$d");
85 PassFmt<3>(
"%2$+9.*3$d");
86 PassFmt<4>(
"%3$*2$.*4$f");
89 PassFmt<3>(
"'%- 0+*.*f'");
90 PassFmt<3>(
"'%1$- 0+*3$.*2$f'");
92 auto err_mix{
"Format specifiers must be all positional or all non-positional!"};
93 FailFmtWithError<1>(
"%s%1$s", err_mix);
94 FailFmtWithError<2>(
"%2$*d", err_mix);
95 FailFmtWithError<2>(
"%*2$d", err_mix);
96 FailFmtWithError<2>(
"%.*3$d", err_mix);
97 FailFmtWithError<2>(
"%2$.*d", err_mix);
99 auto err_num{
"Format specifier count must match the argument count!"};
100 FailFmtWithError<1>(
"", err_num);
101 FailFmtWithError<0>(
"%s", err_num);
102 FailFmtWithError<2>(
"%s", err_num);
103 FailFmtWithError<0>(
"%1$s", err_num);
104 FailFmtWithError<2>(
"%1$s", err_num);
105 FailFmtWithError<1>(
"%*c", err_num);
107 auto err_0_pos{
"Positional format specifier must have position of at least 1"};
108 FailFmtWithError<1>(
"%$s", err_0_pos);
109 FailFmtWithError<1>(
"%$", err_0_pos);
110 FailFmtWithError<0>(
"%0$", err_0_pos);
111 FailFmtWithError<0>(
"%0$s", err_0_pos);
112 FailFmtWithError<2>(
"%2$*$d", err_0_pos);
113 FailFmtWithError<2>(
"%2$*0$d", err_0_pos);
114 FailFmtWithError<3>(
"%3$*2$.*$f", err_0_pos);
115 FailFmtWithError<3>(
"%3$*2$.*0$f", err_0_pos);
117 auto err_term{
"Format specifier incorrectly terminated by end of string"};
118 FailFmtWithError<1>(
"%", err_term);
119 FailFmtWithError<1>(
"%9", err_term);
120 FailFmtWithError<1>(
"%9.", err_term);
121 FailFmtWithError<1>(
"%9.9", err_term);
122 FailFmtWithError<1>(
"%*", err_term);
123 FailFmtWithError<1>(
"%+*", err_term);
124 FailFmtWithError<1>(
"%.*", err_term);
125 FailFmtWithError<1>(
"%9.*", err_term);
126 FailFmtWithError<1>(
"%1$", err_term);
127 FailFmtWithError<1>(
"%1$9", err_term);
128 FailFmtWithError<2>(
"%1$*2$", err_term);
129 FailFmtWithError<2>(
"%1$.*2$", err_term);
130 FailFmtWithError<2>(
"%1$9.*2$", err_term);
135 HasReason{
"tinyformat: %n conversion spec not supported"});
137 HasReason{
"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
139 HasReason{
"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
146 HasReason{
"tinyformat: Not enough conversion specifiers in format string"});
148 HasReason{
"tinyformat: Too many conversion specifiers in format string"});
178 std::string_view input =
"once upon a time\n there was a dog \r\nwho liked food\n";
182 std::optional<std::string> line1{reader.
ReadLine()};
185 std::optional<std::string> line2{reader.
ReadLine()};
188 std::optional<std::string> line3{reader.
ReadLine()};
189 std::optional<std::string> line4{reader.
ReadLine()};
203 std::string_view input =
"once upon a time there\nwas a dog who liked tea\n";
209 BOOST_CHECK_EXCEPTION(reader1.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
221 std::string_view input =
"\n";
228 std::string_view input;
234 std::string_view input =
"ab\n";
237 BOOST_CHECK_EXCEPTION(reader.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
240 std::string_view input =
"a\nb\n";
248 std::string_view input =
"a\nbaboon\n";
252 BOOST_CHECK_EXCEPTION(reader.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
257 BOOST_CHECK_EXCEPTION(reader.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
264 std::string_view input =
"once upon a time there";
276 std::string_view input =
"once upon a time\n there was a dog \r\nwho liked food";
282 BOOST_CHECK_EXCEPTION(reader.
ReadLength(128), std::runtime_error,
HasReason{
"Not enough data in buffer"});
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
BOOST_AUTO_TEST_SUITE_END()
static constexpr void CheckNumFormatSpecifiers(const char *str)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)
size_t Consumed() const
Returns number of bytes already read from buffer.
size_t Remaining() const
Returns remaining size of bytes in buffer.
std::string ReadLength(size_t len)
Returns string from current iterator position of specified length if possible and advances iterator o...
std::optional< std::string > ReadLine()
Returns a string from current iterator position up to (but not including) next and advances iterator...
bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2)
Locale-independent, ASCII-only comparator.
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
void TfmFormatZeroes(const std::string &fmt)
void PassFmt(ConstevalFormatString< NumArgs > fmt)
void FailFmtWithError(const char *wrong_fmt, std::string_view error)