libcruft-util/test/cmdopt2.cpp

191 lines
4.6 KiB
C++

#include <cruft/util/tap.hpp>
#include <cruft/util/cmdopt2/parser.hpp>
#include <cruft/util/cmdopt2/args.hpp>
#include <iostream>
///////////////////////////////////////////////////////////////////////////////
static void
test_combinations (cruft::TAP::logger &tap)
{
static const struct {
std::vector<char const*> args;
int foo;
std::optional<float> bar;
std::string qux;
bool verbose;
} TESTS[] = {
{
.args = { "cmd", "-f", "1", "--bar", "2", "val" },
.foo = 1,
.bar = 2,
.qux = "val",
.verbose = false,
},
{
.args = { "cmd", "--bar", "2", "-f", "1", "val" },
.foo = 1,
.bar = 2,
.qux = "val",
.verbose = false,
},
{
.args = { "cmd", "--bar", "2", "val", "-f", "1", },
.foo = 1,
.bar = 2,
.qux = "val",
.verbose = false,
},
{
.args = { "cmd", "-v", "--bar", "2", "val", "-f", "1", },
.foo = 1,
.bar = 2,
.qux = "val",
.verbose = true,
},
{
.args = { "cmd", "--bar", "2", "val", "-f", "1", },
.foo = 1,
.bar = 2,
.qux = "val",
.verbose = false,
},
{
.args = { "cmd", "val", "--bar", "2", "-f", "1", },
.foo = 1,
.bar = 2,
.qux = "val",
.verbose = false,
},
{
.args = { "cmd", "val", "-v", "-f", "1", },
.foo = 1,
.bar = {},
.qux = "val",
.verbose = true,
},
{
.args = { "cmd", "val", "-f", "1", },
.foo = 1,
.bar = {},
.qux = "val",
.verbose = true,
},
};
int foo;
std::optional<float> bar;
std::string qux;
bool verbose;
using namespace cruft::cmdopt2;
parser p;
p.add (keyword::create ("foo").flag ().flag ('f').bind (foo));
p.add (keyword::create ("bar").bind (bar));
p.add (positional::create ("qux").bind (qux));
p.add (keyword::create ("verbose").flag ().flag ('v').present (verbose));
for (auto const &t: TESTS) {
foo = decltype(foo) {};
bar = decltype(bar) {};
qux = decltype(qux) {};
verbose = false;
p.parse (int (t.args.size ()), t.args.data ());
tap.expect (
foo == t.foo and bar == t.bar and qux == t.qux,
"{}", fmt::join (t.args.begin (), t.args.end (), " ")
);
}
}
///////////////////////////////////////////////////////////////////////////////
static void test_presence (cruft::TAP::logger &tap)
{
static const struct {
std::vector<char const*> args;
int count;
bool present;
} TESTS[] = {
{
.args = { "cmd", "-v", },
.count = 1,
.present = false,
},
{
.args = { "cmd", "-vvv", },
.count = 3,
.present = false,
},
{
.args = { "cmd", "-v", "-vv"},
.count = 3,
.present = false,
},
{
.args = { "cmd", },
.count = 0,
.present = false,
},
{
.args = { "cmd", "-p"},
.count = 0,
.present = true,
},
{
.args = { "cmd", "-ppp"},
.count = 0,
.present = true,
},
{
.args = { "cmd", "-p", "-p"},
.count = 0,
.present = true,
},
{
.args = { "cmd", "-p", "-v", "-p"},
.count = 1,
.present = true,
},
{
.args = { "cmd", "-p", "-v", "-p", "-v"},
.count = 2,
.present = true,
},
};
int count;
bool present;
using namespace cruft::cmdopt2;
parser p;
p.add (keyword::create ("verbose").flag ().flag ('v').count (count));
p.add (keyword::create ("present").flag ().flag ('p').present (present));
for (auto const &t: TESTS) {
count = 0;
present = false;
p.parse (int (t.args.size ()), t.args.data ());
tap.expect (
count == t.count and present == t.present,
"{}", fmt::join (t.args.begin (), t.args.end (), " ")
);
}
}
///////////////////////////////////////////////////////////////////////////////
int main ()
{
cruft::TAP::logger tap;
test_combinations (tap);
test_presence (tap);
return tap.status ();
}