#include "test_helpers.hpp" #include #include TEST_CASE("testing split") { ss::converter c; for (const auto& [s, expected, delim] : // clang-format off {std::make_tuple("a,b,c,d", std::vector{"a", "b", "c", "d"}, ","), {"", {}, " "}, {" x x x x | x ", {" x x x x ", " x "}, "|"}, {"a::b::c::d", {"a", "b", "c", "d"}, "::"}, {"x\t-\ty", {"x", "y"}, "\t-\t"}, {"x", {"x"}, ","}} // clang-format on ) { auto split = c.split(s, delim); CHECK(split.size() == expected.size()); for (size_t i = 0; i < split.size(); ++i) { auto s = std::string(split[i].first, split[i].second); CHECK(s == expected[i]); } } } TEST_CASE("testing valid conversions") { ss::converter c; { auto tup = c.convert("5"); REQUIRE(c.valid()); CHECK(tup == 5); } { auto tup = c.convert("5,junk"); REQUIRE(c.valid()); CHECK(tup == 5); } { auto tup = c.convert("junk,5"); REQUIRE(c.valid()); CHECK(tup == 5); } { auto tup = c.convert("5\njunk\njunk", "\n"); REQUIRE(c.valid()); CHECK(tup == 5); } { auto tup = c.convert("junk 5 junk", " "); REQUIRE(c.valid()); CHECK(tup == 5); } { auto tup = c.convert("junk\tjunk\t5", "\t"); REQUIRE(c.valid()); CHECK(tup == 5); } { auto tup = c.convert>("junk\tjunk\t5", "\t"); REQUIRE(c.valid()); REQUIRE(tup.has_value()); CHECK(tup == 5); } { auto tup = c.convert("5,6.6,junk"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(5, 6.6)); } { auto tup = c.convert("5,junk,6.6"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(5, 6.6)); } { auto tup = c.convert("junk;5;6.6", ";"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(5, 6.6)); } { auto tup = c.convert, double>("junk;5;6.6", ";"); REQUIRE(c.valid()); REQUIRE(std::get<0>(tup).has_value()); CHECK(tup == std::make_tuple(5, 6.6)); } { auto tup = c.convert, double>("junk;5.4;6.6", ";"); REQUIRE(c.valid()); REQUIRE(!std::get<0>(tup).has_value()); CHECK(tup == std::make_tuple(std::optional{}, 6.6)); } { auto tup = c.convert, double>("junk;5;6.6", ";"); REQUIRE(c.valid()); REQUIRE(std::holds_alternative(std::get<0>(tup))); CHECK(tup == std::make_tuple(std::variant{5}, 6.6)); } { auto tup = c.convert, double>("junk;5.5;6.6", ";"); REQUIRE(c.valid()); REQUIRE(std::holds_alternative(std::get<0>(tup))); CHECK(tup == std::make_tuple(std::variant{5.5}, 6.6)); } } TEST_CASE("testing invalid conversions") { ss::converter c; c.convert(""); REQUIRE(!c.valid()); c.convert("10", ""); REQUIRE(!c.valid()); c.convert(""); REQUIRE(!c.valid()); c.convert(",junk"); REQUIRE(!c.valid()); c.convert("junk,"); REQUIRE(!c.valid()); c.convert("x"); REQUIRE(!c.valid()); c.convert("x"); REQUIRE(!c.valid()); c.convert("x,junk"); REQUIRE(!c.valid()); c.convert("junk,x"); REQUIRE(!c.valid()); c.convert, double>("junk;.5.5;6", ";"); REQUIRE(!c.valid()); } TEST_CASE("testing ss:ax restriction (all except)") { ss::converter c; c.convert>("0"); REQUIRE(!c.valid()); c.convert>("1"); REQUIRE(!c.valid()); c.convert>("junk,c,1"); REQUIRE(!c.valid()); c.convert, char>("1,c"); REQUIRE(!c.valid()); { int tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { std::tuple tup = c.convert>("c,3"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple('c', 3)); } { std::tuple tup = c.convert, char>("3,c"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(3, 'c')); } } TEST_CASE("testing ss:nx restriction (none except)") { ss::converter c; c.convert>("3"); REQUIRE(!c.valid()); c.convert>("c,3"); REQUIRE(!c.valid()); c.convert, char>("3,c"); REQUIRE(!c.valid()); { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("2"); REQUIRE(c.valid()); CHECK(tup == 2); } { auto tup = c.convert>("c,junk,1"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple('c', 1)); } { auto tup = c.convert, char>("1,c"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(1, 'c')); } } TEST_CASE("testing ss:ir restriction (in range)") { ss::converter c; c.convert>("3"); REQUIRE(!c.valid()); c.convert>("c,3"); REQUIRE(!c.valid()); c.convert, char>("3,c"); REQUIRE(!c.valid()); { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("2"); REQUIRE(c.valid()); CHECK(tup == 2); } { auto tup = c.convert>("c,junk,1"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple('c', 1)); } { auto tup = c.convert, char>("1,c"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(1, 'c')); } } TEST_CASE("testing ss:oor restriction (out of range)") { ss::converter c; c.convert>("3"); REQUIRE(!c.valid()); c.convert>("2"); REQUIRE(!c.valid()); c.convert, void>("c,1,junk"); REQUIRE(!c.valid()); c.convert, char>("1,c"); REQUIRE(!c.valid()); { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("c,junk,3"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple('c', 3)); } { auto tup = c.convert, char>("3,c"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(3, 'c')); } } const std::vector extracted_vector = {1, 2, 3}; // custom extract template <> inline bool ss::extract(const char* begin, const char* end, std::vector& value) { if (begin == end) { return false; } value = extracted_vector; return true; } TEST_CASE("testing ss:ne restriction (not empty)") { ss::converter c; c.convert>(""); REQUIRE(!c.valid()); c.convert>("3,"); REQUIRE(!c.valid()); c.convert, int>(",3"); REQUIRE(!c.valid()); c.convert, int>("junk,,3"); REQUIRE(!c.valid()); c.convert>>(""); REQUIRE(!c.valid()); { auto tup = c.convert>("s"); REQUIRE(c.valid()); CHECK(tup == "s"); } { auto tup = c.convert, ss::ne>("1,s"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple(1, "s")); } { auto tup = c.convert>>("{1 2 3}"); REQUIRE(c.valid()); CHECK(tup == extracted_vector); } } TEST_CASE("testing ss:lt ss::lte ss::gt ss::gte restriction (in range)") { ss::converter c; c.convert>("3"); REQUIRE(!c.valid()); c.convert>("3"); REQUIRE(!c.valid()); c.convert>("3"); REQUIRE(!c.valid()); c.convert>("3"); REQUIRE(!c.valid()); c.convert>("3"); REQUIRE(!c.valid()); c.convert>("3"); REQUIRE(!c.valid()); { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } { auto tup = c.convert>("3"); REQUIRE(c.valid()); CHECK(tup == 3); } } TEST_CASE("testing error mode") { ss::converter c; c.convert("junk"); CHECK(!c.valid()); CHECK(c.error_msg().empty()); c.set_error_mode(ss::error_mode::error_string); c.convert("junk"); CHECK(!c.valid()); CHECK(!c.error_msg().empty()); } TEST_CASE("testing converter with quotes spacing and escaping") { { ss::converter c; auto tup = c.convert( R"("just","some","strings")"); REQUIRE(c.valid()); CHECK(tup == std::make_tuple("\"just\"", "\"some\"", "\"strings\"")); } { ss::converter> c; auto tup = c.convert( buff(R"("just",some,"12.3","a")")); REQUIRE(c.valid()); CHECK(tup == std::make_tuple("just", "some", 12.3, 'a')); } { ss::converter> c; auto tup = c.convert( buff(R"( just , some , 12.3 ,a )")); REQUIRE(c.valid()); CHECK(tup == std::make_tuple("just", "some", 12.3, 'a')); } { ss::converter> c; auto tup = c.convert(buff(R"(ju\,st,strings)")); REQUIRE(c.valid()); CHECK(tup == std::make_tuple("ju,st", "strings")); } { ss::converter, ss::trim<' '>, ss::quote<'"'>> c; auto tup = c.convert( buff(R"( ju\,st , "so,me" , 12.34 , "str""ings")")); REQUIRE(c.valid()); CHECK(tup == std::make_tuple("ju,st", "so,me", 12.34, "str\"ings")); } } TEST_CASE("testing invalid split conversions") { ss::converter, ss::trim<' '>, ss::quote<'"'>> c; c.set_error_mode(ss::error_mode::error_string); { // mismatched quote auto tup = c.convert( buff(R"( "just , some , "12.3","a" )")); CHECK(!c.valid()); CHECK(!c.unterminated_quote()); } { // unterminated quote auto tup = c.convert( buff(R"( ju\,st , "so,me" , 12.34 , "str""ings)")); CHECK(!c.valid()); CHECK(c.unterminated_quote()); } }