LCOV - code coverage report
Current view: top level - tests - options_parser.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 446 446 100.0 %
Date: 2021-09-08 13:54:31 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2006-2021  Made to Order Software Corp.  All Rights Reserved
       4             :  *
       5             :  *    https://snapwebsites.org/
       6             :  *    contact@m2osw.com
       7             :  *
       8             :  *    This program is free software; you can redistribute it and/or modify
       9             :  *    it under the terms of the GNU General Public License as published by
      10             :  *    the Free Software Foundation; either version 2 of the License, or
      11             :  *    (at your option) any later version.
      12             :  *
      13             :  *    This program is distributed in the hope that it will be useful,
      14             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *    GNU General Public License for more details.
      17             :  *
      18             :  *    You should have received a copy of the GNU General Public License along
      19             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      20             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  *
      22             :  * Authors:
      23             :  *    Alexis Wilke   alexis@m2osw.com
      24             :  */
      25             : 
      26             : // self
      27             : //
      28             : #include "main.h"
      29             : 
      30             : // advgetopt lib
      31             : //
      32             : #include <advgetopt/exception.h>
      33             : 
      34             : // C++ lib
      35             : //
      36             : #include <fstream>
      37             : 
      38             : 
      39             : 
      40             : 
      41             : 
      42           6 : CATCH_TEST_CASE("options_parser", "[options][valid]")
      43             : {
      44           8 :     CATCH_START_SECTION("System options only")
      45           1 :         advgetopt::options_environment environment_options;
      46           1 :         environment_options.f_project_name = "unittest";
      47           1 :         environment_options.f_options = nullptr;
      48           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
      49           1 :         environment_options.f_help_header = "Usage: test valid options from system options only";
      50             : 
      51           1 :         char const * cargv[] =
      52             :         {
      53             :             "tests/options-parser",
      54             :             "--license",
      55             :             nullptr
      56             :         };
      57           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
      58           1 :         char ** argv = const_cast<char **>(cargv);
      59             : 
      60           2 :         advgetopt::getopt opt(environment_options, argc, argv);
      61             : 
      62             :         // check that the result is valid
      63             : 
      64             :         // an invalid parameter, MUST NOT EXIST
      65           1 :         CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
      66           1 :         CATCH_REQUIRE(opt.get_option('Z') == nullptr);
      67           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
      68           1 :         CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
      69           1 :         CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
      70             : 
      71             :         // the valid parameter
      72           1 :         CATCH_REQUIRE(opt.get_option("verbose") == nullptr);
      73           1 :         CATCH_REQUIRE(opt.get_option('v') == nullptr);
      74           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("verbose"));
      75           1 :         CATCH_REQUIRE(opt.get_default("verbose").empty());
      76           1 :         CATCH_REQUIRE(opt.size("verbose") == 0);
      77             : 
      78             :         // "--help"
      79           1 :         CATCH_REQUIRE(opt.get_option("help") != nullptr);
      80           1 :         CATCH_REQUIRE(opt.get_option('h') != nullptr);
      81           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("help"));
      82           1 :         CATCH_REQUIRE(opt.get_default("help").empty());
      83           1 :         CATCH_REQUIRE(opt.size("help") == 0);
      84             : 
      85             :         // "--version"
      86           1 :         CATCH_REQUIRE(opt.get_option("version") != nullptr);
      87           1 :         CATCH_REQUIRE(opt.get_option('V') != nullptr);
      88           1 :         CATCH_REQUIRE(opt.get_option('V') == opt.get_option("version"));
      89           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("version"));
      90           1 :         CATCH_REQUIRE(opt.get_default("version").empty());
      91           1 :         CATCH_REQUIRE(opt.size("version") == 0);
      92             : 
      93             :         // "--copyright"
      94           1 :         CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
      95           1 :         CATCH_REQUIRE(opt.get_option('C') != nullptr);
      96           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
      97           1 :         CATCH_REQUIRE(opt.get_default("copyright").empty());
      98           1 :         CATCH_REQUIRE(opt.size("copyright") == 0);
      99             : 
     100             :         // "--license"
     101           1 :         CATCH_REQUIRE(opt.get_option("license") != nullptr);
     102           1 :         CATCH_REQUIRE(opt.get_option('L') != nullptr);
     103           1 :         CATCH_REQUIRE(opt.is_defined("license"));
     104           1 :         CATCH_REQUIRE(opt.get_string("license").empty());
     105           1 :         CATCH_REQUIRE(opt.get_default("license").empty());
     106           1 :         CATCH_REQUIRE(opt.size("license") == 1);
     107             : 
     108             :         // "--build-date"
     109           1 :         CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
     110           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
     111           1 :         CATCH_REQUIRE(opt.get_default("build-date").empty());
     112           1 :         CATCH_REQUIRE(opt.size("build-date") == 0);
     113             : 
     114             :         // "--environment-variable-name"
     115           1 :         CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
     116           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
     117           1 :         CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
     118           1 :         CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
     119             : 
     120             :         // "--configuration-filename"
     121           1 :         CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
     122           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
     123           1 :         CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
     124           1 :         CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
     125             : 
     126             :         // "--path-to-option-definitions"
     127           1 :         CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
     128           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
     129           1 :         CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
     130           1 :         CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
     131             : 
     132             : 
     133             :         // other parameters
     134           1 :         CATCH_REQUIRE(opt.get_program_name() == "options-parser");
     135           1 :         CATCH_REQUIRE(opt.get_program_fullname() == "tests/options-parser");
     136             :     CATCH_END_SECTION()
     137             : 
     138           8 :     CATCH_START_SECTION("Duplicated options (ignored by system options)")
     139           1 :         advgetopt::option const options[] =
     140             :         {
     141             :             advgetopt::define_option(
     142             :                   advgetopt::Name("verbose")
     143             :                 , advgetopt::ShortName('V')        // duplicate version short name
     144             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     145             :                 , advgetopt::Help("print info as we work.")
     146             :             ),
     147             :             advgetopt::define_option(
     148             :                   advgetopt::Name("copyright")     // duplicate copyright
     149             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     150             :                 , advgetopt::Help("print info as we work.")
     151             :             ),
     152             :             advgetopt::end_options()
     153             :         };
     154             : 
     155           1 :         advgetopt::options_environment environment_options;
     156           1 :         environment_options.f_project_name = "unittest";
     157           1 :         environment_options.f_options = options;
     158           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     159           1 :         environment_options.f_help_header = "Usage: test valid options with duplicates";
     160             : 
     161           1 :         char const * cargv[] =
     162             :         {
     163             :             "options-parser",
     164             :             "--verbose",
     165             :             "--license",
     166             :             nullptr
     167             :         };
     168           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     169           1 :         char ** argv = const_cast<char **>(cargv);
     170             : 
     171           2 :         advgetopt::getopt opt(environment_options, argc, argv);
     172             : 
     173             :         // check that the result is valid
     174             : 
     175             :         // an invalid parameter, MUST NOT EXIST
     176           1 :         CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
     177           1 :         CATCH_REQUIRE(opt.get_option('Z') == nullptr);
     178           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
     179           1 :         CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
     180           1 :         CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
     181             : 
     182             :         // the valid parameter
     183           1 :         CATCH_REQUIRE(opt.get_option("verbose") != nullptr);
     184           1 :         CATCH_REQUIRE(opt.get_option('V') != nullptr);
     185           1 :         CATCH_REQUIRE(opt.get_option('V') == opt.get_option("verbose"));
     186           1 :         CATCH_REQUIRE(opt.is_defined("verbose"));
     187           1 :         CATCH_REQUIRE(opt.get_default("verbose").empty());
     188           1 :         CATCH_REQUIRE(opt.size("verbose") == 1);
     189             : 
     190             :         // "--help"
     191           1 :         CATCH_REQUIRE(opt.get_option("help") != nullptr);
     192           1 :         CATCH_REQUIRE(opt.get_option('h') != nullptr);
     193           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("help"));
     194           1 :         CATCH_REQUIRE(opt.get_default("help").empty());
     195           1 :         CATCH_REQUIRE(opt.size("help") == 0);
     196             : 
     197             :         // "--version"
     198           1 :         CATCH_REQUIRE(opt.get_option("version") != nullptr);
     199           1 :         CATCH_REQUIRE(opt.get_option('V') != nullptr);      // 'V' is defined, but it's for "verbose"...
     200           1 :         CATCH_REQUIRE(opt.get_option('V') != opt.get_option("version"));
     201           1 :         CATCH_REQUIRE(opt.get_option('V') == opt.get_option("verbose"));
     202           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("version"));
     203           1 :         CATCH_REQUIRE(opt.get_default("version").empty());
     204           1 :         CATCH_REQUIRE(opt.size("version") == 0);
     205             : 
     206             :         // "--copyright"
     207           1 :         CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
     208           1 :         CATCH_REQUIRE(opt.get_option('C') == nullptr);      // no short name in our definition (which overwrites the system definition)
     209           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
     210           1 :         CATCH_REQUIRE(opt.get_default("copyright").empty());
     211           1 :         CATCH_REQUIRE(opt.size("copyright") == 0);
     212             : 
     213             :         // "--license"
     214           1 :         CATCH_REQUIRE(opt.get_option("license") != nullptr);
     215           1 :         CATCH_REQUIRE(opt.get_option('L') != nullptr);
     216           1 :         CATCH_REQUIRE(opt.is_defined("license"));
     217           1 :         CATCH_REQUIRE(opt.get_string("license").empty());
     218           1 :         CATCH_REQUIRE(opt.get_default("license").empty());
     219           1 :         CATCH_REQUIRE(opt.size("license") == 1);
     220             : 
     221             :         // "--build-date"
     222           1 :         CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
     223           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
     224           1 :         CATCH_REQUIRE(opt.get_default("build-date").empty());
     225           1 :         CATCH_REQUIRE(opt.size("build-date") == 0);
     226             : 
     227             :         // "--environment-variable-name"
     228           1 :         CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
     229           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
     230           1 :         CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
     231           1 :         CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
     232             : 
     233             :         // "--configuration-filename"
     234           1 :         CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
     235           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
     236           1 :         CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
     237           1 :         CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
     238             : 
     239             :         // "--path-to-option-definitions"
     240           1 :         CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
     241           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
     242           1 :         CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
     243           1 :         CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
     244             : 
     245             :         // other parameters
     246           1 :         CATCH_REQUIRE(opt.get_program_name() == "options-parser");
     247           1 :         CATCH_REQUIRE(opt.get_program_fullname() == "options-parser");
     248             :     CATCH_END_SECTION()
     249             : 
     250           8 :     CATCH_START_SECTION("Default option")
     251           1 :         advgetopt::option const options[] =
     252             :         {
     253             :             advgetopt::define_option(
     254             :                   advgetopt::Name("verbose")
     255             :                 , advgetopt::ShortName('v')
     256             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     257             :                 , advgetopt::Help("print info as we work.")
     258             :             ),
     259             :             advgetopt::define_option(
     260             :                   advgetopt::Name("filenames")
     261             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE, advgetopt::GETOPT_FLAG_DEFAULT_OPTION>())
     262             :                 , advgetopt::Help("enter a list of filenames.")
     263             :                 , advgetopt::DefaultValue("a.out")
     264             :             ),
     265             :             advgetopt::end_options()
     266             :         };
     267             : 
     268           1 :         advgetopt::options_environment environment_options;
     269           1 :         environment_options.f_project_name = "unittest";
     270           1 :         environment_options.f_options = options;
     271           1 :         environment_options.f_environment_flags = 0;
     272           1 :         environment_options.f_help_header = "Usage: test valid options with duplicates";
     273             : 
     274           1 :         char const * cargv[] =
     275             :         {
     276             :             "/usr/bin/options-parser",
     277             :             "file1",
     278             :             "file2",
     279             :             "file3",
     280             :             "file4",
     281             :             "file5",
     282             :             nullptr
     283             :         };
     284           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     285           1 :         char ** argv = const_cast<char **>(cargv);
     286             : 
     287           2 :         advgetopt::getopt opt(environment_options, argc, argv);
     288             : 
     289             :         // check that the result is valid
     290             : 
     291             :         // an invalid parameter, MUST NOT EXIST
     292           1 :         CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
     293           1 :         CATCH_REQUIRE(opt.get_option('Z') == nullptr);
     294           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
     295           1 :         CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
     296           1 :         CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
     297             : 
     298             :         // the valid parameter
     299           1 :         CATCH_REQUIRE(opt.get_option("verbose") != nullptr);
     300           1 :         CATCH_REQUIRE(opt.get_option('v') != nullptr);
     301           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("verbose"));
     302           1 :         CATCH_REQUIRE(opt.get_default("verbose").empty());
     303           1 :         CATCH_REQUIRE(opt.size("verbose") == 0);
     304             : 
     305             :         // "--help"
     306           1 :         CATCH_REQUIRE(opt.get_option("help") == nullptr);
     307           1 :         CATCH_REQUIRE(opt.get_option('h') == nullptr);
     308           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("help"));
     309           1 :         CATCH_REQUIRE(opt.get_default("help").empty());
     310           1 :         CATCH_REQUIRE(opt.size("help") == 0);
     311             : 
     312             :         // "--version"
     313           1 :         CATCH_REQUIRE(opt.get_option("version") == nullptr);
     314           1 :         CATCH_REQUIRE(opt.get_option('V') == nullptr);      // 'V' is defined, but it's for "verbose"...
     315           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("version"));
     316           1 :         CATCH_REQUIRE(opt.get_default("version").empty());
     317           1 :         CATCH_REQUIRE(opt.size("version") == 0);
     318             : 
     319             :         // "--copyright"
     320           1 :         CATCH_REQUIRE(opt.get_option("copyright") == nullptr);
     321           1 :         CATCH_REQUIRE(opt.get_option('C') == nullptr);      // no short name in our definition (which overwrites the system definition)
     322           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
     323           1 :         CATCH_REQUIRE(opt.get_default("copyright").empty());
     324           1 :         CATCH_REQUIRE(opt.size("copyright") == 0);
     325             : 
     326             :         // "--license"
     327           1 :         CATCH_REQUIRE(opt.get_option("license") == nullptr);
     328           1 :         CATCH_REQUIRE(opt.get_option('L') == nullptr);
     329           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("license"));
     330           1 :         CATCH_REQUIRE(opt.get_default("license").empty());
     331           1 :         CATCH_REQUIRE(opt.size("license") == 0);
     332             : 
     333             :         // "--build-date"
     334           1 :         CATCH_REQUIRE(opt.get_option("build-date") == nullptr);
     335           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
     336           1 :         CATCH_REQUIRE(opt.get_default("build-date").empty());
     337           1 :         CATCH_REQUIRE(opt.size("build-date") == 0);
     338             : 
     339             :         // "--environment-variable-name"
     340           1 :         CATCH_REQUIRE(opt.get_option("environment-variable-name") == nullptr);
     341           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
     342           1 :         CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
     343           1 :         CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
     344           1 :         CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
     345             : 
     346             :         // "--configuration-filename"
     347           1 :         CATCH_REQUIRE(opt.get_option("configuration-filenames") == nullptr);
     348           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
     349           1 :         CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
     350           1 :         CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
     351           1 :         CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
     352             : 
     353             :         // "--path-to-option-definitions"
     354           1 :         CATCH_REQUIRE(opt.get_option("path-to-option-definitions") == nullptr);
     355           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
     356           1 :         CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
     357           1 :         CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
     358             : 
     359             :         // "--configuration-filename"
     360           1 :         CATCH_REQUIRE(opt.get_option("filenames") != nullptr);
     361           1 :         CATCH_REQUIRE(opt.is_defined("filenames"));
     362           1 :         CATCH_REQUIRE(opt.get_string("filenames")    == "file1");
     363           1 :         CATCH_REQUIRE(opt.get_string("filenames", 0) == "file1");
     364           1 :         CATCH_REQUIRE(opt.get_string("filenames", 1) == "file2");
     365           1 :         CATCH_REQUIRE(opt.get_string("filenames", 2) == "file3");
     366           1 :         CATCH_REQUIRE(opt.get_string("filenames", 3) == "file4");
     367           1 :         CATCH_REQUIRE(opt.get_string("filenames", 4) == "file5");
     368           1 :         CATCH_REQUIRE(opt.has_default("filenames"));
     369           1 :         CATCH_REQUIRE(opt.get_default("filenames") == "a.out");
     370           1 :         CATCH_REQUIRE(opt.size("filenames") == 5);
     371             : 
     372             :         // other parameters
     373           1 :         CATCH_REQUIRE(opt.get_program_name() == "options-parser");
     374           1 :         CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/options-parser");
     375             :     CATCH_END_SECTION()
     376             : 
     377           8 :     CATCH_START_SECTION("Alias option")
     378           1 :         advgetopt::option const options[] =
     379             :         {
     380             :             advgetopt::define_option(
     381             :                   advgetopt::Name("verbose")
     382             :                 , advgetopt::ShortName(U'v')
     383             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     384             :                 , advgetopt::Help("print info as we work.")
     385             :             ),
     386             :             advgetopt::define_option(
     387             :                   advgetopt::Name("licence")    // to allow French spelling
     388             :                 , advgetopt::Alias("license")
     389             :                 , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
     390             :             ),
     391             :             advgetopt::end_options()
     392             :         };
     393             : 
     394           1 :         advgetopt::options_environment environment_options;
     395           1 :         environment_options.f_project_name = "unittest";
     396           1 :         environment_options.f_options = options;
     397           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     398           1 :         environment_options.f_help_header = "Usage: test valid options with duplicates";
     399             : 
     400           1 :         char const * cargv[] =
     401             :         {
     402             :             "options-parser",
     403             :             "--verbose",
     404             :             "--license",
     405             :             nullptr
     406             :         };
     407           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     408           1 :         char ** argv = const_cast<char **>(cargv);
     409             : 
     410           2 :         advgetopt::getopt opt(environment_options, argc, argv);
     411             : 
     412             :         // check that the result is valid
     413             : 
     414             :         // an invalid parameter, MUST NOT EXIST
     415           1 :         CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
     416           1 :         CATCH_REQUIRE(opt.get_option(U'Z') == nullptr);
     417           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
     418           1 :         CATCH_REQUIRE_FALSE(opt.has_default("invalid-parameter"));
     419           1 :         CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
     420           1 :         CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
     421             : 
     422             :         // the valid parameter
     423           1 :         CATCH_REQUIRE(opt.get_option("verbose") != nullptr);
     424           1 :         CATCH_REQUIRE(opt.get_option(U'v') != nullptr);
     425           1 :         CATCH_REQUIRE(opt.is_defined("verbose"));
     426           1 :         CATCH_REQUIRE_FALSE(opt.has_default("verbose"));
     427           1 :         CATCH_REQUIRE(opt.get_default("verbose").empty());
     428           1 :         CATCH_REQUIRE(opt.size("verbose") == 1);
     429             : 
     430             :         // "--help"
     431           1 :         CATCH_REQUIRE(opt.get_option("help") != nullptr);
     432           1 :         CATCH_REQUIRE(opt.get_option(U'h') != nullptr);
     433           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("help"));
     434           1 :         CATCH_REQUIRE_FALSE(opt.has_default("help"));
     435           1 :         CATCH_REQUIRE(opt.get_default("help").empty());
     436           1 :         CATCH_REQUIRE(opt.size("help") == 0);
     437             : 
     438             :         // "--version"
     439           1 :         CATCH_REQUIRE(opt.get_option("version") != nullptr);
     440           1 :         CATCH_REQUIRE(opt.get_option(U'V') != nullptr);      // 'V' is defined, but it's for "verbose"...
     441           1 :         CATCH_REQUIRE(opt.get_option(U'V') == opt.get_option("version"));
     442           1 :         CATCH_REQUIRE(opt.get_option(U'V') != opt.get_option("verbose"));
     443           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("version"));
     444           1 :         CATCH_REQUIRE_FALSE(opt.has_default("version"));
     445           1 :         CATCH_REQUIRE(opt.get_default("version").empty());
     446           1 :         CATCH_REQUIRE(opt.size("version") == 0);
     447             : 
     448             :         // "--copyright"
     449           1 :         CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
     450           1 :         CATCH_REQUIRE(opt.get_option(U'C') != nullptr);      // no short name in our definition (which overwrites the system definition)
     451           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
     452           1 :         CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
     453           1 :         CATCH_REQUIRE(opt.get_default("copyright").empty());
     454           1 :         CATCH_REQUIRE(opt.size("copyright") == 0);
     455             : 
     456             :         // "--license"
     457           1 :         CATCH_REQUIRE(opt.get_option("license") != nullptr);
     458           1 :         CATCH_REQUIRE(opt.get_option(U'L') != nullptr);
     459           1 :         CATCH_REQUIRE(opt.is_defined("license"));
     460           1 :         CATCH_REQUIRE(opt.get_string("license").empty());
     461           1 :         CATCH_REQUIRE_FALSE(opt.has_default("license"));
     462           1 :         CATCH_REQUIRE(opt.get_default("license").empty());
     463           1 :         CATCH_REQUIRE(opt.size("license") == 1);
     464             : 
     465             :         // "--build-date"
     466           1 :         CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
     467           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
     468           1 :         CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
     469           1 :         CATCH_REQUIRE(opt.get_default("build-date").empty());
     470           1 :         CATCH_REQUIRE(opt.size("build-date") == 0);
     471             : 
     472             :         // "--environment-variable-name"
     473           1 :         CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
     474           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
     475           1 :         CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
     476           1 :         CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
     477             : 
     478             :         // "--configuration-filename"
     479           1 :         CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
     480           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
     481           1 :         CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
     482           1 :         CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
     483           1 :         CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
     484             : 
     485             :         // "--path-to-option-definitions"
     486           1 :         CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
     487           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
     488           1 :         CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
     489           1 :         CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
     490           1 :         CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
     491             : 
     492             :         // other parameters
     493           1 :         CATCH_REQUIRE(opt.get_program_name() == "options-parser");
     494           1 :         CATCH_REQUIRE(opt.get_program_fullname() == "options-parser");
     495             :     CATCH_END_SECTION()
     496           4 : }
     497             : 
     498             : 
     499             : 
     500             : 
     501             : 
     502           3 : CATCH_TEST_CASE("define_option_short_name", "[options][valid][config]")
     503             : {
     504           2 :     CATCH_START_SECTION("Test adding '-<gear>' to '--config-dir'")
     505             :     {
     506           1 :         advgetopt::option const options[] =
     507             :         {
     508             :             advgetopt::define_option(
     509             :                   advgetopt::Name("user")
     510             :                 , advgetopt::ShortName('u')
     511             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
     512             :                 , advgetopt::Help("user name.")
     513             :             ),
     514             :             advgetopt::end_options()
     515             :         };
     516             : 
     517           1 :         advgetopt::options_environment environment_options;
     518           1 :         environment_options.f_project_name = "unittest";
     519           1 :         environment_options.f_options = options;
     520           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     521           1 :         environment_options.f_configuration_filename = "snaplog.conf";
     522           1 :         environment_options.f_help_header = "Usage: test --config-dir";
     523             : 
     524           1 :         char const * cargv[] =
     525             :         {
     526             :             "/usr/bin/arguments",
     527             :             "-u",
     528             :             "alexis",
     529             :             "-L",
     530             :             "-\xE2\x9A\x99",        // GEAR character
     531             :             "/etc/secret/config",
     532             :             nullptr
     533             :         };
     534           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     535           1 :         char ** argv = const_cast<char **>(cargv);
     536             : 
     537           2 :         advgetopt::getopt opt(environment_options);
     538           1 :         opt.parse_program_name(argv);
     539             : 
     540           1 :         CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
     541           1 :         opt.set_short_name("config-dir", 0x2699);
     542             : 
     543           1 :         opt.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     544             : 
     545             :         // check that the result is valid
     546             : 
     547             :         // an invalid parameter, MUST NOT EXIST
     548           1 :         CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
     549           1 :         CATCH_REQUIRE(opt.get_option('Z') == nullptr);
     550           1 :         CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
     551           1 :         CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
     552           1 :         CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
     553             : 
     554             :         // the valid parameter
     555           1 :         CATCH_REQUIRE(opt.get_option("user") != nullptr);
     556           1 :         CATCH_REQUIRE(opt.get_option('u') == opt.get_option("user"));
     557           1 :         CATCH_REQUIRE(opt.is_defined("user"));
     558           1 :         CATCH_REQUIRE(opt.get_string("user") == "alexis");
     559           1 :         CATCH_REQUIRE(opt.get_string("user", 0) == "alexis");
     560           1 :         CATCH_REQUIRE(opt.get_default("user").empty());
     561           1 :         CATCH_REQUIRE(opt.size("user") == 1);
     562             : 
     563             :         // the license system parameter
     564           1 :         CATCH_REQUIRE(opt.get_option("license") != nullptr);
     565           1 :         CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
     566           1 :         CATCH_REQUIRE(opt.is_defined("license"));
     567           1 :         CATCH_REQUIRE(opt.get_default("license").empty());
     568           1 :         CATCH_REQUIRE(opt.size("license") == 1);
     569             : 
     570             :         // the config-dir system parameter
     571           1 :         CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
     572           1 :         CATCH_REQUIRE(opt.get_option(static_cast<advgetopt::short_name_t>(0x2699)) == opt.get_option("config-dir"));
     573           1 :         CATCH_REQUIRE(opt.is_defined("config-dir"));
     574           1 :         CATCH_REQUIRE(opt.get_default("config-dir").empty());
     575           1 :         CATCH_REQUIRE(opt.size("config-dir") == 1);
     576           1 :         CATCH_REQUIRE(opt.get_string("config-dir") == "/etc/secret/config");
     577             : 
     578             :         // other parameters
     579           1 :         CATCH_REQUIRE(opt.get_program_name() == "arguments");
     580           1 :         CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
     581             :     }
     582             :     CATCH_END_SECTION()
     583           1 : }
     584             : 
     585             : 
     586             : 
     587             : 
     588             : 
     589             : 
     590             : 
     591             : // With C++17, all of these invalid cases should be handled in the
     592             : // define_option() and option_flags() templates
     593             : //
     594          12 : CATCH_TEST_CASE("invalid_options_parser", "[options][invalid]")
     595             : {
     596          20 :     CATCH_START_SECTION("No options")
     597           1 :         advgetopt::options_environment environment_options;
     598           1 :         environment_options.f_project_name = "unittest";
     599           1 :         environment_options.f_options = nullptr;
     600           1 :         environment_options.f_environment_flags = 0;
     601           1 :         environment_options.f_help_header = "Usage: test detection of no options available at all";
     602             : 
     603           1 :         char const * cargv[] =
     604             :         {
     605             :             "tests/no-options-parser",
     606             :             "--missing",
     607             :             nullptr
     608             :         };
     609           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     610           1 :         char ** argv = const_cast<char **>(cargv);
     611             : 
     612           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     613             :                 , advgetopt::getopt_logic_error
     614             :                 , Catch::Matchers::ExceptionMessage(
     615             :                           "getopt_logic_error: an empty list of options is not legal, you must"
     616             :                           " defined at least one (i.e. --version, --help...)"));
     617             :     CATCH_END_SECTION()
     618             : 
     619          20 :     CATCH_START_SECTION("Options without a name (null pointer)")
     620           1 :         advgetopt::option const options[] =
     621             :         {
     622             :             advgetopt::define_option(
     623             :                   advgetopt::Name("verbose")
     624             :                 , advgetopt::ShortName('v')
     625             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     626             :                 , advgetopt::Help("print info as we work.")
     627             :             ),
     628             :             {
     629             :                 // we have to enter this manually because define_option()
     630             :                 // forces you to enter a name, with C++17, we will also
     631             :                 // be able to verify the whole table at compile time
     632             :                 //
     633             :                 '\0',
     634             :                 advgetopt::GETOPT_FLAG_FLAG,
     635             :                 nullptr,
     636             :                 nullptr,
     637             :                 nullptr,
     638             :                 nullptr,
     639             :             },
     640             :             advgetopt::define_option(
     641             :                   advgetopt::Name("licence")    // to allow French spelling
     642             :                 , advgetopt::Alias("license")
     643             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     644             :             ),
     645             :             advgetopt::end_options()
     646             :         };
     647             : 
     648           1 :         advgetopt::options_environment environment_options;
     649           1 :         environment_options.f_project_name = "unittest";
     650           1 :         environment_options.f_options = options;
     651           1 :         environment_options.f_environment_flags = 0;
     652           1 :         environment_options.f_help_header = "Usage: name is nullptr";
     653             : 
     654           1 :         char const * cargv[] =
     655             :         {
     656             :             "tests/option-without-a-name",
     657             :             "--missing-name",
     658             :             nullptr
     659             :         };
     660           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     661           1 :         char ** argv = const_cast<char **>(cargv);
     662             : 
     663           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     664             :                 , advgetopt::getopt_logic_error
     665             :                 , Catch::Matchers::ExceptionMessage(
     666             :                           "getopt_logic_error: option long name missing or empty."));
     667             :     CATCH_END_SECTION()
     668             : 
     669          20 :     CATCH_START_SECTION("Options without a name (empty string)")
     670           1 :         advgetopt::option const options[] =
     671             :         {
     672             :             advgetopt::define_option(
     673             :                   advgetopt::Name("verbose")
     674             :                 , advgetopt::ShortName('v')
     675             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     676             :                 , advgetopt::Help("print info as we work.")
     677             :             ),
     678             :             {
     679             :                 // we have to enter this manually because define_option()
     680             :                 // forces you to enter a name, with C++17, we will also
     681             :                 // be able to verify the whole table at compile time
     682             :                 //
     683             :                 '\0',
     684             :                 advgetopt::GETOPT_FLAG_FLAG,
     685             :                 "",
     686             :                 nullptr,
     687             :                 nullptr,
     688             :                 nullptr,
     689             :             },
     690             :             advgetopt::define_option(
     691             :                   advgetopt::Name("licence")
     692             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     693             :             ),
     694             :             advgetopt::end_options()
     695             :         };
     696             : 
     697           1 :         advgetopt::options_environment environment_options;
     698           1 :         environment_options.f_project_name = "unittest";
     699           1 :         environment_options.f_options = options;
     700           1 :         environment_options.f_environment_flags = 0;
     701           1 :         environment_options.f_help_header = "Usage: name has a string but it's empty";
     702             : 
     703           1 :         char const * cargv[] =
     704             :         {
     705             :             "tests/option-without-a-name",
     706             :             "--missing-name",
     707             :             nullptr
     708             :         };
     709           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     710           1 :         char ** argv = const_cast<char **>(cargv);
     711             : 
     712           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     713             :                 , advgetopt::getopt_logic_error
     714             :                 , Catch::Matchers::ExceptionMessage(
     715             :                           "getopt_logic_error: option long name missing or empty."));
     716             :     CATCH_END_SECTION()
     717             : 
     718          20 :     CATCH_START_SECTION("Options with a one letter name")
     719           1 :         advgetopt::option const options[] =
     720             :         {
     721             :             advgetopt::define_option(
     722             :                   advgetopt::Name("verbose")
     723             :                 , advgetopt::ShortName('v')
     724             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     725             :                 , advgetopt::Help("print info as we work.")
     726             :             ),
     727             :             {
     728             :                 // we have to enter this manually because define_option()
     729             :                 // forces you to enter a name, with C++17, we will also
     730             :                 // be able to verify the whole table at compile time
     731             :                 //
     732             :                 '\0',
     733             :                 advgetopt::GETOPT_FLAG_FLAG,
     734             :                 "h",
     735             :                 nullptr,
     736             :                 nullptr,
     737             :                 nullptr,
     738             :             },
     739             :             advgetopt::define_option(
     740             :                   advgetopt::Name("licence")
     741             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     742             :             ),
     743             :             advgetopt::end_options()
     744             :         };
     745             : 
     746           1 :         advgetopt::options_environment environment_options;
     747           1 :         environment_options.f_project_name = "unittest";
     748           1 :         environment_options.f_options = options;
     749           1 :         environment_options.f_environment_flags = 0;
     750           1 :         environment_options.f_help_header = "Usage: name is only one letter";
     751             : 
     752           1 :         char const * cargv[] =
     753             :         {
     754             :             "tests/option-with-name-too-short",
     755             :             "--missing-name",
     756             :             nullptr
     757             :         };
     758           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     759           1 :         char ** argv = const_cast<char **>(cargv);
     760             : 
     761           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     762             :                 , advgetopt::getopt_logic_error
     763             :                 , Catch::Matchers::ExceptionMessage(
     764             :                           "getopt_logic_error: a long name option must be at least 2 characters."));
     765             :     CATCH_END_SECTION()
     766             : 
     767          20 :     CATCH_START_SECTION("Default option with a short name")
     768           1 :         advgetopt::option const options[] =
     769             :         {
     770             :             advgetopt::define_option(
     771             :                   advgetopt::Name("verbose")
     772             :                 , advgetopt::ShortName('v')
     773             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     774             :                 , advgetopt::Help("print info as we work.")
     775             :             ),
     776             :             advgetopt::define_option(
     777             :                   advgetopt::Name("--")
     778             :                 , advgetopt::ShortName('f')
     779             :                 , advgetopt::Flags(advgetopt::option_flags<advgetopt::GETOPT_FLAG_COMMAND_LINE>())
     780             :                 , advgetopt::Help("list of filenames.")
     781             :             ),
     782             :             advgetopt::end_options()
     783             :         };
     784             : 
     785           1 :         advgetopt::options_environment environment_options;
     786           1 :         environment_options.f_project_name = "unittest";
     787           1 :         environment_options.f_options = options;
     788           1 :         environment_options.f_environment_flags = 0;
     789           1 :         environment_options.f_help_header = "Usage: short name not acceptable with \"--\"";
     790             : 
     791           1 :         char const * cargv[] =
     792             :         {
     793             :             "tests/option-with-name-too-short",
     794             :             "--verbose",
     795             :             "file.txt",
     796             :             nullptr
     797             :         };
     798           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     799           1 :         char ** argv = const_cast<char **>(cargv);
     800             : 
     801           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     802             :                 , advgetopt::getopt_logic_error
     803             :                 , Catch::Matchers::ExceptionMessage(
     804             :                           "getopt_logic_error: option_info::option_info(): the default parameter \"--\" cannot include a short name ('f'.)"));
     805             :     CATCH_END_SECTION()
     806             : 
     807          20 :     CATCH_START_SECTION("Duplicated Options (Long Name)")
     808           1 :         advgetopt::option const options[] =
     809             :         {
     810             :             advgetopt::define_option(
     811             :                   advgetopt::Name("verbose")
     812             :                 , advgetopt::ShortName('v')
     813             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     814             :                 , advgetopt::Help("print info as we work.")
     815             :             ),
     816             :             advgetopt::define_option(
     817             :                   advgetopt::Name("licence")
     818             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     819             :             ),
     820             :             advgetopt::define_option(
     821             :                   advgetopt::Name("licence") // duplicate
     822             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     823             :             ),
     824             :             advgetopt::end_options()
     825             :         };
     826             : 
     827           1 :         advgetopt::options_environment environment_options;
     828           1 :         environment_options.f_project_name = "unittest";
     829           1 :         environment_options.f_options = options;
     830           1 :         environment_options.f_environment_flags = 0;
     831           1 :         environment_options.f_help_header = "Usage: one name can't be redefined";
     832             : 
     833           1 :         char const * cargv[] =
     834             :         {
     835             :             "tests/duplicated-option",
     836             :             "--missing-name",
     837             :             nullptr
     838             :         };
     839           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     840           1 :         char ** argv = const_cast<char **>(cargv);
     841             : 
     842           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     843             :                 , advgetopt::getopt_logic_error
     844             :                 , Catch::Matchers::ExceptionMessage(
     845             :                           "getopt_logic_error: option named \"licence\" found twice."));
     846             :     CATCH_END_SECTION()
     847             : 
     848          20 :     CATCH_START_SECTION("Duplicated Options (short name)")
     849           1 :         advgetopt::option const options[] =
     850             :         {
     851             :             advgetopt::define_option(
     852             :                   advgetopt::Name("verbose")
     853             :                 , advgetopt::ShortName('v')
     854             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     855             :                 , advgetopt::Help("print info as we work.")
     856             :             ),
     857             :             advgetopt::define_option(
     858             :                   advgetopt::Name("look")
     859             :                 , advgetopt::ShortName('l')
     860             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     861             :             ),
     862             :             advgetopt::define_option(
     863             :                   advgetopt::Name("lock")
     864             :                 , advgetopt::ShortName('l') // duplicate
     865             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     866             :             ),
     867             :             advgetopt::end_options()
     868             :         };
     869             : 
     870           1 :         advgetopt::options_environment environment_options;
     871           1 :         environment_options.f_project_name = "unittest";
     872           1 :         environment_options.f_options = options;
     873           1 :         environment_options.f_environment_flags = 0;
     874           1 :         environment_options.f_help_header = "Usage: one name can't be redefined";
     875             : 
     876           1 :         char const * cargv[] =
     877             :         {
     878             :             "tests/duplicated-option",
     879             :             "--missing-name",
     880             :             nullptr
     881             :         };
     882           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     883           1 :         char ** argv = const_cast<char **>(cargv);
     884             : 
     885           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     886             :                 , advgetopt::getopt_logic_error
     887             :                 , Catch::Matchers::ExceptionMessage(
     888             :                           "getopt_logic_error: option with short name \"l\" found twice."));
     889             :     CATCH_END_SECTION()
     890             : 
     891          20 :     CATCH_START_SECTION("Duplicated Default Options")
     892           1 :         advgetopt::option const options[] =
     893             :         {
     894             :             advgetopt::define_option(
     895             :                   advgetopt::Name("verbose")
     896             :                 , advgetopt::ShortName('v')
     897             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     898             :                 , advgetopt::Help("print info as we work.")
     899             :             ),
     900             :             advgetopt::define_option(
     901             :                   advgetopt::Name("ins")
     902             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_DEFAULT_OPTION>())
     903             :             ),
     904             :             advgetopt::define_option(
     905             :                   advgetopt::Name("outs")
     906             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_DEFAULT_OPTION>()) // default option again
     907             :             ),
     908             :             advgetopt::end_options()
     909             :         };
     910             : 
     911           1 :         advgetopt::options_environment environment_options;
     912           1 :         environment_options.f_project_name = "unittest";
     913           1 :         environment_options.f_options = options;
     914           1 :         environment_options.f_environment_flags = 0;
     915           1 :         environment_options.f_help_header = "Usage: one name can't be redefined";
     916             : 
     917           1 :         char const * cargv[] =
     918             :         {
     919             :             "tests/duplicated-option",
     920             :             "--missing-name",
     921             :             nullptr
     922             :         };
     923           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     924           1 :         char ** argv = const_cast<char **>(cargv);
     925             : 
     926           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     927             :                 , advgetopt::getopt_logic_error
     928             :                 , Catch::Matchers::ExceptionMessage(
     929             :                           "getopt_logic_error: two default options found."));
     930             :     CATCH_END_SECTION()
     931             : 
     932          20 :     CATCH_START_SECTION("Default Option marked as being a FLAG")
     933           1 :         advgetopt::option const options[] =
     934             :         {
     935             :             advgetopt::define_option(
     936             :                   advgetopt::Name("verbose")
     937             :                 , advgetopt::ShortName('v')
     938             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     939             :                 , advgetopt::Help("print info as we work.")
     940             :             ),
     941             :             // the define_option() already catches this error at compile time
     942             :             {
     943             :                   'o'
     944             :                 , advgetopt::GETOPT_FLAG_COMMAND_LINE | advgetopt::GETOPT_FLAG_DEFAULT_OPTION | advgetopt::GETOPT_FLAG_FLAG
     945             :                 , "output"
     946             :                 , nullptr
     947             :                 , nullptr
     948             :                 , nullptr
     949             :             },
     950             :             advgetopt::end_options()
     951             :         };
     952             : 
     953           1 :         advgetopt::options_environment environment_options;
     954           1 :         environment_options.f_project_name = "unittest";
     955           1 :         environment_options.f_options = options;
     956           1 :         environment_options.f_environment_flags = 0;
     957           1 :         environment_options.f_help_header = "Usage: one name can't be redefined";
     958             : 
     959           1 :         char const * cargv[] =
     960             :         {
     961             :             "tests/duplicated-option",
     962             :             "--missing-name",
     963             :             nullptr
     964             :         };
     965           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     966           1 :         char ** argv = const_cast<char **>(cargv);
     967             : 
     968           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
     969             :                 , advgetopt::getopt_logic_error
     970             :                 , Catch::Matchers::ExceptionMessage(
     971             :                           "getopt_logic_error: a default option must accept parameters, it can't be a GETOPT_FLAG_FLAG."));
     972             :     CATCH_END_SECTION()
     973             : 
     974          20 :     CATCH_START_SECTION("Option with an alias and mismatched flags")
     975           1 :         advgetopt::option const options[] =
     976             :         {
     977             :             advgetopt::define_option(
     978             :                   advgetopt::Name("verbose")
     979             :                 , advgetopt::ShortName('v')
     980             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     981             :                 , advgetopt::Help("print info as we work.")
     982             :             ),
     983             :             advgetopt::define_option(
     984             :                   advgetopt::Name("licence")    // to allow French spelling
     985             :                 , advgetopt::Alias("license")
     986             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_COMMANDS
     987             :                                                           , advgetopt::GETOPT_FLAG_REQUIRED>()) // not a match
     988             :             ),
     989             :             advgetopt::end_options()
     990             :         };
     991             : 
     992           1 :         advgetopt::options_environment environment_options;
     993           1 :         environment_options.f_project_name = "unittest";
     994           1 :         environment_options.f_options = options;
     995           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     996           1 :         environment_options.f_help_header = "Usage: flags are not equal";
     997             : 
     998           1 :         char const * cargv[] =
     999             :         {
    1000             :             "tests/option-without-a-name",
    1001             :             "--incompatible-flags",
    1002             :             nullptr
    1003             :         };
    1004           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
    1005           1 :         char ** argv = const_cast<char **>(cargv);
    1006             : 
    1007           1 :         CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
    1008             :                 , advgetopt::getopt_logic_error
    1009             :                 , Catch::Matchers::ExceptionMessage(
    1010             :                           "getopt_logic_error: the flags of alias \"licence\" (0x100041) are different"
    1011             :                           " than the flags of \"license\" (0x100021)."));
    1012             :     CATCH_END_SECTION()
    1013          10 : }
    1014             : 
    1015             : 
    1016             : 
    1017             : 
    1018           4 : CATCH_TEST_CASE("invalid_config_dir_short_name", "[arguments][invalid][getopt][config]")
    1019             : {
    1020           4 :     CATCH_START_SECTION("Trying to set '-o' as '--config-dir' short name")
    1021             :     {
    1022           1 :         advgetopt::option const options[] =
    1023             :         {
    1024             :             advgetopt::define_option(
    1025             :                   advgetopt::Name("out")
    1026             :                 , advgetopt::ShortName('o')
    1027             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
    1028             :                 , advgetopt::Help("output filename.")
    1029             :             ),
    1030             :             advgetopt::end_options()
    1031             :         };
    1032             : 
    1033           1 :         advgetopt::options_environment environment_options;
    1034           1 :         environment_options.f_project_name = "unittest";
    1035           1 :         environment_options.f_options = options;
    1036           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
    1037           1 :         environment_options.f_configuration_filename = "snapwatchdog.conf";
    1038           1 :         environment_options.f_help_header = "Usage: test --config-dir";
    1039             : 
    1040           2 :         advgetopt::getopt opt(environment_options);
    1041             : 
    1042           1 :         CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
    1043           1 :         CATCH_REQUIRE_THROWS_MATCHES(
    1044             :                   opt.set_short_name("config-dir", U'o')
    1045             :                 , advgetopt::getopt_logic_error
    1046             :                 , Catch::Matchers::ExceptionMessage(
    1047             :                               "getopt_logic_error: found another option (\"out\") with short name 'o'."));
    1048             :     }
    1049             :     CATCH_END_SECTION()
    1050             : 
    1051           4 :     CATCH_START_SECTION("Trying to set '-c' as '--config-dir' short name, buf configuration filename is nullptr")
    1052             :     {
    1053           1 :         advgetopt::option const options[] =
    1054             :         {
    1055             :             advgetopt::define_option(
    1056             :                   advgetopt::Name("out")
    1057             :                 , advgetopt::ShortName('o')
    1058             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
    1059             :                 , advgetopt::Help("output filename.")
    1060             :             ),
    1061             :             advgetopt::end_options()
    1062             :         };
    1063             : 
    1064           1 :         advgetopt::options_environment environment_options;
    1065           1 :         environment_options.f_project_name = "unittest";
    1066           1 :         environment_options.f_options = options;
    1067           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
    1068           1 :         environment_options.f_configuration_filename = nullptr;
    1069           1 :         environment_options.f_help_header = "Usage: test --config-dir";
    1070             : 
    1071           2 :         advgetopt::getopt opt(environment_options);
    1072             : 
    1073             :         advgetopt::option_info::pointer_t config_dir();
    1074           1 :         CATCH_REQUIRE(opt.get_option("config-dir") == nullptr);
    1075           1 :         CATCH_REQUIRE_THROWS_MATCHES(
    1076             :                   opt.set_short_name("config-dir", U'c')
    1077             :                 , advgetopt::getopt_logic_error
    1078             :                 , Catch::Matchers::ExceptionMessage(
    1079             :                               "getopt_logic_error: option with name \"config-dir\" not found."));
    1080             :     }
    1081             :     CATCH_END_SECTION()
    1082             : 
    1083             : //    CATCH_START_SECTION("Trying to set NO_SHORT_NAME as '--config-dir' short name")
    1084             : //    {
    1085             : //        advgetopt::option const options[] =
    1086             : //        {
    1087             : //            advgetopt::define_option(
    1088             : //                  advgetopt::Name("out")
    1089             : //                , advgetopt::ShortName('o')
    1090             : //                , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
    1091             : //                , advgetopt::Help("output filename.")
    1092             : //            ),
    1093             : //            advgetopt::end_options()
    1094             : //        };
    1095             : //
    1096             : //        advgetopt::options_environment environment_options;
    1097             : //        environment_options.f_project_name = "unittest";
    1098             : //        environment_options.f_options = options;
    1099             : //        environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
    1100             : //        environment_options.f_configuration_filename = "snapwatchdog.conf";
    1101             : //        environment_options.f_help_header = "Usage: test --config-dir";
    1102             : //
    1103             : //        advgetopt::getopt opt(environment_options);
    1104             : //
    1105             : //        CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
    1106             : //        CATCH_REQUIRE_THROWS_MATCHES(
    1107             : //                  opt.set_short_name("config-dir", advgetopt::NO_SHORT_NAME)
    1108             : //                , advgetopt::getopt_logic_error
    1109             : //                , Catch::Matchers::ExceptionMessage(
    1110             : //                              "getopt_logic_error: The short name of option \"config-dir\" cannot be set to NO_SHORT_NAME."));
    1111             : //    }
    1112             : //    CATCH_END_SECTION()
    1113             : //
    1114             : //    CATCH_START_SECTION("Trying to change short name of '--version'")
    1115             : //    {
    1116             : //        advgetopt::option const options[] =
    1117             : //        {
    1118             : //            advgetopt::define_option(
    1119             : //                  advgetopt::Name("out")
    1120             : //                , advgetopt::ShortName('o')
    1121             : //                , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
    1122             : //                , advgetopt::Help("output filename.")
    1123             : //            ),
    1124             : //            advgetopt::end_options()
    1125             : //        };
    1126             : //
    1127             : //        advgetopt::options_environment environment_options;
    1128             : //        environment_options.f_project_name = "unittest";
    1129             : //        environment_options.f_options = options;
    1130             : //        environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
    1131             : //        environment_options.f_configuration_filename = "";
    1132             : //        environment_options.f_help_header = "Usage: test --config-dir";
    1133             : //
    1134             : //        advgetopt::getopt opt(environment_options);
    1135             : //
    1136             : //        CATCH_REQUIRE(opt.get_option("version") != nullptr);
    1137             : //        CATCH_REQUIRE_THROWS_MATCHES(
    1138             : //                  opt.set_short_name("version", U'v')   // set to lowercase...
    1139             : //                , advgetopt::getopt_logic_error
    1140             : //                , Catch::Matchers::ExceptionMessage(
    1141             : //                              "getopt_logic_error: The short name of option \"version\" cannot be changed from 'V' to 'v'."));
    1142             : //    }
    1143             : //    CATCH_END_SECTION()
    1144           8 : }
    1145             : 
    1146             : 
    1147             : 
    1148             : 
    1149             : 
    1150             : 
    1151             : 
    1152             : 
    1153             : 
    1154             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13