1//
2// OptionTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "OptionTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/Option.h"
15#include "Poco/Util/OptionException.h"
16
17
18using Poco::Util::Option;
19
20
21OptionTest::OptionTest(const std::string& name): CppUnit::TestCase(name)
22{
23}
24
25
26OptionTest::~OptionTest()
27{
28}
29
30
31void OptionTest::testOption()
32{
33 Option incOpt = Option("include-dir", "I", "specify an include search path")
34 .required(false)
35 .repeatable(true)
36 .argument("path");
37
38 Option libOpt = Option("library-dir", "L", "specify a library search path", false)
39 .repeatable(true)
40 .argument("path");
41
42 Option outOpt = Option("output", "o", "specify the output file", true)
43 .argument("file", true);
44
45 Option vrbOpt = Option("verbose", "v")
46 .description("enable verbose mode")
47 .required(false)
48 .repeatable(false);
49
50 Option optOpt = Option("optimize", "O")
51 .description("enable optimization")
52 .required(false)
53 .repeatable(false)
54 .argument("level", false);
55
56 assertTrue (incOpt.shortName() == "I");
57 assertTrue (incOpt.fullName() == "include-dir");
58 assertTrue (incOpt.repeatable());
59 assertTrue (!incOpt.required());
60 assertTrue (incOpt.argumentName() == "path");
61 assertTrue (incOpt.argumentRequired());
62 assertTrue (incOpt.takesArgument());
63
64 assertTrue (libOpt.shortName() == "L");
65 assertTrue (libOpt.fullName() == "library-dir");
66 assertTrue (libOpt.repeatable());
67 assertTrue (!libOpt.required());
68 assertTrue (libOpt.argumentName() == "path");
69 assertTrue (libOpt.argumentRequired());
70 assertTrue (incOpt.takesArgument());
71
72 assertTrue (outOpt.shortName() == "o");
73 assertTrue (outOpt.fullName() == "output");
74 assertTrue (!outOpt.repeatable());
75 assertTrue (outOpt.required());
76 assertTrue (outOpt.argumentName() == "file");
77 assertTrue (outOpt.argumentRequired());
78 assertTrue (incOpt.takesArgument());
79
80 assertTrue (vrbOpt.shortName() == "v");
81 assertTrue (vrbOpt.fullName() == "verbose");
82 assertTrue (!vrbOpt.repeatable());
83 assertTrue (!vrbOpt.required());
84 assertTrue (!vrbOpt.argumentRequired());
85 assertTrue (!vrbOpt.takesArgument());
86
87 assertTrue (optOpt.shortName() == "O");
88 assertTrue (optOpt.fullName() == "optimize");
89 assertTrue (!optOpt.repeatable());
90 assertTrue (!optOpt.required());
91 assertTrue (optOpt.argumentName() == "level");
92 assertTrue (optOpt.takesArgument());
93 assertTrue (!optOpt.argumentRequired());
94}
95
96
97void OptionTest::testMatches1()
98{
99 Option incOpt = Option("include-dir", "I", "specify an include search path")
100 .required(false)
101 .repeatable(true)
102 .argument("path");
103
104 assertTrue (incOpt.matchesShort("Iinclude"));
105 assertTrue (incOpt.matchesPartial("include:include"));
106 assertTrue (incOpt.matchesPartial("include-dir:include"));
107 assertTrue (incOpt.matchesPartial("inc=include"));
108 assertTrue (incOpt.matchesPartial("INCLUDE=include"));
109 assertTrue (incOpt.matchesPartial("include"));
110 assertTrue (incOpt.matchesShort("I"));
111 assertTrue (incOpt.matchesPartial("i"));
112
113 assertTrue (incOpt.matchesFull("include-dir:include"));
114 assertTrue (incOpt.matchesFull("INClude-dir:include"));
115 assertTrue (!incOpt.matchesFull("include:include"));
116 assertTrue (!incOpt.matchesFull("include-dir2:include"));
117
118 assertTrue (!incOpt.matchesPartial("include-dir2=include"));
119 assertTrue (!incOpt.matchesShort("linclude"));
120}
121
122
123void OptionTest::testMatches2()
124{
125 Option incOpt = Option("include-dir", "", "specify an include search path")
126 .required(false)
127 .repeatable(true)
128 .argument("path");
129
130 assertTrue (!incOpt.matchesShort("Iinclude"));
131 assertTrue (incOpt.matchesPartial("include:include"));
132 assertTrue (incOpt.matchesPartial("include-dir:include"));
133 assertTrue (incOpt.matchesPartial("inc=include"));
134 assertTrue (incOpt.matchesPartial("INCLUDE=include"));
135 assertTrue (incOpt.matchesPartial("I"));
136 assertTrue (incOpt.matchesPartial("i"));
137
138 assertTrue (incOpt.matchesFull("include-dir:include"));
139 assertTrue (incOpt.matchesFull("INClude-dir:include"));
140 assertTrue (!incOpt.matchesFull("include:include"));
141 assertTrue (!incOpt.matchesFull("include-dir2:include"));
142
143 assertTrue (!incOpt.matchesFull("include-dir2=include"));
144 assertTrue (!incOpt.matchesShort("linclude"));
145}
146
147
148void OptionTest::testProcess1()
149{
150 Option incOpt = Option("include-dir", "I", "specify an include search path")
151 .required(false)
152 .repeatable(true)
153 .argument("path");
154
155 std::string arg;
156 incOpt.process("Iinclude", arg);
157 assertTrue (arg == "include");
158 incOpt.process("I/usr/include", arg);
159 assertTrue (arg == "/usr/include");
160 incOpt.process("include:/usr/local/include", arg);
161 assertTrue (arg == "/usr/local/include");
162 incOpt.process("include=/proj/include", arg);
163 assertTrue (arg == "/proj/include");
164 incOpt.process("include-dir=/usr/include", arg);
165 assertTrue (arg == "/usr/include");
166 incOpt.process("Include-dir:/proj/include", arg);
167 assertTrue (arg == "/proj/include");
168
169 try
170 {
171 incOpt.process("I", arg);
172 fail("argument required - must throw");
173 }
174 catch (Poco::Util::MissingArgumentException&)
175 {
176 }
177
178 try
179 {
180 incOpt.process("Include", arg);
181 fail("argument required - must throw");
182 }
183 catch (Poco::Util::MissingArgumentException&)
184 {
185 }
186
187 try
188 {
189 incOpt.process("Llib", arg);
190 fail("wrong option - must throw");
191 }
192 catch (Poco::Util::UnknownOptionException&)
193 {
194 }
195
196 Option vrbOpt = Option("verbose", "v")
197 .description("enable verbose mode")
198 .required(false)
199 .repeatable(false);
200
201 vrbOpt.process("v", arg);
202 assertTrue (arg.empty());
203 vrbOpt.process("verbose", arg);
204 assertTrue (arg.empty());
205
206 try
207 {
208 vrbOpt.process("v2", arg);
209 fail("no argument expected - must throw");
210 }
211 catch (Poco::Util::UnexpectedArgumentException&)
212 {
213 }
214
215 try
216 {
217 vrbOpt.process("verbose:2", arg);
218 fail("no argument expected - must throw");
219 }
220 catch (Poco::Util::UnexpectedArgumentException&)
221 {
222 }
223
224 Option optOpt = Option("optimize", "O")
225 .description("enable optimization")
226 .required(false)
227 .repeatable(false)
228 .argument("level", false);
229
230 optOpt.process("O", arg);
231 assertTrue (arg.empty());
232 optOpt.process("O2", arg);
233 assertTrue (arg == "2");
234 optOpt.process("optimize:1", arg);
235 assertTrue (arg == "1");
236 optOpt.process("opt", arg);
237 assertTrue (arg.empty());
238 optOpt.process("opt=3", arg);
239 assertTrue (arg == "3");
240 optOpt.process("opt=", arg);
241 assertTrue (arg.empty());
242}
243
244
245void OptionTest::testProcess2()
246{
247 Option incOpt = Option("include-dir", "", "specify an include search path")
248 .required(false)
249 .repeatable(true)
250 .argument("path");
251
252 std::string arg;
253 incOpt.process("include:/usr/local/include", arg);
254 assertTrue (arg == "/usr/local/include");
255 incOpt.process("include=/proj/include", arg);
256 assertTrue (arg == "/proj/include");
257 incOpt.process("include-dir=/usr/include", arg);
258 assertTrue (arg == "/usr/include");
259 incOpt.process("Include-dir:/proj/include", arg);
260 assertTrue (arg == "/proj/include");
261
262 try
263 {
264 incOpt.process("Iinclude", arg);
265 fail("unknown option - must throw");
266 }
267 catch (Poco::Util::UnknownOptionException&)
268 {
269 }
270
271 try
272 {
273 incOpt.process("I", arg);
274 fail("argument required - must throw");
275 }
276 catch (Poco::Util::MissingArgumentException&)
277 {
278 }
279
280 try
281 {
282 incOpt.process("Include", arg);
283 fail("argument required - must throw");
284 }
285 catch (Poco::Util::MissingArgumentException&)
286 {
287 }
288
289 try
290 {
291 incOpt.process("Llib", arg);
292 fail("wrong option - must throw");
293 }
294 catch (Poco::Util::UnknownOptionException&)
295 {
296 }
297
298 Option vrbOpt = Option("verbose", "")
299 .description("enable verbose mode")
300 .required(false)
301 .repeatable(false);
302
303 vrbOpt.process("v", arg);
304 assertTrue (arg.empty());
305 vrbOpt.process("verbose", arg);
306 assertTrue (arg.empty());
307
308 try
309 {
310 vrbOpt.process("v2", arg);
311 fail("no argument expected - must throw");
312 }
313 catch (Poco::Util::UnknownOptionException&)
314 {
315 }
316
317 try
318 {
319 vrbOpt.process("verbose:2", arg);
320 fail("no argument expected - must throw");
321 }
322 catch (Poco::Util::UnexpectedArgumentException&)
323 {
324 }
325}
326
327
328void OptionTest::setUp()
329{
330}
331
332
333void OptionTest::tearDown()
334{
335}
336
337
338CppUnit::Test* OptionTest::suite()
339{
340 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OptionTest");
341
342 CppUnit_addTest(pSuite, OptionTest, testOption);
343 CppUnit_addTest(pSuite, OptionTest, testMatches1);
344 CppUnit_addTest(pSuite, OptionTest, testMatches2);
345 CppUnit_addTest(pSuite, OptionTest, testProcess1);
346 CppUnit_addTest(pSuite, OptionTest, testProcess2);
347
348 return pSuite;
349}
350