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"; }
20 void invalid_option();
25 GetOptTests::GetOptTests()
27 add(&GetOptTests::short_options, "Short options");
28 add(&GetOptTests::long_options, "Long options");
29 add(&GetOptTests::arguments, "Option arguments");
30 add(&GetOptTests::non_options, "Non-options");
31 add(&GetOptTests::help, "Help").expect_throw<usage_error>();
32 add(&GetOptTests::invalid_option, "Invalid option").expect_throw<usage_error>();
33 add(&GetOptTests::invalid_arg, "Invalid argument").expect_throw<usage_error>();
34 add(&GetOptTests::missing_arg, "Missing argument").expect_throw<usage_error>();
37 void GetOptTests::short_options()
39 static const char *argv[] = { "test", "-a", "-bc", 0 };
46 getopt.add_option('a', "a", a, GetOpt::NO_ARG);
47 getopt.add_option('b', "b", b, GetOpt::NO_ARG);
48 getopt.add_option('c', "c", c, GetOpt::NO_ARG);
54 void GetOptTests::long_options()
56 static const char *argv[] = { "test", "--foo", "--bar", 0 };
62 getopt.add_option("foo", foo, GetOpt::NO_ARG);
63 getopt.add_option("bar", bar, GetOpt::NO_ARG);
69 void GetOptTests::arguments()
71 static const char *argv[] = { "test", "-aabc", "-b", "x y z", "--foo=42", "--bar", "69", 0 };
79 getopt.add_option('a', "a", a, GetOpt::REQUIRED_ARG);
80 getopt.add_option('b', "b", b, GetOpt::REQUIRED_ARG);
81 getopt.add_option("foo", foo, GetOpt::REQUIRED_ARG);
82 getopt.add_option("bar", bar, GetOpt::REQUIRED_ARG);
85 EXPECT_EQUAL(a, "abc");
86 EXPECT_EQUAL(b, "x y z");
87 EXPECT_EQUAL(foo, 42);
88 EXPECT_EQUAL(bar, 69);
91 void GetOptTests::non_options()
93 static const char *argv[] = { "test", "-a", "foo", "-b", "bar", "baz", 0 };
99 getopt.add_option('a', "a", a, GetOpt::NO_ARG);
100 getopt.add_option('b', "b", b, GetOpt::NO_ARG);
104 EXPECT_EQUAL(getopt.get_args().size(), 3U);
107 void GetOptTests::help()
109 static const char *argv[] = { "test", "--help", 0 };
115 void GetOptTests::invalid_option()
117 static const char *argv[] = { "test", "--invalid", 0 };
123 void GetOptTests::invalid_arg()
125 static const char *argv[] = { "test", "--intval=foo", 0 };
129 getopt.add_option("intval", value, GetOpt::REQUIRED_ARG);
133 void GetOptTests::missing_arg()
135 static const char *argv[] = { "test", "--value", 0 };
139 getopt.add_option("value", value, GetOpt::REQUIRED_ARG);