argparse/test/test_optional_arguments.hpp
Zhihao Yuan 8201a18568
Fix various issues in Argument constructor
Before this change:

1. When the input is built-in string literal or cv-`char*`,
   `is_optional` constructs temporary `std::string` while
   `mNames` initializer is also constructing `std::string`
   due to the use of `std::initializer_list`.
2. When the input is `std::string_view`, doesn't compile.
3. When the input is `std::string`, `mNames` initializer
   moves `args`.  If argument name is longer than
   `std::string`'s SSO buffer, bad thing will happen because
   `is_optional` will be accessing `args` in moved-from
   states.

Because of the use of `strtol` which expects nul-terminated
input, `is_*` series functions must take `std::string`.  This
restriction may be removed after AppleClang adds `<charconv>`.
But for now, it complicates the patch.  My solution is to
create an array prvalue still, but use a array reference
rather than `std::initializer_list` to refer to it, so that
the code in delegated constructor can keep using fold
expressions after the necessary `std::string` objects being
created.
2019-11-17 01:51:36 -06:00

66 lines
2.0 KiB
C++

#pragma once
#include <catch.hpp>
#include <argparse.hpp>
TEST_CASE("Parse toggle arguments with default value", "[optional_arguments]") {
argparse::ArgumentParser program("test");
program.add_argument("--verbose", "-v")
.default_value(false)
.implicit_value(true);
program.parse_args({ "./test.exe" });
REQUIRE(program.get<bool>("--verbose") == false);
REQUIRE(program["--verbose"] == false);
}
TEST_CASE("Parse toggle arguments with implicit value", "[optional_arguments]") {
argparse::ArgumentParser program("test");
program.add_argument("--verbose")
.default_value(false)
.implicit_value(true);
program.parse_args({ "./test.exe", "--verbose" });
REQUIRE(program.get<bool>("--verbose") == true);
REQUIRE(program["--verbose"] == true);
REQUIRE(program["--verbose"] != false);
}
TEST_CASE("Parse multiple toggle arguments with implicit values", "[optional_arguments]") {
argparse::ArgumentParser program("test");
program.add_argument("-a")
.default_value(false)
.implicit_value(true);
program.add_argument("-u")
.default_value(false)
.implicit_value(true);
program.add_argument("-x")
.default_value(false)
.implicit_value(true);
program.parse_args({ "./test.exe", "-a", "-x" });
REQUIRE(program.get<bool>("-a") == true);
REQUIRE(program.get<bool>("-u") == false);
REQUIRE(program.get<bool>("-x") == true);
}
TEST_CASE("Parse arguments of different types", "[optional_arguments]") {
using namespace std::literals;
argparse::ArgumentParser program("test");
program.add_argument("--this-argument-is-longer-than-any-sso-buffer-that-"
"makes-sense-unless-your-cache-line-is-this-long"s);
REQUIRE_NOTHROW(program.parse_args({"test"}));
program.add_argument("-string"s, "-string-view"sv, "-builtin")
.default_value(false)
.implicit_value(true);
program.parse_args({"test", "-string-view"});
REQUIRE(program["-string"sv] == true);
REQUIRE(program["-string-view"] == true);
REQUIRE(program["-builtin"s] == true);
}