Clara.tests.cpp 2.9 KB

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