1 | // $Id: commandline_test.cc 975 2007-10-17 21:03:37Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://trac.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "yat/utility/CommandLine.h" |
---|
25 | #include "yat/utility/OptionArg.h" |
---|
26 | #include "yat/utility/OptionFile.h" |
---|
27 | #include "yat/utility/OptionSwitch.h" |
---|
28 | |
---|
29 | #include <fstream> |
---|
30 | #include <stdexcept> |
---|
31 | |
---|
32 | using namespace theplu::yat::utility; |
---|
33 | bool cmd_help(std::ostream& error); |
---|
34 | bool test_switch(std::ostream& error); |
---|
35 | bool test_arg(std::ostream& error); |
---|
36 | bool test_failures(std::ostream& error); |
---|
37 | |
---|
38 | int main(const int argc,const char* argv[]) |
---|
39 | { |
---|
40 | |
---|
41 | std::ostream* error; |
---|
42 | if (argc>1 && argv[1]==std::string("-v")) |
---|
43 | error = &std::cerr; |
---|
44 | else { |
---|
45 | error = new std::ofstream("/dev/null"); |
---|
46 | if (argc>1) |
---|
47 | std::cout << "commandline_test -v : for printing extra " |
---|
48 | << "information\n"; |
---|
49 | } |
---|
50 | *error << "testing commandline" << std::endl; |
---|
51 | bool ok = true; |
---|
52 | |
---|
53 | try { |
---|
54 | ok &= cmd_help(*error); |
---|
55 | |
---|
56 | ok &= test_switch(*error); |
---|
57 | ok &= test_arg(*error); |
---|
58 | ok &= test_failures(*error); |
---|
59 | } |
---|
60 | catch (std::runtime_error& e) { |
---|
61 | *error << "exception thrown\n" << e.what() << std::endl; |
---|
62 | } |
---|
63 | |
---|
64 | CommandLine cmd; |
---|
65 | OptionFile file(cmd, "f,file", "description"); |
---|
66 | |
---|
67 | if (!ok) |
---|
68 | *error << "commandline test failed" << std::endl; |
---|
69 | else |
---|
70 | *error << "All tests are OK." << std::endl; |
---|
71 | |
---|
72 | if (error!=&std::cerr) |
---|
73 | delete error; |
---|
74 | |
---|
75 | return (ok ? 0 : -1); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | bool cmd_help(std::ostream& error) |
---|
80 | { |
---|
81 | using namespace theplu::yat::utility; |
---|
82 | CommandLine cmd; |
---|
83 | OptionArg<std::string> dir(cmd, "d,dir", "output directory"); |
---|
84 | OptionSwitch help(cmd, "h,help", "display this help and exit"); |
---|
85 | OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true); |
---|
86 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
---|
87 | OptionSwitch version(cmd, "version", "output version and exit"); |
---|
88 | |
---|
89 | error << cmd; |
---|
90 | return true; |
---|
91 | } |
---|
92 | |
---|
93 | bool test_switch(std::ostream& error) |
---|
94 | { |
---|
95 | bool ok=true; |
---|
96 | CommandLine cmd; |
---|
97 | OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true); |
---|
98 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done", true); |
---|
99 | |
---|
100 | error << "Testing OptionSwitch -T..."; |
---|
101 | { |
---|
102 | int ac = 2; |
---|
103 | char* av[] = { "test_prog", "-T" }; |
---|
104 | cmd.parse(ac,av); |
---|
105 | if (target.present() && !verbose.present()) |
---|
106 | error << "ok\n"; |
---|
107 | else { |
---|
108 | error << "failed\n"; |
---|
109 | ok =false; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | error << "Testing OptionSwitch --target..."; |
---|
115 | { |
---|
116 | int ac = 2; |
---|
117 | char* av[] = { "test_prog", "--target" }; |
---|
118 | cmd.parse(ac,av); |
---|
119 | if (target.present() && !verbose.present()) |
---|
120 | error << "ok\n"; |
---|
121 | else { |
---|
122 | error << "failed\n"; |
---|
123 | ok =false; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | error << "Testing OptionSwitch -T -v..."; |
---|
128 | { |
---|
129 | int ac = 3; |
---|
130 | char* av[] = { "test_prog", "-T" , "-v"}; |
---|
131 | cmd.parse(ac,av); |
---|
132 | if (target.present() && verbose.present()) |
---|
133 | error << "ok\n"; |
---|
134 | else { |
---|
135 | error << "failed\n"; |
---|
136 | ok =false; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | error << "Testing OptionSwitch -vT..."; |
---|
141 | { |
---|
142 | int ac = 2; |
---|
143 | char* av[] = { "test_prog", "-vT"}; |
---|
144 | cmd.parse(ac,av); |
---|
145 | if (target.present() && verbose.present()) |
---|
146 | error << "ok\n"; |
---|
147 | else { |
---|
148 | error << "failed\n"; |
---|
149 | ok =false; |
---|
150 | } |
---|
151 | } |
---|
152 | return ok; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | bool test_arg(std::ostream& error) |
---|
157 | { |
---|
158 | bool ok=true; |
---|
159 | CommandLine cmd; |
---|
160 | OptionArg<std::string> input(cmd, "i,input", "input file"); |
---|
161 | OptionArg<u_int> n(cmd, "n", "number of lines"); |
---|
162 | |
---|
163 | error << "Testing OptionArg existence -i file..."; |
---|
164 | { |
---|
165 | int ac = 3; |
---|
166 | char* av[] = { "test_prog", "-i", "file" }; |
---|
167 | cmd.parse(ac,av); |
---|
168 | if (input.present()) |
---|
169 | error << "ok\n"; |
---|
170 | else { |
---|
171 | error << "failed\n"; |
---|
172 | ok =false; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | error << "Testing OptionArg value -i file..."; |
---|
177 | { |
---|
178 | int ac = 3; |
---|
179 | char* av[] = { "test_prog", "-i", "file" }; |
---|
180 | cmd.parse(ac,av); |
---|
181 | if (input.value()=="file") |
---|
182 | error << "ok\n"; |
---|
183 | else { |
---|
184 | error << "failed\n"; |
---|
185 | ok =false; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | error << "Testing OptionArg existence --input file..."; |
---|
190 | { |
---|
191 | int ac = 3; |
---|
192 | char* av[] = { "test_prog", "--input", "file" }; |
---|
193 | cmd.parse(ac,av); |
---|
194 | if (input.present()) |
---|
195 | error << "ok\n"; |
---|
196 | else { |
---|
197 | error << "failed\n"; |
---|
198 | ok =false; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | error << "Testing OptionArg value --input=file..."; |
---|
203 | { |
---|
204 | int ac = 2; |
---|
205 | char* av[] = { "test_prog", "--input=file" }; |
---|
206 | cmd.parse(ac,av); |
---|
207 | if (input.value()=="file") |
---|
208 | error << "ok\n"; |
---|
209 | else { |
---|
210 | error << "failed\n"; |
---|
211 | ok =false; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | error << "Testing OptionArg value --input=\"file called something\"..."; |
---|
216 | { |
---|
217 | int ac = 2; |
---|
218 | char* av[] = { "test_prog", "--input=\"file called something\"" }; |
---|
219 | cmd.parse(ac,av); |
---|
220 | if (input.value()=="file called something") |
---|
221 | error << "ok\n"; |
---|
222 | else { |
---|
223 | error << "failed\n"; |
---|
224 | error << "value is `" << input.value() << "'\n" |
---|
225 | << "expected `file called something'\n"; |
---|
226 | ok =false; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | error << "Testing OptionArg u_int value -n 3..."; |
---|
231 | { |
---|
232 | int ac = 3; |
---|
233 | char* av[] = { "test_prog", "-n", "3" }; |
---|
234 | cmd.parse(ac,av); |
---|
235 | if (n.value()==3) |
---|
236 | error << "ok\n"; |
---|
237 | else { |
---|
238 | error << "failed\n"; |
---|
239 | ok =false; |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | error << "Testing OptionArg 2 value --input file -n 3..."; |
---|
245 | { |
---|
246 | int ac = 5; |
---|
247 | char* av[] = { "test_prog", "--input", "file", "-n", "3" }; |
---|
248 | cmd.parse(ac,av); |
---|
249 | if (input.value()=="file") |
---|
250 | error << "ok\n"; |
---|
251 | else { |
---|
252 | error << "failed\n"; |
---|
253 | ok =false; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | return ok; |
---|
259 | } |
---|
260 | |
---|
261 | bool test_failures(std::ostream& error) |
---|
262 | { |
---|
263 | bool ok=true; |
---|
264 | CommandLine cmd; |
---|
265 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
---|
266 | OptionArg<std::string> input(cmd, "i,input", "input file"); |
---|
267 | OptionArg<u_int> n(cmd, "n", "number of lines"); |
---|
268 | |
---|
269 | error << "Testing unknown option --peter..."; |
---|
270 | { |
---|
271 | int ac = 2; |
---|
272 | char* av[] = { "test_prog", "--peter"}; |
---|
273 | try{ |
---|
274 | cmd.parse(ac,av); |
---|
275 | error << "failed\n"; |
---|
276 | ok =false; |
---|
277 | } |
---|
278 | catch (...) { |
---|
279 | error << "ok\n"; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | error << "Testing unknown option -jvjhsgad..."; |
---|
284 | { |
---|
285 | int ac = 2; |
---|
286 | char* av[] = { "test_prog", "-vjhsgad"}; |
---|
287 | try{ |
---|
288 | cmd.parse(ac,av); |
---|
289 | error << "failed\n"; |
---|
290 | ok =false; |
---|
291 | } |
---|
292 | catch (...) { |
---|
293 | error << "ok\n"; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | error << "Testing invalid option -nv 3..."; |
---|
299 | { |
---|
300 | int ac = 3; |
---|
301 | char* av[] = { "test_prog", "-nv", "3"}; |
---|
302 | try{ |
---|
303 | cmd.parse(ac,av); |
---|
304 | error << "failed\n"; |
---|
305 | ok =false; |
---|
306 | } |
---|
307 | catch (...) { |
---|
308 | error << "ok\n"; |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | error << "Testing 23.12 is not u_int..."; |
---|
314 | { |
---|
315 | int ac = 3; |
---|
316 | char* av[] = { "test_prog", "-n", "23.12"}; |
---|
317 | try{ |
---|
318 | cmd.parse(ac,av); |
---|
319 | error << "failed\n"; |
---|
320 | ok =false; |
---|
321 | } |
---|
322 | catch (std::runtime_error& e) { |
---|
323 | error << "ok\n"; |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | error << "Testing -1 is not u_int..."; |
---|
328 | { |
---|
329 | int ac = 3; |
---|
330 | char* av[] = { "test_prog", "-n" "-1"}; |
---|
331 | try{ |
---|
332 | cmd.parse(ac,av); |
---|
333 | error << "failed\n"; |
---|
334 | ok =false; |
---|
335 | } |
---|
336 | catch (...) { |
---|
337 | error << "ok\n"; |
---|
338 | } |
---|
339 | } |
---|
340 | return ok; |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | |
---|
347 | |
---|