mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Cleaned up unit tests. Added test to implement choices inside .action(...)
This commit is contained in:
parent
0c71e3c6f2
commit
463aded164
@ -4,4 +4,6 @@
|
|||||||
#include <test_add_argument.hpp>
|
#include <test_add_argument.hpp>
|
||||||
#include <test_parse_args.hpp>
|
#include <test_parse_args.hpp>
|
||||||
#include <test_positional_arguments.hpp>
|
#include <test_positional_arguments.hpp>
|
||||||
#include <test_implicit_values.hpp>
|
#include <test_optional_arguments.hpp>
|
||||||
|
#include <test_compound_arguments.hpp>
|
||||||
|
#include <test_actions.hpp>
|
21
tests/test_actions.hpp
Normal file
21
tests/test_actions.hpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <catch.hpp>
|
||||||
|
#include <argparse.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("Users can use defaul value inside actions", "[actions]") {
|
||||||
|
argparse::ArgumentParser program("test");
|
||||||
|
program.add_argument("input")
|
||||||
|
.default_value("bar")
|
||||||
|
.action([=](const std::string& value) {
|
||||||
|
static const std::vector<std::string> choices = { "foo", "bar", "baz" };
|
||||||
|
if (std::find(choices.begin(), choices.end(), value) != choices.end()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return std::string{ "bar" };
|
||||||
|
});
|
||||||
|
|
||||||
|
program.parse_args({ "test", "fez" });
|
||||||
|
auto arguments = program.get_arguments();
|
||||||
|
REQUIRE(arguments.size() == 1);
|
||||||
|
REQUIRE(program.get("input") == "bar");
|
||||||
|
}
|
@ -2,53 +2,7 @@
|
|||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include <argparse.hpp>
|
#include <argparse.hpp>
|
||||||
|
|
||||||
TEST_CASE("Parse toggle arguments with default value", "[parse_args]") {
|
TEST_CASE("Parse compound toggle arguments with implicit values", "[compound_arguments]") {
|
||||||
argparse::ArgumentParser program("test");
|
|
||||||
program.add_argument("--verbose", "-v")
|
|
||||||
.default_value(false)
|
|
||||||
.implicit_value(true);
|
|
||||||
|
|
||||||
program.parse_args({ "./test.exe" });
|
|
||||||
auto arguments = program.get_arguments();
|
|
||||||
REQUIRE(arguments.size() == 2);
|
|
||||||
REQUIRE(program.get<bool>("--verbose") == false);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Parse toggle arguments with implicit value", "[parse_args]") {
|
|
||||||
argparse::ArgumentParser program("test");
|
|
||||||
program.add_argument("--verbose")
|
|
||||||
.default_value(false)
|
|
||||||
.implicit_value(true);
|
|
||||||
|
|
||||||
program.parse_args({ "./test.exe", "--verbose" });
|
|
||||||
auto arguments = program.get_arguments();
|
|
||||||
REQUIRE(arguments.size() == 1);
|
|
||||||
REQUIRE(program.get<bool>("--verbose") == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Parse multiple toggle arguments with implicit values", "[parse_args]") {
|
|
||||||
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" });
|
|
||||||
auto arguments = program.get_arguments();
|
|
||||||
REQUIRE(arguments.size() == 3);
|
|
||||||
REQUIRE(program.get<bool>("-a") == true);
|
|
||||||
REQUIRE(program.get<bool>("-u") == false);
|
|
||||||
REQUIRE(program.get<bool>("-x") == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Parse compound toggle arguments with implicit values", "[parse_args]") {
|
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
program.add_argument("-a")
|
program.add_argument("-a")
|
||||||
.default_value(false)
|
.default_value(false)
|
||||||
@ -70,7 +24,7 @@ TEST_CASE("Parse compound toggle arguments with implicit values", "[parse_args]"
|
|||||||
REQUIRE(program.get<bool>("-x") == true);
|
REQUIRE(program.get<bool>("-x") == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[parse_args]") {
|
TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[compound_arguments]") {
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
program.add_argument("-a")
|
program.add_argument("-a")
|
||||||
.default_value(false)
|
.default_value(false)
|
||||||
@ -88,7 +42,7 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[pa
|
|||||||
.nargs(3);
|
.nargs(3);
|
||||||
|
|
||||||
program.parse_args({ "./test.exe", "-abc", "3.14", "2.718", "--input_files",
|
program.parse_args({ "./test.exe", "-abc", "3.14", "2.718", "--input_files",
|
||||||
"a.txt", "b.txt", "c.txt"});
|
"a.txt", "b.txt", "c.txt" });
|
||||||
auto arguments = program.get_arguments();
|
auto arguments = program.get_arguments();
|
||||||
REQUIRE(arguments.size() == 4);
|
REQUIRE(arguments.size() == 4);
|
||||||
REQUIRE(program.get<bool>("-a") == true);
|
REQUIRE(program.get<bool>("-a") == true);
|
||||||
@ -104,7 +58,7 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[pa
|
|||||||
REQUIRE(input_files[2] == "c.txt");
|
REQUIRE(input_files[2] == "c.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parse compound toggle arguments with implicit values and nargs and other positional arguments", "[parse_args]") {
|
TEST_CASE("Parse compound toggle arguments with implicit values and nargs and other positional arguments", "[compound_arguments]") {
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
|
|
||||||
program.add_argument("numbers")
|
program.add_argument("numbers")
|
49
tests/test_optional_arguments.hpp
Normal file
49
tests/test_optional_arguments.hpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#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" });
|
||||||
|
auto arguments = program.get_arguments();
|
||||||
|
REQUIRE(arguments.size() == 2);
|
||||||
|
REQUIRE(program.get<bool>("--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" });
|
||||||
|
auto arguments = program.get_arguments();
|
||||||
|
REQUIRE(arguments.size() == 1);
|
||||||
|
REQUIRE(program.get<bool>("--verbose") == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
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" });
|
||||||
|
auto arguments = program.get_arguments();
|
||||||
|
REQUIRE(arguments.size() == 3);
|
||||||
|
REQUIRE(program.get<bool>("-a") == true);
|
||||||
|
REQUIRE(program.get<bool>("-u") == false);
|
||||||
|
REQUIRE(program.get<bool>("-x") == true);
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include <argparse.hpp>
|
#include <argparse.hpp>
|
||||||
|
|
||||||
TEST_CASE("Parse positional arguments", "[parse_args]") {
|
TEST_CASE("Parse positional arguments", "[positional_arguments]") {
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
program.add_argument("input");
|
program.add_argument("input");
|
||||||
program.add_argument("output");
|
program.add_argument("output");
|
||||||
@ -13,7 +13,7 @@ TEST_CASE("Parse positional arguments", "[parse_args]") {
|
|||||||
REQUIRE(program.get("output") == "thrust_profile.csv");
|
REQUIRE(program.get("output") == "thrust_profile.csv");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parse positional arguments with fixed nargs", "[parse_args]") {
|
TEST_CASE("Parse positional arguments with fixed nargs", "[positional_arguments]") {
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
program.add_argument("input");
|
program.add_argument("input");
|
||||||
program.add_argument("output").nargs(2);
|
program.add_argument("output").nargs(2);
|
||||||
@ -27,7 +27,7 @@ TEST_CASE("Parse positional arguments with fixed nargs", "[parse_args]") {
|
|||||||
REQUIRE(outputs[1] == "output.mesh");
|
REQUIRE(outputs[1] == "output.mesh");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parse positional arguments with optional arguments", "[parse_args]") {
|
TEST_CASE("Parse positional arguments with optional arguments", "[positional_arguments]") {
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
program.add_argument("input");
|
program.add_argument("input");
|
||||||
program.add_argument("output").nargs(2);
|
program.add_argument("output").nargs(2);
|
||||||
|
Loading…
Reference in New Issue
Block a user