write tests for converting std::optional and std::variant

This commit is contained in:
ado 2020-12-20 19:00:45 +01:00
parent bf92abdb2c
commit 616316d2a7
2 changed files with 199 additions and 1 deletions

View File

@ -57,6 +57,14 @@ TEST_CASE("testing valid conversions") {
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{
auto tup =
c.convert<void, void, std::optional<int>>("junk\tjunk\t5",
"\t");
REQUIRE(c.valid());
REQUIRE(tup.has_value());
CHECK(tup == 5);
}
{ {
auto tup = c.convert<int, double, void>("5,6.6,junk"); auto tup = c.convert<int, double, void>("5,6.6,junk");
REQUIRE(c.valid()); REQUIRE(c.valid());
@ -72,6 +80,36 @@ TEST_CASE("testing valid conversions") {
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6}); CHECK(tup == std::tuple{5, 6.6});
} }
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5;6.6",
";");
REQUIRE(c.valid());
REQUIRE(std::get<0>(tup).has_value());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5.4;6.6",
";");
REQUIRE(c.valid());
REQUIRE(!std::get<0>(tup).has_value());
CHECK(tup == std::tuple{std::nullopt, 6.6});
}
{
auto tup = c.convert<void, std::variant<int, double>,
double>("junk;5;6.6", ";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6});
}
{
auto tup = c.convert<void, std::variant<int, double>,
double>("junk;5.5;6.6", ";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6});
}
} }
TEST_CASE("testing invalid conversions") { TEST_CASE("testing invalid conversions") {
@ -100,6 +138,9 @@ TEST_CASE("testing invalid conversions") {
c.convert<void, int>("junk,x"); c.convert<void, int>("junk,x");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<void, std::variant<int, double>, double>("junk;.5.5;6", ";");
REQUIRE(!c.valid());
} }
TEST_CASE("testing ss:ax restriction (all except)") { TEST_CASE("testing ss:ax restriction (all except)") {
@ -276,7 +317,8 @@ TEST_CASE("testing ss:ne restriction (not empty)") {
CHECK(tup == "s"); CHECK(tup == "s");
} }
{ {
auto tup = c.convert<int, ss::ne<std::string>>("1,s"); auto tup =
c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{1, "s"}); CHECK(tup == std::tuple{1, "s"});
} }

View File

@ -175,3 +175,159 @@ TEST_CASE("testing extract functions for char values") {
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
} }
} }
TEST_CASE("testing extract functions for std::optional") {
for (const auto& [i, s] :
{std::pair<std::optional<int>, std::string>{1, "1"},
{69, "69"},
{-4, "-4"}}) {
std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value());
CHECK(*v == i);
}
for (const auto& [c, s] :
{std::pair<std::optional<char>, std::string>{'a', "a"},
{'x', "x"},
{' ', " "}}) {
std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value());
CHECK(*v == c);
}
for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value());
}
for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value());
}
}
#define REQUIRE_VARIANT(var, el, type) \
{ \
auto ptr = std::get_if<type>(&var); \
REQUIRE(ptr); \
REQUIRE(el == *ptr); \
}
#define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative<type>(var));
TEST_CASE("testing extract functions for std::variant") {
{
std::string s = "22";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, double);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, int);
}
{
std::variant<double, int, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22", std::string);
}
{
std::variant<int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, 22, int);
}
}
{
std::string s = "22.2";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<double, int, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string);
}
}
{
std::string s = "2.2.2";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<double, std::string, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<int, double> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
CHECK_NOT_VARIANT(var, double);
}
{
std::variant<double, int> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, double{}, double);
CHECK_NOT_VARIANT(var, int);
}
{
std::variant<int> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
}
}
}