LCOV - code coverage report
Current view: top level - tests - catch_variables.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 65 100.0 %
Date: 2022-07-15 09:02:52 Functions: 3 3 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/variables.h>
      23             : 
      24             : #include    <advgetopt/exception.h>
      25             : 
      26             : 
      27             : // self
      28             : //
      29             : #include    "catch_main.h"
      30             : 
      31             : 
      32             : // C++
      33             : //
      34             : #include    <fstream>
      35             : #include    <iomanip>
      36             : 
      37             : 
      38             : // last include
      39             : //
      40             : #include    <snapdev/poison.h>
      41             : 
      42             : 
      43             : 
      44             : 
      45             : 
      46             : 
      47           3 : CATCH_TEST_CASE("variables", "[variables][valid]")
      48             : {
      49           2 :     CATCH_START_SECTION("Check the variables class")
      50             :     {
      51           2 :         advgetopt::variables vars;
      52             : 
      53             :         // not set yet
      54           1 :         CATCH_REQUIRE(vars.get_variable("first-variable") == std::string());
      55           1 :         CATCH_REQUIRE(vars.get_variables().empty());
      56             : 
      57           1 :         CATCH_REQUIRE_FALSE(vars.has_variable("first_variable"));
      58           1 :         vars.set_variable("first_variable", "it works");
      59           1 :         CATCH_REQUIRE(vars.get_variables().size() == 1);
      60           1 :         CATCH_REQUIRE(vars.has_variable("first_variable"));
      61           1 :         CATCH_REQUIRE(vars.has_variable("first-variable"));
      62           1 :         CATCH_REQUIRE(vars.get_variable("first_variable") == "it works");
      63           1 :         CATCH_REQUIRE(vars.get_variable("first-variable") == "it works");
      64             : 
      65           1 :         CATCH_REQUIRE_FALSE(vars.has_variable("second::variable"));
      66           1 :         vars.set_variable("second::variable", "double colon");
      67           1 :         CATCH_REQUIRE(vars.get_variables().size() == 2);
      68           1 :         CATCH_REQUIRE(vars.has_variable("second.variable"));
      69           1 :         CATCH_REQUIRE(vars.has_variable("second..variable"));
      70           1 :         CATCH_REQUIRE(vars.has_variable("second...variable"));
      71           1 :         CATCH_REQUIRE(vars.has_variable("second....variable"));
      72           1 :         CATCH_REQUIRE(vars.has_variable("second:variable"));
      73           1 :         CATCH_REQUIRE(vars.has_variable("second::variable"));
      74           1 :         CATCH_REQUIRE(vars.has_variable("second:::variable"));
      75           1 :         CATCH_REQUIRE(vars.has_variable("second::::variable"));
      76           1 :         CATCH_REQUIRE(vars.get_variable("second.variable") == "double colon");
      77           1 :         CATCH_REQUIRE(vars.get_variable("second..variable") == "double colon");
      78           1 :         CATCH_REQUIRE(vars.get_variable("second...variable") == "double colon");
      79           1 :         CATCH_REQUIRE(vars.get_variable("second....variable") == "double colon");
      80           1 :         CATCH_REQUIRE(vars.get_variable("second:variable") == "double colon");
      81           1 :         CATCH_REQUIRE(vars.get_variable("second::variable") == "double colon");
      82           1 :         CATCH_REQUIRE(vars.get_variable("second:::variable") == "double colon");
      83           1 :         CATCH_REQUIRE(vars.get_variable("second::::variable") == "double colon");
      84             : 
      85           1 :         CATCH_REQUIRE_FALSE(vars.has_variable("third::::variable"));
      86           1 :         vars.set_variable("third::::variable", "scope operator");
      87           1 :         CATCH_REQUIRE(vars.get_variables().size() == 3);
      88           1 :         CATCH_REQUIRE(vars.has_variable("third::variable"));
      89           1 :         CATCH_REQUIRE(vars.get_variable("third::variable") == "scope operator");
      90             : 
      91             :         // change value
      92           1 :         vars.set_variable("first_variable", "replaced value");
      93           1 :         CATCH_REQUIRE(vars.get_variable("first_variable") == "replaced value");
      94           1 :         CATCH_REQUIRE(vars.get_variables().size() == 3);
      95             : 
      96             :         // attempt changing value when already set
      97           1 :         vars.set_variable("first_variable", "replaced value", false);
      98           1 :         CATCH_REQUIRE(vars.get_variable("first_variable") == "replaced value");
      99           1 :         CATCH_REQUIRE(vars.get_variables().size() == 3);
     100             : 
     101           2 :         advgetopt::variables::variable_t list(vars.get_variables());
     102           4 :         for(auto l : list)
     103             :         {
     104           3 :             if(l.first == "first-variable")
     105             :             {
     106           1 :                 CATCH_REQUIRE(l.second == "replaced value");
     107             :             }
     108           2 :             else if(l.first == "second::variable")
     109             :             {
     110           1 :                 CATCH_REQUIRE(l.second == "double colon");
     111             :             }
     112             :             else
     113             :             {
     114             :                 // if not 1st or 2nd, it has to be 3rd
     115             :                 //
     116           1 :                 CATCH_REQUIRE(l.first == "third::variable");
     117           1 :                 CATCH_REQUIRE(l.second == "scope operator");
     118             :             }
     119             :         }
     120             : 
     121             :         // valid
     122             :         //
     123           2 :         std::string processed(vars.process_value("First Var = [${first-variable}]"));
     124           1 :         CATCH_REQUIRE(processed == "First Var = [replaced value]");
     125             : 
     126             :         // missing '}'
     127             :         //
     128           1 :         processed = vars.process_value("First Var = [${first-variable]");
     129           1 :         CATCH_REQUIRE(processed == "First Var = [${first-variable]");
     130             : 
     131           1 :         vars.set_variable("loopA", "ref ${loopB}", false);
     132           1 :         CATCH_REQUIRE(vars.get_variable("loopA") == "ref ${loopB}");
     133           1 :         CATCH_REQUIRE(vars.get_variables().size() == 4);
     134             : 
     135           1 :         vars.set_variable("loopB", "ref ${loopA}", false);
     136           1 :         CATCH_REQUIRE(vars.get_variable("loopB") == "ref ${loopA}");
     137           1 :         CATCH_REQUIRE(vars.get_variables().size() == 5);
     138             : 
     139           1 :         processed = vars.process_value("Looping like crazy: ${loopA}");
     140           1 :         CATCH_REQUIRE(processed == "Looping like crazy: ref ref <variable \"loopA\" loops>");
     141             : 
     142           1 :         processed = vars.process_value("Looping like crazy: ${loopB}");
     143           1 :         CATCH_REQUIRE(processed == "Looping like crazy: ref ref <variable \"loopB\" loops>");
     144             :     }
     145             :     CATCH_END_SECTION()
     146           7 : }
     147             : 
     148             : 
     149             : 
     150             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13