source: trunk/test/commandline_test.cc @ 1426

Last change on this file since 1426 was 1426, checked in by Peter, 15 years ago

fixes #414

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