LCOV - code coverage report
Current view: top level - tests - catch_utils.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 590 590
Test Date: 2026-01-18 09:38:57 Functions: 100.0 % 7 7
Legend: Lines: hit not hit

            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/utils.h>
      23              : 
      24              : #include    <advgetopt/conf_file.h>
      25              : #include    <advgetopt/exception.h>
      26              : 
      27              : 
      28              : // self
      29              : //
      30              : #include    "catch_main.h"
      31              : 
      32              : 
      33              : // snapdev
      34              : //
      35              : #include    <snapdev/safe_setenv.h>
      36              : 
      37              : 
      38              : // C++
      39              : //
      40              : #include    <fstream>
      41              : #include    <random>
      42              : 
      43              : 
      44              : // last include
      45              : //
      46              : #include    <snapdev/poison.h>
      47              : 
      48              : 
      49              : 
      50            2 : CATCH_TEST_CASE("utils_unquote", "[utils][valid]")
      51              : {
      52            2 :     CATCH_START_SECTION("utils_unquote: unquote, default pairs")
      53              :     {
      54            5 :         CATCH_REQUIRE(advgetopt::unquote("") == "");
      55            5 :         CATCH_REQUIRE(advgetopt::unquote("a") == "a");
      56            5 :         CATCH_REQUIRE(advgetopt::unquote("ab") == "ab");
      57            5 :         CATCH_REQUIRE(advgetopt::unquote("abc") == "abc");
      58              : 
      59            5 :         CATCH_REQUIRE(advgetopt::unquote("''") == "");
      60            5 :         CATCH_REQUIRE(advgetopt::unquote("'a'") == "a");
      61            5 :         CATCH_REQUIRE(advgetopt::unquote("'ab'") == "ab");
      62            5 :         CATCH_REQUIRE(advgetopt::unquote("'abc'") == "abc");
      63            5 :         CATCH_REQUIRE(advgetopt::unquote("'abcd'") == "abcd");
      64              : 
      65            5 :         CATCH_REQUIRE(advgetopt::unquote("\"\"") == "");
      66            5 :         CATCH_REQUIRE(advgetopt::unquote("\"a\"") == "a");
      67            5 :         CATCH_REQUIRE(advgetopt::unquote("\"ab\"") == "ab");
      68            5 :         CATCH_REQUIRE(advgetopt::unquote("\"abc\"") == "abc");
      69            5 :         CATCH_REQUIRE(advgetopt::unquote("\"abcd\"") == "abcd");
      70              : 
      71            5 :         CATCH_REQUIRE(advgetopt::unquote("\"'") == "\"'");
      72            5 :         CATCH_REQUIRE(advgetopt::unquote("\"a'") == "\"a'");
      73            5 :         CATCH_REQUIRE(advgetopt::unquote("\"ab'") == "\"ab'");
      74            5 :         CATCH_REQUIRE(advgetopt::unquote("\"abc'") == "\"abc'");
      75            5 :         CATCH_REQUIRE(advgetopt::unquote("\"abcd'") == "\"abcd'");
      76              : 
      77            5 :         CATCH_REQUIRE(advgetopt::unquote("'\"") == "'\"");
      78            5 :         CATCH_REQUIRE(advgetopt::unquote("'a\"") == "'a\"");
      79            5 :         CATCH_REQUIRE(advgetopt::unquote("'ab\"") == "'ab\"");
      80            5 :         CATCH_REQUIRE(advgetopt::unquote("'abc\"") == "'abc\"");
      81            5 :         CATCH_REQUIRE(advgetopt::unquote("'abcd\"") == "'abcd\"");
      82              : 
      83            5 :         CATCH_REQUIRE(advgetopt::unquote("\"") == "\"");
      84            5 :         CATCH_REQUIRE(advgetopt::unquote("\"a") == "\"a");
      85            5 :         CATCH_REQUIRE(advgetopt::unquote("\"ab") == "\"ab");
      86            5 :         CATCH_REQUIRE(advgetopt::unquote("\"abc") == "\"abc");
      87            5 :         CATCH_REQUIRE(advgetopt::unquote("\"abcd") == "\"abcd");
      88              : 
      89            5 :         CATCH_REQUIRE(advgetopt::unquote("'") == "'");
      90            5 :         CATCH_REQUIRE(advgetopt::unquote("'a") == "'a");
      91            5 :         CATCH_REQUIRE(advgetopt::unquote("'ab") == "'ab");
      92            5 :         CATCH_REQUIRE(advgetopt::unquote("'abc") == "'abc");
      93            5 :         CATCH_REQUIRE(advgetopt::unquote("'abcd") == "'abcd");
      94              : 
      95            5 :         CATCH_REQUIRE(advgetopt::unquote("'") == "'");
      96            5 :         CATCH_REQUIRE(advgetopt::unquote("a'") == "a'");
      97            5 :         CATCH_REQUIRE(advgetopt::unquote("ab'") == "ab'");
      98            5 :         CATCH_REQUIRE(advgetopt::unquote("abc'") == "abc'");
      99            5 :         CATCH_REQUIRE(advgetopt::unquote("abcd'") == "abcd'");
     100              : 
     101            5 :         CATCH_REQUIRE(advgetopt::unquote("\"") == "\"");
     102            5 :         CATCH_REQUIRE(advgetopt::unquote("a\"") == "a\"");
     103            5 :         CATCH_REQUIRE(advgetopt::unquote("ab\"") == "ab\"");
     104            5 :         CATCH_REQUIRE(advgetopt::unquote("abc\"") == "abc\"");
     105            5 :         CATCH_REQUIRE(advgetopt::unquote("abcd\"") == "abcd\"");
     106              :     }
     107            2 :     CATCH_END_SECTION()
     108              : 
     109            2 :     CATCH_START_SECTION("utils_unquote: unquote, brackets")
     110              :     {
     111            5 :         CATCH_REQUIRE(advgetopt::unquote("", "[]<>{}") == "");
     112            5 :         CATCH_REQUIRE(advgetopt::unquote("a", "[]<>{}") == "a");
     113            5 :         CATCH_REQUIRE(advgetopt::unquote("ab", "[]<>{}") == "ab");
     114            5 :         CATCH_REQUIRE(advgetopt::unquote("abc", "[]<>{}") == "abc");
     115              : 
     116            5 :         CATCH_REQUIRE(advgetopt::unquote("{}", "[]<>{}") == "");
     117            5 :         CATCH_REQUIRE(advgetopt::unquote("{a}", "[]<>{}") == "a");
     118            5 :         CATCH_REQUIRE(advgetopt::unquote("{ab}", "[]<>{}") == "ab");
     119            5 :         CATCH_REQUIRE(advgetopt::unquote("{abc}", "[]<>{}") == "abc");
     120            5 :         CATCH_REQUIRE(advgetopt::unquote("{abcd}", "[]<>{}") == "abcd");
     121              : 
     122            5 :         CATCH_REQUIRE(advgetopt::unquote("[]", "[]<>{}") == "");
     123            5 :         CATCH_REQUIRE(advgetopt::unquote("[a]", "[]<>{}") == "a");
     124            5 :         CATCH_REQUIRE(advgetopt::unquote("[ab]", "[]<>{}") == "ab");
     125            5 :         CATCH_REQUIRE(advgetopt::unquote("[abc]", "[]<>{}") == "abc");
     126            5 :         CATCH_REQUIRE(advgetopt::unquote("[abcd]", "[]<>{}") == "abcd");
     127              : 
     128            5 :         CATCH_REQUIRE(advgetopt::unquote("<>", "[]<>{}") == "");
     129            5 :         CATCH_REQUIRE(advgetopt::unquote("<a>", "[]<>{}") == "a");
     130            5 :         CATCH_REQUIRE(advgetopt::unquote("<ab>", "[]<>{}") == "ab");
     131            5 :         CATCH_REQUIRE(advgetopt::unquote("<abc>", "[]<>{}") == "abc");
     132            5 :         CATCH_REQUIRE(advgetopt::unquote("<abcd>", "[]<>{}") == "abcd");
     133              : 
     134            5 :         CATCH_REQUIRE(advgetopt::unquote("[}", "[]<>{}") == "[}");
     135            5 :         CATCH_REQUIRE(advgetopt::unquote("[a}", "[]<>{}") == "[a}");
     136            5 :         CATCH_REQUIRE(advgetopt::unquote("[ab}", "[]<>{}") == "[ab}");
     137            5 :         CATCH_REQUIRE(advgetopt::unquote("[abc}", "[]<>{}") == "[abc}");
     138            5 :         CATCH_REQUIRE(advgetopt::unquote("[abcd}", "[]<>{}") == "[abcd}");
     139              : 
     140            5 :         CATCH_REQUIRE(advgetopt::unquote("[>", "[]<>{}") == "[>");
     141            5 :         CATCH_REQUIRE(advgetopt::unquote("[a>", "[]<>{}") == "[a>");
     142            5 :         CATCH_REQUIRE(advgetopt::unquote("[ab>", "[]<>{}") == "[ab>");
     143            5 :         CATCH_REQUIRE(advgetopt::unquote("[abc>", "[]<>{}") == "[abc>");
     144            5 :         CATCH_REQUIRE(advgetopt::unquote("[abcd>", "[]<>{}") == "[abcd>");
     145              : 
     146            5 :         CATCH_REQUIRE(advgetopt::unquote("'\"", "[]<>{}") == "'\"");
     147            5 :         CATCH_REQUIRE(advgetopt::unquote("'a\"", "[]<>{}") == "'a\"");
     148            5 :         CATCH_REQUIRE(advgetopt::unquote("'ab\"", "[]<>{}") == "'ab\"");
     149            5 :         CATCH_REQUIRE(advgetopt::unquote("'abc\"", "[]<>{}") == "'abc\"");
     150            5 :         CATCH_REQUIRE(advgetopt::unquote("'abcd\"", "[]<>{}") == "'abcd\"");
     151              : 
     152            5 :         CATCH_REQUIRE(advgetopt::unquote("[", "[]<>{}") == "[");
     153            5 :         CATCH_REQUIRE(advgetopt::unquote("[a", "[]<>{}") == "[a");
     154            5 :         CATCH_REQUIRE(advgetopt::unquote("[ab", "[]<>{}") == "[ab");
     155            5 :         CATCH_REQUIRE(advgetopt::unquote("[abc", "[]<>{}") == "[abc");
     156            5 :         CATCH_REQUIRE(advgetopt::unquote("[abcd", "[]<>{}") == "[abcd");
     157              : 
     158            5 :         CATCH_REQUIRE(advgetopt::unquote("{", "[]<>{}") == "{");
     159            5 :         CATCH_REQUIRE(advgetopt::unquote("{a", "[]<>{}") == "{a");
     160            5 :         CATCH_REQUIRE(advgetopt::unquote("{ab", "[]<>{}") == "{ab");
     161            5 :         CATCH_REQUIRE(advgetopt::unquote("{abc", "[]<>{}") == "{abc");
     162            5 :         CATCH_REQUIRE(advgetopt::unquote("{abcd", "[]<>{}") == "{abcd");
     163              : 
     164            5 :         CATCH_REQUIRE(advgetopt::unquote("<", "[]<>{}") == "<");
     165            5 :         CATCH_REQUIRE(advgetopt::unquote("<a", "[]<>{}") == "<a");
     166            5 :         CATCH_REQUIRE(advgetopt::unquote("<ab", "[]<>{}") == "<ab");
     167            5 :         CATCH_REQUIRE(advgetopt::unquote("<abc", "[]<>{}") == "<abc");
     168            5 :         CATCH_REQUIRE(advgetopt::unquote("<abcd", "[]<>{}") == "<abcd");
     169              : 
     170            5 :         CATCH_REQUIRE(advgetopt::unquote("}", "[]<>{}") == "}");
     171            5 :         CATCH_REQUIRE(advgetopt::unquote("a}", "[]<>{}") == "a}");
     172            5 :         CATCH_REQUIRE(advgetopt::unquote("ab}", "[]<>{}") == "ab}");
     173            5 :         CATCH_REQUIRE(advgetopt::unquote("abc}", "[]<>{}") == "abc}");
     174            5 :         CATCH_REQUIRE(advgetopt::unquote("abcd}", "[]<>{}") == "abcd}");
     175              : 
     176            5 :         CATCH_REQUIRE(advgetopt::unquote("]", "[]<>{}") == "]");
     177            5 :         CATCH_REQUIRE(advgetopt::unquote("a]", "[]<>{}") == "a]");
     178            5 :         CATCH_REQUIRE(advgetopt::unquote("ab]", "[]<>{}") == "ab]");
     179            5 :         CATCH_REQUIRE(advgetopt::unquote("abc]", "[]<>{}") == "abc]");
     180            5 :         CATCH_REQUIRE(advgetopt::unquote("abcd]", "[]<>{}") == "abcd]");
     181              : 
     182            5 :         CATCH_REQUIRE(advgetopt::unquote(">", "[]<>{}") == ">");
     183            5 :         CATCH_REQUIRE(advgetopt::unquote("a>", "[]<>{}") == "a>");
     184            5 :         CATCH_REQUIRE(advgetopt::unquote("ab>", "[]<>{}") == "ab>");
     185            5 :         CATCH_REQUIRE(advgetopt::unquote("abc>", "[]<>{}") == "abc>");
     186            5 :         CATCH_REQUIRE(advgetopt::unquote("abcd>", "[]<>{}") == "abcd>");
     187              :     }
     188            2 :     CATCH_END_SECTION()
     189            2 : }
     190              : 
     191              : 
     192              : 
     193              : 
     194            1 : CATCH_TEST_CASE("utils_quote", "[utils][valid]")
     195              : {
     196            1 :     CATCH_START_SECTION("utils_quote: quote, default pairs")
     197              :     {
     198            3 :         CATCH_REQUIRE(advgetopt::quote("") == "\"\"");
     199            3 :         CATCH_REQUIRE(advgetopt::quote("a") == "\"a\"");
     200            3 :         CATCH_REQUIRE(advgetopt::quote("ab") == "\"ab\"");
     201            3 :         CATCH_REQUIRE(advgetopt::quote("abc") == "\"abc\"");
     202              : 
     203            3 :         CATCH_REQUIRE(advgetopt::quote("", '"') == "\"\"");
     204            3 :         CATCH_REQUIRE(advgetopt::quote("a", '"') == "\"a\"");
     205            3 :         CATCH_REQUIRE(advgetopt::quote("ab", '"') == "\"ab\"");
     206            3 :         CATCH_REQUIRE(advgetopt::quote("abc", '"') == "\"abc\"");
     207            3 :         CATCH_REQUIRE(advgetopt::quote("abcd", '"') == "\"abcd\"");
     208              : 
     209            3 :         CATCH_REQUIRE(advgetopt::quote("", '\'') == "''");
     210            3 :         CATCH_REQUIRE(advgetopt::quote("a", '\'') == "'a'");
     211            3 :         CATCH_REQUIRE(advgetopt::quote("ab", '\'') == "'ab'");
     212            3 :         CATCH_REQUIRE(advgetopt::quote("abc", '\'') == "'abc'");
     213            3 :         CATCH_REQUIRE(advgetopt::quote("abcd", '\'') == "'abcd'");
     214              : 
     215            3 :         CATCH_REQUIRE(advgetopt::quote("", '[', ']') == "[]");
     216            3 :         CATCH_REQUIRE(advgetopt::quote("a", '[', ']') == "[a]");
     217            3 :         CATCH_REQUIRE(advgetopt::quote("ab", '[', ']') == "[ab]");
     218            3 :         CATCH_REQUIRE(advgetopt::quote("abc", '[', ']') == "[abc]");
     219            3 :         CATCH_REQUIRE(advgetopt::quote("abcd", '[', ']') == "[abcd]");
     220              : 
     221            3 :         CATCH_REQUIRE(advgetopt::quote("[]", '[', ']') == "[\\[\\]]");
     222            3 :         CATCH_REQUIRE(advgetopt::quote("[a]", '[', ']') == "[\\[a\\]]");
     223            3 :         CATCH_REQUIRE(advgetopt::quote("[ab]", '[', ']') == "[\\[ab\\]]");
     224            3 :         CATCH_REQUIRE(advgetopt::quote("[abc]", '[', ']') == "[\\[abc\\]]");
     225            3 :         CATCH_REQUIRE(advgetopt::quote("[abcd]", '[', ']') == "[\\[abcd\\]]");
     226              :     }
     227            1 :     CATCH_END_SECTION()
     228            1 : }
     229              : 
     230              : 
     231              : 
     232              : 
     233           11 : CATCH_TEST_CASE("utils_split", "[utils][valid]")
     234              : {
     235           11 :     CATCH_START_SECTION("utils_split: split three words")
     236              :     {
     237            1 :         advgetopt::string_list_t result;
     238            5 :         advgetopt::split_string("test with spaces"
     239              :                               , result
     240              :                               , {" "});
     241            1 :         CATCH_REQUIRE(result.size() == 3);
     242            1 :         CATCH_REQUIRE(result[0] == "test");
     243            1 :         CATCH_REQUIRE(result[1] == "with");
     244            1 :         CATCH_REQUIRE(result[2] == "spaces");
     245            1 :     }
     246           11 :     CATCH_END_SECTION()
     247              : 
     248           11 :     CATCH_START_SECTION("utils_split: split three words, one with single quotes")
     249              :     {
     250            1 :         advgetopt::string_list_t result;
     251            5 :         advgetopt::split_string("test 'with quotes and' spaces"
     252              :                               , result
     253              :                               , {" "});
     254            1 :         CATCH_REQUIRE(result.size() == 3);
     255            1 :         CATCH_REQUIRE(result[0] == "test");
     256            1 :         CATCH_REQUIRE(result[1] == "with quotes and");
     257            1 :         CATCH_REQUIRE(result[2] == "spaces");
     258            1 :     }
     259           11 :     CATCH_END_SECTION()
     260              : 
     261           11 :     CATCH_START_SECTION("utils_split: split three words, one with double quotes")
     262              :     {
     263            1 :         advgetopt::string_list_t result;
     264            5 :         advgetopt::split_string("test \"with quotes and\" spaces"
     265              :                               , result
     266              :                               , {" "});
     267            1 :         CATCH_REQUIRE(result.size() == 3);
     268            1 :         CATCH_REQUIRE(result[0] == "test");
     269            1 :         CATCH_REQUIRE(result[1] == "with quotes and");
     270            1 :         CATCH_REQUIRE(result[2] == "spaces");
     271            1 :     }
     272           11 :     CATCH_END_SECTION()
     273              : 
     274           11 :     CATCH_START_SECTION("utils_split: split three words, one with single quotes but no spaces")
     275              :     {
     276            1 :         advgetopt::string_list_t result;
     277            5 :         advgetopt::split_string("test'with quotes and'nospaces"
     278              :                               , result
     279              :                               , {" "});
     280            1 :         CATCH_REQUIRE(result.size() == 3);
     281            1 :         CATCH_REQUIRE(result[0] == "test");
     282            1 :         CATCH_REQUIRE(result[1] == "with quotes and");
     283            1 :         CATCH_REQUIRE(result[2] == "nospaces");
     284            1 :     }
     285           11 :     CATCH_END_SECTION()
     286              : 
     287           11 :     CATCH_START_SECTION("utils_split: split three words, one with double quotes but no spaces")
     288              :     {
     289            1 :         advgetopt::string_list_t result;
     290            5 :         advgetopt::split_string("test\"with quotes and\"nospaces"
     291              :                               , result
     292              :                               , {" "});
     293            1 :         CATCH_REQUIRE(result.size() == 3);
     294            1 :         CATCH_REQUIRE(result[0] == "test");
     295            1 :         CATCH_REQUIRE(result[1] == "with quotes and");
     296            1 :         CATCH_REQUIRE(result[2] == "nospaces");
     297            1 :     }
     298           11 :     CATCH_END_SECTION()
     299              : 
     300           11 :     CATCH_START_SECTION("utils_split: split five words, four separators")
     301              :     {
     302            1 :         advgetopt::string_list_t result;
     303            5 :         advgetopt::split_string("test,with quite|many;separators"
     304              :                               , result
     305              :                               , {" ",",","|",";"});
     306            1 :         CATCH_REQUIRE(result.size() == 5);
     307            1 :         CATCH_REQUIRE(result[0] == "test");
     308            1 :         CATCH_REQUIRE(result[1] == "with");
     309            1 :         CATCH_REQUIRE(result[2] == "quite");
     310            1 :         CATCH_REQUIRE(result[3] == "many");
     311            1 :         CATCH_REQUIRE(result[4] == "separators");
     312            1 :     }
     313           11 :     CATCH_END_SECTION()
     314              : 
     315           11 :     CATCH_START_SECTION("utils_split: split five words, multiple/repeated separators")
     316              :     {
     317            1 :         advgetopt::string_list_t result;
     318            5 :         advgetopt::split_string("test, with quite|||many ; separators"
     319              :                               , result
     320              :                               , {" ",",","|",";"});
     321            1 :         CATCH_REQUIRE(result.size() == 5);
     322            1 :         CATCH_REQUIRE(result[0] == "test");
     323            1 :         CATCH_REQUIRE(result[1] == "with");
     324            1 :         CATCH_REQUIRE(result[2] == "quite");
     325            1 :         CATCH_REQUIRE(result[3] == "many");
     326            1 :         CATCH_REQUIRE(result[4] == "separators");
     327            1 :     }
     328           11 :     CATCH_END_SECTION()
     329              : 
     330           11 :     CATCH_START_SECTION("utils_split: split five words, and empty entries")
     331              :     {
     332            1 :         advgetopt::string_list_t result;
     333            5 :         advgetopt::split_string("|||test, with quite\"\"many ; ''separators''"
     334              :                               , result
     335              :                               , {" ",",","|",";"});
     336            1 :         CATCH_REQUIRE(result.size() == 5);
     337            1 :         CATCH_REQUIRE(result[0] == "test");
     338            1 :         CATCH_REQUIRE(result[1] == "with");
     339            1 :         CATCH_REQUIRE(result[2] == "quite");
     340            1 :         CATCH_REQUIRE(result[3] == "many");
     341            1 :         CATCH_REQUIRE(result[4] == "separators");
     342            1 :     }
     343           11 :     CATCH_END_SECTION()
     344              : 
     345           11 :     CATCH_START_SECTION("utils_split: split five words, start/end with separator")
     346              :     {
     347            1 :         advgetopt::string_list_t result;
     348            5 :         advgetopt::split_string("|start|and|end|with|separator|"
     349              :                               , result
     350              :                               , {"|"});
     351            1 :         CATCH_REQUIRE(result.size() == 5);
     352            1 :         CATCH_REQUIRE(result[0] == "start");
     353            1 :         CATCH_REQUIRE(result[1] == "and");
     354            1 :         CATCH_REQUIRE(result[2] == "end");
     355            1 :         CATCH_REQUIRE(result[3] == "with");
     356            1 :         CATCH_REQUIRE(result[4] == "separator");
     357            1 :     }
     358           11 :     CATCH_END_SECTION()
     359              : 
     360           11 :     CATCH_START_SECTION("utils_split: split five words, unclosed double quote")
     361              :     {
     362            1 :         advgetopt::string_list_t result;
     363            5 :         advgetopt::split_string("\"unclosed quote|mark"
     364              :                               , result
     365              :                               , {"|"});
     366            1 :         CATCH_REQUIRE(result.size() == 1);
     367            1 :         CATCH_REQUIRE(result[0] == "unclosed quote|mark");
     368            1 :     }
     369           11 :     CATCH_END_SECTION()
     370              : 
     371           11 :     CATCH_START_SECTION("utils_split: split five words, unclosed single quote")
     372              :     {
     373            1 :         advgetopt::string_list_t result;
     374            5 :         advgetopt::split_string("here is an \"unclosed quote|mark"
     375              :                               , result
     376              :                               , {"|"," "});
     377              :         //CATCH_REQUIRE(result.size() == 4);
     378            1 :         CATCH_REQUIRE(result[0] == "here");
     379            1 :         CATCH_REQUIRE(result[1] == "is");
     380            1 :         CATCH_REQUIRE(result[2] == "an");
     381            1 :         CATCH_REQUIRE(result[3] == "unclosed quote|mark");
     382            1 :     }
     383           11 :     CATCH_END_SECTION()
     384           11 : }
     385              : 
     386              : 
     387              : 
     388              : 
     389            5 : CATCH_TEST_CASE("utils_insert_group_name", "[utils][valid]")
     390              : {
     391            5 :     CATCH_START_SECTION("utils_insert_group_name: Full insert")
     392              :     {
     393              :         // CONFIG FILE HAS NO EXTENSION
     394              :         {
     395            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     396              :                               "/this/is/a/path"
     397              :                             , "group-name"
     398            3 :                             , "project-name"));
     399            1 :             CATCH_REQUIRE(fullname.size() == 1);
     400            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/group-name.d/50-path");
     401            1 :         }
     402              : 
     403              :         {
     404            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     405              :                               "/this/is/a/path"
     406              :                             , "group-name"
     407            3 :                             , ""));
     408            1 :             CATCH_REQUIRE(fullname.size() == 1);
     409            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/group-name.d/50-path");
     410            1 :         }
     411              : 
     412              :         {
     413            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     414              :                               "/this/is/a/path"
     415              :                             , "group-name"
     416            3 :                             , nullptr));
     417            1 :             CATCH_REQUIRE(fullname.size() == 1);
     418            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/group-name.d/50-path");
     419            1 :         }
     420              : 
     421              :         {
     422            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     423              :                               "/this/is/a/path"
     424              :                             , ""
     425            3 :                             , "project-name"));
     426            1 :             CATCH_REQUIRE(fullname.size() == 1);
     427            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/project-name.d/50-path");
     428            1 :         }
     429              : 
     430              :         {
     431            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     432              :                               "/this/is/a/path"
     433              :                             , nullptr
     434            3 :                             , "project-name"));
     435            1 :             CATCH_REQUIRE(fullname.size() == 1);
     436            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/project-name.d/50-path");
     437            1 :         }
     438              : 
     439              :         // CONFIG FILE HAS EXTENSION
     440              :         {
     441            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     442              :                               "/this/is/a/basename.ext"
     443              :                             , "group-name"
     444            3 :                             , "project-name"));
     445            1 :             CATCH_REQUIRE(fullname.size() == 1);
     446            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/group-name.d/50-basename.ext");
     447            1 :         }
     448              : 
     449              :         {
     450            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     451              :                               "/this/is/a/basename.ext"
     452              :                             , "group-name"
     453            3 :                             , ""));
     454            1 :             CATCH_REQUIRE(fullname.size() == 1);
     455            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/group-name.d/50-basename.ext");
     456            1 :         }
     457              : 
     458              :         {
     459            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     460              :                               "/this/is/a/basename.ext"
     461              :                             , "group-name"
     462            3 :                             , nullptr));
     463            1 :             CATCH_REQUIRE(fullname.size() == 1);
     464            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/group-name.d/50-basename.ext");
     465            1 :         }
     466              : 
     467              :         {
     468            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     469              :                               "/this/is/a/basename.ext"
     470              :                             , ""
     471            3 :                             , "project-name"));
     472            1 :             CATCH_REQUIRE(fullname.size() == 1);
     473            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/project-name.d/50-basename.ext");
     474            1 :         }
     475              : 
     476              :         {
     477            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     478              :                               "/this/is/a/basename.ext"
     479              :                             , nullptr
     480            3 :                             , "project-name"));
     481            1 :             CATCH_REQUIRE(fullname.size() == 1);
     482            1 :             CATCH_REQUIRE(fullname[0] == "/this/is/a/project-name.d/50-basename.ext");
     483            1 :         }
     484              :     }
     485            5 :     CATCH_END_SECTION()
     486              : 
     487            5 :     CATCH_START_SECTION("utils_insert_group_name: empty cases")
     488              :     {
     489              :         {
     490            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     491              :                               "/this/is/a/path"
     492              :                             , nullptr
     493            3 :                             , nullptr));
     494            1 :             CATCH_REQUIRE(fullname.empty());
     495            1 :         }
     496              : 
     497              :         {
     498            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     499              :                               "/this/is/a/path"
     500              :                             , nullptr
     501            3 :                             , ""));
     502            1 :             CATCH_REQUIRE(fullname.empty());
     503            1 :         }
     504              : 
     505              :         {
     506            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     507              :                               "/this/is/a/path"
     508              :                             , ""
     509            3 :                             , nullptr));
     510            1 :             CATCH_REQUIRE(fullname.empty());
     511            1 :         }
     512              : 
     513              :         {
     514            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     515              :                               "/this/is/a/path"
     516              :                             , ""
     517            3 :                             , ""));
     518            1 :             CATCH_REQUIRE(fullname.empty());
     519            1 :         }
     520              : 
     521              :         {
     522            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     523              :                               ""
     524              :                             , "group-name"
     525            3 :                             , "project-name"));
     526            1 :             CATCH_REQUIRE(fullname.empty());
     527            1 :         }
     528              : 
     529              :         {
     530            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     531              :                               ""
     532              :                             , ""
     533            3 :                             , "project-name"));
     534            1 :             CATCH_REQUIRE(fullname.empty());
     535            1 :         }
     536              : 
     537              :         {
     538            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     539              :                               ""
     540              :                             , nullptr
     541            3 :                             , "project-name"));
     542            1 :             CATCH_REQUIRE(fullname.empty());
     543            1 :         }
     544              : 
     545              :         {
     546            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     547              :                               ""
     548              :                             , nullptr
     549            3 :                             , ""));
     550            1 :             CATCH_REQUIRE(fullname.empty());
     551            1 :         }
     552              : 
     553              :         {
     554            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     555              :                               ""
     556              :                             , nullptr
     557            3 :                             , nullptr));
     558            1 :             CATCH_REQUIRE(fullname.empty());
     559            1 :         }
     560              :     }
     561            5 :     CATCH_END_SECTION()
     562              : 
     563            5 :     CATCH_START_SECTION("utils_insert_group_name: cases")
     564              :     {
     565            8 :         CATCH_REQUIRE_THROWS_MATCHES(advgetopt::insert_group_name(
     566              :                           "/this-is-a-path"
     567              :                         , "group"
     568              :                         , "project")
     569              :                     , advgetopt::getopt_root_filename
     570              :                     , Catch::Matchers::ExceptionMessage(
     571              :                               "getopt_exception: filename \"/this-is-a-path\" last slash (/) is at the start, which is not allowed."));
     572              :     }
     573            5 :     CATCH_END_SECTION()
     574              : 
     575            5 :     CATCH_START_SECTION("utils_insert_group_name: basename only")
     576              :     {
     577              :         {
     578            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     579              :                               "basename"
     580              :                             , nullptr
     581            3 :                             , "advgetopt"));
     582            1 :             CATCH_REQUIRE(fullname.size() == 1);
     583            1 :             CATCH_REQUIRE(fullname[0] == "advgetopt.d/50-basename");
     584            1 :         }
     585              : 
     586              :         {
     587            4 :             advgetopt::string_list_t fullname(advgetopt::insert_group_name(
     588              :                               "basename.ext"
     589              :                             , "advgetopt"
     590            3 :                             , nullptr));
     591            1 :             CATCH_REQUIRE(fullname.size() == 1);
     592            1 :             CATCH_REQUIRE(fullname[0] == "advgetopt.d/50-basename.ext");
     593            1 :         }
     594              :     }
     595            5 :     CATCH_END_SECTION()
     596              : 
     597            5 :     CATCH_START_SECTION("utils_insert_group_name: actual list of files on disk")
     598              :     {
     599            5 :         SNAP_CATCH2_NAMESPACE::init_tmp_dir("advgetopt-multi", "sorted-user-conf", false);
     600              : 
     601              :         // generate an array of numbers from 00 to 99
     602              :         //
     603            1 :         std::vector<int> numbers;
     604          101 :         for(int i = 0; i < 100; ++i)
     605              :         {
     606          100 :             numbers.push_back(i);
     607              :         }
     608            1 :         std::random_device rd;
     609            1 :         std::mt19937 g(rd());
     610            1 :         std::shuffle(numbers.begin(), numbers.end(), g);
     611            1 :         int const max(rand() % 50 + 10);
     612            1 :         numbers.resize(max);
     613            1 :         std::string path(SNAP_CATCH2_NAMESPACE::g_config_project_filename);
     614            1 :         std::string::size_type const pos(path.rfind('/'));
     615            1 :         path = path.substr(0, pos);
     616            1 :         advgetopt::string_list_t filenames;
     617           23 :         for(int i = 0; i < max; ++i)
     618              :         {
     619           22 :             std::stringstream ss;
     620           22 :             ss << path;
     621           22 :             ss << '/';
     622           22 :             int const n(numbers[i]);
     623           22 :             if(n < 10)
     624              :             {
     625            1 :                 ss << '0';
     626              :             }
     627           22 :             ss << n;
     628           22 :             ss << "-sorted-user-conf.config";
     629           22 :             filenames.push_back(ss.str());
     630           22 :             std::ofstream conf;
     631           22 :             conf.open(ss.str(), std::ios_base::out);
     632           22 :             CATCH_REQUIRE(conf.is_open());
     633           22 :             conf << "# Config with a number" << std::endl;
     634           22 :             conf << "var=\"value: " << numbers[i] << "\"" << std::endl;
     635           22 :         }
     636            1 :         std::sort(filenames.begin(), filenames.end());
     637            1 :         std::string const last_filename(*filenames.rbegin());
     638            1 :         std::string::size_type const slash_pos(last_filename.rfind('/'));
     639            1 :         std::string const expected_var("value: " + last_filename.substr(slash_pos + 1, 2));
     640              : 
     641            1 :         advgetopt::string_list_t fullnames(advgetopt::insert_group_name(
     642              :                           SNAP_CATCH2_NAMESPACE::g_config_filename
     643              :                         , "advgetopt-multi"
     644            1 :                         , "multi-channels"));
     645            1 :         CATCH_REQUIRE(fullnames.size() == filenames.size());
     646           23 :         for(size_t idx(0); idx < filenames.size(); ++idx)
     647              :         {
     648           22 :             CATCH_REQUIRE(fullnames[idx] == filenames[idx]);
     649              :         }
     650              : 
     651              :         {
     652            1 :             std::ofstream conf;
     653            1 :             conf.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out);
     654            1 :             CATCH_REQUIRE(conf.is_open());
     655            1 :             conf << "# Original Config with a number" << std::endl;
     656            1 :             conf << "var=master value" << std::endl;
     657              : 
     658              :             // verify the master config file
     659              :             //
     660            1 :             advgetopt::conf_file_setup setup(SNAP_CATCH2_NAMESPACE::g_config_filename);
     661            1 :             advgetopt::conf_file::pointer_t config_file(advgetopt::conf_file::get_conf_file(setup));
     662            3 :             CATCH_REQUIRE(config_file->get_parameter("var") == "master value");
     663            1 :         }
     664              : 
     665              :         {
     666              :             // run a load to verify that we indeed get the last var=...
     667              :             // value and not some random entry
     668              :             //
     669            1 :             std::string temp_dir = SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/.config";
     670            1 :             char const * const dirs[] = {
     671            1 :                 temp_dir.c_str(),
     672              :                 nullptr
     673            1 :             };
     674            1 :             advgetopt::option opts[] = {
     675              :                 advgetopt::define_option(
     676              :                       advgetopt::Name("var")
     677              :                     , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_SHOW_USAGE_ON_ERROR>())
     678              :                     , advgetopt::Help("verify loading configuration files in a serie.")
     679              :                 ),
     680              :                 advgetopt::end_options()
     681              :             };
     682              : #pragma GCC diagnostic push
     683              : #pragma GCC diagnostic ignored "-Wpedantic"
     684            1 :             advgetopt::options_environment env = {
     685              :                 .f_project_name = "sorted-configs",
     686              :                 .f_group_name = "advgetopt-multi",
     687              :                 .f_options = opts,
     688              :                 .f_options_files_directory = nullptr,
     689              :                 .f_environment_variable_name = nullptr,
     690              :                 .f_environment_variable_intro = nullptr,
     691              :                 .f_section_variables_name = nullptr,
     692              :                 .f_configuration_files = nullptr,
     693              :                 .f_configuration_filename = "sorted-user-conf.config",
     694              :                 .f_configuration_directories = dirs,
     695              :                 .f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS,
     696              :                 .f_help_header = nullptr,
     697              :                 .f_help_footer = nullptr,
     698              :                 .f_version = nullptr,
     699              :                 .f_license = nullptr,
     700              :                 .f_copyright = nullptr,
     701              :                 .f_build_date = UTC_BUILD_DATE,
     702              :                 .f_build_time = UTC_BUILD_TIME,
     703              :                 .f_groups = nullptr
     704            1 :             };
     705              : #pragma GCC diagnostic pop
     706            1 :             char const * const argv[] = {
     707              :                 "test",
     708              :                 nullptr
     709              :             };
     710            1 :             advgetopt::getopt opt(env, 1, const_cast<char **>(argv));
     711            1 :             CATCH_REQUIRE(memcmp(&opt.get_options_environment(), &env, sizeof(env)) == 0);
     712            3 :             CATCH_REQUIRE(opt.get_string("var") == expected_var);
     713            1 :         }
     714            1 :     }
     715            5 :     CATCH_END_SECTION()
     716            5 : }
     717              : 
     718              : 
     719            5 : CATCH_TEST_CASE("utils_default_group_name", "[utils][valid]")
     720              : {
     721            5 :     CATCH_START_SECTION("utils_default_group_name: full insert")
     722              :     {
     723              :         // CONFIG FILE HAS NO EXTENSION
     724              :         {
     725            4 :             std::string const fullname(advgetopt::default_group_name(
     726              :                               "/this/is/a/config"
     727              :                             , "group-name"
     728            3 :                             , "project-name"));
     729            1 :             CATCH_REQUIRE(fullname == "/this/is/a/group-name.d/50-config");
     730            1 :         }
     731              : 
     732              :         {
     733            4 :             std::string const fullname(advgetopt::default_group_name(
     734              :                               "/this/is/a/advgetopt"
     735              :                             , "group-name"
     736            3 :                             , ""));
     737            1 :             CATCH_REQUIRE(fullname == "/this/is/a/group-name.d/50-advgetopt");
     738            1 :         }
     739              : 
     740              :         {
     741            4 :             std::string const fullname(advgetopt::default_group_name(
     742              :                               "/this/is/a/complete"
     743              :                             , "group-name"
     744            3 :                             , nullptr));
     745            1 :             CATCH_REQUIRE(fullname == "/this/is/a/group-name.d/50-complete");
     746            1 :         }
     747              : 
     748              :         {
     749            4 :             std::string const fullname(advgetopt::default_group_name(
     750              :                               "/this/is/a/swapped"
     751              :                             , ""
     752            3 :                             , "project-name"));
     753            1 :             CATCH_REQUIRE(fullname == "/this/is/a/project-name.d/50-swapped");
     754            1 :         }
     755              : 
     756              :         {
     757            4 :             std::string const fullname(advgetopt::default_group_name(
     758              :                               "/this/is/a/null"
     759              :                             , nullptr
     760            3 :                             , "project-name"));
     761            1 :             CATCH_REQUIRE(fullname == "/this/is/a/project-name.d/50-null");
     762            1 :         }
     763              : 
     764              :         // CONFIG FILE HAS EXTENSION
     765              :         {
     766            4 :             std::string const fullname(advgetopt::default_group_name(
     767              :                               "/this/is/a/basename.ext"
     768              :                             , "group-name"
     769            3 :                             , "project-name"));
     770            1 :             CATCH_REQUIRE(fullname == "/this/is/a/group-name.d/50-basename.ext");
     771            1 :         }
     772              : 
     773              :         {
     774            4 :             std::string const fullname(advgetopt::default_group_name(
     775              :                               "/this/is/a/basename.ext"
     776              :                             , "group-name"
     777            3 :                             , ""));
     778            1 :             CATCH_REQUIRE(fullname == "/this/is/a/group-name.d/50-basename.ext");
     779            1 :         }
     780              : 
     781              :         {
     782            4 :             std::string const fullname(advgetopt::default_group_name(
     783              :                               "/this/is/a/basename.ext"
     784              :                             , "group-name"
     785            3 :                             , nullptr));
     786            1 :             CATCH_REQUIRE(fullname == "/this/is/a/group-name.d/50-basename.ext");
     787            1 :         }
     788              : 
     789              :         {
     790            4 :             std::string const fullname(advgetopt::default_group_name(
     791              :                               "/this/is/a/basename.ext"
     792              :                             , ""
     793            3 :                             , "project-name"));
     794            1 :             CATCH_REQUIRE(fullname == "/this/is/a/project-name.d/50-basename.ext");
     795            1 :         }
     796              : 
     797              :         {
     798            4 :             std::string const fullname(advgetopt::default_group_name(
     799              :                               "/this/is/a/basename.ext"
     800              :                             , nullptr
     801            3 :                             , "project-name"));
     802            1 :             CATCH_REQUIRE(fullname == "/this/is/a/project-name.d/50-basename.ext");
     803            1 :         }
     804              : 
     805              :         // verify all valid priorities
     806              :         //
     807           11 :         for(int priority(0); priority < 10; ++priority)
     808              :         {
     809           40 :             std::string const fullname(advgetopt::default_group_name(
     810              :                               "/this/is/a/basename.ext"
     811              :                             , "group-name"
     812              :                             , "project-name"
     813           30 :                             , priority));
     814           30 :             std::string expected("/this/is/a/group-name.d/0");
     815           10 :             expected += std::to_string(priority);
     816           10 :             expected += "-basename.ext";
     817           10 :             CATCH_REQUIRE(expected == fullname);
     818           10 :         }
     819           91 :         for(int priority(10); priority < 100; ++priority)
     820              :         {
     821          360 :             std::string const fullname(advgetopt::default_group_name(
     822              :                               "/this/is/a/basename.ext"
     823              :                             , "group-name"
     824              :                             , "project-name"
     825          270 :                             , priority));
     826          270 :             std::string expected("/this/is/a/group-name.d/");
     827           90 :             expected += std::to_string(priority);
     828           90 :             expected += "-basename.ext";
     829           90 :             CATCH_REQUIRE(expected == fullname);
     830           90 :         }
     831              :     }
     832            5 :     CATCH_END_SECTION()
     833              : 
     834            5 :     CATCH_START_SECTION("utils_default_group_name: empty cases")
     835              :     {
     836              :         {
     837            4 :             std::string const fullname(advgetopt::default_group_name(
     838              :                               "/this/is/a/path"
     839              :                             , nullptr
     840            3 :                             , nullptr));
     841            1 :             CATCH_REQUIRE(fullname.empty());
     842            1 :         }
     843              : 
     844              :         {
     845            4 :             std::string const fullname(advgetopt::default_group_name(
     846              :                               "/this/is/a/path"
     847              :                             , nullptr
     848            3 :                             , ""));
     849            1 :             CATCH_REQUIRE(fullname.empty());
     850            1 :         }
     851              : 
     852              :         {
     853            4 :             std::string const fullname(advgetopt::default_group_name(
     854              :                               "/this/is/a/path"
     855              :                             , ""
     856            3 :                             , nullptr));
     857            1 :             CATCH_REQUIRE(fullname.empty());
     858            1 :         }
     859              : 
     860              :         {
     861            4 :             std::string const fullname(advgetopt::default_group_name(
     862              :                               "/this/is/a/path"
     863              :                             , ""
     864            3 :                             , ""));
     865            1 :             CATCH_REQUIRE(fullname.empty());
     866            1 :         }
     867              : 
     868              :         {
     869            4 :             std::string const fullname(advgetopt::default_group_name(
     870              :                               ""
     871              :                             , "group-name"
     872            3 :                             , "project-name"));
     873            1 :             CATCH_REQUIRE(fullname.empty());
     874            1 :         }
     875              : 
     876              :         {
     877            4 :             std::string const fullname(advgetopt::default_group_name(
     878              :                               ""
     879              :                             , ""
     880            3 :                             , "project-name"));
     881            1 :             CATCH_REQUIRE(fullname.empty());
     882            1 :         }
     883              : 
     884              :         {
     885            4 :             std::string const fullname(advgetopt::default_group_name(
     886              :                               ""
     887              :                             , nullptr
     888            3 :                             , "project-name"));
     889            1 :             CATCH_REQUIRE(fullname.empty());
     890            1 :         }
     891              : 
     892              :         {
     893            4 :             std::string const fullname(advgetopt::default_group_name(
     894              :                               ""
     895              :                             , nullptr
     896            3 :                             , ""));
     897            1 :             CATCH_REQUIRE(fullname.empty());
     898            1 :         }
     899              : 
     900              :         {
     901            4 :             std::string const fullname(advgetopt::default_group_name(
     902              :                               ""
     903              :                             , nullptr
     904            3 :                             , nullptr));
     905            1 :             CATCH_REQUIRE(fullname.empty());
     906            1 :         }
     907              :     }
     908            5 :     CATCH_END_SECTION()
     909              : 
     910            5 :     CATCH_START_SECTION("utils_default_group_name: single '/' at the start")
     911              :     {
     912            8 :         CATCH_REQUIRE_THROWS_MATCHES(advgetopt::default_group_name(
     913              :                           "/this-is-a-path"
     914              :                         , "group"
     915              :                         , "project")
     916              :                     , advgetopt::getopt_root_filename
     917              :                     , Catch::Matchers::ExceptionMessage(
     918              :                               "getopt_exception: filename \"/this-is-a-path\" starts with a slash (/), which is not allowed."));
     919              :     }
     920            5 :     CATCH_END_SECTION()
     921              : 
     922            5 :     CATCH_START_SECTION("utils_default_group_name: invalid priority")
     923              :     {
     924              :         // verify that negative priorities are prevented
     925              :         //
     926           21 :         for(int priority(-20); priority < 0; ++priority)
     927              :         {
     928          120 :             CATCH_REQUIRE_THROWS_MATCHES(advgetopt::default_group_name(
     929              :                           "/this/is/a/basename.ext"
     930              :                         , ((rand() & 1) == 0 ? "group-name" : nullptr)
     931              :                         , "project-name"
     932              :                         , priority)
     933              :                     , advgetopt::getopt_invalid_parameter
     934              :                     , Catch::Matchers::ExceptionMessage(
     935              :                           "getopt_exception: priority must be a number between 0 and 99 inclusive; "
     936              :                         + std::to_string(priority)
     937              :                         + " is invalid."));
     938              :         }
     939              : 
     940              :         // verify that large priorities are prevented
     941              :         //
     942           21 :         for(int priority(100); priority < 120; ++priority)
     943              :         {
     944          120 :             CATCH_REQUIRE_THROWS_MATCHES(advgetopt::default_group_name(
     945              :                           "/this/is/a/basename.ext"
     946              :                         , ((rand() & 1) == 0 ? "group-name" : nullptr)
     947              :                         , "project-name"
     948              :                         , priority)
     949              :                     , advgetopt::getopt_invalid_parameter
     950              :                     , Catch::Matchers::ExceptionMessage(
     951              :                           "getopt_exception: priority must be a number between 0 and 99 inclusive; "
     952              :                         + std::to_string(priority)
     953              :                         + " is invalid."));
     954              :         }
     955              :     }
     956            5 :     CATCH_END_SECTION()
     957              : 
     958            5 :     CATCH_START_SECTION("utils_default_group_name: basename Only")
     959              :     {
     960              :         {
     961            4 :             std::string const fullname(advgetopt::default_group_name(
     962              :                               "basename"
     963              :                             , nullptr
     964            3 :                             , "advgetopt"));
     965            1 :             CATCH_REQUIRE(fullname == "advgetopt.d/50-basename");
     966            1 :         }
     967              : 
     968              :         {
     969            4 :             std::string const fullname(advgetopt::default_group_name(
     970              :                               "basename.ext"
     971              :                             , "advgetopt"
     972            3 :                             , nullptr));
     973            1 :             CATCH_REQUIRE(fullname == "advgetopt.d/50-basename.ext");
     974            1 :         }
     975              :     }
     976            5 :     CATCH_END_SECTION()
     977            5 : }
     978              : 
     979              : 
     980              : 
     981              : 
     982              : 
     983              : 
     984              : 
     985            3 : CATCH_TEST_CASE("utils_handle_user_directory", "[utils][valid]")
     986              : {
     987            3 :     CATCH_START_SECTION("utils_handle_user_directory: valid cases")
     988              :     {
     989            5 :         snapdev::safe_setenv env("HOME", "/home/advgetopt");
     990              : 
     991              :         {
     992            3 :             std::string result(advgetopt::handle_user_directory("~"));
     993            1 :             CATCH_REQUIRE(result == "/home/advgetopt");
     994            1 :         }
     995              : 
     996              :         {
     997            3 :             std::string result(advgetopt::handle_user_directory("~/"));
     998            1 :             CATCH_REQUIRE(result == "/home/advgetopt/");
     999            1 :         }
    1000              : 
    1001              :         {
    1002            3 :             std::string result(advgetopt::handle_user_directory("~/.config/advgetopt.conf"));
    1003            1 :             CATCH_REQUIRE(result == "/home/advgetopt/.config/advgetopt.conf");
    1004            1 :         }
    1005            1 :     }
    1006            3 :     CATCH_END_SECTION()
    1007              : 
    1008            3 :     CATCH_START_SECTION("utils_handle_user_directory: $HOME is empty")
    1009              :     {
    1010            5 :         snapdev::safe_setenv env("HOME", "");
    1011              : 
    1012              :         {
    1013            3 :             std::string result(advgetopt::handle_user_directory("~"));
    1014            1 :             CATCH_REQUIRE(result == "~");
    1015            1 :         }
    1016              : 
    1017              :         {
    1018            3 :             std::string result(advgetopt::handle_user_directory("~/.config/advgetopt.conf"));
    1019            1 :             CATCH_REQUIRE(result == "~/.config/advgetopt.conf");
    1020            1 :         }
    1021            1 :     }
    1022            3 :     CATCH_END_SECTION()
    1023              : 
    1024            3 :     CATCH_START_SECTION("utils_handle_user_directory: paths do not start with ~")
    1025              :     {
    1026            5 :         snapdev::safe_setenv env("HOME", "/home/advgetopt");
    1027              : 
    1028              :         {
    1029            3 :             std::string result(advgetopt::handle_user_directory("/~"));
    1030            1 :             CATCH_REQUIRE(result == "/~");
    1031            1 :         }
    1032              : 
    1033              :         {
    1034            3 :             std::string result(advgetopt::handle_user_directory("/~/.config/advgetopt.conf"));
    1035            1 :             CATCH_REQUIRE(result == "/~/.config/advgetopt.conf");
    1036            1 :         }
    1037            1 :     }
    1038            3 :     CATCH_END_SECTION()
    1039            3 : }
    1040              : 
    1041              : 
    1042              : 
    1043              : 
    1044              : 
    1045            2 : CATCH_TEST_CASE("utils_true_false", "[utils][boolean]")
    1046              : {
    1047            2 :     CATCH_START_SECTION("utils_true_false: true values")
    1048              :     {
    1049            3 :         CATCH_REQUIRE(advgetopt::is_true("true"));
    1050            3 :         CATCH_REQUIRE(advgetopt::is_true("on"));
    1051            3 :         CATCH_REQUIRE(advgetopt::is_true("1"));
    1052              : 
    1053            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_true("false"));
    1054            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_true("off"));
    1055            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_true("0"));
    1056              : 
    1057            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_true("random"));
    1058              :     }
    1059            2 :     CATCH_END_SECTION()
    1060              : 
    1061            2 :     CATCH_START_SECTION("utils_true_false: false values")
    1062              :     {
    1063            3 :         CATCH_REQUIRE(advgetopt::is_false("false"));
    1064            3 :         CATCH_REQUIRE(advgetopt::is_false("off"));
    1065            3 :         CATCH_REQUIRE(advgetopt::is_false("0"));
    1066              : 
    1067            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_false("true"));
    1068            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_false("on"));
    1069            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_false("1"));
    1070              : 
    1071            3 :         CATCH_REQUIRE_FALSE(advgetopt::is_false("random"));
    1072              :     }
    1073            2 :     CATCH_END_SECTION()
    1074            2 : }
    1075              : 
    1076              : 
    1077              : 
    1078              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

Snap C++ | List of projects | List of versions