Files
Library/tests/standalone/Configuration/ArgparseReflect.cpp
T

153 lines
5.0 KiB
C++

// @requires-reflection
#include <Void/Configuration/Argparse.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <optional>
#include <string>
namespace arg = Void::Argparse;
namespace
{
struct Opts {
[[= arg::Short{
'v'}]][[= arg::Help{"verbose output"}]] bool verbose = false;
[[= arg::Help{"output path"}]] std::string out = "a.out";
std::optional<std::string> config;
[[= arg::Positional{}]] std::string input = "";
};
} // namespace
// @tag reflection
Void::Testing::Test argparse_reflective_parse()
{
const char* argv[] = {"prog", "-v", "--out=x.bin", "foo.txt"};
const auto o = arg::parse<Opts>(4, argv, "prog");
Void::Testing::expect(o.has_value());
Void::Testing::expect_eq(o->verbose, true);
Void::Testing::expect_eq(o->out, std::string{"x.bin"});
Void::Testing::expect_eq(o->input, std::string{"foo.txt"});
Void::Testing::expect(!o->config.has_value());
}
// @tag reflection
Void::Testing::Test argparse_reflective_defaults()
{
const char* argv[] = {"prog", "bar.txt"};
const auto o = arg::parse<Opts>(2, argv, "prog");
Void::Testing::expect(o.has_value());
Void::Testing::expect_eq(o->verbose, false); // flag default
Void::Testing::expect_eq(o->out, std::string{"a.out"}); // option default
Void::Testing::expect_eq(o->input, std::string{"bar.txt"});
}
Void::Testing::Test argparse_reflect_numeric_fields()
{
struct Options {
int count = 3;
unsigned retries = 1;
double ratio = 0.5;
};
const char* default_argv[] = {"app"};
auto defaults = Void::Argparse::parse<Options>(1, default_argv);
Void::Testing::require(defaults.has_value());
Void::Testing::expect_eq(defaults->count, 3);
Void::Testing::expect_eq(defaults->retries, 1U);
const char* argv[] = {"app", "--count", "42", "--retries", "7",
"--ratio", "0.25"};
auto parsed = Void::Argparse::parse<Options>(7, argv);
Void::Testing::require(parsed.has_value());
Void::Testing::expect_eq(parsed->count, 42);
Void::Testing::expect_eq(parsed->retries, 7U);
Void::Testing::expect(parsed->ratio > 0.24 && parsed->ratio < 0.26);
const char* bad_argv[] = {"app", "--count", "not-a-number"};
auto bad = Void::Argparse::parse<Options>(3, bad_argv);
Void::Testing::expect(!bad.has_value());
}
Void::Testing::Test argparse_reflect_optional_and_vector_numerics()
{
struct Options {
std::optional<int> port;
std::vector<int> ids;
};
const char* absent[] = {"app"};
auto defaults = Void::Argparse::parse<Options>(1, absent);
Void::Testing::require(defaults.has_value());
Void::Testing::expect(!defaults->port.has_value());
Void::Testing::expect(defaults->ids.empty());
const char* argv[] = {"app", "--port", "8080", "--ids", "1", "--ids", "2",
"--ids", "3"};
auto parsed = Void::Argparse::parse<Options>(9, argv);
Void::Testing::require(parsed.has_value());
Void::Testing::require(parsed->port.has_value());
Void::Testing::expect_eq(*parsed->port, 8080);
Void::Testing::expect_eq(parsed->ids.size(), std::size_t{3});
Void::Testing::expect_eq(parsed->ids[2], 3);
const char* bad[] = {"app", "--ids", "nope"};
auto rejected = Void::Argparse::parse<Options>(3, bad);
Void::Testing::expect(!rejected.has_value());
}
Void::Testing::Test argparse_reflect_value_name_alias_and_hide()
{
struct Options {
[[= Void::Argparse::ValueName{"PORT"}]]
[[= Void::Argparse::Alias{"--bind"}]]
std::string address = "";
[[= Void::Argparse::Hide{}]] std::string secret = "";
};
const char* argv[] = {"app", "--bind", "127.0.0.1", "--secret", "x"};
auto parsed = Void::Argparse::parse<Options>(5, argv);
Void::Testing::require(parsed.has_value());
Void::Testing::expect_eq(parsed->address, std::string{"127.0.0.1"});
Void::Testing::expect_eq(parsed->secret, std::string{"x"});
// The fluent API exposes the same customization.
std::optional<std::string> value;
Void::Argparse::Parser parser{"tool"};
parser.option("--out", "-o", "output", value)
.value_name("--out", "FILE")
.alias("--out", "--output")
.hide("--out");
const std::string help = parser.help();
Void::Testing::expect(help.find("--out") == std::string::npos);
const char* alias_argv[] = {"tool", "--output", "f.txt"};
auto result = parser.parse_detailed(3, alias_argv);
Void::Testing::expect(result.ok());
Void::Testing::require(value.has_value());
Void::Testing::expect_eq(*value, std::string{"f.txt"});
}
Void::Testing::Test argparse_reflect_enum_fields()
{
enum class Color { Red, Green, Blue };
struct Options {
Color color = Color::Red;
};
const char* default_argv[] = {"app"};
auto defaults = Void::Argparse::parse<Options>(1, default_argv);
Void::Testing::require(defaults.has_value());
Void::Testing::expect(defaults->color == Color::Red);
const char* argv[] = {"app", "--color", "Blue"};
auto parsed = Void::Argparse::parse<Options>(3, argv);
Void::Testing::require(parsed.has_value());
Void::Testing::expect(parsed->color == Color::Blue);
const char* bad_argv[] = {"app", "--color", "Purple"};
auto bad = Void::Argparse::parse<Options>(3, bad_argv);
Void::Testing::expect(!bad.has_value());
}