2021-01-23 21:39:18 +01:00
|
|
|
#include "test_helpers.hpp"
|
2020-12-10 19:26:56 +01:00
|
|
|
#include <algorithm>
|
2021-01-07 23:07:08 +01:00
|
|
|
#include <ss/converter.hpp>
|
2020-12-10 19:26:56 +01:00
|
|
|
|
|
|
|
TEST_CASE("testing split") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
|
|
|
for (const auto& [s, expected, delim] :
|
|
|
|
// clang-format off
|
2021-01-21 02:13:45 +01:00
|
|
|
{std::make_tuple("a,b,c,d", std::vector{"a", "b", "c", "d"}, ","),
|
2020-12-10 19:26:56 +01:00
|
|
|
{"", {}, " "},
|
|
|
|
{" 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
|
2020-12-26 00:46:42 +01:00
|
|
|
) {
|
2021-02-01 00:56:42 +01:00
|
|
|
auto split = c.split(s, delim);
|
2020-12-26 00:46:42 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing valid conversions") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<int>("5");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<int, void>("5,junk");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<void, int>("junk,5");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<void, int, void>("junk 5 junk", " ");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto tup =
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, void, std::optional<int>>("junk\tjunk\t5", "\t");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
REQUIRE(tup.has_value());
|
|
|
|
CHECK(tup == 5);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<int, double, void>("5,6.6,junk");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(5, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<int, void, double>("5,junk,6.6");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(5, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<void, int, double>("junk;5;6.6", ";");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(5, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
auto tup =
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, std::optional<int>, double>("junk;5;6.6", ";");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
REQUIRE(std::get<0>(tup).has_value());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(5, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
auto tup =
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, std::optional<int>, double>("junk;5.4;6.6", ";");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
REQUIRE(!std::get<0>(tup).has_value());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(std::optional<int>{}, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup =
|
|
|
|
c.convert<void, std::variant<int, double>, double>("junk;5;6.6",
|
|
|
|
";");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(std::variant<int, double>{5}, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup =
|
|
|
|
c.convert<void, std::variant<int, double>, double>("junk;5.5;6.6",
|
|
|
|
";");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(std::variant<int, double>{5.5}, 6.6));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing invalid conversions") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int>("");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-02 02:17:31 +01:00
|
|
|
c.convert<int>("10", "");
|
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int, void>("");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int, void>(",junk");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, int>("junk,");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int>("x");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int, void>("x");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int, void>("x,junk");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, int>("junk,x");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-20 19:00:45 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, std::variant<int, double>, double>("junk;.5.5;6", ";");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing ss:ax restriction (all except)") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ax<int, 0>>("0");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ax<int, 0, 1, 2>>("1");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ax<int, 1>, char>("1,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
int tup = c.convert<ss::ax<int, 1>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
std::tuple<char, int> tup = c.convert<char, ss::ax<int, 1>>("c,3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple('c', 3));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
std::tuple<int, char> tup = c.convert<ss::ax<int, 1>, char>("3,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(3, 'c'));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing ss:nx restriction (none except)") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::nx<int, 1>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<char, ss::nx<int, 1, 2, 69>>("c,3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::nx<int, 1>, char>("3,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::nx<int, 3>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 2);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple('c', 1));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::nx<int, 1>, char>("1,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(1, 'c'));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing ss:ir restriction (in range)") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ir<int, 0, 2>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<char, ss::ir<int, 4, 69>>("c,3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ir<int, 1, 2>, char>("3,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::ir<int, 1, 5>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::ir<int, 0, 2>>("2");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 2);
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple('c', 1));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(1, 'c'));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing ss:oor restriction (out of range)") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::oor<int, 1, 5>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::oor<int, 0, 2>>("2");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::oor<int, 1, 20>, char>("1,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::oor<int, 0, 2>>("3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple('c', 3));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(3, 'c'));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<int> extracted_vector = {1, 2, 3};
|
|
|
|
|
|
|
|
// custom extract
|
|
|
|
template <>
|
|
|
|
inline bool ss::extract(const char* begin, const char* end,
|
|
|
|
std::vector<int>& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (begin == end) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = extracted_vector;
|
|
|
|
return true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("testing ss:ne restriction (not empty)") {
|
2020-12-26 00:46:42 +01:00
|
|
|
ss::converter c;
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ne<std::string>>("");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int, ss::ne<std::string>>("3,");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ne<std::string>, int>(",3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<void, ss::ne<std::string>, int>("junk,,3");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::ne<std::vector<int>>>("");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::ne<std::string>>("s");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == "s");
|
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
2021-01-07 23:24:06 +01:00
|
|
|
CHECK(tup == std::make_tuple(1, "s"));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}");
|
2020-12-26 00:46:42 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == extracted_vector);
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-27 16:51:59 +01:00
|
|
|
|
2021-01-03 15:38:07 +01:00
|
|
|
TEST_CASE("testing ss:lt ss::lte ss::gt ss::gte restriction (in range)") {
|
|
|
|
ss::converter c;
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::lt<int, 3>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::lt<int, 2>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::gt<int, 3>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::gt<int, 4>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::lte<int, 2>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<ss::gte<int, 4>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(!c.valid());
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::lt<int, 4>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::gt<int, 2>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::lte<int, 4>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::lte<int, 3>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::gte<int, 2>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-02-01 00:56:42 +01:00
|
|
|
auto tup = c.convert<ss::gte<int, 3>>("3");
|
2021-01-03 15:38:07 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 16:51:59 +01:00
|
|
|
TEST_CASE("testing error mode") {
|
|
|
|
ss::converter c;
|
|
|
|
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int>("junk");
|
2020-12-27 16:51:59 +01:00
|
|
|
CHECK(!c.valid());
|
2020-12-27 17:00:06 +01:00
|
|
|
CHECK(c.error_msg().empty());
|
2020-12-27 16:51:59 +01:00
|
|
|
|
2021-01-01 21:57:14 +01:00
|
|
|
c.set_error_mode(ss::error_mode::error_string);
|
2021-02-01 00:56:42 +01:00
|
|
|
c.convert<int>("junk");
|
2020-12-27 16:51:59 +01:00
|
|
|
CHECK(!c.valid());
|
2020-12-27 17:00:06 +01:00
|
|
|
CHECK(!c.error_msg().empty());
|
2020-12-27 16:51:59 +01:00
|
|
|
}
|
2021-01-31 23:08:46 +01:00
|
|
|
|
|
|
|
TEST_CASE("testing converter with quotes spacing and escaping") {
|
|
|
|
{
|
|
|
|
ss::converter c;
|
|
|
|
|
|
|
|
auto tup = c.convert<std::string, std::string, std::string>(
|
|
|
|
R"("just","some","strings")");
|
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == std::make_tuple("\"just\"", "\"some\"", "\"strings\""));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ss::converter<ss::quote<'"'>> c;
|
|
|
|
|
|
|
|
auto tup = c.convert<std::string, std::string, double, char>(
|
|
|
|
buff(R"("just",some,"12.3","a")"));
|
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == std::make_tuple("just", "some", 12.3, 'a'));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ss::converter<ss::trim<' '>> c;
|
|
|
|
|
|
|
|
auto tup = c.convert<std::string, std::string, double, char>(
|
2021-02-01 00:56:42 +01:00
|
|
|
buff(R"( just , some , 12.3 ,a )"));
|
2021-01-31 23:08:46 +01:00
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == std::make_tuple("just", "some", 12.3, 'a'));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ss::converter<ss::escape<'\\'>> c;
|
|
|
|
|
|
|
|
auto tup =
|
|
|
|
c.convert<std::string, std::string>(buff(R"(ju\,st,strings)"));
|
|
|
|
REQUIRE(c.valid());
|
|
|
|
CHECK(tup == std::make_tuple("ju,st", "strings"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ss::converter<ss::escape<'\\'>, ss::trim<' '>, ss::quote<'"'>> c;
|
|
|
|
|
|
|
|
auto tup = c.convert<std::string, std::string, double, std::string>(
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 02:17:31 +01:00
|
|
|
|
|
|
|
TEST_CASE("testing invalid split conversions") {
|
|
|
|
ss::converter<ss::escape<'\\'>, ss::trim<' '>, ss::quote<'"'>> c;
|
|
|
|
c.set_error_mode(ss::error_mode::error_string);
|
|
|
|
|
|
|
|
{
|
|
|
|
// mismatched quote
|
|
|
|
auto tup = c.convert<std::string, std::string, double, char>(
|
|
|
|
buff(R"( "just , some , "12.3","a" )"));
|
|
|
|
CHECK(!c.valid());
|
|
|
|
CHECK(!c.unterminated_quote());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// unterminated quote
|
|
|
|
auto tup = c.convert<std::string, std::string, double, std::string>(
|
|
|
|
buff(R"( ju\,st , "so,me" , 12.34 , "str""ings)"));
|
|
|
|
CHECK(!c.valid());
|
|
|
|
CHECK(c.unterminated_quote());
|
|
|
|
}
|
|
|
|
}
|