diff options
author | Taylor C. Richberger <taywee@gmx.com> | 2017-11-09 13:54:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-09 13:54:32 -0700 |
commit | 71b1110cb86838d63957678c1c15c8031588be99 (patch) | |
tree | e28b72f3d9db0ce7d490650598d24b9422cdb7ee /test.cxx | |
parent | replace badges with Taywee ones so the repository is properly described by it... (diff) | |
parent | change default value of RequireCommand (diff) | |
download | args.hxx-71b1110cb86838d63957678c1c15c8031588be99.tar.xz |
Merge pull request #40 from pavel-belikov/subparsers-validation
Subparsers validation
Diffstat (limited to 'test.cxx')
-rw-r--r-- | test.cxx | 123 |
1 files changed, 123 insertions, 0 deletions
@@ -635,8 +635,11 @@ TEST_CASE("Nargs work as expected", "[args]") args::NargsValueFlag<int> a(parser, "", "", {'a'}, 2); args::NargsValueFlag<int> b(parser, "", "", {'b'}, {2, 3}); args::NargsValueFlag<std::string> c(parser, "", "", {'c'}, {0, 2}); + args::NargsValueFlag<int> d(parser, "", "", {'d'}, {1, 3}); args::Flag f(parser, "", "", {'f'}); + REQUIRE_THROWS_AS(args::Nargs(3, 2), args::UsageError); + REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-a", "1", "2"})); REQUIRE((args::get(a) == std::vector<int>{1, 2})); @@ -676,6 +679,9 @@ TEST_CASE("Nargs work as expected", "[args]") REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-cf"})); REQUIRE((args::get(c) == std::vector<std::string>{"f"})); REQUIRE(args::get(f) == false); + + REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-d"}), args::ParseError); + REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-b"}), args::ParseError); } TEST_CASE("Simple commands work as expected", "[args]") @@ -764,6 +770,7 @@ TEST_CASE("Subparser help works as expected", "[args]") }); p.Prog("git"); + p.RequireCommand(false); std::ostringstream s; @@ -857,6 +864,51 @@ TEST_CASE("Subparser help works as expected", "[args]") } +TEST_CASE("Subparser validation works as expected", "[args]") +{ + args::ArgumentParser p("parser"); + args::Command a(p, "a", "command a", [](args::Subparser &s) + { + args::ValueFlag<std::string> f(s, "", "", {'f'}, args::Options::Required); + s.Parse(); + }); + + args::Command b(p, "b", "command b"); + args::ValueFlag<std::string> f(b, "", "", {'f'}, args::Options::Required); + + args::Command c(p, "c", "command c", [](args::Subparser&){}); + + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{}), args::ValidationError); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"a"}), args::RequiredError); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"a", "-f", "F"})); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"b"}), args::RequiredError); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"b", "-f", "F"})); + + p.RequireCommand(false); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{})); + + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"c"}), args::UsageError); + + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"unknown-command"}), args::ParseError); +} + +TEST_CASE("Global options work as expected", "[args]") +{ + args::Group globals; + args::Flag f(globals, "f", "f", {'f'}); + + args::ArgumentParser p("parser"); + args::GlobalOptions g(p, globals); + args::Command a(p, "a", "command a"); + args::Command b(p, "b", "command b"); + + p.RequireCommand(false); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"-f"})); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"a", "-f"})); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"b", "-f"})); +} + #undef ARGS_HXX #define ARGS_TESTNAMESPACE #define ARGS_NOEXCEPT @@ -920,3 +972,74 @@ TEST_CASE("Noexcept mode works as expected", "[args]") parser.ParseArgs(std::vector<std::string>{"--mf", "yellow"}); REQUIRE(parser.GetError() == argstest::Error::None); } + +TEST_CASE("Required flags work as expected in noexcept mode", "[args]") +{ + argstest::ArgumentParser parser1("Test command"); + argstest::ValueFlag<int> foo(parser1, "foo", "foo", {'f', "foo"}, argstest::Options::Required); + argstest::ValueFlag<int> bar(parser1, "bar", "bar", {'b', "bar"}); + + parser1.ParseArgs(std::vector<std::string>{"-f", "42"}); + REQUIRE(foo.Get() == 42); + REQUIRE(parser1.GetError() == argstest::Error::None); + + parser1.ParseArgs(std::vector<std::string>{"-b4"}); + REQUIRE(parser1.GetError() == argstest::Error::Required); + + argstest::ArgumentParser parser2("Test command"); + argstest::Positional<int> pos1(parser2, "a", "a"); + parser2.ParseArgs(std::vector<std::string>{}); + REQUIRE(parser2.GetError() == argstest::Error::None); + + argstest::ArgumentParser parser3("Test command"); + argstest::Positional<int> pos2(parser3, "a", "a", argstest::Options::Required); + parser3.ParseArgs(std::vector<std::string>{}); + REQUIRE(parser3.GetError() == argstest::Error::Required); +} + +TEST_CASE("Subparser validation works as expected in noexcept mode", "[args]") +{ + argstest::ArgumentParser p("parser"); + argstest::Command a(p, "a", "command a", [](argstest::Subparser &s) + { + argstest::ValueFlag<std::string> f(s, "", "", {'f'}, argstest::Options::Required); + s.Parse(); + }); + + argstest::Command b(p, "b", "command b"); + argstest::ValueFlag<std::string> f(b, "", "", {'f'}, argstest::Options::Required); + + argstest::Command c(p, "c", "command c", [](argstest::Subparser&){}); + + p.ParseArgs(std::vector<std::string>{}); + REQUIRE(p.GetError() == argstest::Error::Validation); + + p.ParseArgs(std::vector<std::string>{"a"}); + REQUIRE((size_t)p.GetError() == (size_t)argstest::Error::Required); + + p.ParseArgs(std::vector<std::string>{"a", "-f", "F"}); + REQUIRE(p.GetError() == argstest::Error::None); + + p.ParseArgs(std::vector<std::string>{"b"}); + REQUIRE(p.GetError() == argstest::Error::Required); + + p.ParseArgs(std::vector<std::string>{"b", "-f", "F"}); + REQUIRE(p.GetError() == argstest::Error::None); + + p.RequireCommand(false); + p.ParseArgs(std::vector<std::string>{}); + REQUIRE(p.GetError() == argstest::Error::None); + + p.ParseArgs(std::vector<std::string>{"c"}); + REQUIRE(p.GetError() == argstest::Error::Usage); +} + +TEST_CASE("Nargs work as expected in noexcept mode", "[args]") +{ + argstest::ArgumentParser parser("Test command"); + argstest::NargsValueFlag<int> a(parser, "", "", {'a'}, {3, 2}); + + parser.ParseArgs(std::vector<std::string>{"-a", "1", "2"}); + REQUIRE(parser.GetError() == argstest::Error::Usage); +} + |