diff --git a/test/test_converter.cpp b/test/test_converter.cpp index 5e5fa6a..76f6643 100644 --- a/test/test_converter.cpp +++ b/test/test_converter.cpp @@ -57,6 +57,14 @@ TEST_CASE("testing valid conversions") { 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()); @@ -72,6 +80,36 @@ TEST_CASE("testing valid conversions") { REQUIRE(c.valid()); CHECK(tup == std::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::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::tuple{std::nullopt, 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::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::tuple{std::variant{5.5}, 6.6}); + } } TEST_CASE("testing invalid conversions") { @@ -100,6 +138,9 @@ TEST_CASE("testing invalid conversions") { 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)") { @@ -276,7 +317,8 @@ TEST_CASE("testing ss:ne restriction (not empty)") { CHECK(tup == "s"); } { - auto tup = c.convert>("1,s"); + auto tup = + c.convert, ss::ne>("1,s"); REQUIRE(c.valid()); CHECK(tup == std::tuple{1, "s"}); } diff --git a/test/test_extractions.cpp b/test/test_extractions.cpp index 718bf97..5900d59 100644 --- a/test/test_extractions.cpp +++ b/test/test_extractions.cpp @@ -175,3 +175,159 @@ TEST_CASE("testing extract functions for char values") { 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::string>{1, "1"}, + {69, "69"}, + {-4, "-4"}}) { + std::optional 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::string>{'a', "a"}, + {'x', "x"}, + {' ', " "}}) { + std::optional 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 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 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(&var); \ + REQUIRE(ptr); \ + REQUIRE(el == *ptr); \ + } + +#define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative(var)); + +TEST_CASE("testing extract functions for std::variant") { + { + std::string s = "22"; + { + std::variant 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 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 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 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 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 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 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 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 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 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 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 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 var; + REQUIRE( + !ss::extract(s.c_str(), s.c_str() + s.size(), var)); + + REQUIRE_VARIANT(var, int{}, int); + } + } +}