source: trunk/test/commandline_test.cc @ 1632

Last change on this file since 1632 was 1632, checked in by Peter, 13 years ago

move the setting of print_arg from constructor to function in order to allow usage also in daughter classes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.4 KB
RevLine 
[965]1// $Id: commandline_test.cc 1632 2008-11-19 16:20:49Z peter $
2
3/*
[1275]4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
[965]5
[1437]6  This file is part of the yat library, http://dev.thep.lu.se/yat
[965]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
[1486]10  published by the Free Software Foundation; either version 3 of the
[965]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
[1487]19  along with yat. If not, see <http://www.gnu.org/licenses/>.
[965]20*/
21
[1232]22#include "Suite.h"
23
[965]24#include "yat/utility/CommandLine.h"
25#include "yat/utility/OptionArg.h"
[975]26#include "yat/utility/OptionFile.h"
[981]27#include "yat/utility/OptionHelp.h"
[965]28#include "yat/utility/OptionSwitch.h"
29
30#include <fstream>
31#include <stdexcept>
32
[1232]33using namespace theplu;
34using namespace yat::utility;
35bool cmd_help(yat::test::Suite& error);
36bool test_switch(yat::test::Suite& error);
37bool test_arg(yat::test::Suite& error);
38bool test_file(yat::test::Suite& error);
39bool test_failures(yat::test::Suite& error);
[1426]40bool test_option_name_clash(yat::test::Suite& suite);
[1427]41bool test_free_arg(yat::test::Suite& suite);
[965]42
[1232]43int main(int argc, char* argv[])
[965]44{ 
45
[1232]46  yat::test::Suite suite(argc, argv);
[965]47
[1232]48  suite.err() << "testing commandline" << std::endl;
49
[965]50  try {
[1232]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));
[1426]56    suite.add(test_option_name_clash(suite));
[1468]57    suite.add(test_free_arg(suite));
[965]58  }
59  catch (std::runtime_error& e) {
[1426]60    suite.err() << "Error: unexpected exception thrown\n" << e.what() 
61                << std::endl;
62    suite.add(false);
[965]63  }
64
[975]65  CommandLine cmd;
66  OptionFile file(cmd, "f,file", "description");
67
[1232]68  return suite.return_value();
[965]69}
70
71
[1232]72bool cmd_help(yat::test::Suite& suite)
[965]73{
74  using namespace theplu::yat::utility;
75  CommandLine cmd;
76  OptionArg<std::string> dir(cmd, "d,dir", "output directory");
[981]77  OptionHelp help(cmd);
[1629]78  OptionArg<std::string> target(cmd, "T,target", "treat DEST as a normal file", 
[1632]79                                true);
80  target.print_arg("=TARGET");
[965]81  OptionSwitch verbose(cmd, "v,verbose", "explain what is being done");
82  OptionSwitch version(cmd, "version", "output version and exit");
83
[1232]84  suite.err() << cmd;
[965]85  return true;
86}
87
[1232]88bool test_switch(yat::test::Suite& suite)
[965]89{
90  bool ok=true;
91  CommandLine cmd;
92  OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true);
93  OptionSwitch verbose(cmd, "v,verbose", "explain what is being done", true);
94
[1232]95  suite.err() << "Testing OptionSwitch -T...";
[965]96  {
97    int ac = 2;
98    char* av[] = { "test_prog", "-T" };
99    cmd.parse(ac,av);
100    if (target.present() && !verbose.present())
[1232]101      suite.err() << "ok\n";
[965]102    else {
[1232]103      suite.err() << "failed\n";
[965]104      ok =false;
105    }
106  }
107
108
[1232]109  suite.err() << "Testing OptionSwitch --target...";
[965]110  {
111    int ac = 2;
112    char* av[] = { "test_prog", "--target" };
113    cmd.parse(ac,av);
114    if (target.present() && !verbose.present())
[1232]115      suite.err() << "ok\n";
[965]116    else {
[1232]117      suite.err() << "failed\n";
[965]118      ok =false;
119    }
120  }
121
[1232]122  suite.err() << "Testing OptionSwitch -T -v...";
[965]123  {
124    int ac = 3;
125    char* av[] = { "test_prog", "-T" , "-v"};
126    cmd.parse(ac,av);
127    if (target.present() && verbose.present())
[1232]128      suite.err() << "ok\n";
[965]129    else {
[1232]130      suite.err() << "failed\n";
[965]131      ok =false;
132    }
133  }
134
[1232]135  suite.err() << "Testing OptionSwitch -vT...";
[965]136  {
137    int ac = 2;
138    char* av[] = { "test_prog", "-vT"};
139    cmd.parse(ac,av);
140    if (target.present() && verbose.present())
[1232]141      suite.err() << "ok\n";
[965]142    else {
[1232]143      suite.err() << "failed\n";
[965]144      ok =false;
145    }
146  }
147  return ok;
148}
149
150
[1232]151bool test_arg(yat::test::Suite& suite)
[965]152{
153  bool ok=true;
154  CommandLine cmd;
155  OptionArg<std::string> input(cmd, "i,input", "input file");
[1273]156  OptionArg<unsigned int> n(cmd, "n", "number of lines");
[1465]157  OptionArg<double> x(cmd, "x,extra", "a float number");
[965]158
[1232]159  suite.err() << "Testing OptionArg existence -i file...";
[965]160  {
161    int ac = 3;
162    char* av[] = { "test_prog", "-i", "file" };
163    cmd.parse(ac,av);
164    if (input.present())
[1232]165      suite.err() << "ok\n";
[965]166    else {
[1232]167      suite.err() << "failed\n";
[965]168      ok =false;
169    }
170  }
171
[1232]172  suite.err() << "Testing OptionArg value -i file...";
[965]173  {
174    int ac = 3;
175    char* av[] = { "test_prog", "-i", "file" };
176    cmd.parse(ac,av);
177    if (input.value()=="file")
[1232]178      suite.err() << "ok\n";
[965]179    else {
[1232]180      suite.err() << "failed\n";
[965]181      ok =false;
182    }
183  }
184
[1232]185  suite.err() << "Testing OptionArg existence --input file...";
[965]186  {
187    int ac = 3;
188    char* av[] = { "test_prog", "--input", "file" };
189    cmd.parse(ac,av);
190    if (input.present())
[1232]191      suite.err() << "ok\n";
[965]192    else {
[1232]193      suite.err() << "failed\n";
[965]194      ok =false;
195    }
196  }
197
[1232]198  suite.err() << "Testing OptionArg value --input=file...";
[965]199  {
200    int ac = 2;
201    char* av[] = { "test_prog", "--input=file" };
202    cmd.parse(ac,av);
203    if (input.value()=="file")
[1232]204      suite.err() << "ok\n";
[965]205    else {
[1232]206      suite.err() << "failed\n";
[965]207      ok =false;
208    }
209  }
210
[1232]211  suite.err() << "Testing OptionArg value --input=\"file called something\"...";
[965]212  {
213    int ac = 2;
214    char* av[] = { "test_prog", "--input=\"file called something\"" };
215    cmd.parse(ac,av);
216    if (input.value()=="file called something")
[1232]217      suite.err() << "ok\n";
[965]218    else {
[1232]219      suite.err() << "failed\n";
220      suite.err() << "value is `" << input.value() << "'\n"
[965]221            << "expected `file called something'\n";
222      ok =false;
223    }
224  }
225
[1273]226  suite.err() << "Testing OptionArg unsigned int value -n 3...";
[965]227  {
228    int ac = 3;
229    char* av[] = { "test_prog", "-n", "3" };
230    cmd.parse(ac,av);
231    if (n.value()==3)
[1232]232      suite.err() << "ok\n";
[965]233    else {
[1232]234      suite.err() << "failed\n";
[965]235      ok =false;
236    }
237  }
238
239
[1232]240  suite.err() << "Testing OptionArg 2 value --input file -n 3...";
[965]241  {
242    int ac = 5;
243    char* av[] = { "test_prog", "--input", "file", "-n", "3" };
244    cmd.parse(ac,av);
245    if (input.value()=="file")
[1232]246      suite.err() << "ok\n";
[965]247    else {
[1232]248      suite.err() << "failed\n";
[965]249      ok =false;
250    }
251  }
252
[1437]253  suite.err() << "Testing OptionArg double value -x -6...";
254  try {
255    int ac = 3;
256    char* av[] = { "test_prog", "-x", "-6" };
257    cmd.parse(ac,av);
258    if (x.value()==-6)
259      suite.err() << "ok\n";
260    else {
261      suite.err() << "failed\n";
262      ok =false;
263    }
264  }
265  catch (std::runtime_error& e) {
[1465]266    ok = false;
[1437]267    suite.err() << "failed\nexception thrown with what(): " << e.what() << "\n";
268  }
[965]269
[1465]270  suite.err() << "Testing OptionArg double value --extra -6...";
271  try {
272    int ac = 3;
273    char* av[] = { "test_prog", "--extra", "-6" };
274    cmd.parse(ac,av);
275    if (x.value()==-6)
276      suite.err() << "ok\n";
277    else {
278      suite.err() << "failed\n";
279      ok =false;
280    }
281  }
282  catch (std::runtime_error& e) {
283    ok=false;
284    suite.err() << "failed\nexception thrown with what(): " << e.what() << "\n";
285  }
[1437]286
[1602]287  suite.err() << "Testing OptionArg::value(T) ... ";
288  try {
289    int ac = 1;
290    char* av[] = { "test_prog"};
291    cmd.parse(ac,av);
292    n.value(5);
293    if (n.present()==false && n.value()==5)
294      suite.err() << "ok\n";
295    else {
296      suite.err() << "failed\n";
297      ok = false;
298    }
299  }
300  catch (std::runtime_error& e) {
301    suite.err() << "failed\n";
302    ok = false;
303  }
304
[965]305  return ok;
306}
307
[1232]308bool test_failures(yat::test::Suite& suite)
[965]309{
310  bool ok=true;
311  CommandLine cmd;
312  OptionSwitch verbose(cmd, "v,verbose", "explain what is being done");
313  OptionArg<std::string> input(cmd, "i,input", "input file");
[1273]314  OptionArg<unsigned int> n(cmd, "n", "number of lines");
[965]315
[1232]316  suite.err() << "Testing unknown option --peter...";
[965]317  {
318    int ac = 2;
319    char* av[] = { "test_prog", "--peter"};
320    try{
321      cmd.parse(ac,av);
[1232]322      suite.err() << "failed\n";
[965]323      ok =false;
324    }
325    catch (...) {
[1232]326      suite.err() << "ok\n";
[965]327    }
328  }
329
[1232]330  suite.err() << "Testing unknown option -jvjhsgad...";
[965]331  {
332    int ac = 2;
333    char* av[] = { "test_prog", "-vjhsgad"};
334    try{
335      cmd.parse(ac,av);
[1232]336      suite.err() << "failed\n";
[965]337      ok =false;
338    }
339    catch (...) {
[1232]340      suite.err() << "ok\n";
[965]341    }
342  }
343
344
[1232]345  suite.err() << "Testing invalid option -nv 3...";
[965]346  {
347    int ac = 3;
348    char* av[] = { "test_prog", "-nv", "3"};
349    try{
350      cmd.parse(ac,av);
[1232]351      suite.err() << "failed\n";
[965]352      ok =false;
353    }
354    catch (...) {
[1232]355      suite.err() << "ok\n";
[965]356    }
357  }
358
359
[1273]360  suite.err() << "Testing 23.12 is not unsigned int...";
[965]361  {
362    int ac = 3;
363    char* av[] = { "test_prog", "-n", "23.12"};
364    try{
365      cmd.parse(ac,av);
[1232]366      suite.err() << "failed\n";
[965]367      ok =false;
368    }
369    catch (std::runtime_error& e) {
[1232]370      suite.err() << "ok\n";
[965]371    }
372  }
373
[1273]374  suite.err() << "Testing -1 is not unsigned int...";
[965]375  {
376    int ac = 3;
377    char* av[] = { "test_prog", "-n" "-1"};
378    try{
379      cmd.parse(ac,av);
[1232]380      suite.err() << "failed\n";
[965]381      ok =false;
382    }
383    catch (...) {
[1232]384      suite.err() << "ok\n";
[965]385    }
386  }
[980]387
388
[1466]389  suite.err() << "Testing invalid: test_prog peter...";
390  {
391    int ac = 2;
392    char* av[] = { "test_prog", "peter" };
393    try{
394      cmd.parse(ac,av);
395      suite.err() << "failed\n";
396      ok =false;
397    }
398    catch (...) {
399      suite.err() << "ok\n";
400    }
401  }
402
[1232]403  suite.err() << "Testing OptionArg required ...";
[980]404  {
405    OptionArg<std::string> required(cmd, "required", "required", true); 
406    int ac = 1;
407    char* av[] = { "test_prog" };
408    try{
409      cmd.parse(ac,av);
[1232]410      suite.err() << "failed\n";
[980]411      ok =false;
412    }
413    catch (...) {
[1232]414      suite.err() << "ok\n";
[980]415    }
416  }
417
418
[965]419  return ok;
420}
421
422
[1232]423bool test_file(yat::test::Suite& suite)
[979]424{
425  bool ok=true;
426  CommandLine cmd;
427  OptionFile inclones(cmd, "clones", "file containing clones");
428  OptionFile indata(cmd, "data", "data to merge");
[1178]429  OptionFile out(cmd, "o,out", "data to merge", true, false, "w");
[979]430  OptionSwitch help(cmd, "h,help", "display this help and exit");
[965]431
[1232]432  suite.err() << "Testing OptionFile... ";
[979]433  {
[1426]434    int ac = 7;
435    char* av[] = { "test_prog", "--clones", "commandline_test.cc", 
[1178]436                   "--data", "commandline_test.cc", "-o", 
437                   "commandline_test.cc"};
[979]438    cmd.parse(ac,av);
[1232]439    suite.err() << "ok\n";
[979]440  }
441  return ok;
442}
[965]443
[1426]444bool test_option_name_clash(yat::test::Suite& suite)
445{
446  bool ok=true;
447  suite.err() << "Testing long option name clash ...";
448  try {
449    CommandLine cmd;
450    OptionSwitch op1(cmd, "opt", "bla bla");
451    OptionSwitch op2(cmd, "o,opt", "other bla");
452    ok=false;
453    suite.err() << "failed\n";
454  }
455  catch (std::runtime_error& e) {
456    suite.err() << "ok\n";
457  }
458  suite.err() << "Testing short option name clash ...";
459  try {
460    CommandLine cmd;
461    OptionSwitch op1(cmd, "o", "bla bla");
462    OptionSwitch op2(cmd, "o,opt", "other bla");
463    ok=false;
464    suite.err() << "failed\n";
465  }
466  catch (std::runtime_error& e) {
467    suite.err() << "ok\n";
468  }
[965]469
[1426]470  return ok;
471}
[965]472
[1427]473bool test_free_arg(yat::test::Suite& suite)
474{
475  bool ok=true;
476  suite.err() << "Testing free arguments ...";
[979]477
[1427]478  CommandLine cmd;
[1468]479  cmd.allow_free_args(2);
[1427]480  OptionHelp help(cmd);
481  int ac = 3;
482  char* av[] = { "test_prog", "file", "kl"};
483  cmd.parse(ac, av);
484  suite.err() << "ok\n";
485  return ok;
486}
[979]487
[1426]488
[1427]489
Note: See TracBrowser for help on using the repository browser.