source: trunk/test/commandline_test.cc @ 979

Last change on this file since 979 was 979, checked in by Peter, 16 years ago

fixing bug in OptionFile?. Dont try to create FileUtil? for option not present in commandline

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