121 lines
3.8 KiB
C++
121 lines
3.8 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_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());
|
|
}
|