diff options
| author | Martin Hořeňovský | 2021-10-27 18:57:56 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2021-10-27 20:15:28 +0200 |
| commit | 12d14a3c63e8002017cfead6b632db4a067d125b (patch) | |
| tree | 485a0e990517c6f5655fa04de68b6ff9b8265797 /tests/SelfTest/IntrospectiveTests/Clara.tests.cpp | |
| parent | f17725a186d209a26836fc3e13cdcee423ddd1e8 (diff) | |
| download | catch3-12d14a3c63e8002017cfead6b632db4a067d125b.tar.gz | |
Add support for multiply calling lambda parsers in Clara
Previously a lambda parser in Clara could only be invoked once,
even if it internally was ok with being invoked multiple times.
With this change, a lambda parser can mark itself as `accept_many`,
in which case it will be invoked multiple times if the appropriate
flag was supplied multiple times by the user.
Diffstat (limited to 'tests/SelfTest/IntrospectiveTests/Clara.tests.cpp')
| -rw-r--r-- | tests/SelfTest/IntrospectiveTests/Clara.tests.cpp | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp b/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp index 1259e7ff..054cda55 100644 --- a/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp @@ -4,19 +4,70 @@ // https://www.boost.org/LICENSE_1_0.txt) // SPDX-License-Identifier: BSL-1.0 - + #include <catch2/catch_test_macros.hpp> #include <catch2/internal/catch_clara.hpp> #include <string> +TEST_CASE("is_unary_function", "[clara][compilation]") { + auto unary1 = [](int) {}; + auto unary2 = [](std::string const&) {}; + auto const unary3 = [](std::string const&) {}; + auto unary4 = [](int) { return 42; }; + void unary5(char); + double unary6(long); + + double binary1(long, int); + auto binary2 = [](int, char) {}; + auto nullary1 = []() {}; + auto nullary2 = []() {return 42;}; + + STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary1)>::value); + STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary2)>::value); + STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary3)>::value); + STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary4)>::value); + STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary5)>::value); + STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary6)>::value); + + STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(binary1)>::value); + STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(binary2)>::value); + STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(nullary1)>::value); + STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(nullary2)>::value); + STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<int>::value); + STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<std::string const&>::value); +} + + TEST_CASE("Clara::Arg supports single-arg parse the way Opt does", "[clara][arg][compilation]") { std::string name; auto p = Catch::Clara::Arg(name, "just one arg"); - + CHECK(name.empty()); - + p.parse( Catch::Clara::Args{ "UnitTest", "foo" } ); REQUIRE(name == "foo"); } + +TEST_CASE("Clara::Opt supports accept-many lambdas", "[clara][opt]") { + std::string name; + using namespace Catch::Clara; + std::vector<std::string> res; + const auto push_to_res = [&](std::string const& s) { + res.push_back(s); + return ParserResult::ok(); + }; + + SECTION("Parsing fails on multiple options without accept_many") { + auto p = Parser() | Opt(push_to_res, "value")["-o"]; + auto parse_result = p.parse( Args{ "UnitTest", "-o", "aaa", "-o", "bbb" } ); + CHECK_FALSE(parse_result); + } + SECTION("Parsing succeeds on multiple options with accept_many") { + auto p = Parser() | Opt(accept_many, push_to_res, "value")["-o"]; + auto parse_result = p.parse( Args{ "UnitTest", "-o", "aaa", "-o", "bbb" } ); + CHECK(parse_result); + CHECK(res == std::vector<std::string>{ "aaa", "bbb" }); + } +} |