Line data Source code
1 : // Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // advgetopt
21 : //
22 : #include <advgetopt/exception.h>
23 : #include <advgetopt/version.h>
24 :
25 :
26 : // self
27 : //
28 : #include "catch_main.h"
29 :
30 :
31 : // snapdev
32 : //
33 : #include <snapdev/safe_setenv.h>
34 : #include <snapdev/stringize.h>
35 :
36 :
37 : // C++
38 : //
39 : #include <fstream>
40 :
41 :
42 : // last include
43 : //
44 : #include <snapdev/poison.h>
45 :
46 :
47 :
48 :
49 :
50 2 : CATCH_TEST_CASE("string_access", "[arguments][valid][getopt]")
51 : {
52 2 : CATCH_START_SECTION("string_access: verify a string in a long argument")
53 : {
54 1 : advgetopt::option const options[] =
55 : {
56 : advgetopt::define_option(
57 : advgetopt::Name("user-name")
58 : , advgetopt::ShortName('u')
59 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
60 : , advgetopt::Help("check specified user.")
61 : ),
62 : advgetopt::end_options()
63 : };
64 :
65 1 : advgetopt::options_environment environment_options;
66 1 : environment_options.f_project_name = "unittest";
67 1 : environment_options.f_options = options;
68 1 : environment_options.f_help_header = "Usage: user name as a string";
69 :
70 1 : char const * cargv[] =
71 : {
72 : "/usr/bin/arguments",
73 : "--user-name",
74 : "alexis",
75 : nullptr
76 : };
77 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
78 1 : char ** argv = const_cast<char **>(cargv);
79 :
80 1 : advgetopt::getopt opt(environment_options, argc, argv);
81 :
82 : // check that the result is valid
83 :
84 : // an invalid parameter, MUST NOT EXIST
85 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
86 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
87 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
88 3 : CATCH_REQUIRE_FALSE(opt.has_default("invalid-parameter"));
89 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
90 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
91 :
92 : // no default
93 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
94 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
95 3 : CATCH_REQUIRE_FALSE(opt.has_default("--"));
96 3 : CATCH_REQUIRE(opt.get_default("--").empty());
97 3 : CATCH_REQUIRE(opt.size("--") == 0);
98 :
99 : // the valid parameter
100 3 : CATCH_REQUIRE(opt.get_option("user-name") != nullptr);
101 1 : CATCH_REQUIRE(opt.get_option('u') != nullptr);
102 3 : CATCH_REQUIRE(opt.get_string("user-name") == "alexis");
103 3 : CATCH_REQUIRE(opt.get_string("user-name", 0) == "alexis");
104 3 : CATCH_REQUIRE(opt["user-name"] == "alexis");
105 3 : CATCH_REQUIRE(opt.is_defined("user-name"));
106 3 : CATCH_REQUIRE_FALSE(opt.has_default("user-name"));
107 3 : CATCH_REQUIRE(opt.get_default("user-name").empty());
108 3 : CATCH_REQUIRE(opt.size("user-name") == 1);
109 :
110 : // other parameters
111 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
112 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
113 1 : }
114 2 : CATCH_END_SECTION()
115 :
116 2 : CATCH_START_SECTION("string_access: verify a string in a short argument")
117 : {
118 1 : advgetopt::option const options[] =
119 : {
120 : advgetopt::define_option(
121 : advgetopt::Name("user-name")
122 : , advgetopt::ShortName('u')
123 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
124 : , advgetopt::Help("check specified user.")
125 : ),
126 : advgetopt::end_options()
127 : };
128 :
129 1 : advgetopt::options_environment environment_options;
130 1 : environment_options.f_project_name = "unittest";
131 1 : environment_options.f_options = options;
132 1 : environment_options.f_help_header = "Usage: user name as a string";
133 :
134 1 : char const * cargv[] =
135 : {
136 : "/usr/bin/arguments",
137 : "-u",
138 : "alexis",
139 : nullptr
140 : };
141 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
142 1 : char ** argv = const_cast<char **>(cargv);
143 :
144 1 : advgetopt::getopt opt(environment_options, argc, argv);
145 :
146 : // check that the result is valid
147 :
148 : // an invalid parameter, MUST NOT EXIST
149 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
150 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
151 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
152 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
153 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
154 :
155 : // no default
156 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
157 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
158 3 : CATCH_REQUIRE(opt.get_default("--").empty());
159 3 : CATCH_REQUIRE(opt.size("--") == 0);
160 :
161 : // the valid parameter
162 3 : CATCH_REQUIRE(opt.get_option("user-name") != nullptr);
163 1 : CATCH_REQUIRE(opt.get_option('u') != nullptr);
164 3 : CATCH_REQUIRE(opt.get_string("user-name") == "alexis");
165 3 : CATCH_REQUIRE(opt.get_string("user-name", 0) == "alexis");
166 3 : CATCH_REQUIRE(opt["user-name"] == "alexis");
167 3 : CATCH_REQUIRE(opt.is_defined("user-name"));
168 3 : CATCH_REQUIRE(opt.get_default("user-name").empty());
169 3 : CATCH_REQUIRE(opt.size("user-name") == 1);
170 :
171 : // other parameters
172 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
173 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
174 1 : }
175 2 : CATCH_END_SECTION()
176 2 : }
177 :
178 :
179 3 : CATCH_TEST_CASE("long_access", "[arguments][valid][getopt]")
180 : {
181 3 : CATCH_START_SECTION("long_access: verify an integer (long) value in an argument")
182 : {
183 1 : long const default_value(rand());
184 1 : std::string const default_value_str(std::to_string(default_value));
185 1 : char const * const default_val(default_value_str.c_str());
186 :
187 1 : advgetopt::option const options[] =
188 : {
189 6 : advgetopt::define_option(
190 : advgetopt::Name("size")
191 : , advgetopt::ShortName('s')
192 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
193 : , advgetopt::Help("define the size.")
194 : , advgetopt::DefaultValue(default_val)
195 : ),
196 : advgetopt::end_options()
197 6 : };
198 :
199 1 : advgetopt::options_environment environment_options;
200 1 : environment_options.f_project_name = "unittest";
201 1 : environment_options.f_options = options;
202 1 : environment_options.f_help_header = "Usage: test get_long() functions";
203 :
204 1 : char const * cargv[] =
205 : {
206 : "/usr/bin/arguments",
207 : "--size",
208 : "9821",
209 : nullptr
210 : };
211 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
212 1 : char ** argv = const_cast<char **>(cargv);
213 :
214 1 : advgetopt::getopt opt(environment_options, argc, argv);
215 :
216 : // check that the result is valid
217 :
218 : // an invalid parameter, MUST NOT EXIST
219 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
220 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
221 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
222 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
223 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
224 :
225 : // no default
226 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
227 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
228 3 : CATCH_REQUIRE(opt.get_default("--").empty());
229 3 : CATCH_REQUIRE(opt.size("--") == 0);
230 :
231 : // the valid parameter
232 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
233 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
234 3 : CATCH_REQUIRE(opt.is_defined("size"));
235 3 : CATCH_REQUIRE(opt.get_string("size") == "9821");
236 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "9821");
237 3 : CATCH_REQUIRE(opt["size"] == "9821");
238 3 : CATCH_REQUIRE(opt.get_long("size") == 9821);
239 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 9821);
240 3 : CATCH_REQUIRE(opt.has_default("size"));
241 3 : CATCH_REQUIRE(opt.get_default("size") == default_value_str);
242 3 : CATCH_REQUIRE(opt.size("size") == 1);
243 :
244 : // other parameters
245 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
246 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
247 1 : }
248 3 : CATCH_END_SECTION()
249 :
250 3 : CATCH_START_SECTION("long_access: verify an integer (long) value in a short argument")
251 : {
252 1 : advgetopt::option const options[] =
253 : {
254 : advgetopt::define_option(
255 : advgetopt::Name("size")
256 : , advgetopt::ShortName('s')
257 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
258 : , advgetopt::Help("define the size.")
259 : , advgetopt::DefaultValue("")
260 : ),
261 : advgetopt::end_options()
262 : };
263 :
264 1 : advgetopt::options_environment environment_options;
265 1 : environment_options.f_project_name = "unittest";
266 1 : environment_options.f_options = options;
267 1 : environment_options.f_help_header = "Usage: test get_long() functions";
268 :
269 1 : char const * cargv[] =
270 : {
271 : "/usr/bin/arguments",
272 : "-s",
273 : "9821",
274 : nullptr
275 : };
276 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
277 1 : char ** argv = const_cast<char **>(cargv);
278 :
279 1 : advgetopt::getopt opt(environment_options, argc, argv);
280 :
281 : // check that the result is valid
282 :
283 : // an invalid parameter, MUST NOT EXIST
284 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
285 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
286 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
287 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
288 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
289 :
290 : // to reach the constant version `opt` has to be constant
291 3 : std::string const array_syntax_invalid_parameter(static_cast<advgetopt::getopt const &>(opt)["invalid-parameter"]);
292 1 : CATCH_REQUIRE(array_syntax_invalid_parameter == std::string());
293 :
294 : // no default
295 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
296 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
297 3 : CATCH_REQUIRE(opt.get_default("--").empty());
298 3 : CATCH_REQUIRE(opt.size("--") == 0);
299 :
300 : // the valid parameter
301 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
302 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
303 3 : CATCH_REQUIRE(opt.is_defined("size"));
304 3 : CATCH_REQUIRE(opt.get_string("size") == "9821");
305 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "9821");
306 3 : CATCH_REQUIRE(opt["size"] == "9821");
307 3 : CATCH_REQUIRE(opt.get_long("size") == 9821);
308 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 9821);
309 3 : CATCH_REQUIRE(opt.has_default("size"));
310 3 : CATCH_REQUIRE(opt.get_default("size").empty());
311 3 : CATCH_REQUIRE(opt.size("size") == 1);
312 :
313 : // to access the constant reference we need a constant `opt`...
314 3 : std::string const array_syntax1(static_cast<advgetopt::getopt const &>(opt)["size"]);
315 1 : CATCH_REQUIRE(array_syntax1 == "9821");
316 5 : bool const array_syntax2(static_cast<advgetopt::getopt const &>(opt)["size"] == std::string("9821"));
317 1 : CATCH_REQUIRE(array_syntax2);
318 :
319 : // other parameters
320 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
321 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
322 1 : }
323 3 : CATCH_END_SECTION()
324 :
325 3 : CATCH_START_SECTION("long_access: verify an integer (long) value in no arguments")
326 : {
327 1 : advgetopt::option const options[] =
328 : {
329 : advgetopt::define_option(
330 : advgetopt::Name("size")
331 : , advgetopt::ShortName('s')
332 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
333 : , advgetopt::Help("define the size.")
334 : , advgetopt::DefaultValue("839")
335 : ),
336 : advgetopt::end_options()
337 : };
338 :
339 1 : advgetopt::options_environment environment_options;
340 1 : environment_options.f_project_name = "unittest";
341 1 : environment_options.f_options = options;
342 1 : environment_options.f_help_header = "Usage: test get_long() functions";
343 :
344 1 : char const * cargv[] =
345 : {
346 : "/usr/bin/arguments",
347 : nullptr
348 : };
349 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
350 1 : char ** argv = const_cast<char **>(cargv);
351 :
352 1 : advgetopt::getopt opt(environment_options, argc, argv);
353 :
354 : // check that the result is valid
355 :
356 : // an invalid parameter, MUST NOT EXIST
357 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
358 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
359 3 : CATCH_REQUIRE(opt["invalid-parameter"] == std::string());
360 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
361 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
362 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
363 :
364 : // no default
365 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
366 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
367 3 : CATCH_REQUIRE(opt.get_default("--").empty());
368 3 : CATCH_REQUIRE(opt.size("--") == 0);
369 :
370 : // the valid parameter
371 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
372 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
373 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
374 3 : CATCH_REQUIRE(opt.get_string("size") == "839");
375 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "839");
376 3 : CATCH_REQUIRE(opt.get_long("size") == 839);
377 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 839);
378 3 : CATCH_REQUIRE(opt.has_default("size"));
379 3 : CATCH_REQUIRE(opt.get_default("size") == "839");
380 3 : CATCH_REQUIRE(opt.size("size") == 0);
381 :
382 : // with a constant opt, the array syntax returns the default string
383 3 : CATCH_REQUIRE(static_cast<advgetopt::getopt const &>(opt)["size"] == "839");
384 :
385 : // other parameters
386 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
387 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
388 1 : }
389 3 : CATCH_END_SECTION()
390 3 : }
391 :
392 :
393 :
394 5 : CATCH_TEST_CASE("double_access", "[arguments][valid][getopt]")
395 : {
396 5 : CATCH_START_SECTION("double_access: verify a double value in an argument")
397 : {
398 1 : long const default_value(rand());
399 1 : std::string const default_value_str(std::to_string(default_value));
400 1 : char const * const default_val(default_value_str.c_str());
401 :
402 1 : advgetopt::option const options[] =
403 : {
404 6 : advgetopt::define_option(
405 : advgetopt::Name("size")
406 : , advgetopt::ShortName('s')
407 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
408 : , advgetopt::Help("define the size.")
409 : , advgetopt::DefaultValue(default_val)
410 : ),
411 : advgetopt::end_options()
412 6 : };
413 :
414 1 : advgetopt::options_environment environment_options;
415 1 : environment_options.f_project_name = "unittest";
416 1 : environment_options.f_options = options;
417 1 : environment_options.f_help_header = "Usage: test get_double() functions";
418 :
419 1 : char const * cargv[] =
420 : {
421 : "/usr/bin/arguments",
422 : "--size",
423 : "98.21",
424 : nullptr
425 : };
426 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
427 1 : char ** argv = const_cast<char **>(cargv);
428 :
429 1 : advgetopt::getopt opt(environment_options, argc, argv);
430 :
431 : // check that the result is valid
432 :
433 : // an invalid parameter, MUST NOT EXIST
434 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
435 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
436 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
437 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
438 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
439 :
440 : // no default
441 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
442 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
443 3 : CATCH_REQUIRE(opt.get_default("--").empty());
444 3 : CATCH_REQUIRE(opt.size("--") == 0);
445 :
446 : // the valid parameter
447 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
448 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
449 3 : CATCH_REQUIRE(opt.is_defined("size"));
450 3 : CATCH_REQUIRE(opt.get_string("size") == "98.21");
451 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "98.21");
452 3 : CATCH_REQUIRE(opt["size"] == "98.21");
453 : #pragma GCC diagnostic push
454 : #pragma GCC diagnostic ignored "-Wfloat-equal"
455 : {
456 3 : bool const a1(opt.get_double("size") == 98.21);
457 1 : CATCH_REQUIRE(a1);
458 3 : bool const a2(opt.get_double("size", 0) == 98.21);
459 1 : CATCH_REQUIRE(a2);
460 : }
461 : #pragma GCC diagnostic pop
462 3 : CATCH_REQUIRE(opt.has_default("size"));
463 3 : CATCH_REQUIRE(opt.get_default("size") == default_value_str);
464 3 : CATCH_REQUIRE(opt.size("size") == 1);
465 :
466 : // other parameters
467 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
468 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
469 1 : }
470 5 : CATCH_END_SECTION()
471 :
472 5 : CATCH_START_SECTION("double_access: verify a double value in a short argument")
473 : {
474 1 : advgetopt::option const options[] =
475 : {
476 : advgetopt::define_option(
477 : advgetopt::Name("size")
478 : , advgetopt::ShortName('s')
479 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
480 : , advgetopt::Help("define the size.")
481 : , advgetopt::DefaultValue("")
482 : ),
483 : advgetopt::end_options()
484 : };
485 :
486 1 : advgetopt::options_environment environment_options;
487 1 : environment_options.f_project_name = "unittest";
488 1 : environment_options.f_options = options;
489 1 : environment_options.f_help_header = "Usage: test get_double() functions";
490 :
491 1 : char const * cargv[] =
492 : {
493 : "/usr/bin/arguments",
494 : "-s",
495 : "98.21",
496 : nullptr
497 : };
498 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
499 1 : char ** argv = const_cast<char **>(cargv);
500 :
501 1 : advgetopt::getopt opt(environment_options, argc, argv);
502 :
503 : // check that the result is valid
504 :
505 : // an invalid parameter, MUST NOT EXIST
506 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
507 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
508 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
509 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
510 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
511 :
512 : // to reach the constant version `opt` has to be constant
513 3 : std::string const array_syntax_invalid_parameter(static_cast<advgetopt::getopt const &>(opt)["invalid-parameter"]);
514 1 : CATCH_REQUIRE(array_syntax_invalid_parameter == std::string());
515 :
516 : // no default
517 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
518 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
519 3 : CATCH_REQUIRE(opt.get_default("--").empty());
520 3 : CATCH_REQUIRE(opt.size("--") == 0);
521 :
522 : // the valid parameter
523 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
524 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
525 3 : CATCH_REQUIRE(opt.is_defined("size"));
526 3 : CATCH_REQUIRE(opt.get_string("size") == "98.21");
527 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "98.21");
528 3 : CATCH_REQUIRE(opt["size"] == "98.21");
529 : #pragma GCC diagnostic push
530 : #pragma GCC diagnostic ignored "-Wfloat-equal"
531 : {
532 3 : bool const a3(opt.get_double("size") == 98.21);
533 1 : CATCH_REQUIRE(a3);
534 3 : bool const a4(opt.get_double("size", 0) == 98.21);
535 1 : CATCH_REQUIRE(a4);
536 : }
537 : #pragma GCC diagnostic pop
538 3 : CATCH_REQUIRE(opt.has_default("size"));
539 3 : CATCH_REQUIRE(opt.get_default("size").empty());
540 3 : CATCH_REQUIRE(opt.size("size") == 1);
541 :
542 : // to access the constant reference we need a constant `opt`...
543 3 : std::string const array_syntax1(static_cast<advgetopt::getopt const &>(opt)["size"]);
544 1 : CATCH_REQUIRE(array_syntax1 == "98.21");
545 5 : bool const array_syntax2(static_cast<advgetopt::getopt const &>(opt)["size"] == std::string("98.21"));
546 1 : CATCH_REQUIRE(array_syntax2);
547 :
548 : // other parameters
549 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
550 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
551 1 : }
552 5 : CATCH_END_SECTION()
553 :
554 5 : CATCH_START_SECTION("double_access: verify a double value with no default and nothing on the command line")
555 : {
556 1 : advgetopt::option const options[] =
557 : {
558 : advgetopt::define_option(
559 : advgetopt::Name("size")
560 : , advgetopt::ShortName('s')
561 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
562 : , advgetopt::Help("define the size.")
563 : ),
564 : advgetopt::end_options()
565 : };
566 :
567 1 : advgetopt::options_environment environment_options;
568 1 : environment_options.f_project_name = "unittest";
569 1 : environment_options.f_options = options;
570 1 : environment_options.f_help_header = "Usage: test get_double() functions";
571 :
572 1 : char const * cargv[] =
573 : {
574 : "/usr/bin/arguments",
575 : nullptr
576 : };
577 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
578 1 : char ** argv = const_cast<char **>(cargv);
579 :
580 1 : advgetopt::getopt opt(environment_options, argc, argv);
581 :
582 : // check that the result is valid
583 :
584 : // an invalid parameter, MUST NOT EXIST
585 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
586 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
587 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
588 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
589 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
590 :
591 : // to reach the constant version `opt` has to be constant
592 3 : std::string const array_syntax_invalid_parameter(static_cast<advgetopt::getopt const &>(opt)["invalid-parameter"]);
593 1 : CATCH_REQUIRE(array_syntax_invalid_parameter == std::string());
594 :
595 : // no default
596 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
597 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
598 3 : CATCH_REQUIRE(opt.get_default("--").empty());
599 3 : CATCH_REQUIRE(opt.size("--") == 0);
600 :
601 : // the valid parameter
602 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
603 7 : CATCH_REQUIRE_THROWS_MATCHES(
604 : opt.get_double("size", 3, -100.0, 100.0)
605 : , advgetopt::getopt_logic_error
606 : , Catch::Matchers::ExceptionMessage(
607 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
608 3 : CATCH_REQUIRE_FALSE(opt.has_default("size"));
609 3 : CATCH_REQUIRE(opt.size("size") == 0);
610 :
611 : // other parameters
612 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
613 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
614 1 : }
615 5 : CATCH_END_SECTION()
616 :
617 5 : CATCH_START_SECTION("double_access: verify a double value validity when a default is defined.")
618 : {
619 1 : advgetopt::option const options[] =
620 : {
621 : advgetopt::define_option(
622 : advgetopt::Name("size")
623 : , advgetopt::ShortName('s')
624 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
625 : , advgetopt::DefaultValue("3.34 cans")
626 : , advgetopt::Help("define the size.")
627 : ),
628 : advgetopt::end_options()
629 : };
630 :
631 1 : advgetopt::options_environment environment_options;
632 1 : environment_options.f_project_name = "unittest";
633 1 : environment_options.f_options = options;
634 1 : environment_options.f_help_header = "Usage: test get_double() functions";
635 :
636 1 : char const * cargv[] =
637 : {
638 : "/usr/bin/arguments",
639 : nullptr
640 : };
641 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
642 1 : char ** argv = const_cast<char **>(cargv);
643 :
644 1 : advgetopt::getopt opt(environment_options, argc, argv);
645 :
646 : // check that the result is valid
647 :
648 : // an invalid parameter, MUST NOT EXIST
649 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
650 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
651 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
652 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
653 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
654 :
655 : // to reach the constant version `opt` has to be constant
656 3 : std::string const array_syntax_invalid_parameter(static_cast<advgetopt::getopt const &>(opt)["invalid-parameter"]);
657 1 : CATCH_REQUIRE(array_syntax_invalid_parameter == std::string());
658 :
659 : // no default
660 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
661 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
662 3 : CATCH_REQUIRE(opt.get_default("--").empty());
663 3 : CATCH_REQUIRE(opt.size("--") == 0);
664 :
665 : // the valid parameter
666 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
667 7 : CATCH_REQUIRE_THROWS_MATCHES(
668 : opt.get_double("size", 5, -100.0, 100.0)
669 : , advgetopt::getopt_logic_error
670 : , Catch::Matchers::ExceptionMessage(
671 : "getopt_logic_error: invalid default number \"3.34 cans\" for option --size."));
672 3 : CATCH_REQUIRE(opt.has_default("size"));
673 3 : CATCH_REQUIRE(opt.get_default("size") == "3.34 cans");
674 3 : CATCH_REQUIRE(opt.size("size") == 0);
675 :
676 : // other parameters
677 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
678 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
679 1 : }
680 5 : CATCH_END_SECTION()
681 :
682 5 : CATCH_START_SECTION("double_access: verify a double value in no arguments")
683 : {
684 1 : advgetopt::option const options[] =
685 : {
686 : advgetopt::define_option(
687 : advgetopt::Name("size")
688 : , advgetopt::ShortName('s')
689 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
690 : , advgetopt::Help("define the size.")
691 : , advgetopt::DefaultValue("8.39")
692 : ),
693 : advgetopt::end_options()
694 : };
695 :
696 1 : advgetopt::options_environment environment_options;
697 1 : environment_options.f_project_name = "unittest";
698 1 : environment_options.f_options = options;
699 1 : environment_options.f_help_header = "Usage: test get_double() functions";
700 :
701 1 : char const * cargv[] =
702 : {
703 : "/usr/bin/arguments",
704 : nullptr
705 : };
706 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
707 1 : char ** argv = const_cast<char **>(cargv);
708 :
709 1 : advgetopt::getopt opt(environment_options, argc, argv);
710 :
711 : // check that the result is valid
712 7 : CATCH_REQUIRE_THROWS_MATCHES(
713 : opt.get_double("non-existant", 3, -100.0, 100.0)
714 : , advgetopt::getopt_logic_error
715 : , Catch::Matchers::ExceptionMessage(
716 : "getopt_logic_error: there is no --non-existant option defined."));
717 :
718 : // an invalid parameter, MUST NOT EXIST
719 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
720 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
721 3 : CATCH_REQUIRE(opt["invalid-parameter"] == std::string());
722 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
723 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
724 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
725 :
726 : // no default
727 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
728 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
729 3 : CATCH_REQUIRE(opt.get_default("--").empty());
730 3 : CATCH_REQUIRE(opt.size("--") == 0);
731 :
732 : // the valid parameter
733 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
734 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
735 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
736 3 : CATCH_REQUIRE(opt.get_string("size") == "8.39");
737 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "8.39");
738 : #pragma GCC diagnostic push
739 : #pragma GCC diagnostic ignored "-Wfloat-equal"
740 : {
741 3 : bool const a1(opt.get_double("size") == 8.39);
742 1 : CATCH_REQUIRE(a1);
743 3 : bool const a2(opt.get_double("size", 0, 0.0, 10.0) == 8.39);
744 1 : CATCH_REQUIRE(a2);
745 :
746 3 : SNAP_CATCH2_NAMESPACE::push_expected_log(
747 : "error: 8.39 is out of bounds (-5..5 inclusive) in parameter --size.");
748 3 : bool const a3(opt.get_double("size", 0, -5.0, +5.0) == -1.0);
749 1 : CATCH_REQUIRE(a3);
750 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
751 :
752 : }
753 : #pragma GCC diagnostic pop
754 3 : CATCH_REQUIRE(opt.has_default("size"));
755 3 : CATCH_REQUIRE(opt.get_default("size") == "8.39");
756 3 : CATCH_REQUIRE(opt.size("size") == 0);
757 :
758 : // with a constant opt, the array syntax returns the default string
759 3 : CATCH_REQUIRE(static_cast<advgetopt::getopt const &>(opt)["size"] == "8.39");
760 :
761 : // other parameters
762 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
763 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
764 1 : }
765 5 : CATCH_END_SECTION()
766 5 : }
767 :
768 :
769 :
770 2 : CATCH_TEST_CASE("system_flags_version", "[arguments][valid][getopt][system_flags]")
771 : {
772 2 : CATCH_START_SECTION("system_flags_version: check with the --version system flag")
773 : {
774 1 : long const major_version(rand());
775 1 : long const minor_version(rand());
776 1 : long const patch_version(rand());
777 1 : long const build_version(rand());
778 3 : std::string const version(std::to_string(major_version)
779 4 : + "."
780 4 : + std::to_string(minor_version)
781 4 : + "."
782 4 : + std::to_string(patch_version)
783 4 : + "."
784 3 : + std::to_string(build_version));
785 :
786 1 : long const default_value(rand());
787 1 : std::string const default_val(std::to_string(default_value));
788 1 : advgetopt::option const options[] =
789 : {
790 6 : advgetopt::define_option(
791 : advgetopt::Name("size")
792 : , advgetopt::ShortName('s')
793 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
794 : , advgetopt::Help("define the size.")
795 : , advgetopt::DefaultValue(default_val.c_str())
796 : ),
797 : advgetopt::end_options()
798 6 : };
799 :
800 1 : advgetopt::options_environment environment_options;
801 1 : environment_options.f_project_name = "unittest";
802 1 : environment_options.f_options = options;
803 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
804 1 : environment_options.f_help_header = "Usage: test system commands";
805 1 : environment_options.f_version = version.c_str();
806 :
807 1 : char const * cargv[] =
808 : {
809 : "/usr/bin/arguments",
810 : "--version",
811 : nullptr
812 : };
813 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
814 1 : char ** argv = const_cast<char **>(cargv);
815 :
816 1 : advgetopt::getopt opt(environment_options, argc, argv);
817 :
818 : // check that the result is valid
819 :
820 : // an invalid parameter, MUST NOT EXIST
821 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
822 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
823 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
824 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
825 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
826 :
827 : // no default
828 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
829 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
830 3 : CATCH_REQUIRE(opt.get_default("--").empty());
831 3 : CATCH_REQUIRE(opt.size("--") == 0);
832 :
833 : // valid parameter
834 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
835 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
836 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
837 3 : CATCH_REQUIRE(opt.get_string("size") == default_val);
838 3 : CATCH_REQUIRE(opt.get_string("size", 0) == default_val);
839 3 : CATCH_REQUIRE(opt["size"] == default_val);
840 3 : CATCH_REQUIRE(opt.get_long("size") == default_value);
841 3 : CATCH_REQUIRE(opt.get_long("size", 0) == default_value);
842 3 : CATCH_REQUIRE(opt.has_default("size"));
843 3 : CATCH_REQUIRE(opt.get_default("size") == default_val);
844 3 : CATCH_REQUIRE(opt.size("size") == 0);
845 :
846 : // version parameter
847 3 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
848 3 : CATCH_REQUIRE(opt.get_option('V') == opt.get_option("version"));
849 3 : CATCH_REQUIRE(opt.is_defined("version"));
850 3 : CATCH_REQUIRE(opt.get_string("version") == "");
851 3 : CATCH_REQUIRE(opt.get_string("version", 0) == "");
852 3 : CATCH_REQUIRE(opt["version"] == "");
853 3 : CATCH_REQUIRE_FALSE(opt.has_default("version"));
854 3 : CATCH_REQUIRE(opt.get_default("version").empty());
855 3 : CATCH_REQUIRE(opt.size("version") == 1);
856 :
857 : // other parameters
858 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
859 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
860 :
861 : // process system options now
862 1 : std::stringstream ss;
863 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
864 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_VERSION);
865 1 : CATCH_REQUIRE(ss.str() == version + '\n');
866 1 : }
867 2 : CATCH_END_SECTION()
868 :
869 2 : CATCH_START_SECTION("system_flags_version: check with the --version system flag, without a --version on the command line")
870 : {
871 1 : long const major_version(rand());
872 1 : long const minor_version(rand());
873 1 : long const patch_version(rand());
874 1 : long const build_version(rand());
875 3 : std::string const version(std::to_string(major_version)
876 4 : + "."
877 4 : + std::to_string(minor_version)
878 4 : + "."
879 4 : + std::to_string(patch_version)
880 4 : + "."
881 3 : + std::to_string(build_version));
882 :
883 1 : long const default_value(rand());
884 1 : std::string const default_val(std::to_string(default_value));
885 1 : advgetopt::option const options[] =
886 : {
887 6 : advgetopt::define_option(
888 : advgetopt::Name("size")
889 : , advgetopt::ShortName('s')
890 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
891 : , advgetopt::Help("define the size.")
892 : , advgetopt::DefaultValue(default_val.c_str())
893 : ),
894 : advgetopt::end_options()
895 6 : };
896 :
897 1 : advgetopt::options_environment environment_options;
898 1 : environment_options.f_project_name = "unittest";
899 1 : environment_options.f_options = options;
900 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
901 1 : environment_options.f_help_header = "Usage: test system commands";
902 1 : environment_options.f_version = version.c_str();
903 :
904 1 : char const * cargv[] =
905 : {
906 : "/usr/bin/arguments",
907 : "--size",
908 : "1221",
909 : nullptr
910 : };
911 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
912 1 : char ** argv = const_cast<char **>(cargv);
913 :
914 1 : advgetopt::getopt opt(environment_options, argc, argv);
915 :
916 : // check that the result is valid
917 :
918 : // an invalid parameter, MUST NOT EXIST
919 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
920 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
921 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
922 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
923 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
924 :
925 : // no default
926 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
927 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
928 3 : CATCH_REQUIRE(opt.get_default("--").empty());
929 3 : CATCH_REQUIRE(opt.size("--") == 0);
930 :
931 : // valid parameter
932 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
933 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
934 3 : CATCH_REQUIRE(opt.is_defined("size"));
935 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
936 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
937 3 : CATCH_REQUIRE(opt["size"] == "1221");
938 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
939 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
940 3 : CATCH_REQUIRE(opt.has_default("size"));
941 3 : CATCH_REQUIRE(opt.get_default("size") == default_val);
942 3 : CATCH_REQUIRE(opt.size("size") == 1);
943 :
944 : // version parameter
945 3 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
946 3 : CATCH_REQUIRE(opt.get_option('V') == opt.get_option("version"));
947 3 : CATCH_REQUIRE_FALSE(opt.is_defined("version"));
948 3 : CATCH_REQUIRE_FALSE(opt.has_default("version"));
949 3 : CATCH_REQUIRE(opt.get_default("version").empty());
950 3 : CATCH_REQUIRE(opt.size("version") == 0);
951 :
952 : // other parameters
953 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
954 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
955 :
956 : // process system options now
957 1 : std::stringstream ss;
958 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
959 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
960 1 : CATCH_REQUIRE(ss.str().empty());
961 1 : }
962 2 : CATCH_END_SECTION()
963 2 : }
964 :
965 :
966 :
967 2 : CATCH_TEST_CASE("system_flags_has_sanitizer", "[arguments][valid][getopt][system_flags]")
968 : {
969 2 : CATCH_START_SECTION("system_flags_has_sanitizer: check with the --has-sanitizer system flag")
970 : {
971 1 : long const default_value(rand());
972 1 : std::string const default_val(std::to_string(default_value));
973 1 : advgetopt::option const options[] =
974 : {
975 6 : advgetopt::define_option(
976 : advgetopt::Name("size")
977 : , advgetopt::ShortName('s')
978 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
979 : , advgetopt::Help("define the size.")
980 : , advgetopt::DefaultValue(default_val.c_str())
981 : ),
982 : advgetopt::end_options()
983 6 : };
984 :
985 1 : advgetopt::options_environment environment_options;
986 1 : environment_options.f_project_name = "unittest";
987 1 : environment_options.f_options = options;
988 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
989 1 : environment_options.f_help_header = "Usage: test system commands";
990 1 : environment_options.f_version = "2.0.24.0";
991 :
992 1 : char const * cargv[] =
993 : {
994 : "/usr/bin/arguments",
995 : "--has-sanitizer",
996 : nullptr
997 : };
998 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
999 1 : char ** argv = const_cast<char **>(cargv);
1000 :
1001 1 : advgetopt::getopt opt(environment_options, argc, argv);
1002 :
1003 : // check that the result is valid
1004 :
1005 : // an invalid parameter, MUST NOT EXIST
1006 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1007 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1008 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1009 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1010 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1011 :
1012 : // no default
1013 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1014 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1015 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1016 3 : CATCH_REQUIRE(opt.size("--") == 0);
1017 :
1018 : // valid parameter
1019 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1020 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1021 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1022 3 : CATCH_REQUIRE(opt.get_string("size") == default_val);
1023 3 : CATCH_REQUIRE(opt.get_string("size", 0) == default_val);
1024 3 : CATCH_REQUIRE(opt["size"] == default_val);
1025 3 : CATCH_REQUIRE(opt.get_long("size") == default_value);
1026 3 : CATCH_REQUIRE(opt.get_long("size", 0) == default_value);
1027 3 : CATCH_REQUIRE(opt.has_default("size"));
1028 3 : CATCH_REQUIRE(opt.get_default("size") == default_val);
1029 3 : CATCH_REQUIRE(opt.size("size") == 0);
1030 :
1031 : // has-sanitizer parameter
1032 3 : CATCH_REQUIRE(opt.get_option("has-sanitizer") != nullptr);
1033 3 : CATCH_REQUIRE(opt.is_defined("has-sanitizer"));
1034 3 : CATCH_REQUIRE(opt.get_string("has-sanitizer") == "");
1035 3 : CATCH_REQUIRE(opt.get_string("has-sanitizer", 0) == "");
1036 3 : CATCH_REQUIRE(opt["has-sanitizer"] == "");
1037 3 : CATCH_REQUIRE_FALSE(opt.has_default("has-sanitizer"));
1038 3 : CATCH_REQUIRE(opt.get_default("has-sanitizer").empty());
1039 3 : CATCH_REQUIRE(opt.size("has-sanitizer") == 1);
1040 :
1041 : // other parameters
1042 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1043 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1044 :
1045 : // process system options now
1046 1 : std::stringstream ss;
1047 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1048 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1049 : #if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
1050 : // when running coverage or in the Sanitize version
1051 : //
1052 1 : std::string const expected(
1053 : #ifdef __SANITIZE_ADDRESS__
1054 : "The address sanitizer is compiled in.\n"
1055 : #endif
1056 : #ifdef __SANITIZE_THREAD__
1057 : "The thread sanitizer is compiled in.\n"
1058 : #endif
1059 3 : );
1060 1 : CATCH_REQUIRE(ss.str() == expected);
1061 : #else
1062 : // when running in Debug or Release
1063 : //
1064 : CATCH_REQUIRE(ss.str() == "The address and thread sanitizers are not compiled in.\n");
1065 : #endif
1066 1 : }
1067 2 : CATCH_END_SECTION()
1068 :
1069 2 : CATCH_START_SECTION("system_flags_has_sanitizer: check with the --has-sanitizer system flag, without a --has-sanitizer on the command line")
1070 : {
1071 1 : long const default_value(rand());
1072 1 : std::string const default_val(std::to_string(default_value));
1073 1 : advgetopt::option const options[] =
1074 : {
1075 6 : advgetopt::define_option(
1076 : advgetopt::Name("size")
1077 : , advgetopt::ShortName('s')
1078 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1079 : , advgetopt::Help("define the size.")
1080 : , advgetopt::DefaultValue(default_val.c_str())
1081 : ),
1082 : advgetopt::end_options()
1083 6 : };
1084 :
1085 1 : advgetopt::options_environment environment_options;
1086 1 : environment_options.f_project_name = "unittest";
1087 1 : environment_options.f_options = options;
1088 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1089 1 : environment_options.f_help_header = "Usage: test system commands";
1090 1 : environment_options.f_version = "2.0.24.1";
1091 :
1092 1 : char const * cargv[] =
1093 : {
1094 : "/usr/bin/arguments",
1095 : "--size",
1096 : "1221",
1097 : nullptr
1098 : };
1099 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1100 1 : char ** argv = const_cast<char **>(cargv);
1101 :
1102 1 : advgetopt::getopt opt(environment_options, argc, argv);
1103 :
1104 : // check that the result is valid
1105 :
1106 : // an invalid parameter, MUST NOT EXIST
1107 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1108 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1109 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1110 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1111 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1112 :
1113 : // no default
1114 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1115 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1116 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1117 3 : CATCH_REQUIRE(opt.size("--") == 0);
1118 :
1119 : // valid parameter
1120 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1121 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1122 3 : CATCH_REQUIRE(opt.is_defined("size"));
1123 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1124 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1125 3 : CATCH_REQUIRE(opt["size"] == "1221");
1126 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1127 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1128 3 : CATCH_REQUIRE(opt.has_default("size"));
1129 3 : CATCH_REQUIRE(opt.get_default("size") == default_val);
1130 3 : CATCH_REQUIRE(opt.size("size") == 1);
1131 :
1132 : // has-sanitizer parameter
1133 3 : CATCH_REQUIRE(opt.get_option("has-sanitizer") != nullptr);
1134 3 : CATCH_REQUIRE_FALSE(opt.is_defined("has-sanitizer"));
1135 3 : CATCH_REQUIRE_FALSE(opt.has_default("has-sanitizer"));
1136 3 : CATCH_REQUIRE(opt.get_default("has-sanitizer").empty());
1137 3 : CATCH_REQUIRE(opt.size("has-sanitizer") == 0);
1138 :
1139 : // other parameters
1140 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1141 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1142 :
1143 : // process system options now
1144 1 : std::stringstream ss;
1145 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1146 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1147 1 : CATCH_REQUIRE(ss.str().empty());
1148 1 : }
1149 2 : CATCH_END_SECTION()
1150 2 : }
1151 :
1152 :
1153 :
1154 2 : CATCH_TEST_CASE("system_flags_compiler_version", "[arguments][valid][getopt][system_flags]")
1155 : {
1156 2 : CATCH_START_SECTION("system_flags_compiler_version: check with the --compiler-version system flag")
1157 : {
1158 1 : long const default_value(rand());
1159 1 : std::string const default_val(std::to_string(default_value));
1160 1 : advgetopt::option const options[] =
1161 : {
1162 6 : advgetopt::define_option(
1163 : advgetopt::Name("size")
1164 : , advgetopt::ShortName('s')
1165 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1166 : , advgetopt::Help("define the size.")
1167 : , advgetopt::DefaultValue(default_val.c_str())
1168 : ),
1169 : advgetopt::end_options()
1170 6 : };
1171 :
1172 1 : advgetopt::options_environment environment_options;
1173 1 : environment_options.f_project_name = "unittest";
1174 1 : environment_options.f_options = options;
1175 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1176 1 : environment_options.f_help_header = "Usage: test system commands";
1177 1 : environment_options.f_version = "2.0.24.0";
1178 :
1179 1 : char const * cargv[] =
1180 : {
1181 : "/usr/bin/arguments",
1182 : "--compiler-version",
1183 : nullptr
1184 : };
1185 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1186 1 : char ** argv = const_cast<char **>(cargv);
1187 :
1188 1 : advgetopt::getopt opt(environment_options, argc, argv);
1189 :
1190 : // check that the result is valid
1191 :
1192 : // an invalid parameter, MUST NOT EXIST
1193 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1194 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1195 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1196 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1197 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1198 :
1199 : // no default
1200 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1201 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1202 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1203 3 : CATCH_REQUIRE(opt.size("--") == 0);
1204 :
1205 : // valid parameter
1206 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1207 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1208 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1209 3 : CATCH_REQUIRE(opt.get_string("size") == default_val);
1210 3 : CATCH_REQUIRE(opt.get_string("size", 0) == default_val);
1211 3 : CATCH_REQUIRE(opt["size"] == default_val);
1212 3 : CATCH_REQUIRE(opt.get_long("size") == default_value);
1213 3 : CATCH_REQUIRE(opt.get_long("size", 0) == default_value);
1214 3 : CATCH_REQUIRE(opt.has_default("size"));
1215 3 : CATCH_REQUIRE(opt.get_default("size") == default_val);
1216 3 : CATCH_REQUIRE(opt.size("size") == 0);
1217 :
1218 : // compiler-version parameter
1219 3 : CATCH_REQUIRE(opt.get_option("compiler-version") != nullptr);
1220 3 : CATCH_REQUIRE(opt.is_defined("compiler-version"));
1221 3 : CATCH_REQUIRE(opt.get_string("compiler-version") == "");
1222 3 : CATCH_REQUIRE(opt.get_string("compiler-version", 0) == "");
1223 3 : CATCH_REQUIRE(opt["compiler-version"] == "");
1224 3 : CATCH_REQUIRE_FALSE(opt.has_default("compiler-version"));
1225 3 : CATCH_REQUIRE(opt.get_default("compiler-version").empty());
1226 3 : CATCH_REQUIRE(opt.size("compiler-version") == 1);
1227 :
1228 : // other parameters
1229 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1230 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1231 :
1232 : // process system options now
1233 1 : std::stringstream ss;
1234 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1235 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1236 1 : CATCH_REQUIRE(ss.str() == LIBADVGETOPT_COMPILER_VERSION "\n");
1237 1 : }
1238 2 : CATCH_END_SECTION()
1239 :
1240 2 : CATCH_START_SECTION("system_flags_compiler_version: check with the --compiler-version system flag, without a --compiler-version on the command line")
1241 : {
1242 1 : long const default_value(rand());
1243 1 : std::string const default_val(std::to_string(default_value));
1244 1 : advgetopt::option const options[] =
1245 : {
1246 6 : advgetopt::define_option(
1247 : advgetopt::Name("size")
1248 : , advgetopt::ShortName('s')
1249 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1250 : , advgetopt::Help("define the size.")
1251 : , advgetopt::DefaultValue(default_val.c_str())
1252 : ),
1253 : advgetopt::end_options()
1254 6 : };
1255 :
1256 1 : advgetopt::options_environment environment_options;
1257 1 : environment_options.f_project_name = "unittest";
1258 1 : environment_options.f_options = options;
1259 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1260 1 : environment_options.f_help_header = "Usage: test system commands";
1261 1 : environment_options.f_version = "2.0.24.1";
1262 :
1263 1 : char const * cargv[] =
1264 : {
1265 : "/usr/bin/arguments",
1266 : "--size",
1267 : "1221",
1268 : nullptr
1269 : };
1270 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1271 1 : char ** argv = const_cast<char **>(cargv);
1272 :
1273 1 : advgetopt::getopt opt(environment_options, argc, argv);
1274 :
1275 : // check that the result is valid
1276 :
1277 : // an invalid parameter, MUST NOT EXIST
1278 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1279 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1280 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1281 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1282 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1283 :
1284 : // no default
1285 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1286 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1287 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1288 3 : CATCH_REQUIRE(opt.size("--") == 0);
1289 :
1290 : // valid parameter
1291 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1292 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1293 3 : CATCH_REQUIRE(opt.is_defined("size"));
1294 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1295 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1296 3 : CATCH_REQUIRE(opt["size"] == "1221");
1297 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1298 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1299 3 : CATCH_REQUIRE(opt.has_default("size"));
1300 3 : CATCH_REQUIRE(opt.get_default("size") == default_val);
1301 3 : CATCH_REQUIRE(opt.size("size") == 1);
1302 :
1303 : // compiler-version parameter
1304 3 : CATCH_REQUIRE(opt.get_option("compiler-version") != nullptr);
1305 3 : CATCH_REQUIRE_FALSE(opt.is_defined("compiler-version"));
1306 3 : CATCH_REQUIRE_FALSE(opt.has_default("compiler-version"));
1307 3 : CATCH_REQUIRE(opt.get_default("compiler-version").empty());
1308 3 : CATCH_REQUIRE(opt.size("compiler-version") == 0);
1309 :
1310 : // other parameters
1311 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1312 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1313 :
1314 : // process system options now
1315 1 : std::stringstream ss;
1316 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1317 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1318 1 : CATCH_REQUIRE(ss.str().empty());
1319 1 : }
1320 2 : CATCH_END_SECTION()
1321 2 : }
1322 :
1323 :
1324 :
1325 5 : CATCH_TEST_CASE("system_flags_help", "[arguments][valid][getopt][system_flags]")
1326 : {
1327 5 : CATCH_START_SECTION("system_flags_help: check with the --help system flag")
1328 : {
1329 1 : advgetopt::option const options[] =
1330 : {
1331 : advgetopt::define_option(
1332 : advgetopt::Name("size")
1333 : , advgetopt::ShortName('s')
1334 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1335 : , advgetopt::Help("define the size.")
1336 : , advgetopt::DefaultValue("33")
1337 : ),
1338 : advgetopt::define_option(
1339 : advgetopt::Name("obscure")
1340 : , advgetopt::ShortName('o')
1341 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1342 : , advgetopt::GETOPT_FLAG_SHOW_GROUP1>())
1343 : , advgetopt::Help("obscure command, hidden by default.")
1344 : ),
1345 : advgetopt::define_option(
1346 : advgetopt::Name("secret")
1347 : , advgetopt::ShortName('S')
1348 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1349 : , advgetopt::GETOPT_FLAG_SHOW_GROUP2>())
1350 : , advgetopt::Help("even more secret command, hidden by default.")
1351 : ),
1352 : advgetopt::end_options()
1353 : };
1354 :
1355 1 : advgetopt::options_environment environment_options;
1356 1 : environment_options.f_project_name = "unittest";
1357 1 : environment_options.f_options = options;
1358 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1359 1 : environment_options.f_help_header = "Usage: test system commands";
1360 1 : environment_options.f_help_footer = "Copyright matters";
1361 :
1362 1 : char const * cargv[] =
1363 : {
1364 : "/usr/bin/arguments",
1365 : "--help",
1366 : nullptr
1367 : };
1368 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1369 1 : char ** argv = const_cast<char **>(cargv);
1370 :
1371 1 : advgetopt::getopt opt(environment_options, argc, argv);
1372 :
1373 : // check that the result is valid
1374 :
1375 : // check the list of options
1376 : // 3 -- the number of user defined option (see above)
1377 : // 13 -- the number of system options (see advgetopt.cpp)
1378 : // 2 -- auto-added long-help
1379 1 : advgetopt::option_info::map_by_name_t const & list_of_options(opt.get_options());
1380 1 : CATCH_REQUIRE(list_of_options.size() == 3 + 13 + 2);
1381 :
1382 : // user options
1383 3 : CATCH_REQUIRE(list_of_options.find("size") != list_of_options.end());
1384 3 : CATCH_REQUIRE(list_of_options.find("obscure") != list_of_options.end());
1385 3 : CATCH_REQUIRE(list_of_options.find("secret") != list_of_options.end());
1386 :
1387 : // system options
1388 3 : CATCH_REQUIRE(list_of_options.find("build-date") != list_of_options.end());
1389 3 : CATCH_REQUIRE(list_of_options.find("configuration-filenames") != list_of_options.end());
1390 3 : CATCH_REQUIRE(list_of_options.find("copyright") != list_of_options.end());
1391 3 : CATCH_REQUIRE(list_of_options.find("environment-variable-name") != list_of_options.end());
1392 3 : CATCH_REQUIRE(list_of_options.find("help") != list_of_options.end());
1393 3 : CATCH_REQUIRE(list_of_options.find("license") != list_of_options.end());
1394 3 : CATCH_REQUIRE(list_of_options.find("long-help") != list_of_options.end());
1395 3 : CATCH_REQUIRE(list_of_options.find("path-to-option-definitions") != list_of_options.end());
1396 3 : CATCH_REQUIRE(list_of_options.find("system-help") != list_of_options.end());
1397 3 : CATCH_REQUIRE(list_of_options.find("version") != list_of_options.end());
1398 :
1399 : // an invalid parameter, MUST NOT EXIST
1400 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1401 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1402 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1403 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1404 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1405 :
1406 : // no default
1407 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1408 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1409 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1410 3 : CATCH_REQUIRE(opt.size("--") == 0);
1411 :
1412 : // valid parameter
1413 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1414 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1415 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1416 3 : CATCH_REQUIRE(opt.get_string("size") == "33");
1417 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1418 3 : CATCH_REQUIRE(opt["size"] == "33");
1419 3 : CATCH_REQUIRE(opt.get_long("size") == 33);
1420 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1421 3 : CATCH_REQUIRE(opt.has_default("size"));
1422 3 : CATCH_REQUIRE(opt.get_default("size") == "33");
1423 3 : CATCH_REQUIRE(opt.size("size") == 0);
1424 :
1425 : // help parameter
1426 3 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
1427 3 : CATCH_REQUIRE(opt.get_option('h') == opt.get_option("help"));
1428 3 : CATCH_REQUIRE(opt.is_defined("help"));
1429 3 : CATCH_REQUIRE(opt.get_string("help") == "");
1430 3 : CATCH_REQUIRE(opt.get_string("help", 0) == "");
1431 3 : CATCH_REQUIRE(opt["help"] == "");
1432 3 : CATCH_REQUIRE_FALSE(opt.has_default("help"));
1433 3 : CATCH_REQUIRE(opt.get_default("help").empty());
1434 3 : CATCH_REQUIRE(opt.size("help") == 1);
1435 :
1436 : // other parameters
1437 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1438 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1439 :
1440 : // process system options now
1441 1 : std::stringstream ss;
1442 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1443 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1444 31 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1445 : advgetopt::breakup_line(
1446 : "Usage: test system commands"
1447 : , 0
1448 : , advgetopt::get_screen_width())
1449 : + advgetopt::format_usage_string(
1450 : "--copyright or -C"
1451 : , "print out the copyright of arguments and exit."
1452 : , 30
1453 : , advgetopt::get_screen_width())
1454 : + advgetopt::format_usage_string(
1455 : "--help or -h"
1456 : , "print out this help screen and exit."
1457 : , 30
1458 : , advgetopt::get_screen_width())
1459 : + advgetopt::format_usage_string(
1460 : "--license or -L"
1461 : , "print out the license of arguments and exit."
1462 : , 30
1463 : , advgetopt::get_screen_width())
1464 : + advgetopt::format_usage_string(
1465 : "--long-help or -?"
1466 : , "show all the help from all the available options."
1467 : , 30
1468 : , advgetopt::get_screen_width())
1469 : + advgetopt::format_usage_string(
1470 : "--size or -s <arg> (default is \"33\")"
1471 : , "define the size."
1472 : , 30
1473 : , advgetopt::get_screen_width())
1474 : + advgetopt::format_usage_string(
1475 : "--system-help"
1476 : , "show commands and options added by libraries."
1477 : , 30
1478 : , advgetopt::get_screen_width())
1479 : + advgetopt::format_usage_string(
1480 : "--version or -V"
1481 : , "print out the version of arguments and exit."
1482 : , 30
1483 : , advgetopt::get_screen_width())
1484 : + "\n"
1485 : "Copyright matters\n"
1486 : "\n"
1487 : );
1488 1 : }
1489 5 : CATCH_END_SECTION()
1490 :
1491 5 : CATCH_START_SECTION("system_flags_help: check with the --long-help system flag")
1492 : {
1493 1 : advgetopt::option const options[] =
1494 : {
1495 : advgetopt::define_option(
1496 : advgetopt::Name("size")
1497 : , advgetopt::ShortName('s')
1498 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1499 : , advgetopt::Help("define the size.")
1500 : , advgetopt::DefaultValue("33")
1501 : ),
1502 : advgetopt::define_option(
1503 : advgetopt::Name("obscure")
1504 : , advgetopt::ShortName('o')
1505 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1506 : , advgetopt::GETOPT_FLAG_SHOW_GROUP1>())
1507 : , advgetopt::Help("obscure command, hidden by default.")
1508 : ),
1509 : advgetopt::define_option(
1510 : advgetopt::Name("secret")
1511 : , advgetopt::ShortName('S')
1512 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1513 : , advgetopt::GETOPT_FLAG_SHOW_GROUP2>())
1514 : , advgetopt::Help("even more secret command, hidden by default.")
1515 : ),
1516 : advgetopt::end_options()
1517 : };
1518 :
1519 1 : advgetopt::options_environment environment_options;
1520 1 : environment_options.f_project_name = "unittest";
1521 1 : environment_options.f_options = options;
1522 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1523 1 : environment_options.f_help_header = "Usage: test system commands";
1524 1 : environment_options.f_help_footer = "Copyright matters";
1525 :
1526 1 : char const * cargv[] =
1527 : {
1528 : "/usr/bin/arguments",
1529 : "--long-help",
1530 : nullptr
1531 : };
1532 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1533 1 : char ** argv = const_cast<char **>(cargv);
1534 :
1535 1 : advgetopt::getopt opt(environment_options, argc, argv);
1536 :
1537 1 : CATCH_REQUIRE(opt.get_group_name() == std::string());
1538 :
1539 : // check that the result is valid
1540 :
1541 : // an invalid parameter, MUST NOT EXIST
1542 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1543 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1544 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1545 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1546 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1547 :
1548 : // no default
1549 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1550 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1551 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1552 3 : CATCH_REQUIRE(opt.size("--") == 0);
1553 :
1554 : // valid parameter
1555 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1556 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1557 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1558 3 : CATCH_REQUIRE(opt.get_string("size") == "33");
1559 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1560 3 : CATCH_REQUIRE(opt["size"] == "33");
1561 3 : CATCH_REQUIRE(opt.get_long("size") == 33);
1562 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1563 3 : CATCH_REQUIRE(opt.has_default("size"));
1564 3 : CATCH_REQUIRE(opt.get_default("size") == "33");
1565 3 : CATCH_REQUIRE(opt.size("size") == 0);
1566 :
1567 : // help parameter
1568 3 : CATCH_REQUIRE(opt.get_option("long-help") != nullptr);
1569 3 : CATCH_REQUIRE(opt.get_option('?') == opt.get_option("long-help"));
1570 3 : CATCH_REQUIRE(opt.is_defined("long-help"));
1571 3 : CATCH_REQUIRE(opt.get_string("long-help") == "");
1572 3 : CATCH_REQUIRE(opt.get_string("long-help", 0) == "");
1573 3 : CATCH_REQUIRE(opt["long-help"] == "");
1574 3 : CATCH_REQUIRE_FALSE(opt.has_default("long-help"));
1575 3 : CATCH_REQUIRE(opt.get_default("long-help").empty());
1576 3 : CATCH_REQUIRE(opt.size("long-help") == 1);
1577 :
1578 : // other parameters
1579 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1580 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1581 :
1582 : // process system options now
1583 1 : std::stringstream ss;
1584 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1585 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1586 75 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1587 : advgetopt::breakup_line(
1588 : "Usage: test system commands"
1589 : , 0
1590 : , advgetopt::get_screen_width())
1591 : + advgetopt::format_usage_string(
1592 : "--build-date"
1593 : , "print out the time and date when arguments was built and exit."
1594 : , 30
1595 : , advgetopt::get_screen_width())
1596 : + advgetopt::format_usage_string(
1597 : "--compiler-version"
1598 : , "print the version of the compiler used to compile the advgetopt library."
1599 : , 30
1600 : , advgetopt::get_screen_width())
1601 : + advgetopt::format_usage_string(
1602 : "--configuration-filenames"
1603 : , "print out the list of configuration files checked out by this"
1604 : " tool."
1605 : , 30
1606 : , advgetopt::get_screen_width())
1607 : + advgetopt::format_usage_string(
1608 : "--copyright or -C"
1609 : , "print out the copyright of arguments and exit."
1610 : , 30
1611 : , advgetopt::get_screen_width())
1612 : + advgetopt::format_usage_string(
1613 : "--environment-variable-name"
1614 : , "print out the name of the environment variable supported by"
1615 : " arguments (if any.)"
1616 : , 30
1617 : , advgetopt::get_screen_width())
1618 : + advgetopt::format_usage_string(
1619 : "--filenames-of-option-definitions"
1620 : , "print out the full filename of the main option definitions"
1621 : " and if any, the filenames of additional option definitions"
1622 : " (often used by plugins)."
1623 : , 30
1624 : , advgetopt::get_screen_width())
1625 : + advgetopt::format_usage_string(
1626 : "--has-sanitizer"
1627 : , "print whether the advgetopt was compiled with the sanitizer extension."
1628 : , 30
1629 : , advgetopt::get_screen_width())
1630 : + advgetopt::format_usage_string(
1631 : "--help or -h"
1632 : , "print out this help screen and exit."
1633 : , 30
1634 : , advgetopt::get_screen_width())
1635 : + advgetopt::format_usage_string(
1636 : "--license or -L"
1637 : , "print out the license of arguments and exit."
1638 : , 30
1639 : , advgetopt::get_screen_width())
1640 : + advgetopt::format_usage_string(
1641 : "--long-help or -?"
1642 : , "show all the help from all the available options."
1643 : , 30
1644 : , advgetopt::get_screen_width())
1645 : + advgetopt::format_usage_string(
1646 : "--obscure or -o <arg>"
1647 : , "obscure command, hidden by default."
1648 : , 30
1649 : , advgetopt::get_screen_width())
1650 : + advgetopt::format_usage_string(
1651 : "--path-to-option-definitions"
1652 : , "print out the path to the option definitions."
1653 : , 30
1654 : , advgetopt::get_screen_width())
1655 : + advgetopt::format_usage_string(
1656 : "--print-option <arg>"
1657 : , "print the value of the named option after parsing all the options from the command line, environment variables, and configuration files."
1658 : , 30
1659 : , advgetopt::get_screen_width())
1660 : + advgetopt::format_usage_string(
1661 : "--secret or -S <arg>"
1662 : , "even more secret command, hidden by default."
1663 : , 30
1664 : , advgetopt::get_screen_width())
1665 : + advgetopt::format_usage_string(
1666 : "--show-option-sources"
1667 : , "parse all the options and then print out the source of each"
1668 : " value and each override."
1669 : , 30
1670 : , advgetopt::get_screen_width())
1671 : + advgetopt::format_usage_string(
1672 : "--size or -s <arg> (default is \"33\")"
1673 : , "define the size."
1674 : , 30
1675 : , advgetopt::get_screen_width())
1676 : + advgetopt::format_usage_string(
1677 : "--system-help"
1678 : , "show commands and options added by libraries."
1679 : , 30
1680 : , advgetopt::get_screen_width())
1681 : + advgetopt::format_usage_string(
1682 : "--version or -V"
1683 : , "print out the version of arguments and exit."
1684 : , 30
1685 : , advgetopt::get_screen_width())
1686 : + "\n"
1687 : "Copyright matters\n"
1688 : "\n"
1689 : );
1690 1 : }
1691 5 : CATCH_END_SECTION()
1692 :
1693 5 : CATCH_START_SECTION("system_flags_help: check with the --help system flag, without a --help on the command line")
1694 : {
1695 1 : advgetopt::option const options[] =
1696 : {
1697 : advgetopt::define_option(
1698 : advgetopt::Name("size")
1699 : , advgetopt::ShortName('s')
1700 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1701 : , advgetopt::Help("define the size.")
1702 : , advgetopt::DefaultValue("33")
1703 : ),
1704 : advgetopt::end_options()
1705 : };
1706 :
1707 1 : advgetopt::options_environment environment_options;
1708 1 : environment_options.f_project_name = "unittest";
1709 1 : environment_options.f_options = options;
1710 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1711 1 : environment_options.f_help_header = "Usage: test system commands";
1712 1 : environment_options.f_help_footer = "Copyright matters";
1713 :
1714 1 : char const * cargv[] =
1715 : {
1716 : "/usr/bin/arguments",
1717 : "--size",
1718 : "1221",
1719 : nullptr
1720 : };
1721 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1722 1 : char ** argv = const_cast<char **>(cargv);
1723 :
1724 1 : advgetopt::getopt opt(environment_options, argc, argv);
1725 :
1726 : // check that the result is valid
1727 :
1728 : // an invalid parameter, MUST NOT EXIST
1729 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1730 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1731 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1732 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1733 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1734 :
1735 : // no default
1736 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1737 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1738 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1739 3 : CATCH_REQUIRE(opt.size("--") == 0);
1740 :
1741 : // valid parameter
1742 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1743 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1744 3 : CATCH_REQUIRE(opt.is_defined("size"));
1745 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1746 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1747 3 : CATCH_REQUIRE(opt["size"] == "1221");
1748 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1749 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1750 3 : CATCH_REQUIRE(opt.has_default("size"));
1751 3 : CATCH_REQUIRE(opt.get_default("size") == "33");
1752 3 : CATCH_REQUIRE(opt.size("size") == 1);
1753 :
1754 : // help parameter
1755 3 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
1756 3 : CATCH_REQUIRE(opt.get_option('h') == opt.get_option("help"));
1757 3 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
1758 3 : CATCH_REQUIRE_FALSE(opt.has_default("help"));
1759 3 : CATCH_REQUIRE(opt.get_default("help").empty());
1760 3 : CATCH_REQUIRE(opt.size("help") == 0);
1761 :
1762 : // other parameters
1763 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1764 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1765 :
1766 : // process system options now
1767 1 : std::stringstream ss;
1768 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1769 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1770 1 : CATCH_REQUIRE(ss.str().empty());
1771 1 : }
1772 5 : CATCH_END_SECTION()
1773 :
1774 5 : CATCH_START_SECTION("system_flags_help: check with the --commands-help system flag")
1775 : {
1776 1 : advgetopt::option const options[] =
1777 : {
1778 : advgetopt::define_option(
1779 : advgetopt::Name("size")
1780 : , advgetopt::ShortName('s')
1781 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1782 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1783 : , advgetopt::Help("define the size.")
1784 : , advgetopt::DefaultValue("33")
1785 : ),
1786 : advgetopt::define_option(
1787 : advgetopt::Name("obscure")
1788 : , advgetopt::ShortName('o')
1789 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1790 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1791 : , advgetopt::Help("obscure command, hidden by default.")
1792 : ),
1793 : advgetopt::define_option(
1794 : advgetopt::Name("secret")
1795 : , advgetopt::ShortName('S')
1796 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1797 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
1798 : , advgetopt::Help("even more secret command, hidden by default.")
1799 : ),
1800 : advgetopt::end_options()
1801 : };
1802 :
1803 1 : advgetopt::group_description const groups[] =
1804 : {
1805 : advgetopt::define_group(
1806 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
1807 : , advgetopt::GroupName("commands")
1808 : , advgetopt::GroupDescription("Commands:")
1809 : ),
1810 : advgetopt::define_group(
1811 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
1812 : , advgetopt::GroupName("option")
1813 : , advgetopt::GroupDescription("Options:")
1814 : ),
1815 : advgetopt::end_groups()
1816 : };
1817 :
1818 1 : advgetopt::options_environment environment_options;
1819 1 : environment_options.f_project_name = "unittest";
1820 1 : environment_options.f_options = options;
1821 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1822 1 : environment_options.f_help_header = "Usage: test system commands";
1823 1 : environment_options.f_help_footer = "Copyright matters";
1824 1 : environment_options.f_groups = groups;
1825 :
1826 1 : char const * cargv[] =
1827 : {
1828 : "/usr/bin/arguments",
1829 : "--commands-help",
1830 : nullptr
1831 : };
1832 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1833 1 : char ** argv = const_cast<char **>(cargv);
1834 :
1835 1 : advgetopt::getopt opt(environment_options, argc, argv);
1836 :
1837 : // check that the result is valid
1838 :
1839 : // an invalid parameter, MUST NOT EXIST
1840 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1841 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1842 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1843 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1844 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1845 :
1846 : // no default
1847 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1848 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1849 3 : CATCH_REQUIRE(opt.get_default("--").empty());
1850 3 : CATCH_REQUIRE(opt.size("--") == 0);
1851 :
1852 : // valid parameter
1853 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1854 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1855 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1856 3 : CATCH_REQUIRE(opt.get_string("size") == "33");
1857 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1858 3 : CATCH_REQUIRE(opt["size"] == "33");
1859 3 : CATCH_REQUIRE(opt.get_long("size") == 33);
1860 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1861 3 : CATCH_REQUIRE(opt.has_default("size"));
1862 3 : CATCH_REQUIRE(opt.get_default("size") == "33");
1863 3 : CATCH_REQUIRE(opt.size("size") == 0);
1864 :
1865 : // help parameter
1866 3 : CATCH_REQUIRE(opt.get_option("commands-help") != nullptr);
1867 3 : CATCH_REQUIRE(opt.is_defined("commands-help"));
1868 3 : CATCH_REQUIRE(opt.get_string("commands-help") == "");
1869 3 : CATCH_REQUIRE(opt.get_string("commands-help", 0) == "");
1870 3 : CATCH_REQUIRE(opt["commands-help"] == "");
1871 3 : CATCH_REQUIRE_FALSE(opt.has_default("commands-help"));
1872 3 : CATCH_REQUIRE(opt.get_default("commands-help").empty());
1873 3 : CATCH_REQUIRE(opt.size("commands-help") == 1);
1874 :
1875 : // other parameters
1876 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1877 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1878 :
1879 : // process system options now
1880 1 : std::stringstream ss;
1881 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1882 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1883 43 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1884 : advgetopt::breakup_line(
1885 : "Usage: test system commands"
1886 : , 0
1887 : , advgetopt::get_screen_width())
1888 : + "\n"
1889 : "Commands:\n"
1890 : + advgetopt::format_usage_string(
1891 : "--commands-help"
1892 : , "show help from the \"commands\" group of options."
1893 : , 30
1894 : , advgetopt::get_screen_width())
1895 : + advgetopt::format_usage_string(
1896 : "--copyright or -C"
1897 : , "print out the copyright of arguments and exit."
1898 : , 30
1899 : , advgetopt::get_screen_width())
1900 : + advgetopt::format_usage_string(
1901 : "--help or -h"
1902 : , "print out this help screen and exit."
1903 : , 30
1904 : , advgetopt::get_screen_width())
1905 : + advgetopt::format_usage_string(
1906 : "--license or -L"
1907 : , "print out the license of arguments and exit."
1908 : , 30
1909 : , advgetopt::get_screen_width())
1910 : + advgetopt::format_usage_string(
1911 : "--long-help or -?"
1912 : , "show all the help from all the available options."
1913 : , 30
1914 : , advgetopt::get_screen_width())
1915 : + advgetopt::format_usage_string(
1916 : "--obscure or -o <arg>"
1917 : , "obscure command, hidden by default."
1918 : , 30
1919 : , advgetopt::get_screen_width())
1920 : + advgetopt::format_usage_string(
1921 : "--option-help"
1922 : , "show help from the \"option\" group of options."
1923 : , 30
1924 : , advgetopt::get_screen_width())
1925 : + advgetopt::format_usage_string(
1926 : "--size or -s <arg> (default is \"33\")"
1927 : , "define the size."
1928 : , 30
1929 : , advgetopt::get_screen_width())
1930 : + advgetopt::format_usage_string(
1931 : "--system-help"
1932 : , "show commands and options added by libraries."
1933 : , 30
1934 : , advgetopt::get_screen_width())
1935 : + advgetopt::format_usage_string(
1936 : "--version or -V"
1937 : , "print out the version of arguments and exit."
1938 : , 30
1939 : , advgetopt::get_screen_width())
1940 : + "\n"
1941 : "Copyright matters\n"
1942 : "\n"
1943 : );
1944 1 : }
1945 5 : CATCH_END_SECTION()
1946 :
1947 5 : CATCH_START_SECTION("system_flags_help: check with the --options-help system flag")
1948 : {
1949 1 : advgetopt::option const options[] =
1950 : {
1951 : advgetopt::define_option(
1952 : advgetopt::Name("size")
1953 : , advgetopt::ShortName('s')
1954 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1955 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1956 : , advgetopt::Help("define the size.")
1957 : , advgetopt::DefaultValue("33")
1958 : ),
1959 : advgetopt::define_option(
1960 : advgetopt::Name("obscure")
1961 : , advgetopt::ShortName('o')
1962 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1963 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1964 : , advgetopt::Help("obscure command, hidden by default.")
1965 : ),
1966 : advgetopt::define_option(
1967 : advgetopt::Name("secret")
1968 : , advgetopt::ShortName('S')
1969 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1970 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
1971 : , advgetopt::Help("even more secret command, hidden by default.")
1972 : ),
1973 : advgetopt::end_options()
1974 : };
1975 :
1976 1 : advgetopt::group_description const groups[] =
1977 : {
1978 : advgetopt::define_group(
1979 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
1980 : , advgetopt::GroupName("commands")
1981 : , advgetopt::GroupDescription("Commands:")
1982 : ),
1983 : advgetopt::define_group(
1984 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
1985 : , advgetopt::GroupName("options")
1986 : , advgetopt::GroupDescription("Options:")
1987 : ),
1988 : advgetopt::end_groups()
1989 : };
1990 :
1991 1 : advgetopt::options_environment environment_options;
1992 1 : environment_options.f_project_name = "unittest";
1993 1 : environment_options.f_options = options;
1994 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1995 1 : environment_options.f_help_header = "Usage: test system commands";
1996 1 : environment_options.f_help_footer = "Copyright matters";
1997 1 : environment_options.f_groups = groups;
1998 :
1999 1 : char const * cargv[] =
2000 : {
2001 : "/usr/bin/arguments",
2002 : "--options-help",
2003 : nullptr
2004 : };
2005 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2006 1 : char ** argv = const_cast<char **>(cargv);
2007 :
2008 1 : advgetopt::getopt opt(environment_options, argc, argv);
2009 :
2010 : // check that the result is valid
2011 :
2012 : // an invalid parameter, MUST NOT EXIST
2013 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2014 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2015 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2016 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2017 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2018 :
2019 : // no default
2020 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2021 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2022 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2023 3 : CATCH_REQUIRE(opt.size("--") == 0);
2024 :
2025 : // valid parameter
2026 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2027 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2028 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2029 3 : CATCH_REQUIRE(opt.get_string("size") == "33");
2030 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
2031 3 : CATCH_REQUIRE(opt["size"] == "33");
2032 3 : CATCH_REQUIRE(opt.get_long("size") == 33);
2033 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
2034 3 : CATCH_REQUIRE(opt.has_default("size"));
2035 3 : CATCH_REQUIRE(opt.get_default("size") == "33");
2036 3 : CATCH_REQUIRE(opt.size("size") == 0);
2037 :
2038 : // help parameter
2039 3 : CATCH_REQUIRE(opt.get_option("options-help") != nullptr);
2040 3 : CATCH_REQUIRE(opt.is_defined("options-help"));
2041 3 : CATCH_REQUIRE(opt.get_string("options-help") == "");
2042 3 : CATCH_REQUIRE(opt.get_string("options-help", 0) == "");
2043 3 : CATCH_REQUIRE(opt["options-help"] == "");
2044 3 : CATCH_REQUIRE_FALSE(opt.has_default("options-help"));
2045 3 : CATCH_REQUIRE(opt.get_default("options-help").empty());
2046 3 : CATCH_REQUIRE(opt.size("options-help") == 1);
2047 :
2048 : // other parameters
2049 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2050 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2051 :
2052 : // process system options now
2053 1 : std::stringstream ss;
2054 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2055 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
2056 3 : CATCH_REQUIRE_LONG_STRING(ss.str(),
2057 : "Usage: test system commands\n"
2058 : "\n"
2059 : "Options:\n"
2060 : " --secret or -S <arg> even more secret command, hidden by default.\n"
2061 : "\n"
2062 : "Copyright matters\n"
2063 : "\n"
2064 : );
2065 1 : }
2066 5 : CATCH_END_SECTION()
2067 5 : }
2068 :
2069 :
2070 :
2071 2 : CATCH_TEST_CASE("system_flags_copyright", "[arguments][valid][getopt][system_flags]")
2072 : {
2073 2 : CATCH_START_SECTION("system_flags_copyright: check with the --copyright system flag")
2074 : {
2075 1 : advgetopt::option const options[] =
2076 : {
2077 : advgetopt::define_option(
2078 : advgetopt::Name("size")
2079 : , advgetopt::ShortName('s')
2080 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2081 : , advgetopt::Help("define the size.")
2082 : , advgetopt::DefaultValue("23")
2083 : ),
2084 : advgetopt::end_options()
2085 : };
2086 :
2087 1 : advgetopt::options_environment environment_options;
2088 1 : environment_options.f_project_name = "unittest";
2089 1 : environment_options.f_options = options;
2090 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2091 1 : environment_options.f_copyright = "Copyright (c) " SNAPDEV_STRINGIZE(UTC_BUILD_YEAR) " Made to Order Software Corporation";
2092 :
2093 1 : char const * cargv[] =
2094 : {
2095 : "/usr/bin/arguments",
2096 : "--copyright",
2097 : nullptr
2098 : };
2099 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2100 1 : char ** argv = const_cast<char **>(cargv);
2101 :
2102 1 : advgetopt::getopt opt(environment_options, argc, argv);
2103 :
2104 : // check that the result is valid
2105 :
2106 : // an invalid parameter, MUST NOT EXIST
2107 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2108 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2109 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2110 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2111 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2112 :
2113 : // no default
2114 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2115 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2116 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2117 3 : CATCH_REQUIRE(opt.size("--") == 0);
2118 :
2119 : // valid parameter
2120 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2121 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2122 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2123 3 : CATCH_REQUIRE(opt.get_string("size") == "23");
2124 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "23");
2125 3 : CATCH_REQUIRE(opt["size"] == "23");
2126 3 : CATCH_REQUIRE(opt.get_long("size") == 23);
2127 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 23);
2128 3 : CATCH_REQUIRE(opt.has_default("size"));
2129 3 : CATCH_REQUIRE(opt.get_default("size") == "23");
2130 3 : CATCH_REQUIRE(opt.size("size") == 0);
2131 :
2132 : // copyright parameter
2133 3 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
2134 3 : CATCH_REQUIRE(opt.get_option('C') == opt.get_option("copyright"));
2135 3 : CATCH_REQUIRE(opt.is_defined("copyright"));
2136 3 : CATCH_REQUIRE(opt.get_string("copyright") == "");
2137 3 : CATCH_REQUIRE(opt.get_string("copyright", 0) == "");
2138 3 : CATCH_REQUIRE(opt["copyright"] == "");
2139 3 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
2140 3 : CATCH_REQUIRE(opt.get_default("copyright").empty());
2141 3 : CATCH_REQUIRE(opt.size("copyright") == 1);
2142 :
2143 : // other parameters
2144 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2145 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2146 :
2147 : // process system options now
2148 1 : std::stringstream ss;
2149 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2150 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_COPYRIGHT);
2151 1 : CATCH_REQUIRE(ss.str() == "Copyright (c) " SNAPDEV_STRINGIZE(UTC_BUILD_YEAR) " Made to Order Software Corporation\n");
2152 1 : }
2153 2 : CATCH_END_SECTION()
2154 :
2155 2 : CATCH_START_SECTION("system_flags_copyright: check with the --copyright system flag, without a --copyright on the command line")
2156 : {
2157 1 : advgetopt::option const options[] =
2158 : {
2159 : advgetopt::define_option(
2160 : advgetopt::Name("size")
2161 : , advgetopt::ShortName('s')
2162 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2163 : , advgetopt::Help("define the size.")
2164 : , advgetopt::DefaultValue("53")
2165 : ),
2166 : advgetopt::end_options()
2167 : };
2168 :
2169 1 : advgetopt::options_environment environment_options;
2170 1 : environment_options.f_project_name = "unittest";
2171 1 : environment_options.f_options = options;
2172 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2173 1 : environment_options.f_help_header = "Usage: test system commands";
2174 1 : environment_options.f_help_footer = "Copyright matters";
2175 :
2176 1 : char const * cargv[] =
2177 : {
2178 : "/usr/bin/arguments",
2179 : "--size",
2180 : "1221",
2181 : nullptr
2182 : };
2183 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2184 1 : char ** argv = const_cast<char **>(cargv);
2185 :
2186 1 : advgetopt::getopt opt(environment_options, argc, argv);
2187 :
2188 : // check that the result is valid
2189 :
2190 : // an invalid parameter, MUST NOT EXIST
2191 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2192 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2193 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2194 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2195 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2196 :
2197 : // no default
2198 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2199 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2200 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2201 3 : CATCH_REQUIRE(opt.size("--") == 0);
2202 :
2203 : // valid parameter
2204 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2205 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2206 3 : CATCH_REQUIRE(opt.is_defined("size"));
2207 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2208 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2209 3 : CATCH_REQUIRE(opt["size"] == "1221");
2210 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2211 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2212 3 : CATCH_REQUIRE(opt.has_default("size"));
2213 3 : CATCH_REQUIRE(opt.get_default("size") == "53");
2214 3 : CATCH_REQUIRE(opt.size("size") == 1);
2215 :
2216 : // copyright parameter
2217 3 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
2218 3 : CATCH_REQUIRE(opt.get_option('C') == opt.get_option("copyright"));
2219 3 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
2220 3 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
2221 3 : CATCH_REQUIRE(opt.get_default("copyright").empty());
2222 3 : CATCH_REQUIRE(opt.size("copyright") == 0);
2223 :
2224 : // other parameters
2225 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2226 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2227 :
2228 : // process system options now
2229 1 : std::stringstream ss;
2230 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2231 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2232 1 : CATCH_REQUIRE(ss.str().empty());
2233 1 : }
2234 2 : CATCH_END_SECTION()
2235 2 : }
2236 :
2237 :
2238 :
2239 2 : CATCH_TEST_CASE("system_flags_license", "[arguments][valid][getopt][system_flags]")
2240 : {
2241 2 : CATCH_START_SECTION("system_flags_license: check with the --license system flag")
2242 : {
2243 1 : advgetopt::option const options[] =
2244 : {
2245 : advgetopt::define_option(
2246 : advgetopt::Name("size")
2247 : , advgetopt::ShortName('s')
2248 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2249 : , advgetopt::Help("define the size.")
2250 : , advgetopt::DefaultValue("73")
2251 : ),
2252 : advgetopt::end_options()
2253 : };
2254 :
2255 1 : advgetopt::options_environment environment_options;
2256 1 : environment_options.f_project_name = "unittest";
2257 1 : environment_options.f_options = options;
2258 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2259 1 : environment_options.f_license = "GPL v2";
2260 :
2261 1 : char const * cargv[] =
2262 : {
2263 : "/usr/bin/arguments",
2264 : "--license",
2265 : nullptr
2266 : };
2267 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2268 1 : char ** argv = const_cast<char **>(cargv);
2269 :
2270 1 : advgetopt::getopt opt(environment_options, argc, argv);
2271 :
2272 : // check that the result is valid
2273 :
2274 : // an invalid parameter, MUST NOT EXIST
2275 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2276 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2277 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2278 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2279 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2280 :
2281 : // no default
2282 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2283 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2284 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2285 3 : CATCH_REQUIRE(opt.size("--") == 0);
2286 :
2287 : // valid parameter
2288 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2289 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2290 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2291 3 : CATCH_REQUIRE(opt.get_string("size") == "73");
2292 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "73");
2293 3 : CATCH_REQUIRE(opt["size"] == "73");
2294 3 : CATCH_REQUIRE(opt.get_long("size") == 73);
2295 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 73);
2296 3 : CATCH_REQUIRE(opt.has_default("size"));
2297 3 : CATCH_REQUIRE(opt.get_default("size") == "73");
2298 3 : CATCH_REQUIRE(opt.size("size") == 0);
2299 :
2300 : // license parameter
2301 3 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
2302 3 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
2303 3 : CATCH_REQUIRE(opt.is_defined("license"));
2304 3 : CATCH_REQUIRE(opt.get_string("license") == "");
2305 3 : CATCH_REQUIRE(opt.get_string("license", 0) == "");
2306 3 : CATCH_REQUIRE(opt["license"] == "");
2307 3 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
2308 3 : CATCH_REQUIRE(opt.get_default("license").empty());
2309 3 : CATCH_REQUIRE(opt.size("license") == 1);
2310 :
2311 : // other parameters
2312 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2313 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2314 :
2315 : // process system options now
2316 1 : std::stringstream ss;
2317 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2318 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_LICENSE);
2319 1 : CATCH_REQUIRE(ss.str() == "GPL v2\n");
2320 1 : }
2321 2 : CATCH_END_SECTION()
2322 :
2323 2 : CATCH_START_SECTION("system_flags_license: check with the --license system flag, without a --license on the command line")
2324 : {
2325 1 : advgetopt::option const options[] =
2326 : {
2327 : advgetopt::define_option(
2328 : advgetopt::Name("size")
2329 : , advgetopt::ShortName('s')
2330 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2331 : , advgetopt::Help("define the size.")
2332 : , advgetopt::DefaultValue("103")
2333 : ),
2334 : advgetopt::end_options()
2335 : };
2336 :
2337 1 : advgetopt::options_environment environment_options;
2338 1 : environment_options.f_project_name = "unittest";
2339 1 : environment_options.f_options = options;
2340 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2341 1 : environment_options.f_help_header = "Usage: test system commands";
2342 1 : environment_options.f_help_footer = "Copyright matters";
2343 :
2344 1 : char const * cargv[] =
2345 : {
2346 : "/usr/bin/arguments",
2347 : "--size",
2348 : "1221",
2349 : nullptr
2350 : };
2351 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2352 1 : char ** argv = const_cast<char **>(cargv);
2353 :
2354 1 : advgetopt::getopt opt(environment_options, argc, argv);
2355 :
2356 : // check that the result is valid
2357 :
2358 : // an invalid parameter, MUST NOT EXIST
2359 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2360 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2361 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2362 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2363 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2364 :
2365 : // no default
2366 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2367 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2368 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2369 3 : CATCH_REQUIRE(opt.size("--") == 0);
2370 :
2371 : // valid parameter
2372 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2373 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2374 3 : CATCH_REQUIRE(opt.is_defined("size"));
2375 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2376 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2377 3 : CATCH_REQUIRE(opt["size"] == "1221");
2378 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2379 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2380 3 : CATCH_REQUIRE(opt.has_default("size"));
2381 3 : CATCH_REQUIRE(opt.get_default("size") == "103");
2382 3 : CATCH_REQUIRE(opt.size("size") == 1);
2383 :
2384 : // license parameter
2385 3 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
2386 3 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
2387 3 : CATCH_REQUIRE_FALSE(opt.is_defined("license"));
2388 3 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
2389 3 : CATCH_REQUIRE(opt.get_default("license").empty());
2390 3 : CATCH_REQUIRE(opt.size("license") == 0);
2391 :
2392 : // other parameters
2393 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2394 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2395 :
2396 : // process system options now
2397 1 : std::stringstream ss;
2398 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2399 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2400 1 : CATCH_REQUIRE(ss.str().empty());
2401 1 : }
2402 2 : CATCH_END_SECTION()
2403 2 : }
2404 :
2405 :
2406 :
2407 2 : CATCH_TEST_CASE("system_flags_build_date", "[arguments][valid][getopt][system_flags]")
2408 : {
2409 2 : CATCH_START_SECTION("system_flags_build_date: check with the --build-date system flag")
2410 : {
2411 1 : advgetopt::option const options[] =
2412 : {
2413 : advgetopt::define_option(
2414 : advgetopt::Name("size")
2415 : , advgetopt::ShortName('s')
2416 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2417 : , advgetopt::Help("define the size.")
2418 : , advgetopt::DefaultValue("7301")
2419 : ),
2420 : advgetopt::end_options()
2421 : };
2422 :
2423 1 : advgetopt::options_environment environment_options;
2424 1 : environment_options.f_project_name = "unittest";
2425 1 : environment_options.f_options = options;
2426 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2427 :
2428 1 : char const * cargv[] =
2429 : {
2430 : "/usr/bin/arguments",
2431 : "--build-date",
2432 : nullptr
2433 : };
2434 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2435 1 : char ** argv = const_cast<char **>(cargv);
2436 :
2437 1 : advgetopt::getopt opt(environment_options, argc, argv);
2438 :
2439 : // check that the result is valid
2440 :
2441 : // an invalid parameter, MUST NOT EXIST
2442 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2443 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2444 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2445 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2446 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2447 :
2448 : // no default
2449 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2450 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2451 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2452 3 : CATCH_REQUIRE(opt.size("--") == 0);
2453 :
2454 : // valid parameter
2455 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2456 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2457 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2458 3 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2459 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2460 3 : CATCH_REQUIRE(opt["size"] == "7301");
2461 3 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2462 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2463 3 : CATCH_REQUIRE(opt.has_default("size"));
2464 3 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2465 3 : CATCH_REQUIRE(opt.size("size") == 0);
2466 :
2467 : // build-date parameter
2468 3 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
2469 3 : CATCH_REQUIRE(opt.is_defined("build-date"));
2470 3 : CATCH_REQUIRE(opt.get_string("build-date") == "");
2471 3 : CATCH_REQUIRE(opt.get_string("build-date", 0) == "");
2472 3 : CATCH_REQUIRE(opt["build-date"] == "");
2473 3 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
2474 3 : CATCH_REQUIRE(opt.get_default("build-date").empty());
2475 3 : CATCH_REQUIRE(opt.size("build-date") == 1);
2476 :
2477 : // other parameters
2478 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2479 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2480 :
2481 : // process system options now
2482 1 : std::stringstream ss;
2483 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2484 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_BUILD_DATE);
2485 3 : CATCH_REQUIRE(ss.str() == "Built on "
2486 : + std::string(environment_options.f_build_date)
2487 : + " at "
2488 : + environment_options.f_build_time
2489 : + "\n");
2490 1 : }
2491 2 : CATCH_END_SECTION()
2492 :
2493 2 : CATCH_START_SECTION("system_flags_build_date: check with the --build-date system flag, without a --build-date on the command line")
2494 : {
2495 1 : advgetopt::option const options[] =
2496 : {
2497 : advgetopt::define_option(
2498 : advgetopt::Name("size")
2499 : , advgetopt::ShortName('s')
2500 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2501 : , advgetopt::Help("define the size.")
2502 : , advgetopt::DefaultValue("103")
2503 : ),
2504 : advgetopt::end_options()
2505 : };
2506 :
2507 1 : advgetopt::options_environment environment_options;
2508 1 : environment_options.f_project_name = "unittest";
2509 1 : environment_options.f_options = options;
2510 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2511 1 : environment_options.f_help_header = "Usage: test system commands";
2512 1 : environment_options.f_help_footer = "Copyright matters";
2513 :
2514 1 : char const * cargv[] =
2515 : {
2516 : "/usr/bin/arguments",
2517 : "--size",
2518 : "1221",
2519 : nullptr
2520 : };
2521 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2522 1 : char ** argv = const_cast<char **>(cargv);
2523 :
2524 1 : advgetopt::getopt opt(environment_options, argc, argv);
2525 :
2526 : // check that the result is valid
2527 :
2528 : // an invalid parameter, MUST NOT EXIST
2529 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2530 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2531 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2532 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2533 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2534 :
2535 : // no default
2536 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2537 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2538 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2539 3 : CATCH_REQUIRE(opt.size("--") == 0);
2540 :
2541 : // valid parameter
2542 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2543 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2544 3 : CATCH_REQUIRE(opt.is_defined("size"));
2545 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2546 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2547 3 : CATCH_REQUIRE(opt["size"] == "1221");
2548 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2549 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2550 3 : CATCH_REQUIRE(opt.has_default("size"));
2551 3 : CATCH_REQUIRE(opt.get_default("size") == "103");
2552 3 : CATCH_REQUIRE(opt.size("size") == 1);
2553 :
2554 : // build-date parameter
2555 3 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
2556 3 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
2557 3 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
2558 3 : CATCH_REQUIRE(opt.get_default("build-date").empty());
2559 3 : CATCH_REQUIRE(opt.size("build-date") == 0);
2560 :
2561 : // other parameters
2562 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2563 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2564 :
2565 : // process system options now
2566 1 : std::stringstream ss;
2567 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2568 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2569 1 : CATCH_REQUIRE(ss.str().empty());
2570 1 : }
2571 2 : CATCH_END_SECTION()
2572 2 : }
2573 :
2574 :
2575 :
2576 4 : CATCH_TEST_CASE("system_flags_environment_variable_name", "[arguments][valid][getopt][system_flags]")
2577 : {
2578 4 : CATCH_START_SECTION("system_flags_environment_variable_name: check with the --environment-variable-name system flag")
2579 : {
2580 1 : advgetopt::option const options[] =
2581 : {
2582 : advgetopt::define_option(
2583 : advgetopt::Name("size")
2584 : , advgetopt::ShortName('s')
2585 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2586 : , advgetopt::Help("define the size.")
2587 : , advgetopt::DefaultValue("7301")
2588 : ),
2589 : advgetopt::end_options()
2590 : };
2591 :
2592 1 : advgetopt::options_environment environment_options;
2593 1 : environment_options.f_project_name = "unittest";
2594 1 : environment_options.f_options = options;
2595 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2596 1 : environment_options.f_environment_variable_name = "ADVGETOPT_OPTIONS";
2597 :
2598 1 : char const * cargv[] =
2599 : {
2600 : "/usr/bin/arguments",
2601 : "--environment-variable-name",
2602 : nullptr
2603 : };
2604 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2605 1 : char ** argv = const_cast<char **>(cargv);
2606 :
2607 1 : advgetopt::getopt opt(environment_options, argc, argv);
2608 :
2609 : // check that the result is valid
2610 :
2611 : // an invalid parameter, MUST NOT EXIST
2612 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2613 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2614 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2615 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2616 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2617 :
2618 : // no default
2619 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2620 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2621 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2622 3 : CATCH_REQUIRE(opt.size("--") == 0);
2623 :
2624 : // valid parameter
2625 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2626 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2627 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2628 3 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2629 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2630 3 : CATCH_REQUIRE(opt["size"] == "7301");
2631 3 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2632 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2633 3 : CATCH_REQUIRE(opt.has_default("size"));
2634 3 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2635 3 : CATCH_REQUIRE(opt.size("size") == 0);
2636 :
2637 : // environment-variable-name parameter
2638 3 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2639 3 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
2640 3 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
2641 3 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
2642 3 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
2643 3 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2644 3 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2645 3 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
2646 :
2647 : // other parameters
2648 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2649 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2650 :
2651 : // process system options now
2652 1 : std::stringstream ss;
2653 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2654 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
2655 1 : CATCH_REQUIRE(ss.str() == "ADVGETOPT_OPTIONS\n");
2656 1 : }
2657 4 : CATCH_END_SECTION()
2658 :
2659 4 : CATCH_START_SECTION("system_flags_environment_variable_name: check with the --environment-variable-name system flag with nullptr")
2660 : {
2661 1 : advgetopt::option const options[] =
2662 : {
2663 : advgetopt::define_option(
2664 : advgetopt::Name("size")
2665 : , advgetopt::ShortName('s')
2666 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2667 : , advgetopt::Help("define the size.")
2668 : , advgetopt::DefaultValue("7301")
2669 : ),
2670 : advgetopt::end_options()
2671 : };
2672 :
2673 1 : advgetopt::options_environment environment_options;
2674 1 : environment_options.f_project_name = "unittest";
2675 1 : environment_options.f_options = options;
2676 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2677 1 : environment_options.f_environment_variable_name = nullptr;
2678 :
2679 1 : char const * cargv[] =
2680 : {
2681 : "/usr/bin/arguments",
2682 : "--environment-variable-name",
2683 : nullptr
2684 : };
2685 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2686 1 : char ** argv = const_cast<char **>(cargv);
2687 :
2688 1 : advgetopt::getopt opt(environment_options, argc, argv);
2689 :
2690 : // check that the result is valid
2691 :
2692 : // an invalid parameter, MUST NOT EXIST
2693 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2694 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2695 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2696 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2697 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2698 :
2699 : // no default
2700 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2701 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2702 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2703 3 : CATCH_REQUIRE(opt.size("--") == 0);
2704 :
2705 : // valid parameter
2706 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2707 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2708 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2709 3 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2710 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2711 3 : CATCH_REQUIRE(opt["size"] == "7301");
2712 3 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2713 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2714 3 : CATCH_REQUIRE(opt.has_default("size"));
2715 3 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2716 3 : CATCH_REQUIRE(opt.size("size") == 0);
2717 :
2718 : // environment-variable-name parameter
2719 3 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2720 3 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
2721 3 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
2722 3 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
2723 3 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
2724 3 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2725 3 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2726 3 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
2727 :
2728 : // other parameters
2729 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2730 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2731 :
2732 : // process system options now
2733 1 : std::stringstream ss;
2734 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2735 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
2736 1 : CATCH_REQUIRE(ss.str() == "unittest does not support an environment variable.\n");
2737 1 : }
2738 4 : CATCH_END_SECTION()
2739 :
2740 4 : CATCH_START_SECTION("system_flags_environment_variable_name: check with the --environment-variable-name system flag with \"\"")
2741 : {
2742 1 : advgetopt::option const options[] =
2743 : {
2744 : advgetopt::define_option(
2745 : advgetopt::Name("size")
2746 : , advgetopt::ShortName('s')
2747 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2748 : , advgetopt::Help("define the size.")
2749 : , advgetopt::DefaultValue("7301")
2750 : ),
2751 : advgetopt::end_options()
2752 : };
2753 :
2754 1 : advgetopt::options_environment environment_options;
2755 1 : environment_options.f_project_name = "unittest";
2756 1 : environment_options.f_options = options;
2757 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2758 1 : environment_options.f_environment_variable_name = "";
2759 :
2760 1 : char const * cargv[] =
2761 : {
2762 : "/usr/bin/arguments",
2763 : "--environment-variable-name",
2764 : nullptr
2765 : };
2766 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2767 1 : char ** argv = const_cast<char **>(cargv);
2768 :
2769 1 : advgetopt::getopt opt(environment_options, argc, argv);
2770 :
2771 : // check that the result is valid
2772 :
2773 : // an invalid parameter, MUST NOT EXIST
2774 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2775 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2776 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2777 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2778 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2779 :
2780 : // no default
2781 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2782 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2783 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2784 3 : CATCH_REQUIRE(opt.size("--") == 0);
2785 :
2786 : // valid parameter
2787 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2788 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2789 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2790 3 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2791 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2792 3 : CATCH_REQUIRE(opt["size"] == "7301");
2793 3 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2794 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2795 3 : CATCH_REQUIRE(opt.has_default("size"));
2796 3 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2797 3 : CATCH_REQUIRE(opt.size("size") == 0);
2798 :
2799 : // environment-variable-name parameter
2800 3 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2801 3 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
2802 3 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
2803 3 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
2804 3 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
2805 3 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2806 3 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2807 3 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
2808 :
2809 : // other parameters
2810 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2811 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2812 :
2813 : // process system options now
2814 1 : std::stringstream ss;
2815 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2816 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
2817 1 : CATCH_REQUIRE(ss.str() == "unittest does not support an environment variable.\n");
2818 1 : }
2819 4 : CATCH_END_SECTION()
2820 :
2821 4 : CATCH_START_SECTION("system_flags_environment_variable_name: check with the --environment-variable-name system flag, without a --environment-variable-name on the command line")
2822 : {
2823 1 : advgetopt::option const options[] =
2824 : {
2825 : advgetopt::define_option(
2826 : advgetopt::Name("size")
2827 : , advgetopt::ShortName('s')
2828 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2829 : , advgetopt::Help("define the size.")
2830 : , advgetopt::DefaultValue("103")
2831 : ),
2832 : advgetopt::end_options()
2833 : };
2834 :
2835 1 : advgetopt::options_environment environment_options;
2836 1 : environment_options.f_project_name = "unittest";
2837 1 : environment_options.f_options = options;
2838 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2839 1 : environment_options.f_help_header = "Usage: test system commands";
2840 1 : environment_options.f_help_footer = "Copyright matters";
2841 :
2842 1 : char const * cargv[] =
2843 : {
2844 : "/usr/bin/arguments",
2845 : "--size",
2846 : "1221",
2847 : nullptr
2848 : };
2849 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2850 1 : char ** argv = const_cast<char **>(cargv);
2851 :
2852 1 : advgetopt::getopt opt(environment_options, argc, argv);
2853 :
2854 : // check that the result is valid
2855 :
2856 : // an invalid parameter, MUST NOT EXIST
2857 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2858 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2859 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2860 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2861 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2862 :
2863 : // no default
2864 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2865 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2866 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2867 3 : CATCH_REQUIRE(opt.size("--") == 0);
2868 :
2869 : // valid parameter
2870 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2871 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2872 3 : CATCH_REQUIRE(opt.is_defined("size"));
2873 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2874 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2875 3 : CATCH_REQUIRE(opt["size"] == "1221");
2876 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2877 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2878 3 : CATCH_REQUIRE(opt.has_default("size"));
2879 3 : CATCH_REQUIRE(opt.get_default("size") == "103");
2880 3 : CATCH_REQUIRE(opt.size("size") == 1);
2881 :
2882 : // environment-variable-name parameter
2883 3 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2884 3 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
2885 3 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2886 3 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2887 3 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
2888 :
2889 : // other parameters
2890 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2891 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2892 :
2893 : // process system options now
2894 1 : std::stringstream ss;
2895 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2896 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2897 1 : CATCH_REQUIRE(ss.str().empty());
2898 1 : }
2899 4 : CATCH_END_SECTION()
2900 4 : }
2901 :
2902 :
2903 :
2904 4 : CATCH_TEST_CASE("system_flags_configuration_filenames", "[arguments][valid][getopt][system_flags]")
2905 : {
2906 4 : CATCH_START_SECTION("system_flags_configuration_filenames: check with the --configuration-filenames system flag")
2907 : {
2908 5 : snapdev::safe_setenv env("HOME", "/home/advgetopt");
2909 :
2910 1 : advgetopt::option const options[] =
2911 : {
2912 : advgetopt::define_option(
2913 : advgetopt::Name("size")
2914 : , advgetopt::ShortName('s')
2915 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2916 : , advgetopt::Help("define the size.")
2917 : , advgetopt::DefaultValue("3101")
2918 : ),
2919 : advgetopt::end_options()
2920 : };
2921 :
2922 1 : char const * confs[] =
2923 : {
2924 : ".config/file.mdi",
2925 : "/etc/snapwebsites/server.conf",
2926 : "~/.config/advgetopt/snap.conf",
2927 : nullptr
2928 : };
2929 1 : char const * dirs[] =
2930 : {
2931 : ".config",
2932 : "/etc/secret",
2933 : "~/.config/snapwebsites",
2934 : nullptr
2935 : };
2936 :
2937 1 : advgetopt::options_environment environment_options;
2938 1 : environment_options.f_project_name = "unittest";
2939 1 : environment_options.f_options = options;
2940 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2941 1 : environment_options.f_configuration_files = confs;
2942 1 : environment_options.f_configuration_filename = "snapdb.conf";
2943 1 : environment_options.f_configuration_directories = dirs;
2944 :
2945 1 : char const * cargv[] =
2946 : {
2947 : "/usr/bin/arguments",
2948 : "--configuration-filenames",
2949 : nullptr
2950 : };
2951 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2952 1 : char ** argv = const_cast<char **>(cargv);
2953 :
2954 1 : advgetopt::getopt opt(environment_options, argc, argv);
2955 :
2956 : // check that the result is valid
2957 :
2958 : // an invalid parameter, MUST NOT EXIST
2959 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2960 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2961 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2962 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2963 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2964 :
2965 : // no default
2966 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2967 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2968 3 : CATCH_REQUIRE(opt.get_default("--").empty());
2969 3 : CATCH_REQUIRE(opt.size("--") == 0);
2970 :
2971 : // valid parameter
2972 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2973 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2974 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2975 3 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2976 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2977 3 : CATCH_REQUIRE(opt["size"] == "3101");
2978 3 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2979 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2980 3 : CATCH_REQUIRE(opt.has_default("size"));
2981 3 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2982 3 : CATCH_REQUIRE(opt.size("size") == 0);
2983 :
2984 : // configuration-filenames parameter
2985 3 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2986 3 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2987 3 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2988 3 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2989 3 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2990 3 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2991 3 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2992 3 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2993 :
2994 : // other parameters
2995 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2996 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2997 :
2998 : // process system options now
2999 1 : std::stringstream ss;
3000 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3001 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES);
3002 1 : CATCH_REQUIRE(getenv("HOME") != nullptr);
3003 3 : std::string const home(getenv("HOME"));
3004 1 : CATCH_REQUIRE(ss.str() ==
3005 : "Configuration filenames:\n"
3006 : " . .config/snapdb.conf\n"
3007 : " . .config/unittest.d/50-snapdb.conf\n"
3008 : " . /etc/secret/snapdb.conf\n"
3009 : " . /etc/secret/unittest.d/50-snapdb.conf\n"
3010 : " . " + home + "/.config/snapwebsites/snapdb.conf\n"
3011 : " . .config/file.mdi\n"
3012 : " . .config/unittest.d/50-file.mdi\n"
3013 : " . /etc/snapwebsites/server.conf\n"
3014 : " . /etc/snapwebsites/unittest.d/50-server.conf\n"
3015 : " . " + home + "/.config/advgetopt/snap.conf\n"
3016 : );
3017 1 : }
3018 4 : CATCH_END_SECTION()
3019 :
3020 4 : CATCH_START_SECTION("system_flags_configuration_filenames: check with the --configuration-filenames system flag with --config-dir too")
3021 : {
3022 5 : snapdev::safe_setenv env("HOME", "/home/advgetopt");
3023 :
3024 1 : advgetopt::option const options[] =
3025 : {
3026 : advgetopt::define_option(
3027 : advgetopt::Name("size")
3028 : , advgetopt::ShortName('s')
3029 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3030 : , advgetopt::Help("define the size.")
3031 : , advgetopt::DefaultValue("3101")
3032 : ),
3033 : advgetopt::end_options()
3034 : };
3035 :
3036 1 : char const * confs[] =
3037 : {
3038 : ".config/file.mdi",
3039 : "/etc/snapwebsites/server.conf",
3040 : "~/.config/advgetopt/snap.conf",
3041 : nullptr
3042 : };
3043 1 : char const * dirs[] =
3044 : {
3045 : ".config",
3046 : "/etc/secret",
3047 : "~/.config/snapwebsites",
3048 : nullptr
3049 : };
3050 :
3051 1 : advgetopt::options_environment environment_options;
3052 1 : environment_options.f_project_name = "unittest";
3053 1 : environment_options.f_options = options;
3054 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3055 1 : environment_options.f_configuration_files = confs;
3056 1 : environment_options.f_configuration_filename = "snapdb.conf";
3057 1 : environment_options.f_configuration_directories = dirs;
3058 :
3059 1 : char const * cargv[] =
3060 : {
3061 : "/usr/bin/arguments",
3062 : "--config-dir",
3063 : "/var/lib/advgetopt",
3064 : "--configuration-filenames",
3065 : "--config-dir",
3066 : "/opt/config",
3067 : nullptr
3068 : };
3069 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3070 1 : char ** argv = const_cast<char **>(cargv);
3071 :
3072 1 : advgetopt::getopt opt(environment_options, argc, argv);
3073 :
3074 : // check that the result is valid
3075 :
3076 : // an invalid parameter, MUST NOT EXIST
3077 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3078 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3079 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3080 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3081 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3082 :
3083 : // no default
3084 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3085 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3086 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3087 3 : CATCH_REQUIRE(opt.size("--") == 0);
3088 :
3089 : // valid parameter
3090 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3091 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
3092 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3093 3 : CATCH_REQUIRE(opt.get_string("size") == "3101");
3094 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
3095 3 : CATCH_REQUIRE(opt["size"] == "3101");
3096 3 : CATCH_REQUIRE(opt.get_long("size") == 3101);
3097 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
3098 3 : CATCH_REQUIRE(opt.has_default("size"));
3099 3 : CATCH_REQUIRE(opt.get_default("size") == "3101");
3100 3 : CATCH_REQUIRE(opt.size("size") == 0);
3101 :
3102 : // configuration-filenames parameter
3103 3 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
3104 3 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
3105 3 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
3106 3 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
3107 3 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
3108 3 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
3109 3 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
3110 3 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
3111 :
3112 : // other parameters
3113 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3114 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3115 :
3116 : // process system options now
3117 1 : std::stringstream ss;
3118 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3119 1 : CATCH_REQUIRE(result == (advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES
3120 : | advgetopt::SYSTEM_OPTION_CONFIG_DIR));
3121 1 : CATCH_REQUIRE(getenv("HOME") != nullptr);
3122 3 : std::string const home(getenv("HOME"));
3123 1 : CATCH_REQUIRE(ss.str() ==
3124 : "Configuration filenames:\n"
3125 : " . /var/lib/advgetopt/snapdb.conf\n"
3126 : " . /var/lib/advgetopt/unittest.d/50-snapdb.conf\n"
3127 : " . /opt/config/snapdb.conf\n"
3128 : " . /opt/config/unittest.d/50-snapdb.conf\n"
3129 : " . .config/snapdb.conf\n"
3130 : " . .config/unittest.d/50-snapdb.conf\n"
3131 : " . /etc/secret/snapdb.conf\n"
3132 : " . /etc/secret/unittest.d/50-snapdb.conf\n"
3133 : " . " + home + "/.config/snapwebsites/snapdb.conf\n"
3134 : " . .config/file.mdi\n"
3135 : " . .config/unittest.d/50-file.mdi\n"
3136 : " . /etc/snapwebsites/server.conf\n"
3137 : " . /etc/snapwebsites/unittest.d/50-server.conf\n"
3138 : " . " + home + "/.config/advgetopt/snap.conf\n"
3139 : );
3140 1 : }
3141 4 : CATCH_END_SECTION()
3142 :
3143 4 : CATCH_START_SECTION("system_flags_configuration_filenames: check with the --configuration-filenames system flag without any configuration files")
3144 : {
3145 1 : advgetopt::option const options[] =
3146 : {
3147 : advgetopt::define_option(
3148 : advgetopt::Name("size")
3149 : , advgetopt::ShortName('s')
3150 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3151 : , advgetopt::Help("define the size.")
3152 : , advgetopt::DefaultValue("3101")
3153 : ),
3154 : advgetopt::end_options()
3155 : };
3156 :
3157 1 : advgetopt::options_environment environment_options;
3158 1 : environment_options.f_project_name = "unittest";
3159 1 : environment_options.f_options = options;
3160 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3161 :
3162 1 : char const * cargv[] =
3163 : {
3164 : "/usr/bin/arguments",
3165 : "--configuration-filenames",
3166 : nullptr
3167 : };
3168 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3169 1 : char ** argv = const_cast<char **>(cargv);
3170 :
3171 1 : advgetopt::getopt opt(environment_options, argc, argv);
3172 :
3173 : // check that the result is valid
3174 :
3175 : // an invalid parameter, MUST NOT EXIST
3176 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3177 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3178 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3179 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3180 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3181 :
3182 : // no default
3183 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3184 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3185 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3186 3 : CATCH_REQUIRE(opt.size("--") == 0);
3187 :
3188 : // valid parameter
3189 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3190 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
3191 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3192 3 : CATCH_REQUIRE(opt.get_string("size") == "3101");
3193 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
3194 3 : CATCH_REQUIRE(opt["size"] == "3101");
3195 3 : CATCH_REQUIRE(opt.get_long("size") == 3101);
3196 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
3197 3 : CATCH_REQUIRE(opt.has_default("size"));
3198 3 : CATCH_REQUIRE(opt.get_default("size") == "3101");
3199 3 : CATCH_REQUIRE(opt.size("size") == 0);
3200 :
3201 : // configuration-filenames parameter
3202 3 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
3203 3 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
3204 3 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
3205 3 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
3206 3 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
3207 3 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
3208 3 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
3209 3 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
3210 :
3211 : // other parameters
3212 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3213 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3214 :
3215 : // process system options now
3216 1 : std::stringstream ss;
3217 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3218 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES);
3219 1 : CATCH_REQUIRE(ss.str() == "unittest does not support configuration files.\n");
3220 1 : }
3221 4 : CATCH_END_SECTION()
3222 :
3223 4 : CATCH_START_SECTION("system_flags_configuration_filenames: check with the --configuration-filenames system flag, without a --configuration-filenames on the command line")
3224 : {
3225 1 : advgetopt::option const options[] =
3226 : {
3227 : advgetopt::define_option(
3228 : advgetopt::Name("size")
3229 : , advgetopt::ShortName('s')
3230 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3231 : , advgetopt::Help("define the size.")
3232 : , advgetopt::DefaultValue("193")
3233 : ),
3234 : advgetopt::end_options()
3235 : };
3236 :
3237 1 : char const * confs[] =
3238 : {
3239 : ".config/file.mdi",
3240 : "/etc/snapwebsites/server.conf",
3241 : "~/.config/advgetopt/snap.conf",
3242 : nullptr
3243 : };
3244 1 : char const * dirs[] =
3245 : {
3246 : ".config",
3247 : "/etc/secret",
3248 : "~/.config/snapwebsites",
3249 : nullptr
3250 : };
3251 :
3252 1 : advgetopt::options_environment environment_options;
3253 1 : environment_options.f_project_name = "unittest";
3254 1 : environment_options.f_options = options;
3255 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3256 1 : environment_options.f_help_header = "Usage: test system commands";
3257 1 : environment_options.f_help_footer = "Copyright matters";
3258 1 : environment_options.f_configuration_files = confs;
3259 1 : environment_options.f_configuration_filename = "snapdb.conf";
3260 1 : environment_options.f_configuration_directories = dirs;
3261 :
3262 1 : char const * cargv[] =
3263 : {
3264 : "/usr/bin/arguments",
3265 : "--size",
3266 : "1221",
3267 : nullptr
3268 : };
3269 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3270 1 : char ** argv = const_cast<char **>(cargv);
3271 :
3272 1 : advgetopt::getopt opt(environment_options, argc, argv);
3273 :
3274 : // check that the result is valid
3275 :
3276 : // an invalid parameter, MUST NOT EXIST
3277 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3278 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3279 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3280 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3281 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3282 :
3283 : // no default
3284 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3285 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3286 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3287 3 : CATCH_REQUIRE(opt.size("--") == 0);
3288 :
3289 : // valid parameter
3290 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3291 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
3292 3 : CATCH_REQUIRE(opt.is_defined("size"));
3293 3 : CATCH_REQUIRE(opt.get_string("size") == "1221");
3294 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
3295 3 : CATCH_REQUIRE(opt["size"] == "1221");
3296 3 : CATCH_REQUIRE(opt.get_long("size") == 1221);
3297 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
3298 3 : CATCH_REQUIRE(opt.has_default("size"));
3299 3 : CATCH_REQUIRE(opt.get_default("size") == "193");
3300 3 : CATCH_REQUIRE(opt.size("size") == 1);
3301 :
3302 : // configuration-filenames parameter
3303 3 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
3304 3 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
3305 3 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
3306 3 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
3307 3 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
3308 :
3309 : // other parameters
3310 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3311 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3312 :
3313 : // process system options now
3314 1 : std::stringstream ss;
3315 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3316 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
3317 1 : CATCH_REQUIRE(ss.str().empty());
3318 1 : }
3319 4 : CATCH_END_SECTION()
3320 4 : }
3321 :
3322 :
3323 :
3324 3 : CATCH_TEST_CASE("system_flags_path_to_option_definitions", "[arguments][valid][getopt][system_flags]")
3325 : {
3326 3 : CATCH_START_SECTION("system_flags_path_to_option_definitions: check with the --path-to-option-definitions system flag (Default)")
3327 : {
3328 1 : advgetopt::option const options[] =
3329 : {
3330 : advgetopt::define_option(
3331 : advgetopt::Name("size")
3332 : , advgetopt::ShortName('s')
3333 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3334 : , advgetopt::Help("define the size.")
3335 : , advgetopt::DefaultValue("7301")
3336 : ),
3337 : advgetopt::end_options()
3338 : };
3339 :
3340 1 : advgetopt::options_environment environment_options;
3341 1 : environment_options.f_project_name = "unittest";
3342 1 : environment_options.f_options = options;
3343 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3344 :
3345 1 : char const * cargv[] =
3346 : {
3347 : "/usr/bin/arguments",
3348 : "--path-to-option-definitions",
3349 : nullptr
3350 : };
3351 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3352 1 : char ** argv = const_cast<char **>(cargv);
3353 :
3354 1 : advgetopt::getopt opt(environment_options, argc, argv);
3355 :
3356 : // check that the result is valid
3357 :
3358 : // an invalid parameter, MUST NOT EXIST
3359 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3360 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3361 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3362 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3363 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3364 :
3365 : // no default
3366 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3367 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3368 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3369 3 : CATCH_REQUIRE(opt.size("--") == 0);
3370 :
3371 : // valid parameter
3372 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3373 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
3374 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3375 3 : CATCH_REQUIRE(opt.get_string("size") == "7301");
3376 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
3377 3 : CATCH_REQUIRE(opt["size"] == "7301");
3378 3 : CATCH_REQUIRE(opt.get_long("size") == 7301);
3379 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
3380 3 : CATCH_REQUIRE(opt.has_default("size"));
3381 3 : CATCH_REQUIRE(opt.get_default("size") == "7301");
3382 3 : CATCH_REQUIRE(opt.size("size") == 0);
3383 :
3384 : // path-to-option-definitions parameter
3385 3 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
3386 3 : CATCH_REQUIRE(opt.is_defined("path-to-option-definitions"));
3387 3 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions") == "");
3388 3 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions", 0) == "");
3389 3 : CATCH_REQUIRE(opt["path-to-option-definitions"] == "");
3390 3 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
3391 3 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
3392 3 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 1);
3393 :
3394 : // other parameters
3395 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3396 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3397 :
3398 : // process system options now
3399 1 : std::stringstream ss;
3400 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3401 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS);
3402 1 : CATCH_REQUIRE(ss.str() == "/usr/share/advgetopt/options/\n");
3403 1 : }
3404 3 : CATCH_END_SECTION()
3405 :
3406 3 : CATCH_START_SECTION("system_flags_path_to_option_definitions: check with the --path-to-option-definitions system flag (Specified)")
3407 : {
3408 1 : advgetopt::option const options[] =
3409 : {
3410 : advgetopt::define_option(
3411 : advgetopt::Name("size")
3412 : , advgetopt::ShortName('s')
3413 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3414 : , advgetopt::Help("define the size.")
3415 : , advgetopt::DefaultValue("7301")
3416 : ),
3417 : advgetopt::end_options()
3418 : };
3419 :
3420 1 : advgetopt::options_environment environment_options;
3421 1 : environment_options.f_project_name = "unittest";
3422 1 : environment_options.f_options = options;
3423 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3424 1 : environment_options.f_options_files_directory = "/opt/advgetopt/configs";
3425 :
3426 1 : char const * cargv[] =
3427 : {
3428 : "/usr/bin/arguments",
3429 : "--path-to-option-definitions",
3430 : nullptr
3431 : };
3432 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3433 1 : char ** argv = const_cast<char **>(cargv);
3434 :
3435 1 : advgetopt::getopt opt(environment_options, argc, argv);
3436 :
3437 : // check that the result is valid
3438 :
3439 : // an invalid parameter, MUST NOT EXIST
3440 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3441 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3442 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3443 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3444 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3445 :
3446 : // no default
3447 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3448 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3449 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3450 3 : CATCH_REQUIRE(opt.size("--") == 0);
3451 :
3452 : // valid parameter
3453 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3454 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
3455 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3456 3 : CATCH_REQUIRE(opt.get_string("size") == "7301");
3457 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
3458 3 : CATCH_REQUIRE(opt["size"] == "7301");
3459 3 : CATCH_REQUIRE(opt.get_long("size") == 7301);
3460 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
3461 3 : CATCH_REQUIRE(opt.has_default("size"));
3462 3 : CATCH_REQUIRE(opt.get_default("size") == "7301");
3463 3 : CATCH_REQUIRE(opt.size("size") == 0);
3464 :
3465 : // path-to-option-definitions parameter
3466 3 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
3467 3 : CATCH_REQUIRE(opt.is_defined("path-to-option-definitions"));
3468 3 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions") == "");
3469 3 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions", 0) == "");
3470 3 : CATCH_REQUIRE(opt["path-to-option-definitions"] == "");
3471 3 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
3472 3 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
3473 3 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 1);
3474 :
3475 : // other parameters
3476 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3477 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3478 :
3479 : // process system options now
3480 1 : std::stringstream ss;
3481 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3482 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS);
3483 1 : CATCH_REQUIRE(ss.str() == "/opt/advgetopt/configs/\n");
3484 1 : }
3485 3 : CATCH_END_SECTION()
3486 :
3487 3 : CATCH_START_SECTION("system_flags_path_to_option_definitions: check with the --path-to-option-definitions system flag, without a --path-to-option-definitions on the command line")
3488 : {
3489 1 : advgetopt::option const options[] =
3490 : {
3491 : advgetopt::define_option(
3492 : advgetopt::Name("size")
3493 : , advgetopt::ShortName('s')
3494 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3495 : , advgetopt::Help("define the size.")
3496 : , advgetopt::DefaultValue("303")
3497 : ),
3498 : advgetopt::end_options()
3499 : };
3500 :
3501 1 : advgetopt::options_environment environment_options;
3502 1 : environment_options.f_project_name = "unittest";
3503 1 : environment_options.f_options = options;
3504 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3505 1 : environment_options.f_help_header = "Usage: test system commands";
3506 1 : environment_options.f_help_footer = "Copyright matters";
3507 :
3508 1 : char const * cargv[] =
3509 : {
3510 : "/usr/bin/arguments",
3511 : "--size",
3512 : "1919",
3513 : nullptr
3514 : };
3515 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3516 1 : char ** argv = const_cast<char **>(cargv);
3517 :
3518 1 : advgetopt::getopt opt(environment_options, argc, argv);
3519 :
3520 : // check that the result is valid
3521 :
3522 : // an invalid parameter, MUST NOT EXIST
3523 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3524 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3525 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3526 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3527 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3528 :
3529 : // no default
3530 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3531 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3532 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3533 3 : CATCH_REQUIRE(opt.size("--") == 0);
3534 :
3535 : // valid parameter
3536 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3537 3 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
3538 3 : CATCH_REQUIRE(opt.is_defined("size"));
3539 3 : CATCH_REQUIRE(opt.get_string("size") == "1919");
3540 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "1919");
3541 3 : CATCH_REQUIRE(opt["size"] == "1919");
3542 3 : CATCH_REQUIRE(opt.get_long("size") == 1919);
3543 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 1919);
3544 3 : CATCH_REQUIRE(opt.has_default("size"));
3545 3 : CATCH_REQUIRE(opt.get_default("size") == "303");
3546 3 : CATCH_REQUIRE(opt.size("size") == 1);
3547 :
3548 : // environment-variable-name parameter
3549 3 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
3550 3 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
3551 3 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
3552 3 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
3553 3 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
3554 :
3555 : // other parameters
3556 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3557 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3558 :
3559 : // process system options now
3560 1 : std::stringstream ss;
3561 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
3562 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
3563 1 : CATCH_REQUIRE(ss.str().empty());
3564 1 : }
3565 3 : CATCH_END_SECTION()
3566 3 : }
3567 :
3568 :
3569 :
3570 :
3571 :
3572 :
3573 :
3574 :
3575 4 : CATCH_TEST_CASE("invalid_option_name", "[arguments][invalid][getopt]")
3576 : {
3577 4 : CATCH_START_SECTION("invalid_option_name: verify that asking for the string of a non-existant option fails")
3578 : {
3579 1 : advgetopt::options_environment environment_options;
3580 1 : environment_options.f_project_name = "unittest";
3581 1 : environment_options.f_options = nullptr;
3582 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3583 1 : environment_options.f_help_header = "Usage: test get_string() functions";
3584 :
3585 1 : advgetopt::getopt opt(environment_options);
3586 :
3587 1 : char const * cargv[] =
3588 : {
3589 : "tests/options-parser",
3590 : "--license",
3591 : nullptr
3592 : };
3593 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3594 1 : char ** argv = const_cast<char **>(cargv);
3595 :
3596 1 : opt.finish_parsing(argc, argv);
3597 :
3598 8 : CATCH_REQUIRE_THROWS_MATCHES(
3599 : opt.get_string("non-existant")
3600 : , advgetopt::getopt_logic_error
3601 : , Catch::Matchers::ExceptionMessage(
3602 : "getopt_logic_error: there is no --non-existant option defined."));
3603 :
3604 8 : CATCH_REQUIRE_THROWS_MATCHES(
3605 : opt.get_string("non-existant", 0)
3606 : , advgetopt::getopt_logic_error
3607 : , Catch::Matchers::ExceptionMessage(
3608 : "getopt_logic_error: there is no --non-existant option defined."));
3609 :
3610 8 : CATCH_REQUIRE_THROWS_MATCHES(
3611 : opt.get_string("non-existant", 1)
3612 : , advgetopt::getopt_logic_error
3613 : , Catch::Matchers::ExceptionMessage(
3614 : "getopt_logic_error: there is no --non-existant option defined."));
3615 1 : }
3616 4 : CATCH_END_SECTION()
3617 :
3618 4 : CATCH_START_SECTION("invalid_option_name: verify that asking for the long of a non-existant option fails")
3619 : {
3620 1 : advgetopt::options_environment environment_options;
3621 1 : environment_options.f_project_name = "unittest";
3622 1 : environment_options.f_options = nullptr;
3623 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3624 1 : environment_options.f_help_header = "Usage: test get_string() functions";
3625 :
3626 1 : advgetopt::getopt opt(environment_options);
3627 :
3628 1 : char const * cargv[] =
3629 : {
3630 : "tests/options-parser",
3631 : "--license",
3632 : nullptr
3633 : };
3634 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3635 1 : char ** argv = const_cast<char **>(cargv);
3636 :
3637 1 : opt.finish_parsing(argc, argv);
3638 :
3639 7 : CATCH_REQUIRE_THROWS_MATCHES(
3640 : opt.get_long("non-existant")
3641 : , advgetopt::getopt_logic_error
3642 : , Catch::Matchers::ExceptionMessage(
3643 : "getopt_logic_error: there is no --non-existant option defined."));
3644 :
3645 7 : CATCH_REQUIRE_THROWS_MATCHES(
3646 : opt.get_long("non-existant", 0)
3647 : , advgetopt::getopt_logic_error
3648 : , Catch::Matchers::ExceptionMessage(
3649 : "getopt_logic_error: there is no --non-existant option defined."));
3650 :
3651 7 : CATCH_REQUIRE_THROWS_MATCHES(
3652 : opt.get_long("non-existant", 1)
3653 : , advgetopt::getopt_logic_error
3654 : , Catch::Matchers::ExceptionMessage(
3655 : "getopt_logic_error: there is no --non-existant option defined."));
3656 1 : }
3657 4 : CATCH_END_SECTION()
3658 :
3659 4 : CATCH_START_SECTION("invalid_option_name: verify that asking for a default with an empty string fails")
3660 : {
3661 1 : advgetopt::options_environment environment_options;
3662 1 : environment_options.f_project_name = "unittest";
3663 1 : environment_options.f_options = nullptr;
3664 1 : environment_options.f_help_header = "Usage: test get_default() functions";
3665 :
3666 1 : advgetopt::getopt opt(environment_options);
3667 :
3668 7 : CATCH_REQUIRE_THROWS_MATCHES(
3669 : opt.has_default("")
3670 : , advgetopt::getopt_invalid_parameter
3671 : , Catch::Matchers::ExceptionMessage(
3672 : "getopt_exception: get_option() `name` argument cannot be empty."));
3673 :
3674 5 : CATCH_REQUIRE_THROWS_MATCHES(
3675 : opt.has_default(std::string())
3676 : , advgetopt::getopt_invalid_parameter
3677 : , Catch::Matchers::ExceptionMessage(
3678 : "getopt_exception: get_option() `name` argument cannot be empty."));
3679 :
3680 8 : CATCH_REQUIRE_THROWS_MATCHES(
3681 : opt.get_default("")
3682 : , advgetopt::getopt_invalid_parameter
3683 : , Catch::Matchers::ExceptionMessage(
3684 : "getopt_exception: get_option() `name` argument cannot be empty."));
3685 :
3686 6 : CATCH_REQUIRE_THROWS_MATCHES(
3687 : opt.get_default(std::string())
3688 : , advgetopt::getopt_invalid_parameter
3689 : , Catch::Matchers::ExceptionMessage(
3690 : "getopt_exception: get_option() `name` argument cannot be empty."));
3691 1 : }
3692 4 : CATCH_END_SECTION()
3693 :
3694 4 : CATCH_START_SECTION("invalid_option_name: [] operators want a valid name")
3695 : {
3696 1 : advgetopt::options_environment environment_options;
3697 1 : environment_options.f_project_name = "unittest";
3698 1 : environment_options.f_options = nullptr;
3699 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
3700 1 : environment_options.f_help_header = "Usage: test get_default() functions";
3701 :
3702 1 : advgetopt::getopt opt(environment_options);
3703 :
3704 1 : char const * cargv[] =
3705 : {
3706 : "tests/options-parser",
3707 : "--license",
3708 : nullptr
3709 : };
3710 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3711 1 : char ** argv = const_cast<char **>(cargv);
3712 :
3713 1 : opt.finish_parsing(argc, argv);
3714 :
3715 8 : CATCH_REQUIRE_THROWS_MATCHES(
3716 : opt[""]
3717 : , advgetopt::getopt_logic_error
3718 : , Catch::Matchers::ExceptionMessage(
3719 : "getopt_logic_error: argument name cannot be empty."));
3720 :
3721 6 : CATCH_REQUIRE_THROWS_MATCHES(
3722 : opt[std::string()]
3723 : , advgetopt::getopt_logic_error
3724 : , Catch::Matchers::ExceptionMessage(
3725 : "getopt_logic_error: argument name cannot be empty."));
3726 :
3727 8 : CATCH_REQUIRE_THROWS_MATCHES(
3728 : opt["g"]
3729 : , advgetopt::getopt_logic_error
3730 : , Catch::Matchers::ExceptionMessage(
3731 : "getopt_logic_error: argument name cannot be one letter if it does not exist in operator []."));
3732 :
3733 8 : CATCH_REQUIRE_THROWS_MATCHES(
3734 : opt[std::string("g")]
3735 : , advgetopt::getopt_logic_error
3736 : , Catch::Matchers::ExceptionMessage(
3737 : "getopt_logic_error: argument name cannot be one letter if it does not exist in operator []."));
3738 :
3739 1 : advgetopt::getopt const & const_opt(opt);
3740 :
3741 8 : CATCH_REQUIRE_THROWS_MATCHES(
3742 : const_opt[""]
3743 : , advgetopt::getopt_logic_error
3744 : , Catch::Matchers::ExceptionMessage(
3745 : "getopt_logic_error: argument name cannot be empty."));
3746 :
3747 6 : CATCH_REQUIRE_THROWS_MATCHES(
3748 : const_opt[std::string()]
3749 : , advgetopt::getopt_logic_error
3750 : , Catch::Matchers::ExceptionMessage(
3751 : "getopt_logic_error: argument name cannot be empty."));
3752 1 : }
3753 4 : CATCH_END_SECTION()
3754 4 : }
3755 :
3756 :
3757 :
3758 3 : CATCH_TEST_CASE("missing_default_value", "[arguments][invalid][getopt]")
3759 : {
3760 3 : CATCH_START_SECTION("missing_default_value: verify a string value without arguments and no default")
3761 : {
3762 1 : advgetopt::option const options[] =
3763 : {
3764 : advgetopt::define_option(
3765 : advgetopt::Name("size")
3766 : , advgetopt::ShortName('s')
3767 : , advgetopt::Flags(advgetopt::command_flags<
3768 : advgetopt::GETOPT_FLAG_REQUIRED
3769 : , advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION>())
3770 : , advgetopt::Help("define the size.")
3771 : ),
3772 : advgetopt::end_options()
3773 : };
3774 :
3775 1 : advgetopt::options_environment environment_options;
3776 1 : environment_options.f_project_name = "unittest";
3777 1 : environment_options.f_options = options;
3778 1 : environment_options.f_help_header = "Usage: test get_string() functions";
3779 :
3780 1 : char const * cargv[] =
3781 : {
3782 : "/usr/bin/arguments",
3783 : nullptr
3784 : };
3785 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3786 1 : char ** argv = const_cast<char **>(cargv);
3787 :
3788 1 : advgetopt::getopt opt(environment_options, argc, argv);
3789 :
3790 : // check that the result is valid
3791 :
3792 : // an invalid parameter, MUST NOT EXIST
3793 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3794 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3795 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3796 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3797 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3798 :
3799 : // no default
3800 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3801 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3802 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3803 3 : CATCH_REQUIRE(opt.size("--") == 0);
3804 :
3805 : // the valid parameter
3806 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3807 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3808 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3809 3 : CATCH_REQUIRE(opt.size("size") == 0);
3810 3 : CATCH_REQUIRE(static_cast<advgetopt::getopt const &>(opt)["size"].empty());
3811 :
3812 8 : CATCH_REQUIRE_THROWS_MATCHES(
3813 : opt.get_string("size")
3814 : , advgetopt::getopt_logic_error
3815 : , Catch::Matchers::ExceptionMessage(
3816 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3817 :
3818 8 : CATCH_REQUIRE_THROWS_MATCHES(
3819 : opt.get_string("size", 0)
3820 : , advgetopt::getopt_logic_error
3821 : , Catch::Matchers::ExceptionMessage(
3822 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3823 :
3824 8 : CATCH_REQUIRE_THROWS_MATCHES(
3825 : opt.get_string("size", 1)
3826 : , advgetopt::getopt_logic_error
3827 : , Catch::Matchers::ExceptionMessage(
3828 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3829 :
3830 : // these do not create an entry (even though it looks like it,
3831 : // i.e. it would for an std::map)
3832 : //
3833 3 : CATCH_REQUIRE(opt["size"].empty());
3834 3 : CATCH_REQUIRE(opt["size"].length() == 0);
3835 3 : CATCH_REQUIRE(opt["size"].size() == 0);
3836 :
3837 3 : CATCH_REQUIRE(opt.size("size") == 0);
3838 :
3839 8 : CATCH_REQUIRE_THROWS_MATCHES(
3840 : opt.get_string("size", 0)
3841 : , advgetopt::getopt_logic_error
3842 : , Catch::Matchers::ExceptionMessage(
3843 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3844 :
3845 8 : CATCH_REQUIRE_THROWS_MATCHES(
3846 : opt.get_string("size", 1)
3847 : , advgetopt::getopt_logic_error
3848 : , Catch::Matchers::ExceptionMessage(
3849 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3850 :
3851 : // now this one does create a value
3852 : //
3853 3 : opt["size"] = "45.3";
3854 :
3855 3 : CATCH_REQUIRE(opt.get_string("size") == "45.3");
3856 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "45.3");
3857 :
3858 8 : CATCH_REQUIRE_THROWS_MATCHES(
3859 : opt.get_string("size", 1)
3860 : , advgetopt::getopt_undefined
3861 : , Catch::Matchers::ExceptionMessage(
3862 : "getopt_exception: option_info::get_value(): no value at index 1 (idx >= 1) for --size so you can't get this value."));
3863 :
3864 : // other parameters
3865 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3866 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3867 1 : }
3868 3 : CATCH_END_SECTION()
3869 :
3870 3 : CATCH_START_SECTION("missing_default_value: verify an integer (long) value without arguments and no default")
3871 : {
3872 1 : advgetopt::option const options[] =
3873 : {
3874 : advgetopt::define_option(
3875 : advgetopt::Name("size")
3876 : , advgetopt::ShortName('s')
3877 : , advgetopt::Flags(advgetopt::command_flags<
3878 : advgetopt::GETOPT_FLAG_REQUIRED
3879 : , advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION>())
3880 : , advgetopt::Help("define the size.")
3881 : ),
3882 : advgetopt::end_options()
3883 : };
3884 :
3885 1 : advgetopt::options_environment environment_options;
3886 1 : environment_options.f_project_name = "unittest";
3887 1 : environment_options.f_options = options;
3888 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3889 :
3890 1 : char const * cargv[] =
3891 : {
3892 : "/usr/bin/arguments",
3893 : nullptr
3894 : };
3895 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3896 1 : char ** argv = const_cast<char **>(cargv);
3897 :
3898 1 : advgetopt::getopt opt(environment_options, argc, argv);
3899 :
3900 : // check that the result is valid
3901 :
3902 : // an invalid parameter, MUST NOT EXIST
3903 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3904 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3905 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3906 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3907 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3908 :
3909 : // no default
3910 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3911 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3912 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3913 3 : CATCH_REQUIRE(opt.size("--") == 0);
3914 :
3915 : // the valid parameter
3916 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3917 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3918 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3919 3 : CATCH_REQUIRE_FALSE(opt.has_default("size"));
3920 3 : CATCH_REQUIRE(opt.size("size") == 0);
3921 :
3922 7 : CATCH_REQUIRE_THROWS_MATCHES(
3923 : opt.get_long("size")
3924 : , advgetopt::getopt_logic_error
3925 : , Catch::Matchers::ExceptionMessage(
3926 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3927 :
3928 7 : CATCH_REQUIRE_THROWS_MATCHES(
3929 : opt.get_long("size", 0)
3930 : , advgetopt::getopt_logic_error
3931 : , Catch::Matchers::ExceptionMessage(
3932 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3933 :
3934 7 : CATCH_REQUIRE_THROWS_MATCHES(
3935 : opt.get_long("size", 1)
3936 : , advgetopt::getopt_logic_error
3937 : , Catch::Matchers::ExceptionMessage(
3938 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3939 :
3940 : // other parameters
3941 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3942 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3943 1 : }
3944 3 : CATCH_END_SECTION()
3945 :
3946 3 : CATCH_START_SECTION("missing_default_value: verify an integer (long) value without arguments and an empty string as default")
3947 : {
3948 1 : advgetopt::option const options[] =
3949 : {
3950 : advgetopt::define_option(
3951 : advgetopt::Name("size")
3952 : , advgetopt::ShortName('s')
3953 : , advgetopt::Flags(advgetopt::command_flags<
3954 : advgetopt::GETOPT_FLAG_REQUIRED
3955 : , advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION>())
3956 : , advgetopt::Help("define the size.")
3957 : , advgetopt::DefaultValue("")
3958 : ),
3959 : advgetopt::end_options()
3960 : };
3961 :
3962 1 : advgetopt::options_environment environment_options;
3963 1 : environment_options.f_project_name = "unittest";
3964 1 : environment_options.f_options = options;
3965 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3966 :
3967 1 : char const * cargv[] =
3968 : {
3969 : "/usr/bin/arguments",
3970 : nullptr
3971 : };
3972 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3973 1 : char ** argv = const_cast<char **>(cargv);
3974 :
3975 1 : advgetopt::getopt opt(environment_options, argc, argv);
3976 :
3977 : // check that the result is valid
3978 :
3979 : // an invalid parameter, MUST NOT EXIST
3980 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3981 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3982 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3983 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3984 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3985 :
3986 : // no default
3987 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3988 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3989 3 : CATCH_REQUIRE(opt.get_default("--").empty());
3990 3 : CATCH_REQUIRE(opt.size("--") == 0);
3991 :
3992 : // the valid parameter
3993 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3994 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3995 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3996 3 : CATCH_REQUIRE(opt.has_default("size"));
3997 3 : CATCH_REQUIRE(opt.size("size") == 0);
3998 :
3999 7 : CATCH_REQUIRE_THROWS_MATCHES(
4000 : opt.get_long("size")
4001 : , advgetopt::getopt_logic_error
4002 : , Catch::Matchers::ExceptionMessage(
4003 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
4004 :
4005 7 : CATCH_REQUIRE_THROWS_MATCHES(
4006 : opt.get_long("size", 0)
4007 : , advgetopt::getopt_logic_error
4008 : , Catch::Matchers::ExceptionMessage(
4009 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
4010 :
4011 7 : CATCH_REQUIRE_THROWS_MATCHES(
4012 : opt.get_long("size", 1)
4013 : , advgetopt::getopt_logic_error
4014 : , Catch::Matchers::ExceptionMessage(
4015 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
4016 :
4017 : // other parameters
4018 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
4019 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
4020 1 : }
4021 3 : CATCH_END_SECTION()
4022 3 : }
4023 :
4024 :
4025 :
4026 1 : CATCH_TEST_CASE("incompatible_default_value", "[arguments][invalid][getopt]")
4027 : {
4028 1 : CATCH_START_SECTION("incompatible_default_value: verify an integer (long) value without arguments and a non-numeric default")
4029 : {
4030 1 : advgetopt::option const options[] =
4031 : {
4032 : advgetopt::define_option(
4033 : advgetopt::Name("size")
4034 : , advgetopt::ShortName('s')
4035 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
4036 : , advgetopt::Help("define the size.")
4037 : , advgetopt::DefaultValue("undefined")
4038 : ),
4039 : advgetopt::end_options()
4040 : };
4041 :
4042 1 : advgetopt::options_environment environment_options;
4043 1 : environment_options.f_project_name = "unittest";
4044 1 : environment_options.f_options = options;
4045 1 : environment_options.f_help_header = "Usage: test get_long() functions";
4046 :
4047 1 : char const * cargv[] =
4048 : {
4049 : "/usr/bin/arguments",
4050 : nullptr
4051 : };
4052 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
4053 1 : char ** argv = const_cast<char **>(cargv);
4054 :
4055 1 : advgetopt::getopt opt(environment_options, argc, argv);
4056 :
4057 : // check that the result is valid
4058 :
4059 : // an invalid parameter, MUST NOT EXIST
4060 : //
4061 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
4062 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
4063 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
4064 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
4065 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
4066 :
4067 : // no default
4068 : //
4069 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
4070 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
4071 3 : CATCH_REQUIRE(opt.get_default("--").empty());
4072 3 : CATCH_REQUIRE(opt.size("--") == 0);
4073 :
4074 : // the valid parameter
4075 : //
4076 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
4077 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
4078 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
4079 3 : CATCH_REQUIRE(opt.has_default("size"));
4080 3 : CATCH_REQUIRE(opt.get_default("size") == "undefined"); // this works, it fails with get_long() though
4081 3 : CATCH_REQUIRE(opt.size("size") == 0);
4082 :
4083 7 : CATCH_REQUIRE_THROWS_MATCHES(
4084 : opt.get_long("size")
4085 : , advgetopt::getopt_logic_error
4086 : , Catch::Matchers::ExceptionMessage(
4087 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
4088 :
4089 7 : CATCH_REQUIRE_THROWS_MATCHES(
4090 : opt.get_long("size", 0)
4091 : , advgetopt::getopt_logic_error
4092 : , Catch::Matchers::ExceptionMessage(
4093 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
4094 :
4095 7 : CATCH_REQUIRE_THROWS_MATCHES(
4096 : opt.get_long("size", 1)
4097 : , advgetopt::getopt_logic_error
4098 : , Catch::Matchers::ExceptionMessage(
4099 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
4100 :
4101 : // other parameters
4102 : //
4103 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
4104 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
4105 1 : }
4106 1 : CATCH_END_SECTION()
4107 1 : }
4108 :
4109 :
4110 :
4111 2 : CATCH_TEST_CASE("out_of_range_value", "[arguments][invalid][getopt]")
4112 : {
4113 2 : CATCH_START_SECTION("out_of_range_value: verify an integer (long) value without arguments and a non-numeric default")
4114 : {
4115 1 : advgetopt::option const options[] =
4116 : {
4117 : advgetopt::define_option(
4118 : advgetopt::Name("size")
4119 : , advgetopt::ShortName('s')
4120 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
4121 : , advgetopt::Help("define the size.")
4122 : , advgetopt::DefaultValue("-300")
4123 : ),
4124 : advgetopt::end_options()
4125 : };
4126 :
4127 1 : advgetopt::options_environment environment_options;
4128 1 : environment_options.f_project_name = "unittest";
4129 1 : environment_options.f_options = options;
4130 1 : environment_options.f_help_header = "Usage: test get_long() functions";
4131 :
4132 1 : char const * cargv[] =
4133 : {
4134 : "/usr/bin/arguments",
4135 : "--size",
4136 : "312",
4137 : nullptr
4138 : };
4139 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
4140 1 : char ** argv = const_cast<char **>(cargv);
4141 :
4142 1 : advgetopt::getopt opt(environment_options, argc, argv);
4143 :
4144 : // check that the result is valid
4145 :
4146 : // an invalid parameter, MUST NOT EXIST
4147 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
4148 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
4149 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
4150 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
4151 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
4152 :
4153 : // no default
4154 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
4155 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
4156 3 : CATCH_REQUIRE(opt.get_default("--").empty());
4157 3 : CATCH_REQUIRE(opt.size("--") == 0);
4158 :
4159 : // the valid parameter
4160 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
4161 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
4162 3 : CATCH_REQUIRE(opt.is_defined("size"));
4163 3 : CATCH_REQUIRE(opt.get_string("size") == "312");
4164 3 : CATCH_REQUIRE(opt.get_string("size", 0) == "312");
4165 3 : CATCH_REQUIRE(opt["size"] == "312");
4166 3 : CATCH_REQUIRE(opt.get_long("size") == 312);
4167 3 : CATCH_REQUIRE(opt.get_long("size", 0) == 312);
4168 3 : CATCH_REQUIRE(opt.get_default("size") == "-300");
4169 3 : CATCH_REQUIRE(opt.size("size") == 1);
4170 :
4171 3 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: 312 is out of bounds (-100..100 inclusive) in parameter --size.");
4172 3 : CATCH_REQUIRE(opt.get_long("size", 0, -100, 100) == -1);
4173 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
4174 :
4175 : // other parameters
4176 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
4177 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
4178 1 : }
4179 2 : CATCH_END_SECTION()
4180 :
4181 2 : CATCH_START_SECTION("out_of_range_value: verify an integer (long) value without arguments and a non-numeric default")
4182 : {
4183 1 : advgetopt::option const options[] =
4184 : {
4185 : advgetopt::define_option(
4186 : advgetopt::Name("size")
4187 : , advgetopt::ShortName('s')
4188 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
4189 : , advgetopt::Help("define the size.")
4190 : , advgetopt::DefaultValue("-300")
4191 : ),
4192 : advgetopt::end_options()
4193 : };
4194 :
4195 1 : advgetopt::options_environment environment_options;
4196 1 : environment_options.f_project_name = "unittest";
4197 1 : environment_options.f_options = options;
4198 1 : environment_options.f_help_header = "Usage: test get_long() functions";
4199 :
4200 1 : char const * cargv[] =
4201 : {
4202 : "/usr/bin/arguments",
4203 : nullptr
4204 : };
4205 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
4206 1 : char ** argv = const_cast<char **>(cargv);
4207 :
4208 1 : advgetopt::getopt opt(environment_options, argc, argv);
4209 :
4210 : // check that the result is valid
4211 :
4212 : // an invalid parameter, MUST NOT EXIST
4213 3 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
4214 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
4215 3 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
4216 3 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
4217 3 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
4218 :
4219 : // no default
4220 3 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
4221 3 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
4222 3 : CATCH_REQUIRE(opt.get_default("--").empty());
4223 3 : CATCH_REQUIRE(opt.size("--") == 0);
4224 :
4225 : // the valid parameter
4226 3 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
4227 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
4228 3 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
4229 3 : CATCH_REQUIRE(opt.get_default("size") == "-300");
4230 3 : CATCH_REQUIRE(opt.size("size") == 0);
4231 :
4232 3 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: -300 is out of bounds (-100..100 inclusive) in parameter --size.");
4233 3 : CATCH_REQUIRE(opt.get_long("size", 0, -100, 100) == -1);
4234 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
4235 :
4236 : // other parameters
4237 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
4238 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
4239 1 : }
4240 2 : CATCH_END_SECTION()
4241 2 : }
4242 :
4243 :
4244 1 : CATCH_TEST_CASE("check_sanitizer", "[arguments][valid][getopt]")
4245 : {
4246 1 : CATCH_START_SECTION("check_sanitizer: check that the sanitizer is detected")
4247 : {
4248 : // when compiling the tests with coverage we turn on the sanitizer
4249 : // so here we should get output that show the sanitizer as being
4250 : // turned on; unfortunately, we can't test all cases in our current
4251 : // situation
4252 : //
4253 : #if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
4254 : // when running coverage or in the Sanitize version
4255 : //
4256 1 : std::string const expected(
4257 : #ifdef __SANITIZE_ADDRESS__
4258 : "The address sanitizer is compiled in.\n"
4259 : #endif
4260 : #ifdef __SANITIZE_THREAD__
4261 : "The thread sanitizer is compiled in.\n"
4262 : #endif
4263 3 : );
4264 1 : CATCH_REQUIRE(advgetopt::sanitizer_details() == expected);
4265 : #else
4266 : // when running in Debug or Release
4267 : //
4268 : CATCH_REQUIRE(advgetopt::sanitizer_details() == "The address and thread sanitizers are not compiled in.\n");
4269 : #endif
4270 1 : }
4271 1 : CATCH_END_SECTION()
4272 1 : }
4273 :
4274 :
4275 :
4276 : // vim: ts=4 sw=4 et
|