LCOV - code coverage report
Current view: top level - tests - catch_string.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 56 56 100.0 %
Date: 2022-07-15 09:02:52 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2022  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/flags.h>
      23             : #include    <advgetopt/exception.h>
      24             : 
      25             : 
      26             : // self
      27             : //
      28             : #include    "catch_main.h"
      29             : 
      30             : 
      31             : // snapdev
      32             : //
      33             : #include    <snapdev/safe_setenv.h>
      34             : 
      35             : 
      36             : // C++
      37             : //
      38             : #include    <fstream>
      39             : 
      40             : 
      41             : // last include
      42             : //
      43             : #include    <snapdev/poison.h>
      44             : 
      45             : 
      46             : 
      47             : 
      48             : 
      49             : 
      50             : 
      51           7 : CATCH_TEST_CASE("option_string", "[getopt][string]")
      52             : {
      53          10 :     CATCH_START_SECTION("empty string returns an empty empty")
      54             :     {
      55           1 :         CATCH_REQUIRE(advgetopt::escape_shell_argument(std::string()) == std::string("\"\""));
      56             :     }
      57             :     CATCH_END_SECTION()
      58             : 
      59          10 :     CATCH_START_SECTION("empty string returns empty")
      60             :     {
      61           2 :         std::string const g_simple_characters("_/.-+=0123456789ABCEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz");
      62          67 :         for(std::size_t i(0); i < g_simple_characters.length(); ++i)
      63             :         {
      64         132 :             std::string t;
      65          66 :             t += g_simple_characters[i];
      66          66 :             CATCH_REQUIRE(advgetopt::escape_shell_argument(t) == t);
      67             :         }
      68             :     }
      69             :     CATCH_END_SECTION()
      70             : 
      71          10 :     CATCH_START_SECTION("string in single quotes")
      72             :     {
      73           1 :         CATCH_REQUIRE(advgetopt::escape_shell_argument("'between quotes'") == std::string("''\\''between quotes'\\'''"));
      74             :     }
      75             :     CATCH_END_SECTION()
      76             : 
      77          10 :     CATCH_START_SECTION("string with apostrophe")
      78             :     {
      79           1 :         CATCH_REQUIRE(advgetopt::escape_shell_argument("c'est un test") == std::string("'c'\\''est un test'"));
      80             :     }
      81             :     CATCH_END_SECTION()
      82             : 
      83          10 :     CATCH_START_SECTION("string with special characters")
      84             :     {
      85           1 :         CATCH_REQUIRE(advgetopt::escape_shell_argument("space colon: and semi-colon;") == std::string("'space colon: and semi-colon;'"));
      86             :     }
      87             :     CATCH_END_SECTION()
      88           5 : }
      89             : 
      90             : 
      91           3 : CATCH_TEST_CASE("options_to_string", "[arguments][valid][getopt]")
      92             : {
      93           2 :     CATCH_START_SECTION("Transform command line options back to a shell compatible command.")
      94             :     {
      95             :         // create a getopt object with options
      96             :         //
      97           1 :         advgetopt::option const options[] =
      98             :         {
      99             :             advgetopt::define_option(
     100             :                   advgetopt::Name("verbose")
     101             :                 , advgetopt::ShortName('v')
     102             :                 , advgetopt::Flags(advgetopt::standalone_command_flags())
     103             :                 , advgetopt::EnvironmentVariableName("VERBOSE")
     104             :                 , advgetopt::Help("print info as we work.")
     105             :             ),
     106             :             advgetopt::define_option(
     107             :                   advgetopt::Name("coordinates")
     108             :                 , advgetopt::ShortName('C')
     109             :                 , advgetopt::Flags(advgetopt::command_flags<
     110             :                           advgetopt::GETOPT_FLAG_REQUIRED
     111             :                         , advgetopt::GETOPT_FLAG_MULTIPLE>())
     112             :                 , advgetopt::EnvironmentVariableName("COORDINATES")
     113             :                 , advgetopt::Help("define the angle.")
     114             :             ),
     115             :             advgetopt::define_option(
     116             :                   advgetopt::Name("angle")
     117             :                 , advgetopt::ShortName('a')
     118             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
     119             :                 , advgetopt::DefaultValue("90")
     120             :                 , advgetopt::EnvironmentVariableName("ANGLE")
     121             :                 , advgetopt::Help("define the angle.")
     122             :             ),
     123             :             advgetopt::define_option(
     124             :                   advgetopt::Name("weight")
     125             :                 , advgetopt::ShortName('w')
     126             :                 , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
     127             :                 , advgetopt::DefaultValue("455")
     128             :                 , advgetopt::EnvironmentVariableName("WEIGHT")
     129             :                 , advgetopt::Help("define the weight.")
     130             :             ),
     131             :             advgetopt::define_option(
     132             :                   advgetopt::Name("--")
     133             :                 , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_MULTIPLE>())
     134             :                 , advgetopt::EnvironmentVariableName("FILES")
     135             :                 , advgetopt::Help("list of filenames.")
     136             :             ),
     137             :             advgetopt::end_options()
     138             :         };
     139             : 
     140           1 :         advgetopt::options_environment environment_options;
     141           1 :         environment_options.f_project_name = "unittest";
     142           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS;
     143           1 :         environment_options.f_help_header = "Usage: testing system arguments.";
     144           1 :         environment_options.f_options = options;
     145           1 :         environment_options.f_version = "3.1.5";
     146           1 :         environment_options.f_license = "MIT";
     147           1 :         environment_options.f_copyright = "Copyright (c) 2022  Made to Order Software Corp. -- All Rights Reserved";
     148           1 :         environment_options.f_build_date = "Jun  4 2019";
     149           1 :         environment_options.f_build_time = "23:02:36";
     150             : 
     151           2 :         CATCH_WHEN("Mix environment variable and command line options")
     152             :         {
     153           2 :             snapdev::safe_setenv env_size("WEIGHT", "303.183");
     154             : 
     155           1 :             char const * cargv[] =
     156             :             {
     157             :                 "tests/system-arguments",
     158             :                 "-C",
     159             :                 "33",
     160             :                 "52",
     161             :                 "109",
     162             :                 "17",
     163             :                 "37",
     164             :                 "--verbose",
     165             :                 "file1",
     166             :                 "more2",
     167             :                 "info3",
     168             :                 "-a",
     169             :                 "90",
     170             :                 nullptr
     171             :             };
     172           1 :             int const argc = sizeof(cargv) / sizeof(cargv[0]) - 1;
     173           1 :             char ** argv = const_cast<char **>(cargv);
     174             : 
     175             :             // the command line has priority, but the MULTIPLE creates a
     176             :             // problem here...
     177             :             //
     178           2 :             advgetopt::getopt::pointer_t opt(std::make_shared<advgetopt::getopt>(environment_options, argc, argv));
     179           1 :             CATCH_REQUIRE(opt != nullptr);
     180             : 
     181           1 :             CATCH_REQUIRE(opt->is_defined("verbose"));
     182             : 
     183           1 :             CATCH_REQUIRE(opt->is_defined("coordinates"));
     184           1 :             CATCH_REQUIRE(opt->get_string("coordinates") == "33");
     185           1 :             CATCH_REQUIRE(opt->get_string("coordinates", 1) == "52");
     186           1 :             CATCH_REQUIRE(opt->get_string("coordinates", 2) == "109");
     187           1 :             CATCH_REQUIRE(opt->get_string("coordinates", 3) == "17");
     188           1 :             CATCH_REQUIRE(opt->get_string("coordinates", 4) == "37");
     189             : 
     190           1 :             CATCH_REQUIRE(opt->is_defined("weight"));
     191           1 :             CATCH_REQUIRE(opt->get_string("weight") == "303.183");
     192             : 
     193           1 :             CATCH_REQUIRE(opt->is_defined("angle"));
     194           1 :             CATCH_REQUIRE(opt->get_string("angle") == "90");
     195             : 
     196           1 :             CATCH_REQUIRE(opt->is_defined("--"));
     197           1 :             CATCH_REQUIRE(opt->get_string("--", 0) == "file1");
     198           1 :             CATCH_REQUIRE(opt->get_string("--", 1) == "more2");
     199           1 :             CATCH_REQUIRE(opt->get_string("--", 2) == "info3");
     200             : 
     201           1 :             CATCH_REQUIRE(opt->options_to_string() == "--coordinates 33 52 109 17 37 --verbose --weight 303.183 -- file1 more2 info3");
     202           1 :             CATCH_REQUIRE(opt->options_to_string(true) == "tests/system-arguments --coordinates 33 52 109 17 37 --verbose --weight 303.183 -- file1 more2 info3");
     203           1 :             CATCH_REQUIRE(opt->options_to_string(false, true) == "--angle 90 --coordinates 33 52 109 17 37 --verbose --weight 303.183 -- file1 more2 info3");
     204           1 :             CATCH_REQUIRE(opt->options_to_string(true, true) == "tests/system-arguments --angle 90 --coordinates 33 52 109 17 37 --verbose --weight 303.183 -- file1 more2 info3");
     205             :         }
     206             :     }
     207             :     CATCH_END_SECTION()
     208           7 : }
     209             : 
     210             : 
     211             : 
     212             : 
     213             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13