| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/internal/catch_clara.hpp> |
| 11 | |
| 12 | |
| 13 | #include <string> |
| 14 | |
| 15 | TEST_CASE("is_unary_function", "[clara][compilation]") { |
| 16 | auto unary1 = [](int) {}; |
| 17 | auto unary2 = [](std::string const&) {}; |
| 18 | auto const unary3 = [](std::string const&) {}; |
| 19 | auto unary4 = [](int) { return 42; }; |
| 20 | void unary5(char); |
| 21 | double unary6(long); |
| 22 | |
| 23 | double binary1(long, int); |
| 24 | auto binary2 = [](int, char) {}; |
| 25 | auto nullary1 = []() {}; |
| 26 | auto nullary2 = []() {return 42;}; |
| 27 | |
| 28 | STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary1)>::value); |
| 29 | STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary2)>::value); |
| 30 | STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary3)>::value); |
| 31 | STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary4)>::value); |
| 32 | STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary5)>::value); |
| 33 | STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function<decltype(unary6)>::value); |
| 34 | |
| 35 | STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(binary1)>::value); |
| 36 | STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(binary2)>::value); |
| 37 | STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(nullary1)>::value); |
| 38 | STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<decltype(nullary2)>::value); |
| 39 | STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<int>::value); |
| 40 | STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function<std::string const&>::value); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | TEST_CASE("Clara::Arg supports single-arg parse the way Opt does", "[clara][arg][compilation]") { |
| 45 | std::string name; |
| 46 | auto p = Catch::Clara::Arg(name, "just one arg"); |
| 47 | |
| 48 | CHECK(name.empty()); |
| 49 | |
| 50 | p.parse( Catch::Clara::Args{ "UnitTest", "foo" } ); |
| 51 | REQUIRE(name == "foo"); |
| 52 | } |
| 53 | |
| 54 | TEST_CASE("Clara::Opt supports accept-many lambdas", "[clara][opt]") { |
| 55 | using namespace Catch::Clara; |
| 56 | std::vector<std::string> res; |
| 57 | const auto push_to_res = [&](std::string const& s) { |
| 58 | res.push_back(s); |
| 59 | return ParserResult::ok( ParseResultType::Matched ); |
| 60 | }; |
| 61 | |
| 62 | SECTION("Parsing fails on multiple options without accept_many") { |
| 63 | auto p = Parser() | Opt(push_to_res, "value")["-o"]; |
| 64 | auto parse_result = p.parse( Args{ "UnitTest", "-o", "aaa", "-o", "bbb" } ); |
| 65 | CHECK_FALSE(parse_result); |
| 66 | } |
| 67 | SECTION("Parsing succeeds on multiple options with accept_many") { |
| 68 | auto p = Parser() | Opt(accept_many, push_to_res, "value")["-o"]; |
| 69 | auto parse_result = p.parse( Args{ "UnitTest", "-o", "aaa", "-o", "bbb" } ); |
| 70 | CHECK(parse_result); |
| 71 | CHECK(res == std::vector<std::string>{ "aaa", "bbb" }); |
| 72 | } |
| 73 | } |