965 |
11 Oct 07 |
peter |
// $Id$ |
965 |
11 Oct 07 |
peter |
2 |
|
965 |
11 Oct 07 |
peter |
3 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008, 2009 Jari Häkkinen, Peter Johansson |
2703 |
12 Mar 12 |
peter |
Copyright (C) 2010, 2011, 2012 Peter Johansson |
965 |
11 Oct 07 |
peter |
7 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
965 |
11 Oct 07 |
peter |
9 |
|
965 |
11 Oct 07 |
peter |
The yat library is free software; you can redistribute it and/or |
965 |
11 Oct 07 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
965 |
11 Oct 07 |
peter |
License, or (at your option) any later version. |
965 |
11 Oct 07 |
peter |
14 |
|
965 |
11 Oct 07 |
peter |
The yat library is distributed in the hope that it will be useful, |
965 |
11 Oct 07 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
965 |
11 Oct 07 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
965 |
11 Oct 07 |
peter |
General Public License for more details. |
965 |
11 Oct 07 |
peter |
19 |
|
965 |
11 Oct 07 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
965 |
11 Oct 07 |
peter |
22 |
*/ |
965 |
11 Oct 07 |
peter |
23 |
|
2881 |
18 Nov 12 |
peter |
24 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
25 |
|
1232 |
15 Mar 08 |
peter |
26 |
#include "Suite.h" |
1232 |
15 Mar 08 |
peter |
27 |
|
965 |
11 Oct 07 |
peter |
28 |
#include "yat/utility/CommandLine.h" |
1954 |
07 May 09 |
jari |
29 |
#include "yat/utility/FileUtil.h" |
965 |
11 Oct 07 |
peter |
30 |
#include "yat/utility/OptionArg.h" |
975 |
17 Oct 07 |
peter |
31 |
#include "yat/utility/OptionFile.h" |
981 |
22 Oct 07 |
peter |
32 |
#include "yat/utility/OptionHelp.h" |
965 |
11 Oct 07 |
peter |
33 |
#include "yat/utility/OptionSwitch.h" |
965 |
11 Oct 07 |
peter |
34 |
|
2202 |
21 Feb 10 |
peter |
35 |
#include <cassert> |
2382 |
21 Dec 10 |
peter |
36 |
#include <cstdlib> |
965 |
11 Oct 07 |
peter |
37 |
#include <fstream> |
965 |
11 Oct 07 |
peter |
38 |
#include <stdexcept> |
2202 |
21 Feb 10 |
peter |
39 |
#include <string> |
965 |
11 Oct 07 |
peter |
40 |
|
2202 |
21 Feb 10 |
peter |
41 |
#include <string.h> |
2202 |
21 Feb 10 |
peter |
42 |
|
1232 |
15 Mar 08 |
peter |
43 |
using namespace theplu; |
1232 |
15 Mar 08 |
peter |
44 |
using namespace yat::utility; |
1232 |
15 Mar 08 |
peter |
45 |
bool cmd_help(yat::test::Suite& error); |
2202 |
21 Feb 10 |
peter |
46 |
char** stradup(const char* str[], int size); |
2202 |
21 Feb 10 |
peter |
47 |
void strafree(char** str, int size); |
1232 |
15 Mar 08 |
peter |
48 |
bool test_switch(yat::test::Suite& error); |
1232 |
15 Mar 08 |
peter |
49 |
bool test_arg(yat::test::Suite& error); |
1874 |
17 Mar 09 |
peter |
50 |
void test_exception_msg(yat::test::Suite& error); |
1232 |
15 Mar 08 |
peter |
51 |
bool test_file(yat::test::Suite& error); |
1954 |
07 May 09 |
jari |
52 |
bool test_file2(yat::test::Suite& error); |
2049 |
04 Sep 09 |
peter |
53 |
bool test_file3(yat::test::Suite& error); |
2049 |
04 Sep 09 |
peter |
54 |
bool test_file3_(yat::test::Suite& error, const std::string&); |
2049 |
04 Sep 09 |
peter |
55 |
bool test_file4(yat::test::Suite& error); |
2049 |
04 Sep 09 |
peter |
56 |
bool test_file4_(yat::test::Suite& error, const std::string&); |
1232 |
15 Mar 08 |
peter |
57 |
bool test_failures(yat::test::Suite& error); |
1426 |
20 Aug 08 |
peter |
58 |
bool test_option_name_clash(yat::test::Suite& suite); |
1427 |
20 Aug 08 |
peter |
59 |
bool test_free_arg(yat::test::Suite& suite); |
965 |
11 Oct 07 |
peter |
60 |
|
1232 |
15 Mar 08 |
peter |
61 |
int main(int argc, char* argv[]) |
2739 |
07 Jun 12 |
peter |
62 |
{ |
1232 |
15 Mar 08 |
peter |
63 |
yat::test::Suite suite(argc, argv); |
965 |
11 Oct 07 |
peter |
64 |
|
1232 |
15 Mar 08 |
peter |
65 |
suite.err() << "testing commandline" << std::endl; |
1232 |
15 Mar 08 |
peter |
66 |
|
965 |
11 Oct 07 |
peter |
67 |
try { |
1232 |
15 Mar 08 |
peter |
68 |
suite.add(cmd_help(suite)); |
1232 |
15 Mar 08 |
peter |
69 |
suite.add(test_switch(suite)); |
1232 |
15 Mar 08 |
peter |
70 |
suite.add(test_arg(suite)); |
1232 |
15 Mar 08 |
peter |
71 |
suite.add(test_file(suite)); |
1954 |
07 May 09 |
jari |
72 |
suite.add(test_file2(suite)); |
2049 |
04 Sep 09 |
peter |
73 |
suite.add(test_file3(suite)); |
2049 |
04 Sep 09 |
peter |
74 |
suite.add(test_file4(suite)); |
1232 |
15 Mar 08 |
peter |
75 |
suite.add(test_failures(suite)); |
1426 |
20 Aug 08 |
peter |
76 |
suite.add(test_option_name_clash(suite)); |
1468 |
02 Sep 08 |
peter |
77 |
suite.add(test_free_arg(suite)); |
1874 |
17 Mar 09 |
peter |
78 |
test_exception_msg(suite); |
965 |
11 Oct 07 |
peter |
79 |
} |
965 |
11 Oct 07 |
peter |
80 |
catch (std::runtime_error& e) { |
2739 |
07 Jun 12 |
peter |
81 |
suite.err() << "Error: unexpected exception thrown\n" << e.what() |
1426 |
20 Aug 08 |
peter |
82 |
<< std::endl; |
1426 |
20 Aug 08 |
peter |
83 |
suite.add(false); |
965 |
11 Oct 07 |
peter |
84 |
} |
965 |
11 Oct 07 |
peter |
85 |
|
975 |
17 Oct 07 |
peter |
86 |
CommandLine cmd; |
975 |
17 Oct 07 |
peter |
87 |
OptionFile file(cmd, "f,file", "description"); |
975 |
17 Oct 07 |
peter |
88 |
|
1232 |
15 Mar 08 |
peter |
89 |
return suite.return_value(); |
965 |
11 Oct 07 |
peter |
90 |
} |
965 |
11 Oct 07 |
peter |
91 |
|
965 |
11 Oct 07 |
peter |
92 |
|
1232 |
15 Mar 08 |
peter |
93 |
bool cmd_help(yat::test::Suite& suite) |
965 |
11 Oct 07 |
peter |
94 |
{ |
965 |
11 Oct 07 |
peter |
95 |
using namespace theplu::yat::utility; |
965 |
11 Oct 07 |
peter |
96 |
CommandLine cmd; |
965 |
11 Oct 07 |
peter |
97 |
OptionArg<std::string> dir(cmd, "d,dir", "output directory"); |
981 |
22 Oct 07 |
peter |
98 |
OptionHelp help(cmd); |
2739 |
07 Jun 12 |
peter |
99 |
OptionArg<std::string> target(cmd, "T,target", "treat DEST as a normal file", |
1632 |
19 Nov 08 |
peter |
100 |
true); |
1632 |
19 Nov 08 |
peter |
101 |
target.print_arg("=TARGET"); |
965 |
11 Oct 07 |
peter |
102 |
OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
965 |
11 Oct 07 |
peter |
103 |
OptionSwitch version(cmd, "version", "output version and exit"); |
965 |
11 Oct 07 |
peter |
104 |
|
1232 |
15 Mar 08 |
peter |
105 |
suite.err() << cmd; |
965 |
11 Oct 07 |
peter |
106 |
return true; |
965 |
11 Oct 07 |
peter |
107 |
} |
965 |
11 Oct 07 |
peter |
108 |
|
1874 |
17 Mar 09 |
peter |
109 |
|
2202 |
21 Feb 10 |
peter |
110 |
char** stradup(const char** str, int size) |
2202 |
21 Feb 10 |
peter |
111 |
{ |
2202 |
21 Feb 10 |
peter |
112 |
char** res = new char*[size]; |
2202 |
21 Feb 10 |
peter |
113 |
for (int i = 0; i<size; ++i) { |
2202 |
21 Feb 10 |
peter |
114 |
res[i] = strdup(str[i]); |
2202 |
21 Feb 10 |
peter |
115 |
assert(res[i]); |
2202 |
21 Feb 10 |
peter |
116 |
} |
2202 |
21 Feb 10 |
peter |
117 |
return res; |
2202 |
21 Feb 10 |
peter |
118 |
} |
2202 |
21 Feb 10 |
peter |
119 |
|
2202 |
21 Feb 10 |
peter |
120 |
|
2202 |
21 Feb 10 |
peter |
121 |
void strafree(char** str, int size) |
2202 |
21 Feb 10 |
peter |
122 |
{ |
2202 |
21 Feb 10 |
peter |
123 |
for (int i = 0; i<size; ++i) { |
2202 |
21 Feb 10 |
peter |
124 |
free(str[i]); |
2202 |
21 Feb 10 |
peter |
125 |
} |
2202 |
21 Feb 10 |
peter |
126 |
} |
2202 |
21 Feb 10 |
peter |
127 |
|
2202 |
21 Feb 10 |
peter |
128 |
|
1874 |
17 Mar 09 |
peter |
129 |
void test_exception_msg(yat::test::Suite& suite) |
1874 |
17 Mar 09 |
peter |
130 |
{ |
1874 |
17 Mar 09 |
peter |
// test for ticket 508 |
1874 |
17 Mar 09 |
peter |
132 |
using namespace theplu::yat::utility; |
1874 |
17 Mar 09 |
peter |
133 |
CommandLine cmd; |
1874 |
17 Mar 09 |
peter |
134 |
OptionHelp help(cmd, "h,help", ""); |
1874 |
17 Mar 09 |
peter |
135 |
OptionFile indata(cmd, "i,in", "input file", true, true,"r"); |
1874 |
17 Mar 09 |
peter |
136 |
OptionSwitch cmd_memory(cmd, "m", "transpose in a memory cheap manner"); |
1874 |
17 Mar 09 |
peter |
137 |
OptionArg<size_t> cmd_n(cmd, "n", "number of rows to print in each iteration"); |
2739 |
07 Jun 12 |
peter |
138 |
OptionSwitch cmd_numeric(cmd, "numeric", |
1874 |
17 Mar 09 |
peter |
139 |
"input is a numeric matrix"); |
1874 |
17 Mar 09 |
peter |
140 |
OptionFile outdata(cmd, "o,out", "output file",true, false,"w"); |
1874 |
17 Mar 09 |
peter |
141 |
OptionSwitch verbose(cmd, "v,verbose", "display progress"); |
1874 |
17 Mar 09 |
peter |
142 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
143 |
const char* cav[] = { "test_prog", "--haha" }; |
2202 |
21 Feb 10 |
peter |
144 |
char** av = stradup(cav, ac); |
1874 |
17 Mar 09 |
peter |
145 |
try { |
1874 |
17 Mar 09 |
peter |
146 |
cmd.parse(ac,av); |
1874 |
17 Mar 09 |
peter |
147 |
} |
1874 |
17 Mar 09 |
peter |
148 |
catch (std::runtime_error& e) { |
1874 |
17 Mar 09 |
peter |
149 |
std::string msg(e.what()); |
1874 |
17 Mar 09 |
peter |
150 |
if (msg.size()<15) { |
2705 |
13 Mar 12 |
peter |
151 |
suite.add(false); |
2739 |
07 Jun 12 |
peter |
152 |
suite.err() << "Error: short exception message\n" |
2739 |
07 Jun 12 |
peter |
153 |
<< " exception message is: `" << msg |
2739 |
07 Jun 12 |
peter |
154 |
<< "' that is " << msg.size() << " characters long.\n" |
1874 |
17 Mar 09 |
peter |
155 |
<< " expected at least 15 characters\n"; |
1874 |
17 Mar 09 |
peter |
156 |
} |
1874 |
17 Mar 09 |
peter |
157 |
} |
2202 |
21 Feb 10 |
peter |
158 |
strafree(av, ac); |
1874 |
17 Mar 09 |
peter |
159 |
} |
1874 |
17 Mar 09 |
peter |
160 |
|
1874 |
17 Mar 09 |
peter |
161 |
|
1232 |
15 Mar 08 |
peter |
162 |
bool test_switch(yat::test::Suite& suite) |
965 |
11 Oct 07 |
peter |
163 |
{ |
965 |
11 Oct 07 |
peter |
164 |
bool ok=true; |
965 |
11 Oct 07 |
peter |
165 |
CommandLine cmd; |
965 |
11 Oct 07 |
peter |
166 |
OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true); |
965 |
11 Oct 07 |
peter |
167 |
OptionSwitch verbose(cmd, "v,verbose", "explain what is being done", true); |
965 |
11 Oct 07 |
peter |
168 |
|
1232 |
15 Mar 08 |
peter |
169 |
suite.err() << "Testing OptionSwitch -T..."; |
965 |
11 Oct 07 |
peter |
170 |
{ |
965 |
11 Oct 07 |
peter |
171 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
172 |
const char* cav[] = { "test_prog", "-T" }; |
2202 |
21 Feb 10 |
peter |
173 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
174 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
175 |
if (target.present() && !verbose.present()) |
1232 |
15 Mar 08 |
peter |
176 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
177 |
else { |
1232 |
15 Mar 08 |
peter |
178 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
179 |
ok =false; |
965 |
11 Oct 07 |
peter |
180 |
} |
2202 |
21 Feb 10 |
peter |
181 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
182 |
} |
965 |
11 Oct 07 |
peter |
183 |
|
965 |
11 Oct 07 |
peter |
184 |
|
1232 |
15 Mar 08 |
peter |
185 |
suite.err() << "Testing OptionSwitch --target..."; |
965 |
11 Oct 07 |
peter |
186 |
{ |
965 |
11 Oct 07 |
peter |
187 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
188 |
const char* cav[] = { "test_prog", "--target" }; |
2202 |
21 Feb 10 |
peter |
189 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
190 |
cmd.parse(ac,av); |
1634 |
19 Nov 08 |
peter |
191 |
if (target.present() && target.value() && !verbose.present()) |
1232 |
15 Mar 08 |
peter |
192 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
193 |
else { |
1232 |
15 Mar 08 |
peter |
194 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
195 |
ok =false; |
965 |
11 Oct 07 |
peter |
196 |
} |
2202 |
21 Feb 10 |
peter |
197 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
198 |
} |
965 |
11 Oct 07 |
peter |
199 |
|
1232 |
15 Mar 08 |
peter |
200 |
suite.err() << "Testing OptionSwitch -T -v..."; |
965 |
11 Oct 07 |
peter |
201 |
{ |
965 |
11 Oct 07 |
peter |
202 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
203 |
const char* cav[] = { "test_prog", "-T" , "-v"}; |
2202 |
21 Feb 10 |
peter |
204 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
205 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
206 |
if (target.present() && verbose.present()) |
1232 |
15 Mar 08 |
peter |
207 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
208 |
else { |
1232 |
15 Mar 08 |
peter |
209 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
210 |
ok =false; |
965 |
11 Oct 07 |
peter |
211 |
} |
2202 |
21 Feb 10 |
peter |
212 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
213 |
} |
965 |
11 Oct 07 |
peter |
214 |
|
1232 |
15 Mar 08 |
peter |
215 |
suite.err() << "Testing OptionSwitch -vT..."; |
965 |
11 Oct 07 |
peter |
216 |
{ |
965 |
11 Oct 07 |
peter |
217 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
218 |
const char* cav[] = { "test_prog", "-vT"}; |
2202 |
21 Feb 10 |
peter |
219 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
220 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
221 |
if (target.present() && verbose.present()) |
1232 |
15 Mar 08 |
peter |
222 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
223 |
else { |
1232 |
15 Mar 08 |
peter |
224 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
225 |
ok =false; |
965 |
11 Oct 07 |
peter |
226 |
} |
2202 |
21 Feb 10 |
peter |
227 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
228 |
} |
1634 |
19 Nov 08 |
peter |
229 |
|
1634 |
19 Nov 08 |
peter |
230 |
suite.err() << "Testing OptionSwitch --no-target..."; |
1634 |
19 Nov 08 |
peter |
231 |
{ |
1634 |
19 Nov 08 |
peter |
232 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
233 |
const char* cav[] = { "test_prog", "--no-target" }; |
2202 |
21 Feb 10 |
peter |
234 |
char** av = stradup(cav, ac); |
1634 |
19 Nov 08 |
peter |
235 |
cmd.parse(ac,av); |
1634 |
19 Nov 08 |
peter |
236 |
if (target.present() && !target.value()) |
1634 |
19 Nov 08 |
peter |
237 |
suite.err() << "ok\n"; |
1634 |
19 Nov 08 |
peter |
238 |
else { |
1634 |
19 Nov 08 |
peter |
239 |
suite.err() << "failed\n"; |
1634 |
19 Nov 08 |
peter |
240 |
if (!target.present()) |
1634 |
19 Nov 08 |
peter |
241 |
suite.err() << "target.present() returned false\n"; |
1634 |
19 Nov 08 |
peter |
242 |
if (target.value()) |
1634 |
19 Nov 08 |
peter |
243 |
suite.err() << "target.value() returned true\n"; |
1634 |
19 Nov 08 |
peter |
244 |
ok =false; |
1634 |
19 Nov 08 |
peter |
245 |
} |
2202 |
21 Feb 10 |
peter |
246 |
strafree(av, ac); |
1634 |
19 Nov 08 |
peter |
247 |
} |
965 |
11 Oct 07 |
peter |
248 |
return ok; |
965 |
11 Oct 07 |
peter |
249 |
} |
965 |
11 Oct 07 |
peter |
250 |
|
965 |
11 Oct 07 |
peter |
251 |
|
1232 |
15 Mar 08 |
peter |
252 |
bool test_arg(yat::test::Suite& suite) |
965 |
11 Oct 07 |
peter |
253 |
{ |
965 |
11 Oct 07 |
peter |
254 |
bool ok=true; |
965 |
11 Oct 07 |
peter |
255 |
CommandLine cmd; |
965 |
11 Oct 07 |
peter |
256 |
OptionArg<std::string> input(cmd, "i,input", "input file"); |
1273 |
10 Apr 08 |
jari |
257 |
OptionArg<unsigned int> n(cmd, "n", "number of lines"); |
1465 |
02 Sep 08 |
peter |
258 |
OptionArg<double> x(cmd, "x,extra", "a float number"); |
965 |
11 Oct 07 |
peter |
259 |
|
1232 |
15 Mar 08 |
peter |
260 |
suite.err() << "Testing OptionArg existence -i file..."; |
965 |
11 Oct 07 |
peter |
261 |
{ |
965 |
11 Oct 07 |
peter |
262 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
263 |
const char* cav[] = { "test_prog", "-i", "file" }; |
2202 |
21 Feb 10 |
peter |
264 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
265 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
266 |
if (input.present()) |
1232 |
15 Mar 08 |
peter |
267 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
268 |
else { |
1232 |
15 Mar 08 |
peter |
269 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
270 |
ok =false; |
965 |
11 Oct 07 |
peter |
271 |
} |
2202 |
21 Feb 10 |
peter |
272 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
273 |
} |
965 |
11 Oct 07 |
peter |
274 |
|
1232 |
15 Mar 08 |
peter |
275 |
suite.err() << "Testing OptionArg value -i file..."; |
965 |
11 Oct 07 |
peter |
276 |
{ |
965 |
11 Oct 07 |
peter |
277 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
278 |
const char* cav[] = { "test_prog", "-i", "file" }; |
2202 |
21 Feb 10 |
peter |
279 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
280 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
281 |
if (input.value()=="file") |
1232 |
15 Mar 08 |
peter |
282 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
283 |
else { |
1232 |
15 Mar 08 |
peter |
284 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
285 |
ok =false; |
965 |
11 Oct 07 |
peter |
286 |
} |
2202 |
21 Feb 10 |
peter |
287 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
288 |
} |
965 |
11 Oct 07 |
peter |
289 |
|
1232 |
15 Mar 08 |
peter |
290 |
suite.err() << "Testing OptionArg existence --input file..."; |
965 |
11 Oct 07 |
peter |
291 |
{ |
965 |
11 Oct 07 |
peter |
292 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
293 |
const char* cav[] = { "test_prog", "--input", "file" }; |
2202 |
21 Feb 10 |
peter |
294 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
295 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
296 |
if (input.present()) |
1232 |
15 Mar 08 |
peter |
297 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
298 |
else { |
1232 |
15 Mar 08 |
peter |
299 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
300 |
ok =false; |
965 |
11 Oct 07 |
peter |
301 |
} |
2202 |
21 Feb 10 |
peter |
302 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
303 |
} |
965 |
11 Oct 07 |
peter |
304 |
|
1232 |
15 Mar 08 |
peter |
305 |
suite.err() << "Testing OptionArg value --input=file..."; |
965 |
11 Oct 07 |
peter |
306 |
{ |
965 |
11 Oct 07 |
peter |
307 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
308 |
const char* cav[] = { "test_prog", "--input=file" }; |
2202 |
21 Feb 10 |
peter |
309 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
310 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
311 |
if (input.value()=="file") |
1232 |
15 Mar 08 |
peter |
312 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
313 |
else { |
1232 |
15 Mar 08 |
peter |
314 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
315 |
ok =false; |
965 |
11 Oct 07 |
peter |
316 |
} |
2202 |
21 Feb 10 |
peter |
317 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
318 |
} |
965 |
11 Oct 07 |
peter |
319 |
|
1232 |
15 Mar 08 |
peter |
320 |
suite.err() << "Testing OptionArg value --input=\"file called something\"..."; |
965 |
11 Oct 07 |
peter |
321 |
{ |
965 |
11 Oct 07 |
peter |
322 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
323 |
const char* cav[] = { "test_prog", "--input=\"file called something\"" }; |
2202 |
21 Feb 10 |
peter |
324 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
325 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
326 |
if (input.value()=="file called something") |
1232 |
15 Mar 08 |
peter |
327 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
328 |
else { |
1232 |
15 Mar 08 |
peter |
329 |
suite.err() << "failed\n"; |
1232 |
15 Mar 08 |
peter |
330 |
suite.err() << "value is `" << input.value() << "'\n" |
965 |
11 Oct 07 |
peter |
331 |
<< "expected `file called something'\n"; |
965 |
11 Oct 07 |
peter |
332 |
ok =false; |
965 |
11 Oct 07 |
peter |
333 |
} |
2202 |
21 Feb 10 |
peter |
334 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
335 |
} |
965 |
11 Oct 07 |
peter |
336 |
|
1273 |
10 Apr 08 |
jari |
337 |
suite.err() << "Testing OptionArg unsigned int value -n 3..."; |
965 |
11 Oct 07 |
peter |
338 |
{ |
965 |
11 Oct 07 |
peter |
339 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
340 |
const char* cav[] = { "test_prog", "-n", "3" }; |
2202 |
21 Feb 10 |
peter |
341 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
342 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
343 |
if (n.value()==3) |
1232 |
15 Mar 08 |
peter |
344 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
345 |
else { |
1232 |
15 Mar 08 |
peter |
346 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
347 |
ok =false; |
965 |
11 Oct 07 |
peter |
348 |
} |
2202 |
21 Feb 10 |
peter |
349 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
350 |
} |
965 |
11 Oct 07 |
peter |
351 |
|
965 |
11 Oct 07 |
peter |
352 |
|
1232 |
15 Mar 08 |
peter |
353 |
suite.err() << "Testing OptionArg 2 value --input file -n 3..."; |
965 |
11 Oct 07 |
peter |
354 |
{ |
965 |
11 Oct 07 |
peter |
355 |
int ac = 5; |
2202 |
21 Feb 10 |
peter |
356 |
const char* cav[] = { "test_prog", "--input", "file", "-n", "3" }; |
2202 |
21 Feb 10 |
peter |
357 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
358 |
cmd.parse(ac,av); |
965 |
11 Oct 07 |
peter |
359 |
if (input.value()=="file") |
1232 |
15 Mar 08 |
peter |
360 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
361 |
else { |
1232 |
15 Mar 08 |
peter |
362 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
363 |
ok =false; |
965 |
11 Oct 07 |
peter |
364 |
} |
2202 |
21 Feb 10 |
peter |
365 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
366 |
} |
965 |
11 Oct 07 |
peter |
367 |
|
1437 |
25 Aug 08 |
peter |
368 |
suite.err() << "Testing OptionArg double value -x -6..."; |
1437 |
25 Aug 08 |
peter |
369 |
try { |
1437 |
25 Aug 08 |
peter |
370 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
371 |
const char* cav[] = { "test_prog", "-x", "-6" }; |
2202 |
21 Feb 10 |
peter |
372 |
char** av = stradup(cav, ac); |
1437 |
25 Aug 08 |
peter |
373 |
cmd.parse(ac,av); |
1437 |
25 Aug 08 |
peter |
374 |
if (x.value()==-6) |
1437 |
25 Aug 08 |
peter |
375 |
suite.err() << "ok\n"; |
1437 |
25 Aug 08 |
peter |
376 |
else { |
1437 |
25 Aug 08 |
peter |
377 |
suite.err() << "failed\n"; |
1437 |
25 Aug 08 |
peter |
378 |
ok =false; |
1437 |
25 Aug 08 |
peter |
379 |
} |
2202 |
21 Feb 10 |
peter |
380 |
strafree(av, ac); |
1437 |
25 Aug 08 |
peter |
381 |
} |
1437 |
25 Aug 08 |
peter |
382 |
catch (std::runtime_error& e) { |
1465 |
02 Sep 08 |
peter |
383 |
ok = false; |
1437 |
25 Aug 08 |
peter |
384 |
suite.err() << "failed\nexception thrown with what(): " << e.what() << "\n"; |
1437 |
25 Aug 08 |
peter |
385 |
} |
965 |
11 Oct 07 |
peter |
386 |
|
1465 |
02 Sep 08 |
peter |
387 |
suite.err() << "Testing OptionArg double value --extra -6..."; |
1465 |
02 Sep 08 |
peter |
388 |
try { |
1465 |
02 Sep 08 |
peter |
389 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
390 |
const char* cav[] = { "test_prog", "--extra", "-6" }; |
2202 |
21 Feb 10 |
peter |
391 |
char** av = stradup(cav, ac); |
1465 |
02 Sep 08 |
peter |
392 |
cmd.parse(ac,av); |
1465 |
02 Sep 08 |
peter |
393 |
if (x.value()==-6) |
1465 |
02 Sep 08 |
peter |
394 |
suite.err() << "ok\n"; |
1465 |
02 Sep 08 |
peter |
395 |
else { |
1465 |
02 Sep 08 |
peter |
396 |
suite.err() << "failed\n"; |
1465 |
02 Sep 08 |
peter |
397 |
ok =false; |
1465 |
02 Sep 08 |
peter |
398 |
} |
2202 |
21 Feb 10 |
peter |
399 |
strafree(av, ac); |
1465 |
02 Sep 08 |
peter |
400 |
} |
1465 |
02 Sep 08 |
peter |
401 |
catch (std::runtime_error& e) { |
1465 |
02 Sep 08 |
peter |
402 |
ok=false; |
1465 |
02 Sep 08 |
peter |
403 |
suite.err() << "failed\nexception thrown with what(): " << e.what() << "\n"; |
1465 |
02 Sep 08 |
peter |
404 |
} |
1437 |
25 Aug 08 |
peter |
405 |
|
1602 |
27 Oct 08 |
peter |
406 |
suite.err() << "Testing OptionArg::value(T) ... "; |
1602 |
27 Oct 08 |
peter |
407 |
try { |
1602 |
27 Oct 08 |
peter |
408 |
int ac = 1; |
2202 |
21 Feb 10 |
peter |
409 |
const char* cav[] = { "test_prog"}; |
2202 |
21 Feb 10 |
peter |
410 |
char** av = stradup(cav, ac); |
1602 |
27 Oct 08 |
peter |
411 |
cmd.parse(ac,av); |
1602 |
27 Oct 08 |
peter |
412 |
n.value(5); |
1602 |
27 Oct 08 |
peter |
413 |
if (n.present()==false && n.value()==5) |
1602 |
27 Oct 08 |
peter |
414 |
suite.err() << "ok\n"; |
1602 |
27 Oct 08 |
peter |
415 |
else { |
1602 |
27 Oct 08 |
peter |
416 |
suite.err() << "failed\n"; |
1602 |
27 Oct 08 |
peter |
417 |
ok = false; |
1602 |
27 Oct 08 |
peter |
418 |
} |
2202 |
21 Feb 10 |
peter |
419 |
strafree(av, ac); |
1602 |
27 Oct 08 |
peter |
420 |
} |
1602 |
27 Oct 08 |
peter |
421 |
catch (std::runtime_error& e) { |
1602 |
27 Oct 08 |
peter |
422 |
suite.err() << "failed\n"; |
1602 |
27 Oct 08 |
peter |
423 |
ok = false; |
1602 |
27 Oct 08 |
peter |
424 |
} |
1602 |
27 Oct 08 |
peter |
425 |
|
2352 |
27 Nov 10 |
peter |
// test for ticket 645 |
2352 |
27 Nov 10 |
peter |
427 |
suite.err() << "Testing error message with -n STRING\n"; |
2352 |
27 Nov 10 |
peter |
428 |
try { |
2352 |
27 Nov 10 |
peter |
429 |
int ac = 3; |
2352 |
27 Nov 10 |
peter |
430 |
const char* cav[] = { "test_prog", "-n", "STRING"}; |
2352 |
27 Nov 10 |
peter |
431 |
char** av = stradup(cav, ac); |
2352 |
27 Nov 10 |
peter |
432 |
try { |
2352 |
27 Nov 10 |
peter |
433 |
cmd.parse(ac,av); |
2352 |
27 Nov 10 |
peter |
434 |
strafree(av, ac); |
2352 |
27 Nov 10 |
peter |
435 |
ok=false; |
2739 |
07 Jun 12 |
peter |
436 |
suite.err() << "error: expected parse to throw\n"; |
2352 |
27 Nov 10 |
peter |
437 |
} |
2352 |
27 Nov 10 |
peter |
438 |
catch (std::runtime_error& e) { |
2352 |
27 Nov 10 |
peter |
439 |
strafree(av, ac); |
2352 |
27 Nov 10 |
peter |
440 |
suite.err() << "expected exception: what(): " << e.what() << "\n"; |
2352 |
27 Nov 10 |
peter |
441 |
} |
2352 |
27 Nov 10 |
peter |
442 |
} |
2352 |
27 Nov 10 |
peter |
443 |
catch (std::runtime_error& e) { |
2352 |
27 Nov 10 |
peter |
444 |
suite.err() << "failed: " << e.what() << "\n"; |
2352 |
27 Nov 10 |
peter |
445 |
ok=false; |
2352 |
27 Nov 10 |
peter |
446 |
} |
965 |
11 Oct 07 |
peter |
447 |
return ok; |
965 |
11 Oct 07 |
peter |
448 |
} |
965 |
11 Oct 07 |
peter |
449 |
|
1232 |
15 Mar 08 |
peter |
450 |
bool test_failures(yat::test::Suite& suite) |
965 |
11 Oct 07 |
peter |
451 |
{ |
965 |
11 Oct 07 |
peter |
452 |
bool ok=true; |
965 |
11 Oct 07 |
peter |
453 |
CommandLine cmd; |
965 |
11 Oct 07 |
peter |
454 |
OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
965 |
11 Oct 07 |
peter |
455 |
OptionArg<std::string> input(cmd, "i,input", "input file"); |
1273 |
10 Apr 08 |
jari |
456 |
OptionArg<unsigned int> n(cmd, "n", "number of lines"); |
965 |
11 Oct 07 |
peter |
457 |
|
1232 |
15 Mar 08 |
peter |
458 |
suite.err() << "Testing unknown option --peter..."; |
965 |
11 Oct 07 |
peter |
459 |
{ |
965 |
11 Oct 07 |
peter |
460 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
461 |
const char* cav[] = { "test_prog", "--peter"}; |
2202 |
21 Feb 10 |
peter |
462 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
463 |
try{ |
965 |
11 Oct 07 |
peter |
464 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
465 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
466 |
ok =false; |
965 |
11 Oct 07 |
peter |
467 |
} |
965 |
11 Oct 07 |
peter |
468 |
catch (...) { |
1232 |
15 Mar 08 |
peter |
469 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
470 |
} |
2202 |
21 Feb 10 |
peter |
471 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
472 |
} |
965 |
11 Oct 07 |
peter |
473 |
|
1232 |
15 Mar 08 |
peter |
474 |
suite.err() << "Testing unknown option -jvjhsgad..."; |
965 |
11 Oct 07 |
peter |
475 |
{ |
965 |
11 Oct 07 |
peter |
476 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
477 |
const char* cav[] = { "test_prog", "-vjhsgad"}; |
2202 |
21 Feb 10 |
peter |
478 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
479 |
try{ |
965 |
11 Oct 07 |
peter |
480 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
481 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
482 |
ok =false; |
965 |
11 Oct 07 |
peter |
483 |
} |
965 |
11 Oct 07 |
peter |
484 |
catch (...) { |
1232 |
15 Mar 08 |
peter |
485 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
486 |
} |
2202 |
21 Feb 10 |
peter |
487 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
488 |
} |
965 |
11 Oct 07 |
peter |
489 |
|
965 |
11 Oct 07 |
peter |
490 |
|
1232 |
15 Mar 08 |
peter |
491 |
suite.err() << "Testing invalid option -nv 3..."; |
965 |
11 Oct 07 |
peter |
492 |
{ |
965 |
11 Oct 07 |
peter |
493 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
494 |
const char* cav[] = { "test_prog", "-nv", "3"}; |
2202 |
21 Feb 10 |
peter |
495 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
496 |
try{ |
965 |
11 Oct 07 |
peter |
497 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
498 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
499 |
ok =false; |
965 |
11 Oct 07 |
peter |
500 |
} |
965 |
11 Oct 07 |
peter |
501 |
catch (...) { |
1232 |
15 Mar 08 |
peter |
502 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
503 |
} |
2202 |
21 Feb 10 |
peter |
504 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
505 |
} |
965 |
11 Oct 07 |
peter |
506 |
|
965 |
11 Oct 07 |
peter |
507 |
|
1273 |
10 Apr 08 |
jari |
508 |
suite.err() << "Testing 23.12 is not unsigned int..."; |
965 |
11 Oct 07 |
peter |
509 |
{ |
965 |
11 Oct 07 |
peter |
510 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
511 |
const char* cav[] = { "test_prog", "-n", "23.12"}; |
2202 |
21 Feb 10 |
peter |
512 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
513 |
try{ |
965 |
11 Oct 07 |
peter |
514 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
515 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
516 |
ok =false; |
965 |
11 Oct 07 |
peter |
517 |
} |
965 |
11 Oct 07 |
peter |
518 |
catch (std::runtime_error& e) { |
1232 |
15 Mar 08 |
peter |
519 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
520 |
} |
2202 |
21 Feb 10 |
peter |
521 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
522 |
} |
965 |
11 Oct 07 |
peter |
523 |
|
1273 |
10 Apr 08 |
jari |
524 |
suite.err() << "Testing -1 is not unsigned int..."; |
965 |
11 Oct 07 |
peter |
525 |
{ |
965 |
11 Oct 07 |
peter |
526 |
int ac = 3; |
2673 |
03 Dec 11 |
peter |
527 |
const char* cav[] = { "test_prog", "-n", "-1"}; |
2202 |
21 Feb 10 |
peter |
528 |
char** av = stradup(cav, ac); |
965 |
11 Oct 07 |
peter |
529 |
try{ |
965 |
11 Oct 07 |
peter |
530 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
531 |
suite.err() << "failed\n"; |
965 |
11 Oct 07 |
peter |
532 |
ok =false; |
965 |
11 Oct 07 |
peter |
533 |
} |
965 |
11 Oct 07 |
peter |
534 |
catch (...) { |
1232 |
15 Mar 08 |
peter |
535 |
suite.err() << "ok\n"; |
965 |
11 Oct 07 |
peter |
536 |
} |
2202 |
21 Feb 10 |
peter |
537 |
strafree(av, ac); |
965 |
11 Oct 07 |
peter |
538 |
} |
980 |
22 Oct 07 |
peter |
539 |
|
980 |
22 Oct 07 |
peter |
540 |
|
1466 |
02 Sep 08 |
peter |
541 |
suite.err() << "Testing invalid: test_prog peter..."; |
1466 |
02 Sep 08 |
peter |
542 |
{ |
1466 |
02 Sep 08 |
peter |
543 |
int ac = 2; |
2202 |
21 Feb 10 |
peter |
544 |
const char* cav[] = { "test_prog", "peter" }; |
2202 |
21 Feb 10 |
peter |
545 |
char** av = stradup(cav, ac); |
1466 |
02 Sep 08 |
peter |
546 |
try{ |
1466 |
02 Sep 08 |
peter |
547 |
cmd.parse(ac,av); |
1466 |
02 Sep 08 |
peter |
548 |
suite.err() << "failed\n"; |
1466 |
02 Sep 08 |
peter |
549 |
ok =false; |
1466 |
02 Sep 08 |
peter |
550 |
} |
1466 |
02 Sep 08 |
peter |
551 |
catch (...) { |
1466 |
02 Sep 08 |
peter |
552 |
suite.err() << "ok\n"; |
1466 |
02 Sep 08 |
peter |
553 |
} |
2202 |
21 Feb 10 |
peter |
554 |
strafree(av, ac); |
1466 |
02 Sep 08 |
peter |
555 |
} |
1466 |
02 Sep 08 |
peter |
556 |
|
1232 |
15 Mar 08 |
peter |
557 |
suite.err() << "Testing OptionArg required ..."; |
980 |
22 Oct 07 |
peter |
558 |
{ |
2739 |
07 Jun 12 |
peter |
559 |
OptionArg<std::string> required(cmd, "required", "required", true); |
980 |
22 Oct 07 |
peter |
560 |
int ac = 1; |
2202 |
21 Feb 10 |
peter |
561 |
const char* cav[] = { "test_prog" }; |
2202 |
21 Feb 10 |
peter |
562 |
char** av = stradup(cav, ac); |
980 |
22 Oct 07 |
peter |
563 |
try{ |
980 |
22 Oct 07 |
peter |
564 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
565 |
suite.err() << "failed\n"; |
980 |
22 Oct 07 |
peter |
566 |
ok =false; |
980 |
22 Oct 07 |
peter |
567 |
} |
980 |
22 Oct 07 |
peter |
568 |
catch (...) { |
1232 |
15 Mar 08 |
peter |
569 |
suite.err() << "ok\n"; |
980 |
22 Oct 07 |
peter |
570 |
} |
2202 |
21 Feb 10 |
peter |
571 |
strafree(av, ac); |
980 |
22 Oct 07 |
peter |
572 |
} |
980 |
22 Oct 07 |
peter |
573 |
|
980 |
22 Oct 07 |
peter |
574 |
|
965 |
11 Oct 07 |
peter |
575 |
return ok; |
965 |
11 Oct 07 |
peter |
576 |
} |
965 |
11 Oct 07 |
peter |
577 |
|
965 |
11 Oct 07 |
peter |
578 |
|
1232 |
15 Mar 08 |
peter |
579 |
bool test_file(yat::test::Suite& suite) |
979 |
21 Oct 07 |
peter |
580 |
{ |
979 |
21 Oct 07 |
peter |
581 |
bool ok=true; |
979 |
21 Oct 07 |
peter |
582 |
CommandLine cmd; |
979 |
21 Oct 07 |
peter |
583 |
OptionFile inclones(cmd, "clones", "file containing clones"); |
979 |
21 Oct 07 |
peter |
584 |
OptionFile indata(cmd, "data", "data to merge"); |
1178 |
27 Feb 08 |
peter |
585 |
OptionFile out(cmd, "o,out", "data to merge", true, false, "w"); |
979 |
21 Oct 07 |
peter |
586 |
OptionSwitch help(cmd, "h,help", "display this help and exit"); |
965 |
11 Oct 07 |
peter |
587 |
|
1232 |
15 Mar 08 |
peter |
588 |
suite.err() << "Testing OptionFile... "; |
979 |
21 Oct 07 |
peter |
589 |
{ |
1426 |
20 Aug 08 |
peter |
590 |
int ac = 7; |
2739 |
07 Jun 12 |
peter |
591 |
const char* cav[] = { "test_prog", "--clones", "commandline_test.cc", |
2739 |
07 Jun 12 |
peter |
592 |
"--data", "commandline_test.cc", "-o", |
2202 |
21 Feb 10 |
peter |
593 |
"commandline_test.cc"}; |
2202 |
21 Feb 10 |
peter |
594 |
char** av = stradup(cav, ac); |
979 |
21 Oct 07 |
peter |
595 |
cmd.parse(ac,av); |
1232 |
15 Mar 08 |
peter |
596 |
suite.err() << "ok\n"; |
2202 |
21 Feb 10 |
peter |
597 |
strafree(av, ac); |
979 |
21 Oct 07 |
peter |
598 |
} |
979 |
21 Oct 07 |
peter |
599 |
return ok; |
979 |
21 Oct 07 |
peter |
600 |
} |
965 |
11 Oct 07 |
peter |
601 |
|
2049 |
04 Sep 09 |
peter |
602 |
|
1954 |
07 May 09 |
jari |
603 |
bool test_file2(yat::test::Suite& suite) |
1954 |
07 May 09 |
jari |
604 |
{ |
2057 |
11 Sep 09 |
peter |
605 |
suite.err() << "Testing OptionFile in write-protected directory... "; |
1954 |
07 May 09 |
jari |
606 |
|
1998 |
13 Jun 09 |
peter |
607 |
if (yat::test::run_as_root()) { |
1998 |
13 Jun 09 |
peter |
608 |
suite.err() << "skipped because user is root\n"; |
1998 |
13 Jun 09 |
peter |
// skipping means test should still be successful |
1998 |
13 Jun 09 |
peter |
610 |
return true; |
1998 |
13 Jun 09 |
peter |
611 |
} |
1998 |
13 Jun 09 |
peter |
612 |
|
2893 |
09 Dec 12 |
peter |
613 |
FileUtil subdir("write-protected"); |
1954 |
07 May 09 |
jari |
614 |
if (!subdir.exists()) { |
1954 |
07 May 09 |
jari |
615 |
if (mkdir(subdir.path().c_str(), 0555)) { |
1954 |
07 May 09 |
jari |
616 |
suite.err() << "mkdir " << subdir.path() << " failed\n"; |
1954 |
07 May 09 |
jari |
617 |
return false; |
1954 |
07 May 09 |
jari |
618 |
} |
1954 |
07 May 09 |
jari |
619 |
} |
1954 |
07 May 09 |
jari |
620 |
else |
1954 |
07 May 09 |
jari |
621 |
if (!subdir.permissions("w")) { |
1954 |
07 May 09 |
jari |
622 |
suite.err() << subdir.path() << " already exists\n"; |
1954 |
07 May 09 |
jari |
623 |
return false; |
1954 |
07 May 09 |
jari |
624 |
} |
1954 |
07 May 09 |
jari |
625 |
|
2739 |
07 Jun 12 |
peter |
626 |
FileUtil file("test/testSubDir/commandline_test.dir/write-protected/out.txt"); |
1998 |
13 Jun 09 |
peter |
627 |
if (file.exists()) { |
1998 |
13 Jun 09 |
peter |
628 |
suite.err() << "FileUtil::exists returns true unexpectedly\n"; |
1998 |
13 Jun 09 |
peter |
629 |
return false; |
1998 |
13 Jun 09 |
peter |
630 |
} |
1998 |
13 Jun 09 |
peter |
631 |
if (file.permissions("w")==0) { |
1998 |
13 Jun 09 |
peter |
632 |
suite.err() << "FileUtil::permission(\"w\") returns 0 while " |
1998 |
13 Jun 09 |
peter |
633 |
<< "-1 was expected\n"; |
1998 |
13 Jun 09 |
peter |
634 |
return false; |
1998 |
13 Jun 09 |
peter |
635 |
} |
1998 |
13 Jun 09 |
peter |
636 |
|
1954 |
07 May 09 |
jari |
637 |
bool ok=true; |
1954 |
07 May 09 |
jari |
638 |
CommandLine cmd; |
2057 |
11 Sep 09 |
peter |
639 |
OptionFile out(cmd, "o,out", "output to file", false, false, "w"); |
1954 |
07 May 09 |
jari |
640 |
OptionSwitch help(cmd, "h,help", "display this help and exit"); |
1954 |
07 May 09 |
jari |
641 |
|
1954 |
07 May 09 |
jari |
642 |
{ |
1954 |
07 May 09 |
jari |
643 |
int ac = 3; |
2739 |
07 Jun 12 |
peter |
644 |
const char* cav[] = { "test_prog", "--out", |
2739 |
07 Jun 12 |
peter |
645 |
"test/testSubDir/commandline_test.dir/write-protected/out.txt"}; |
2202 |
21 Feb 10 |
peter |
646 |
char** av = stradup(cav, ac); |
1954 |
07 May 09 |
jari |
647 |
try { |
1954 |
07 May 09 |
jari |
648 |
cmd.parse(ac,av); |
1954 |
07 May 09 |
jari |
649 |
ok =false; |
1998 |
13 Jun 09 |
peter |
650 |
suite.err() << "failed\n"; |
1954 |
07 May 09 |
jari |
651 |
} |
1954 |
07 May 09 |
jari |
652 |
catch (cmd_error& e) { |
1998 |
13 Jun 09 |
peter |
653 |
suite.err() << "ok\n"; |
1954 |
07 May 09 |
jari |
654 |
suite.err() << "catch expected error: " << e.what() << "\n"; |
1954 |
07 May 09 |
jari |
655 |
} |
2202 |
21 Feb 10 |
peter |
656 |
strafree(av, ac); |
1954 |
07 May 09 |
jari |
657 |
} |
1954 |
07 May 09 |
jari |
658 |
return ok; |
1954 |
07 May 09 |
jari |
659 |
} |
1954 |
07 May 09 |
jari |
660 |
|
2049 |
04 Sep 09 |
peter |
661 |
|
2049 |
04 Sep 09 |
peter |
662 |
bool test_file3(yat::test::Suite& suite) |
2049 |
04 Sep 09 |
peter |
663 |
{ |
2049 |
04 Sep 09 |
peter |
664 |
suite.err() << "Testing OptionFile in non-existing tree\n"; |
2049 |
04 Sep 09 |
peter |
// testing error message from OptionFile when tree is not existing |
2049 |
04 Sep 09 |
peter |
666 |
bool ok=true; |
2049 |
04 Sep 09 |
peter |
667 |
ok &= test_file3_(suite, "r"); |
2049 |
04 Sep 09 |
peter |
668 |
ok &= test_file3_(suite, "w"); |
2049 |
04 Sep 09 |
peter |
669 |
ok &= test_file3_(suite, "x"); |
2049 |
04 Sep 09 |
peter |
670 |
ok &= test_file3_(suite, "d"); |
2049 |
04 Sep 09 |
peter |
671 |
return ok; |
2049 |
04 Sep 09 |
peter |
672 |
} |
2049 |
04 Sep 09 |
peter |
673 |
|
2049 |
04 Sep 09 |
peter |
674 |
|
2049 |
04 Sep 09 |
peter |
675 |
bool test_file3_(yat::test::Suite& suite, const std::string& perm) |
2049 |
04 Sep 09 |
peter |
676 |
{ |
2049 |
04 Sep 09 |
peter |
677 |
CommandLine cmd; |
2049 |
04 Sep 09 |
peter |
678 |
OptionFile file(cmd, "file", "", true, false, perm); |
2049 |
04 Sep 09 |
peter |
679 |
|
2739 |
07 Jun 12 |
peter |
680 |
suite.err() << "Testing OptionFile '" << perm << "' ... "; |
2202 |
21 Feb 10 |
peter |
681 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
682 |
const char* cav[] = { "test_prog", "--file", "sjhgaw/jmegb/tmp.tmpx"}; |
2202 |
21 Feb 10 |
peter |
683 |
char** av = stradup(cav, ac); |
2049 |
04 Sep 09 |
peter |
684 |
try { |
2049 |
04 Sep 09 |
peter |
685 |
cmd.parse(ac,av); |
2049 |
04 Sep 09 |
peter |
686 |
suite.err() << "no\n"; |
2202 |
21 Feb 10 |
peter |
687 |
strafree(av, ac); |
2049 |
04 Sep 09 |
peter |
688 |
return false; |
2049 |
04 Sep 09 |
peter |
689 |
} |
2049 |
04 Sep 09 |
peter |
690 |
catch (cmd_error& e) { |
2049 |
04 Sep 09 |
peter |
691 |
suite.err() << "ok\n"; |
2049 |
04 Sep 09 |
peter |
692 |
suite.err() << "catch expected error: " << e.what() << "\n"; |
2049 |
04 Sep 09 |
peter |
693 |
} |
2202 |
21 Feb 10 |
peter |
694 |
strafree(av, ac); |
2049 |
04 Sep 09 |
peter |
695 |
return true; |
2049 |
04 Sep 09 |
peter |
696 |
} |
2049 |
04 Sep 09 |
peter |
697 |
|
2049 |
04 Sep 09 |
peter |
698 |
|
2049 |
04 Sep 09 |
peter |
699 |
bool test_file4(yat::test::Suite& suite) |
2049 |
04 Sep 09 |
peter |
700 |
{ |
2739 |
07 Jun 12 |
peter |
701 |
FileUtil file("test/testSubDir/commandline_test.dir/test_file4.txt"); |
2049 |
04 Sep 09 |
peter |
702 |
if (!file.exists()) { |
2049 |
04 Sep 09 |
peter |
703 |
std::ofstream os(file.path().c_str()); |
2739 |
07 Jun 12 |
peter |
704 |
} |
2049 |
04 Sep 09 |
peter |
705 |
chmod(file.path().c_str(), 0); |
2049 |
04 Sep 09 |
peter |
706 |
|
2049 |
04 Sep 09 |
peter |
707 |
suite.err() << "Testing OptionFile with no permssions\n"; |
2049 |
04 Sep 09 |
peter |
708 |
bool ok=true; |
2049 |
04 Sep 09 |
peter |
709 |
ok &= test_file4_(suite, "r"); |
2049 |
04 Sep 09 |
peter |
710 |
ok &= test_file4_(suite, "w"); |
2049 |
04 Sep 09 |
peter |
711 |
ok &= test_file4_(suite, "x"); |
2049 |
04 Sep 09 |
peter |
712 |
ok &= test_file4_(suite, "d"); |
2049 |
04 Sep 09 |
peter |
713 |
return ok; |
2049 |
04 Sep 09 |
peter |
714 |
} |
2049 |
04 Sep 09 |
peter |
715 |
|
2049 |
04 Sep 09 |
peter |
716 |
|
2049 |
04 Sep 09 |
peter |
717 |
bool test_file4_(yat::test::Suite& suite, const std::string& perm) |
2049 |
04 Sep 09 |
peter |
718 |
{ |
2049 |
04 Sep 09 |
peter |
719 |
CommandLine cmd; |
2049 |
04 Sep 09 |
peter |
720 |
OptionFile file(cmd, "file", "", true, false, perm); |
2049 |
04 Sep 09 |
peter |
721 |
|
2739 |
07 Jun 12 |
peter |
722 |
suite.err() << "Testing OptionFile '" << perm << "' ... "; |
2703 |
12 Mar 12 |
peter |
723 |
if (yat::test::run_as_root()) { |
2703 |
12 Mar 12 |
peter |
724 |
suite.err() << "skipped because user is root\n"; |
2703 |
12 Mar 12 |
peter |
725 |
return true; |
2703 |
12 Mar 12 |
peter |
726 |
} |
2049 |
04 Sep 09 |
peter |
727 |
try { |
2049 |
04 Sep 09 |
peter |
728 |
int ac = 3; |
2739 |
07 Jun 12 |
peter |
729 |
const char* cav[] = { "test_prog", "--file", |
2739 |
07 Jun 12 |
peter |
730 |
"test/testSubDir/commandline_test.dir/test_file4.txt"}; |
2202 |
21 Feb 10 |
peter |
731 |
char** av = stradup(cav, ac); |
2049 |
04 Sep 09 |
peter |
732 |
cmd.parse(ac,av); |
2049 |
04 Sep 09 |
peter |
733 |
suite.err() << "no\n"; |
2202 |
21 Feb 10 |
peter |
734 |
strafree(av, ac); |
2049 |
04 Sep 09 |
peter |
735 |
return false; |
2049 |
04 Sep 09 |
peter |
736 |
} |
2049 |
04 Sep 09 |
peter |
737 |
catch (cmd_error& e) { |
2049 |
04 Sep 09 |
peter |
738 |
suite.err() << "ok\n"; |
2049 |
04 Sep 09 |
peter |
739 |
suite.err() << "catch expected error: " << e.what() << "\n"; |
2049 |
04 Sep 09 |
peter |
740 |
} |
2049 |
04 Sep 09 |
peter |
741 |
return true; |
2049 |
04 Sep 09 |
peter |
742 |
} |
2049 |
04 Sep 09 |
peter |
743 |
|
2049 |
04 Sep 09 |
peter |
744 |
|
1426 |
20 Aug 08 |
peter |
745 |
bool test_option_name_clash(yat::test::Suite& suite) |
1426 |
20 Aug 08 |
peter |
746 |
{ |
1426 |
20 Aug 08 |
peter |
747 |
bool ok=true; |
1426 |
20 Aug 08 |
peter |
748 |
suite.err() << "Testing long option name clash ..."; |
1426 |
20 Aug 08 |
peter |
749 |
try { |
1426 |
20 Aug 08 |
peter |
750 |
CommandLine cmd; |
1426 |
20 Aug 08 |
peter |
751 |
OptionSwitch op1(cmd, "opt", "bla bla"); |
1426 |
20 Aug 08 |
peter |
752 |
OptionSwitch op2(cmd, "o,opt", "other bla"); |
1426 |
20 Aug 08 |
peter |
753 |
ok=false; |
1426 |
20 Aug 08 |
peter |
754 |
suite.err() << "failed\n"; |
1426 |
20 Aug 08 |
peter |
755 |
} |
1426 |
20 Aug 08 |
peter |
756 |
catch (std::runtime_error& e) { |
1426 |
20 Aug 08 |
peter |
757 |
suite.err() << "ok\n"; |
1426 |
20 Aug 08 |
peter |
758 |
} |
1426 |
20 Aug 08 |
peter |
759 |
suite.err() << "Testing short option name clash ..."; |
1426 |
20 Aug 08 |
peter |
760 |
try { |
1426 |
20 Aug 08 |
peter |
761 |
CommandLine cmd; |
1426 |
20 Aug 08 |
peter |
762 |
OptionSwitch op1(cmd, "o", "bla bla"); |
1426 |
20 Aug 08 |
peter |
763 |
OptionSwitch op2(cmd, "o,opt", "other bla"); |
1426 |
20 Aug 08 |
peter |
764 |
ok=false; |
1426 |
20 Aug 08 |
peter |
765 |
suite.err() << "failed\n"; |
1426 |
20 Aug 08 |
peter |
766 |
} |
1426 |
20 Aug 08 |
peter |
767 |
catch (std::runtime_error& e) { |
1426 |
20 Aug 08 |
peter |
768 |
suite.err() << "ok\n"; |
1426 |
20 Aug 08 |
peter |
769 |
} |
965 |
11 Oct 07 |
peter |
770 |
|
1426 |
20 Aug 08 |
peter |
771 |
return ok; |
1426 |
20 Aug 08 |
peter |
772 |
} |
965 |
11 Oct 07 |
peter |
773 |
|
2049 |
04 Sep 09 |
peter |
774 |
|
1427 |
20 Aug 08 |
peter |
775 |
bool test_free_arg(yat::test::Suite& suite) |
1427 |
20 Aug 08 |
peter |
776 |
{ |
1427 |
20 Aug 08 |
peter |
777 |
bool ok=true; |
1427 |
20 Aug 08 |
peter |
778 |
suite.err() << "Testing free arguments ..."; |
979 |
21 Oct 07 |
peter |
779 |
|
1427 |
20 Aug 08 |
peter |
780 |
CommandLine cmd; |
1468 |
02 Sep 08 |
peter |
781 |
cmd.allow_free_args(2); |
1427 |
20 Aug 08 |
peter |
782 |
OptionHelp help(cmd); |
1427 |
20 Aug 08 |
peter |
783 |
int ac = 3; |
2202 |
21 Feb 10 |
peter |
784 |
const char* cav[] = { "test_prog", "file", "kl"}; |
2202 |
21 Feb 10 |
peter |
785 |
char** av = stradup(cav, ac); |
1427 |
20 Aug 08 |
peter |
786 |
cmd.parse(ac, av); |
1427 |
20 Aug 08 |
peter |
787 |
suite.err() << "ok\n"; |
2202 |
21 Feb 10 |
peter |
788 |
strafree(av, ac); |
1427 |
20 Aug 08 |
peter |
789 |
return ok; |
1427 |
20 Aug 08 |
peter |
790 |
} |