]> git.tdb.fi Git - libs/core.git/blobdiff - tests/getopt.cpp
Add new test cases to cover positional arguments
[libs/core.git] / tests / getopt.cpp
index cb5df6295ffd34a07ab55d9a537ed4191087fc7e..e6888680ad39f69a18468efcc549743040afd1a3 100644 (file)
@@ -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<usage_error>();
        add(&GetOptTests::invalid_option, "Invalid option").expect_throw<usage_error>();
-       add(&GetOptTests::invalid_arg, "Invalid argument").expect_throw<usage_error>();
-       add(&GetOptTests::missing_arg, "Missing argument").expect_throw<usage_error>();
+       add(&GetOptTests::invalid_arg, "Invalid option argument").expect_throw<usage_error>();
+       add(&GetOptTests::missing_arg, "Missing option argument").expect_throw<usage_error>();
+       add(&GetOptTests::missing_positional, "Missing positional argument").expect_throw<usage_error>();
+       add(&GetOptTests::missing_positional_list, "Missing positional argument list").expect_throw<usage_error>();
 }
 
 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<string> 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<string> 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<string> args;
+
+       GetOpt getopt;
+       getopt.add_argument("arg", args);
+       getopt(1, argv);
+}