From: Mikko Rasa Date: Fri, 3 May 2013 09:46:49 +0000 (+0300) Subject: Add new test cases to cover positional arguments X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=47750d0e06f00e46ea03eda334a4c777bf213d72 Add new test cases to cover positional arguments --- diff --git a/tests/getopt.cpp b/tests/getopt.cpp index cb5df62..e688868 100644 --- a/tests/getopt.cpp +++ b/tests/getopt.cpp @@ -15,11 +15,16 @@ private: void short_options(); void long_options(); void arguments(); - void non_options(); + void positional(); + void positional_list(); + void empty_list(); + void mixed(); void help(); void invalid_option(); void invalid_arg(); void missing_arg(); + void missing_positional(); + void missing_positional_list(); }; GetOptTests::GetOptTests() @@ -27,11 +32,16 @@ GetOptTests::GetOptTests() add(&GetOptTests::short_options, "Short options"); add(&GetOptTests::long_options, "Long options"); add(&GetOptTests::arguments, "Option arguments"); - add(&GetOptTests::non_options, "Non-options"); + add(&GetOptTests::positional, "Positional arguments"); + add(&GetOptTests::positional_list, "Positional argument list"); + add(&GetOptTests::empty_list, "Empty positional argument list"); + add(&GetOptTests::mixed, "Mixed options and positional arguments"); add(&GetOptTests::help, "Help").expect_throw(); add(&GetOptTests::invalid_option, "Invalid option").expect_throw(); - add(&GetOptTests::invalid_arg, "Invalid argument").expect_throw(); - add(&GetOptTests::missing_arg, "Missing argument").expect_throw(); + add(&GetOptTests::invalid_arg, "Invalid option argument").expect_throw(); + add(&GetOptTests::missing_arg, "Missing option argument").expect_throw(); + add(&GetOptTests::missing_positional, "Missing positional argument").expect_throw(); + add(&GetOptTests::missing_positional_list, "Missing positional argument list").expect_throw(); } void GetOptTests::short_options() @@ -88,20 +98,87 @@ void GetOptTests::arguments() EXPECT_EQUAL(bar, 69); } -void GetOptTests::non_options() +void GetOptTests::positional() +{ + static const char *argv[] = { "test", "foo", "42", "bar", 0 }; + + string first; + unsigned second = 0; + string third; + unsigned fourth = 13; + + GetOpt getopt; + getopt.add_argument("first", first); + getopt.add_argument("second", second); + getopt.add_argument("third", third); + getopt.add_argument("fourth", fourth, GetOpt::OPTIONAL_ARG); + getopt(4, argv); + + EXPECT_EQUAL(first, "foo"); + EXPECT_EQUAL(second, 42); + EXPECT_EQUAL(third, "bar"); + EXPECT_EQUAL(fourth, 13); +} + +void GetOptTests::positional_list() +{ + static const char *argv[] = { "test", "x", "y", "z", "foo", 0 }; + + list args; + string tail; + + GetOpt getopt; + getopt.add_argument("arg", args); + getopt.add_argument("tail", tail); + getopt(5, argv); + + EXPECT_EQUAL(args.size(), 3U); + EXPECT_EQUAL(args.front(), "x"); + EXPECT_EQUAL(args.back(), "z"); + EXPECT_EQUAL(tail, "foo"); +} + +void GetOptTests::empty_list() +{ + static const char *argv[] = { "test", "foo", "bar", 0 }; + + string head; + list mid; + string tail; + + GetOpt getopt; + getopt.add_argument("head", head); + getopt.add_argument("mid", mid, GetOpt::OPTIONAL_ARG); + getopt.add_argument("tail", tail); + getopt(3, argv); + + EXPECT_EQUAL(head, "foo"); + EXPECT(mid.empty()); + EXPECT_EQUAL(tail, "bar"); +} + +void GetOptTests::mixed() { static const char *argv[] = { "test", "-a", "foo", "-b", "bar", "baz", 0 }; bool a = false; bool b = false; + string first; + string second; + string third; GetOpt getopt; getopt.add_option('a', "a", a, GetOpt::NO_ARG); getopt.add_option('b', "b", b, GetOpt::NO_ARG); + getopt.add_argument("first", first); + getopt.add_argument("second", second); + getopt.add_argument("third", third); getopt(6, argv); EXPECT(a && b); - EXPECT_EQUAL(getopt.get_args().size(), 3U); + EXPECT_EQUAL(first, "foo"); + EXPECT_EQUAL(second, "bar"); + EXPECT_EQUAL(third, "baz"); } void GetOptTests::help() @@ -139,3 +216,25 @@ void GetOptTests::missing_arg() getopt.add_option("value", value, GetOpt::REQUIRED_ARG); getopt(2, argv); } + +void GetOptTests::missing_positional() +{ + static const char *argv[] = { "test", 0 }; + + string arg; + + GetOpt getopt; + getopt.add_argument("arg", arg); + getopt(1, argv); +} + +void GetOptTests::missing_positional_list() +{ + static const char *argv[] = { "test", 0 }; + + list args; + + GetOpt getopt; + getopt.add_argument("arg", args); + getopt(1, argv); +}