]> git.tdb.fi Git - libs/core.git/blob - tests/getopt.cpp
Simplify the SFINAE construct a bit
[libs/core.git] / tests / getopt.cpp
1 #include <msp/core/getopt.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class GetOptTests: public Test::RegisteredTest<GetOptTests>
8 {
9 public:
10         GetOptTests();
11
12         static const char *get_name() { return "GetOpt"; }
13
14 private:
15         void short_options();
16         void long_options();
17         void arguments();
18         void positional();
19         void positional_list();
20         void empty_list();
21         void mixed();
22         void help();
23         void invalid_option();
24         void invalid_arg();
25         void missing_arg();
26         void missing_positional();
27         void missing_positional_list();
28 };
29
30 GetOptTests::GetOptTests()
31 {
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>();
45 }
46
47 void GetOptTests::short_options()
48 {
49         static const char *argv[] = { "test", "-a", "-bc", 0 };
50
51         bool a = false;
52         bool b = false;
53         bool c = false;
54
55         GetOpt getopt;
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);
59         getopt(3, argv);
60
61         EXPECT(a && b && c);
62 }
63
64 void GetOptTests::long_options()
65 {
66         static const char *argv[] = { "test", "--foo", "--bar", 0 };
67
68         bool foo = false;
69         bool bar = false;
70
71         GetOpt getopt;
72         getopt.add_option("foo", foo, GetOpt::NO_ARG);
73         getopt.add_option("bar", bar, GetOpt::NO_ARG);
74         getopt(3, argv);
75
76         EXPECT(foo && bar);
77 }
78
79 void GetOptTests::arguments()
80 {
81         static const char *argv[] = { "test", "-aabc", "-b", "x y z", "--foo=42", "--bar", "69", 0 };
82
83         string a;
84         string b;
85         int foo = 0;
86         int bar = 0;
87
88         GetOpt getopt;
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);
93         getopt(7, argv);
94
95         EXPECT_EQUAL(a, "abc");
96         EXPECT_EQUAL(b, "x y z");
97         EXPECT_EQUAL(foo, 42);
98         EXPECT_EQUAL(bar, 69);
99 }
100
101 void GetOptTests::positional()
102 {
103         static const char *argv[] = { "test", "foo", "42", "bar", 0 };
104
105         string first;
106         unsigned second = 0;
107         string third;
108         unsigned fourth = 13;
109
110         GetOpt getopt;
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);
115         getopt(4, argv);
116
117         EXPECT_EQUAL(first, "foo");
118         EXPECT_EQUAL(second, 42);
119         EXPECT_EQUAL(third, "bar");
120         EXPECT_EQUAL(fourth, 13);
121 }
122
123 void GetOptTests::positional_list()
124 {
125         static const char *argv[] = { "test", "x", "y", "z", "foo", 0 };
126
127         list<string> args;
128         string tail;
129
130         GetOpt getopt;
131         getopt.add_argument("arg", args);
132         getopt.add_argument("tail", tail);
133         getopt(5, argv);
134
135         EXPECT_EQUAL(args.size(), 3U);
136         EXPECT_EQUAL(args.front(), "x");
137         EXPECT_EQUAL(args.back(), "z");
138         EXPECT_EQUAL(tail, "foo");
139 }
140
141 void GetOptTests::empty_list()
142 {
143         static const char *argv[] = { "test", "foo", "bar", 0 };
144
145         string head;
146         list<string> mid;
147         string tail;
148
149         GetOpt getopt;
150         getopt.add_argument("head", head);
151         getopt.add_argument("mid", mid, GetOpt::OPTIONAL_ARG);
152         getopt.add_argument("tail", tail);
153         getopt(3, argv);
154
155         EXPECT_EQUAL(head, "foo");
156         EXPECT(mid.empty());
157         EXPECT_EQUAL(tail, "bar");
158 }
159
160 void GetOptTests::mixed()
161 {
162         static const char *argv[] = { "test", "-a", "foo", "-b", "bar", "baz", 0 };
163
164         bool a = false;
165         bool b = false;
166         string first;
167         string second;
168         string third;
169
170         GetOpt getopt;
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);
176         getopt(6, argv);
177
178         EXPECT(a && b);
179         EXPECT_EQUAL(first, "foo");
180         EXPECT_EQUAL(second, "bar");
181         EXPECT_EQUAL(third, "baz");
182 }
183
184 void GetOptTests::help()
185 {
186         static const char *argv[] = { "test", "--help", 0 };
187
188         GetOpt getopt;
189         getopt(2, argv);
190 }
191
192 void GetOptTests::invalid_option()
193 {
194         static const char *argv[] = { "test", "--invalid", 0 };
195
196         GetOpt getopt;
197         getopt(2, argv);
198 }
199
200 void GetOptTests::invalid_arg()
201 {
202         static const char *argv[] = { "test", "--intval=foo", 0 };
203
204         int value;
205         GetOpt getopt;
206         getopt.add_option("intval", value, GetOpt::REQUIRED_ARG);
207         getopt(2, argv);
208 }
209
210 void GetOptTests::missing_arg()
211 {
212         static const char *argv[] = { "test", "--value", 0 };
213
214         string value;
215         GetOpt getopt;
216         getopt.add_option("value", value, GetOpt::REQUIRED_ARG);
217         getopt(2, argv);
218 }
219
220 void GetOptTests::missing_positional()
221 {
222         static const char *argv[] = { "test", 0 };
223
224         string arg;
225
226         GetOpt getopt;
227         getopt.add_argument("arg", arg);
228         getopt(1, argv);
229 }
230
231 void GetOptTests::missing_positional_list()
232 {
233         static const char *argv[] = { "test", 0 };
234
235         list<string> args;
236
237         GetOpt getopt;
238         getopt.add_argument("arg", args);
239         getopt(1, argv);
240 }