make on_error accept lambda without arguments, update tests

This commit is contained in:
ado 2020-12-26 00:42:01 +01:00
parent 371da072be
commit 16f4648cb3
2 changed files with 15 additions and 11 deletions

View File

@ -120,9 +120,14 @@ class parser {
} }
template <typename Fun> template <typename Fun>
auto on_error(Fun fun) { auto on_error(Fun&& fun) {
if (!parser_.valid()) { if (!parser_.valid()) {
fun(parser_.error_msg()); if constexpr (std::is_invocable_v<Fun>) {
fun();
} else {
std::invoke(std::forward<Fun>(fun),
parser_.error_msg());
}
} }
return *this; return *this;
} }

View File

@ -189,7 +189,6 @@ TEST_CASE("testing composite conversion") {
ss::parser p{f.name, ","}; ss::parser p{f.name, ","};
auto fail = [] { FAIL(""); }; auto fail = [] { FAIL(""); };
auto fail_if_error = [](auto& error) { CHECK(error.empty()); };
auto expect_error = [](auto error) { CHECK(!error.empty()); }; auto expect_error = [](auto error) { CHECK(!error.empty()); };
REQUIRE(p.valid()); REQUIRE(p.valid());
@ -203,7 +202,7 @@ TEST_CASE("testing composite conversion") {
.or_else<test_struct>(fail) .or_else<test_struct>(fail)
.or_else<int, char, double>( .or_else<int, char, double>(
[](auto&& data) { CHECK(data == expectedData); }) [](auto&& data) { CHECK(data == expectedData); })
.on_error(fail_if_error) .on_error(fail)
.or_else<test_tuple>(fail) .or_else<test_tuple>(fail)
.values(); .values();
@ -224,11 +223,11 @@ TEST_CASE("testing composite conversion") {
[](auto& i1, auto i2, double d) { [](auto& i1, auto i2, double d) {
CHECK(std::tie(i1, i2, d) == expectedData); CHECK(std::tie(i1, i2, d) == expectedData);
}) })
.on_error(fail_if_error) .on_error(fail)
.or_else_object<test_struct, int, double, char>(fail) .or_else_object<test_struct, int, double, char>(fail)
.on_error(fail_if_error) .on_error(fail)
.or_else<test_tuple>(fail) .or_else<test_tuple>(fail)
.on_error(fail_if_error) .on_error(fail)
.or_else<int, char, double>(fail) .or_else<int, char, double>(fail)
.values(); .values();
@ -313,9 +312,9 @@ TEST_CASE("testing composite conversion") {
REQUIRE(!p.eof()); REQUIRE(!p.eof());
auto [d1, d2] = p.try_next<int, std::optional<int>>() auto [d1, d2] = p.try_next<int, std::optional<int>>()
.on_error(fail_if_error) .on_error(fail)
.or_else<std::tuple<int, std::string>>(fail) .or_else<std::tuple<int, std::string>>(fail)
.on_error(fail_if_error) .on_error(fail)
.values(); .values();
REQUIRE(p.valid()); REQUIRE(p.valid());
@ -329,9 +328,9 @@ TEST_CASE("testing composite conversion") {
auto [d1, d2] = auto [d1, d2] =
p.try_next<int, std::variant<int, std::string>>() p.try_next<int, std::variant<int, std::string>>()
.on_error(fail_if_error) .on_error(fail)
.or_else<std::tuple<int, std::string>>(fail) .or_else<std::tuple<int, std::string>>(fail)
.on_error(fail_if_error) .on_error(fail)
.values(); .values();
REQUIRE(p.valid()); REQUIRE(p.valid());