LCOV - code coverage report
Current view: top level - tests - catch_join_strings.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 44 100.0 %
Date: 2023-05-29 16:11:08 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011-2023  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snapdev
       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 3 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
      17             : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18             : 
      19             : /** \file
      20             :  * \brief Verify the join_strings function & templates.
      21             :  *
      22             :  * This file implements tests to verify that the join_strings function works
      23             :  * as expected.
      24             :  *
      25             :  * Further it verifies that string concatenation happens as expected.
      26             :  */
      27             : 
      28             : // snapdev
      29             : //
      30             : #include    <snapdev/join_strings.h>
      31             : 
      32             : #include    <snapdev/to_string_literal.h>
      33             : 
      34             : 
      35             : // self
      36             : //
      37             : #include    "catch_main.h"
      38             : 
      39             : 
      40             : 
      41             : namespace
      42             : {
      43             : 
      44             : // example taken from communicatord project
      45             : //
      46             : constexpr int const         LOCAL_PORT = 4040;
      47             : constexpr std::string_view  g_communicatord_default_ip = "127.0.0.1";
      48             : constexpr std::string_view  g_communicatord_default_port = snapdev::integer_to_string_literal<LOCAL_PORT>.data();
      49             : constexpr std::string_view  g_communicatord_colon = ":";
      50             : constexpr std::string_view  g_communicatord_default_ip_port = snapdev::join_string_views<g_communicatord_default_ip, g_communicatord_colon, g_communicatord_default_port>;
      51             : 
      52             : constexpr std::string_view  g_communicatord_default_ip_port_v2 = snapdev::join_string_views_with_separator<g_communicatord_colon, g_communicatord_default_ip, g_communicatord_default_port>;
      53             : 
      54             : 
      55             : }
      56             : 
      57             : 
      58           1 : CATCH_TEST_CASE("join_string_view", "[basic][strings]")
      59             : {
      60           1 :     CATCH_START_SECTION("join_string_view: join strings at compile time")
      61             :     {
      62           2 :         std::string concat(g_communicatord_default_ip);
      63           1 :         concat += ':';
      64           1 :         concat += std::to_string(LOCAL_PORT);
      65             : 
      66             :         // the cast here is because catch2 doesn't handle the
      67             :         //
      68             :         //     string == string_view
      69             :         //
      70           1 :         CATCH_REQUIRE(concat == std::string(g_communicatord_default_ip_port));
      71           1 :         CATCH_REQUIRE(concat == std::string(g_communicatord_default_ip_port_v2));
      72           1 :     }
      73           1 :     CATCH_END_SECTION()
      74           1 : }
      75             : 
      76             : 
      77           4 : CATCH_TEST_CASE("join_strings", "[basic][strings]")
      78             : {
      79           4 :     CATCH_START_SECTION("join_strings: join strings at runtime with empty string in between")
      80             :     {
      81           2 :         std::string manual_concat(g_communicatord_default_ip);
      82           1 :         manual_concat += ':';
      83           1 :         manual_concat += std::to_string(LOCAL_PORT);
      84             : 
      85           1 :         std::vector<std::string> const list = {
      86           1 :                   std::string(g_communicatord_default_ip)
      87           1 :                 , std::string(":")
      88           1 :                 , std::string(g_communicatord_default_port)
      89          12 :             };
      90           3 :         std::string const join_concat(snapdev::join_strings(list, ""));
      91             : 
      92           1 :         CATCH_REQUIRE(manual_concat == join_concat);
      93           1 :     }
      94           4 :     CATCH_END_SECTION()
      95             : 
      96           4 :     CATCH_START_SECTION("join_strings: join strings at runtime with commas")
      97             :     {
      98           1 :         std::vector<std::string> const list = {
      99             :                   "Item 1"
     100             :                 , "Item 2"
     101             :                 , "Item 3"
     102           9 :             };
     103           3 :         std::string const join_concat(snapdev::join_strings(list, ", "));
     104             : 
     105           1 :         CATCH_REQUIRE(join_concat == "Item 1, Item 2, Item 3");
     106           1 :     }
     107           4 :     CATCH_END_SECTION()
     108             : 
     109           4 :     CATCH_START_SECTION("join_strings: \"join\" one string with commas")
     110             :     {
     111           5 :         std::vector<std::string> const list = { "Item 1" };
     112           3 :         std::string const join_concat(snapdev::join_strings(list, ", "));
     113             : 
     114           1 :         CATCH_REQUIRE(join_concat == "Item 1");
     115           1 :     }
     116           4 :     CATCH_END_SECTION()
     117             : 
     118           4 :     CATCH_START_SECTION("join_strings: nothing to join with commas")
     119             :     {
     120           1 :         std::vector<std::string> const list;
     121           3 :         std::string const join_concat(snapdev::join_strings(list, ", "));
     122             : 
     123           1 :         CATCH_REQUIRE(join_concat == std::string());
     124           1 :     }
     125           4 :     CATCH_END_SECTION()
     126           4 : }
     127             : 
     128             : 
     129             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.14