| 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_v<decltype(unary1)>); |
| 29 |
STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function_v<decltype(unary2)>); |
| 30 |
STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function_v<decltype(unary3)>); |
| 31 |
STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function_v<decltype(unary4)>); |
| 32 |
STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function_v<decltype(unary5)>); |
| 33 |
STATIC_REQUIRE(Catch::Clara::Detail::is_unary_function_v<decltype(unary6)>); |
| 34 |
|
| 35 |
STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function_v<decltype(binary1)>); |
| 36 |
STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function_v<decltype(binary2)>); |
| 37 |
STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function_v<decltype(nullary1)>); |
| 38 |
STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function_v<decltype(nullary2)>); |
| 39 |
STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function_v<int>); |
| 40 |
STATIC_REQUIRE_FALSE(Catch::Clara::Detail::is_unary_function_v<std::string const&>); |
| 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::Arg does not crash on incomplete input", "[clara][arg][compilation]") { |
| 55 |
std::string name; |
| 56 |
auto p = Catch::Clara::Arg(name, "-"); |
| 57 |
|
| 58 |
CHECK(name.empty()); |
| 59 |
|
| 60 |
auto result = p.parse( Catch::Clara::Args{ "UnitTest", "-" } ); |
| 61 |
CHECK( result ); |
| 62 |
CHECK( result.type() == Catch::Clara::Detail::ResultType::Ok ); |
| 63 |
const auto& parsed = result.value(); |
| 64 |
CHECK( parsed.type() == Catch::Clara::ParseResultType::NoMatch ); |
| 65 |
CHECK( parsed.remainingTokens().count() == 2 ); |
| 66 |
CHECK( name.empty() ); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("Clara::Opt supports accept-many lambdas", "[clara][opt]") { |
| 70 |
using namespace Catch::Clara; |
| 71 |
std::vector<std::string> res; |
| 72 |
const auto push_to_res = [&](std::string const& s) { |
| 73 |
res.push_back(s); |
| 74 |
return ParserResult::ok( ParseResultType::Matched ); |
| 75 |
}; |
| 76 |
|
| 77 |
SECTION("Parsing fails on multiple options without accept_many") { |
| 78 |
auto p = Parser() | Opt(push_to_res, "value")["-o"]; |
| 79 |
auto parse_result = p.parse( Args{ "UnitTest", "-o", "aaa", "-o", "bbb" } ); |
| 80 |
CHECK_FALSE(parse_result); |
| 81 |
} |
| 82 |
SECTION("Parsing succeeds on multiple options with accept_many") { |
| 83 |
auto p = Parser() | Opt(accept_many, push_to_res, "value")["-o"]; |
| 84 |
auto parse_result = p.parse( Args{ "UnitTest", "-o", "aaa", "-o", "bbb" } ); |
| 85 |
CHECK(parse_result); |
| 86 |
CHECK(res == std::vector<std::string>{ "aaa", "bbb" }); |
| 87 |
} |
| 88 |
} |