1 #include <msp/core/getopt.h>
2 #include <msp/test/test.h>
7 class GetOptTests: public Test::RegisteredTest<GetOptTests>
12 static const char *get_name() { return "GetOpt"; }
19 void positional_list();
23 void invalid_option();
26 void missing_positional();
27 void missing_positional_list();
30 GetOptTests::GetOptTests()
32 add(&GetOptTests::short_options, "Short options");
33 add(&GetOptTests::long_options, "Long options");
34 add(&GetOptTests::arguments, "Option arguments");
35 add(&GetOptTests::positional, "Positional arguments");
36 add(&GetOptTests::positional_list, "Positional argument list");
37 add(&GetOptTests::empty_list, "Empty positional argument list");
38 add(&GetOptTests::mixed, "Mixed options and positional arguments");
39 add(&GetOptTests::help, "Help").expect_throw<usage_error>();
40 add(&GetOptTests::invalid_option, "Invalid option").expect_throw<usage_error>();
41 add(&GetOptTests::invalid_arg, "Invalid option argument").expect_throw<usage_error>();
42 add(&GetOptTests::missing_arg, "Missing option argument").expect_throw<usage_error>();
43 add(&GetOptTests::missing_positional, "Missing positional argument").expect_throw<usage_error>();
44 add(&GetOptTests::missing_positional_list, "Missing positional argument list").expect_throw<usage_error>();
47 void GetOptTests::short_options()
49 static const char *argv[] = { "test", "-a", "-bc", 0 };
56 getopt.add_option('a', "a", a, GetOpt::NO_ARG);
57 getopt.add_option('b', "b", b, GetOpt::NO_ARG);
58 getopt.add_option('c', "c", c, GetOpt::NO_ARG);
64 void GetOptTests::long_options()
66 static const char *argv[] = { "test", "--foo", "--bar", 0 };
72 getopt.add_option("foo", foo, GetOpt::NO_ARG);
73 getopt.add_option("bar", bar, GetOpt::NO_ARG);
79 void GetOptTests::arguments()
81 static const char *argv[] = { "test", "-aabc", "-b", "x y z", "--foo=42", "--bar", "69", 0 };
89 getopt.add_option('a', "a", a, GetOpt::REQUIRED_ARG);
90 getopt.add_option('b', "b", b, GetOpt::REQUIRED_ARG);
91 getopt.add_option("foo", foo, GetOpt::REQUIRED_ARG);
92 getopt.add_option("bar", bar, GetOpt::REQUIRED_ARG);
95 EXPECT_EQUAL(a, "abc");
96 EXPECT_EQUAL(b, "x y z");
97 EXPECT_EQUAL(foo, 42);
98 EXPECT_EQUAL(bar, 69);
101 void GetOptTests::positional()
103 static const char *argv[] = { "test", "foo", "42", "bar", 0 };
108 unsigned fourth = 13;
111 getopt.add_argument("first", first);
112 getopt.add_argument("second", second);
113 getopt.add_argument("third", third);
114 getopt.add_argument("fourth", fourth, GetOpt::OPTIONAL_ARG);
117 EXPECT_EQUAL(first, "foo");
118 EXPECT_EQUAL(second, 42);
119 EXPECT_EQUAL(third, "bar");
120 EXPECT_EQUAL(fourth, 13);
123 void GetOptTests::positional_list()
125 static const char *argv[] = { "test", "x", "y", "z", "foo", 0 };
131 getopt.add_argument("arg", args);
132 getopt.add_argument("tail", tail);
135 EXPECT_EQUAL(args.size(), 3U);
136 EXPECT_EQUAL(args.front(), "x");
137 EXPECT_EQUAL(args.back(), "z");
138 EXPECT_EQUAL(tail, "foo");
141 void GetOptTests::empty_list()
143 static const char *argv[] = { "test", "foo", "bar", 0 };
150 getopt.add_argument("head", head);
151 getopt.add_argument("mid", mid, GetOpt::OPTIONAL_ARG);
152 getopt.add_argument("tail", tail);
155 EXPECT_EQUAL(head, "foo");
157 EXPECT_EQUAL(tail, "bar");
160 void GetOptTests::mixed()
162 static const char *argv[] = { "test", "-a", "foo", "-b", "bar", "baz", 0 };
171 getopt.add_option('a', "a", a, GetOpt::NO_ARG);
172 getopt.add_option('b', "b", b, GetOpt::NO_ARG);
173 getopt.add_argument("first", first);
174 getopt.add_argument("second", second);
175 getopt.add_argument("third", third);
179 EXPECT_EQUAL(first, "foo");
180 EXPECT_EQUAL(second, "bar");
181 EXPECT_EQUAL(third, "baz");
184 void GetOptTests::help()
186 static const char *argv[] = { "test", "--help", 0 };
192 void GetOptTests::invalid_option()
194 static const char *argv[] = { "test", "--invalid", 0 };
200 void GetOptTests::invalid_arg()
202 static const char *argv[] = { "test", "--intval=foo", 0 };
206 getopt.add_option("intval", value, GetOpt::REQUIRED_ARG);
210 void GetOptTests::missing_arg()
212 static const char *argv[] = { "test", "--value", 0 };
216 getopt.add_option("value", value, GetOpt::REQUIRED_ARG);
220 void GetOptTests::missing_positional()
222 static const char *argv[] = { "test", 0 };
227 getopt.add_argument("arg", arg);
231 void GetOptTests::missing_positional_list()
233 static const char *argv[] = { "test", 0 };
238 getopt.add_argument("arg", args);