aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-29 19:28:09 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-29 19:28:09 +0100
commit67b2fae1fa8b033045a44c1355d9dfd8f83e0d9b (patch)
tree1ecd818f4bcf7d12622d43dc92c4d4bb9b746d0f /contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp
parenta8b0ea58e60bb0326b7f7c8f3c736d89ce9ef1df (diff)
parentbc524d70253a4ab2fe40c3ca3e5666e267c0a4d1 (diff)
downloadnihil-67b2fae1fa8b033045a44c1355d9dfd8f83e0d9b.tar.gz
nihil-67b2fae1fa8b033045a44c1355d9dfd8f83e0d9b.tar.bz2
Add 'contrib/catch2/' from commit 'bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1'
git-subtree-dir: contrib/catch2 git-subtree-mainline: a8b0ea58e60bb0326b7f7c8f3c736d89ce9ef1df git-subtree-split: bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1
Diffstat (limited to 'contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp')
-rw-r--r--contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp b/contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp
new file mode 100644
index 0000000..53023b5
--- /dev/null
+++ b/contrib/catch2/tests/SelfTest/IntrospectiveTests/Clara.tests.cpp
@@ -0,0 +1,88 @@
+
+// Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.txt or copy at
+// 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::Arg does not crash on incomplete input", "[clara][arg][compilation]") {
+ std::string name;
+ auto p = Catch::Clara::Arg(name, "-");
+
+ CHECK(name.empty());
+
+ auto result = p.parse( Catch::Clara::Args{ "UnitTest", "-" } );
+ CHECK( result );
+ CHECK( result.type() == Catch::Clara::Detail::ResultType::Ok );
+ const auto& parsed = result.value();
+ CHECK( parsed.type() == Catch::Clara::ParseResultType::NoMatch );
+ CHECK( parsed.remainingTokens().count() == 2 );
+ CHECK( name.empty() );
+}
+
+TEST_CASE("Clara::Opt supports accept-many lambdas", "[clara][opt]") {
+ 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( ParseResultType::Matched );
+ };
+
+ 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" });
+ }
+}