1 | // $Id: commandline_test.cc 1275 2008-04-11 06:10:12Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://trac.thep.lu.se/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 "Suite.h" |
---|
25 | |
---|
26 | #include "yat/utility/CommandLine.h" |
---|
27 | #include "yat/utility/OptionArg.h" |
---|
28 | #include "yat/utility/OptionFile.h" |
---|
29 | #include "yat/utility/OptionHelp.h" |
---|
30 | #include "yat/utility/OptionSwitch.h" |
---|
31 | |
---|
32 | #include <fstream> |
---|
33 | #include <stdexcept> |
---|
34 | |
---|
35 | using namespace theplu; |
---|
36 | using namespace yat::utility; |
---|
37 | bool cmd_help(yat::test::Suite& error); |
---|
38 | bool test_switch(yat::test::Suite& error); |
---|
39 | bool test_arg(yat::test::Suite& error); |
---|
40 | bool test_file(yat::test::Suite& error); |
---|
41 | bool test_failures(yat::test::Suite& error); |
---|
42 | |
---|
43 | int main(int argc, char* argv[]) |
---|
44 | { |
---|
45 | |
---|
46 | yat::test::Suite suite(argc, argv); |
---|
47 | |
---|
48 | suite.err() << "testing commandline" << std::endl; |
---|
49 | |
---|
50 | try { |
---|
51 | suite.add(cmd_help(suite)); |
---|
52 | suite.add(test_switch(suite)); |
---|
53 | suite.add(test_arg(suite)); |
---|
54 | suite.add(test_file(suite)); |
---|
55 | suite.add(test_failures(suite)); |
---|
56 | } |
---|
57 | catch (std::runtime_error& e) { |
---|
58 | suite.err() << "exception thrown\n" << e.what() << std::endl; |
---|
59 | } |
---|
60 | |
---|
61 | CommandLine cmd; |
---|
62 | OptionFile file(cmd, "f,file", "description"); |
---|
63 | |
---|
64 | return suite.return_value(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | bool cmd_help(yat::test::Suite& suite) |
---|
69 | { |
---|
70 | using namespace theplu::yat::utility; |
---|
71 | CommandLine cmd; |
---|
72 | OptionArg<std::string> dir(cmd, "d,dir", "output directory"); |
---|
73 | OptionHelp help(cmd); |
---|
74 | OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true); |
---|
75 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
---|
76 | OptionSwitch version(cmd, "version", "output version and exit"); |
---|
77 | |
---|
78 | suite.err() << cmd; |
---|
79 | return true; |
---|
80 | } |
---|
81 | |
---|
82 | bool test_switch(yat::test::Suite& suite) |
---|
83 | { |
---|
84 | bool ok=true; |
---|
85 | CommandLine cmd; |
---|
86 | OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true); |
---|
87 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done", true); |
---|
88 | |
---|
89 | suite.err() << "Testing OptionSwitch -T..."; |
---|
90 | { |
---|
91 | int ac = 2; |
---|
92 | char* av[] = { "test_prog", "-T" }; |
---|
93 | cmd.parse(ac,av); |
---|
94 | if (target.present() && !verbose.present()) |
---|
95 | suite.err() << "ok\n"; |
---|
96 | else { |
---|
97 | suite.err() << "failed\n"; |
---|
98 | ok =false; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | suite.err() << "Testing OptionSwitch --target..."; |
---|
104 | { |
---|
105 | int ac = 2; |
---|
106 | char* av[] = { "test_prog", "--target" }; |
---|
107 | cmd.parse(ac,av); |
---|
108 | if (target.present() && !verbose.present()) |
---|
109 | suite.err() << "ok\n"; |
---|
110 | else { |
---|
111 | suite.err() << "failed\n"; |
---|
112 | ok =false; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | suite.err() << "Testing OptionSwitch -T -v..."; |
---|
117 | { |
---|
118 | int ac = 3; |
---|
119 | char* av[] = { "test_prog", "-T" , "-v"}; |
---|
120 | cmd.parse(ac,av); |
---|
121 | if (target.present() && verbose.present()) |
---|
122 | suite.err() << "ok\n"; |
---|
123 | else { |
---|
124 | suite.err() << "failed\n"; |
---|
125 | ok =false; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | suite.err() << "Testing OptionSwitch -vT..."; |
---|
130 | { |
---|
131 | int ac = 2; |
---|
132 | char* av[] = { "test_prog", "-vT"}; |
---|
133 | cmd.parse(ac,av); |
---|
134 | if (target.present() && verbose.present()) |
---|
135 | suite.err() << "ok\n"; |
---|
136 | else { |
---|
137 | suite.err() << "failed\n"; |
---|
138 | ok =false; |
---|
139 | } |
---|
140 | } |
---|
141 | return ok; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | bool test_arg(yat::test::Suite& suite) |
---|
146 | { |
---|
147 | bool ok=true; |
---|
148 | CommandLine cmd; |
---|
149 | OptionArg<std::string> input(cmd, "i,input", "input file"); |
---|
150 | OptionArg<unsigned int> n(cmd, "n", "number of lines"); |
---|
151 | |
---|
152 | suite.err() << "Testing OptionArg existence -i file..."; |
---|
153 | { |
---|
154 | int ac = 3; |
---|
155 | char* av[] = { "test_prog", "-i", "file" }; |
---|
156 | cmd.parse(ac,av); |
---|
157 | if (input.present()) |
---|
158 | suite.err() << "ok\n"; |
---|
159 | else { |
---|
160 | suite.err() << "failed\n"; |
---|
161 | ok =false; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | suite.err() << "Testing OptionArg value -i file..."; |
---|
166 | { |
---|
167 | int ac = 3; |
---|
168 | char* av[] = { "test_prog", "-i", "file" }; |
---|
169 | cmd.parse(ac,av); |
---|
170 | if (input.value()=="file") |
---|
171 | suite.err() << "ok\n"; |
---|
172 | else { |
---|
173 | suite.err() << "failed\n"; |
---|
174 | ok =false; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | suite.err() << "Testing OptionArg existence --input file..."; |
---|
179 | { |
---|
180 | int ac = 3; |
---|
181 | char* av[] = { "test_prog", "--input", "file" }; |
---|
182 | cmd.parse(ac,av); |
---|
183 | if (input.present()) |
---|
184 | suite.err() << "ok\n"; |
---|
185 | else { |
---|
186 | suite.err() << "failed\n"; |
---|
187 | ok =false; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | suite.err() << "Testing OptionArg value --input=file..."; |
---|
192 | { |
---|
193 | int ac = 2; |
---|
194 | char* av[] = { "test_prog", "--input=file" }; |
---|
195 | cmd.parse(ac,av); |
---|
196 | if (input.value()=="file") |
---|
197 | suite.err() << "ok\n"; |
---|
198 | else { |
---|
199 | suite.err() << "failed\n"; |
---|
200 | ok =false; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | suite.err() << "Testing OptionArg value --input=\"file called something\"..."; |
---|
205 | { |
---|
206 | int ac = 2; |
---|
207 | char* av[] = { "test_prog", "--input=\"file called something\"" }; |
---|
208 | cmd.parse(ac,av); |
---|
209 | if (input.value()=="file called something") |
---|
210 | suite.err() << "ok\n"; |
---|
211 | else { |
---|
212 | suite.err() << "failed\n"; |
---|
213 | suite.err() << "value is `" << input.value() << "'\n" |
---|
214 | << "expected `file called something'\n"; |
---|
215 | ok =false; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | suite.err() << "Testing OptionArg unsigned int value -n 3..."; |
---|
220 | { |
---|
221 | int ac = 3; |
---|
222 | char* av[] = { "test_prog", "-n", "3" }; |
---|
223 | cmd.parse(ac,av); |
---|
224 | if (n.value()==3) |
---|
225 | suite.err() << "ok\n"; |
---|
226 | else { |
---|
227 | suite.err() << "failed\n"; |
---|
228 | ok =false; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | suite.err() << "Testing OptionArg 2 value --input file -n 3..."; |
---|
234 | { |
---|
235 | int ac = 5; |
---|
236 | char* av[] = { "test_prog", "--input", "file", "-n", "3" }; |
---|
237 | cmd.parse(ac,av); |
---|
238 | if (input.value()=="file") |
---|
239 | suite.err() << "ok\n"; |
---|
240 | else { |
---|
241 | suite.err() << "failed\n"; |
---|
242 | ok =false; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | return ok; |
---|
248 | } |
---|
249 | |
---|
250 | bool test_failures(yat::test::Suite& suite) |
---|
251 | { |
---|
252 | bool ok=true; |
---|
253 | CommandLine cmd; |
---|
254 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
---|
255 | OptionArg<std::string> input(cmd, "i,input", "input file"); |
---|
256 | OptionArg<unsigned int> n(cmd, "n", "number of lines"); |
---|
257 | |
---|
258 | suite.err() << "Testing unknown option --peter..."; |
---|
259 | { |
---|
260 | int ac = 2; |
---|
261 | char* av[] = { "test_prog", "--peter"}; |
---|
262 | try{ |
---|
263 | cmd.parse(ac,av); |
---|
264 | suite.err() << "failed\n"; |
---|
265 | ok =false; |
---|
266 | } |
---|
267 | catch (...) { |
---|
268 | suite.err() << "ok\n"; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | suite.err() << "Testing unknown option -jvjhsgad..."; |
---|
273 | { |
---|
274 | int ac = 2; |
---|
275 | char* av[] = { "test_prog", "-vjhsgad"}; |
---|
276 | try{ |
---|
277 | cmd.parse(ac,av); |
---|
278 | suite.err() << "failed\n"; |
---|
279 | ok =false; |
---|
280 | } |
---|
281 | catch (...) { |
---|
282 | suite.err() << "ok\n"; |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | suite.err() << "Testing invalid option -nv 3..."; |
---|
288 | { |
---|
289 | int ac = 3; |
---|
290 | char* av[] = { "test_prog", "-nv", "3"}; |
---|
291 | try{ |
---|
292 | cmd.parse(ac,av); |
---|
293 | suite.err() << "failed\n"; |
---|
294 | ok =false; |
---|
295 | } |
---|
296 | catch (...) { |
---|
297 | suite.err() << "ok\n"; |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | suite.err() << "Testing 23.12 is not unsigned int..."; |
---|
303 | { |
---|
304 | int ac = 3; |
---|
305 | char* av[] = { "test_prog", "-n", "23.12"}; |
---|
306 | try{ |
---|
307 | cmd.parse(ac,av); |
---|
308 | suite.err() << "failed\n"; |
---|
309 | ok =false; |
---|
310 | } |
---|
311 | catch (std::runtime_error& e) { |
---|
312 | suite.err() << "ok\n"; |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | suite.err() << "Testing -1 is not unsigned int..."; |
---|
317 | { |
---|
318 | int ac = 3; |
---|
319 | char* av[] = { "test_prog", "-n" "-1"}; |
---|
320 | try{ |
---|
321 | cmd.parse(ac,av); |
---|
322 | suite.err() << "failed\n"; |
---|
323 | ok =false; |
---|
324 | } |
---|
325 | catch (...) { |
---|
326 | suite.err() << "ok\n"; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | suite.err() << "Testing OptionArg required ..."; |
---|
332 | { |
---|
333 | OptionArg<std::string> required(cmd, "required", "required", true); |
---|
334 | int ac = 1; |
---|
335 | char* av[] = { "test_prog" }; |
---|
336 | try{ |
---|
337 | cmd.parse(ac,av); |
---|
338 | suite.err() << "failed\n"; |
---|
339 | ok =false; |
---|
340 | } |
---|
341 | catch (...) { |
---|
342 | suite.err() << "ok\n"; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | return ok; |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | bool test_file(yat::test::Suite& suite) |
---|
352 | { |
---|
353 | bool ok=true; |
---|
354 | CommandLine cmd; |
---|
355 | OptionFile inclones(cmd, "clones", "file containing clones"); |
---|
356 | OptionFile indata(cmd, "data", "data to merge"); |
---|
357 | OptionFile out(cmd, "o,out", "data to merge", true, false, "w"); |
---|
358 | OptionSwitch help(cmd, "h,help", "display this help and exit"); |
---|
359 | |
---|
360 | suite.err() << "Testing OptionFile... "; |
---|
361 | { |
---|
362 | int ac = 2; |
---|
363 | char* av[] = { "test_prog", "--clones", "commandline_test.cc" |
---|
364 | "--data", "commandline_test.cc", "-o", |
---|
365 | "commandline_test.cc"}; |
---|
366 | cmd.parse(ac,av); |
---|
367 | suite.err() << "ok\n"; |
---|
368 | } |
---|
369 | return ok; |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | |
---|
374 | |
---|
375 | |
---|