LCOV - code coverage report
Current view: top level - tests - catch_join_strings.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 31 100.0 %
Date: 2022-07-09 19:51:09 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011-2022  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             : // boost
      41             : //
      42             : #include    <boost/preprocessor/stringize.hpp>
      43             : 
      44             : 
      45             : // C++
      46             : //
      47             : //#include    <fstream>
      48             : 
      49             : 
      50             : 
      51             : namespace
      52             : {
      53             : 
      54             : // example taken from communicatord project
      55             : //
      56             : constexpr int const         LOCAL_PORT = 4040;
      57             : constexpr std::string_view  g_communicatord_default_ip = "127.0.0.1";
      58             : constexpr std::string_view  g_communicatord_default_port = snapdev::integer_to_string_literal<LOCAL_PORT>.data();
      59             : constexpr std::string_view  g_communicatord_colon = ":";
      60             : constexpr std::string_view  g_communicatord_default_ip_port = snapdev::join_string_views<g_communicatord_default_ip, g_communicatord_colon, g_communicatord_default_port>;
      61             : 
      62             : 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>;
      63             : 
      64             : 
      65             : }
      66             : 
      67             : 
      68           3 : CATCH_TEST_CASE("join_string_view", "[basic][strings]")
      69             : {
      70           2 :     CATCH_START_SECTION("join_string_view: join strings at compile time")
      71             :     {
      72           2 :         std::string concat(g_communicatord_default_ip);
      73           1 :         concat += ':';
      74           1 :         concat += std::to_string(LOCAL_PORT);
      75             : 
      76             :         // the cast here is because catch2 doesn't handle the
      77             :         //
      78             :         //     string == string_view
      79             :         //
      80           1 :         CATCH_REQUIRE(concat == std::string(g_communicatord_default_ip_port));
      81           1 :         CATCH_REQUIRE(concat == std::string(g_communicatord_default_ip_port_v2));
      82             :     }
      83             :     CATCH_END_SECTION()
      84           1 : }
      85             : 
      86             : 
      87           6 : CATCH_TEST_CASE("join_strings", "[basic][strings]")
      88             : {
      89           8 :     CATCH_START_SECTION("join_strings: join strings at runtime with empty string in between")
      90             :     {
      91           2 :         std::string manual_concat(g_communicatord_default_ip);
      92           1 :         manual_concat += ':';
      93           1 :         manual_concat += std::to_string(LOCAL_PORT);
      94             : 
      95           1 :         std::vector<std::string> const list = {
      96             :                   std::string(g_communicatord_default_ip)
      97             :                 , std::string(":")
      98             :                 , std::string(g_communicatord_default_port)
      99           2 :             };
     100           2 :         std::string const join_concat(snapdev::join_strings(list, ""));
     101             : 
     102           1 :         CATCH_REQUIRE(manual_concat == join_concat);
     103             :     }
     104             :     CATCH_END_SECTION()
     105             : 
     106           8 :     CATCH_START_SECTION("join_strings: join strings at runtime with commas")
     107             :     {
     108           1 :         std::vector<std::string> const list = {
     109             :                   "Item 1"
     110             :                 , "Item 2"
     111             :                 , "Item 3"
     112           2 :             };
     113           2 :         std::string const join_concat(snapdev::join_strings(list, ", "));
     114             : 
     115           1 :         CATCH_REQUIRE(join_concat == "Item 1, Item 2, Item 3");
     116             :     }
     117             :     CATCH_END_SECTION()
     118             : 
     119           8 :     CATCH_START_SECTION("join_strings: \"join\" one string with commas")
     120             :     {
     121           2 :         std::vector<std::string> const list = { "Item 1" };
     122           2 :         std::string const join_concat(snapdev::join_strings(list, ", "));
     123             : 
     124           1 :         CATCH_REQUIRE(join_concat == "Item 1");
     125             :     }
     126             :     CATCH_END_SECTION()
     127             : 
     128           8 :     CATCH_START_SECTION("join_strings: nothing to join with commas")
     129             :     {
     130           2 :         std::vector<std::string> const list;
     131           2 :         std::string const join_concat(snapdev::join_strings(list, ", "));
     132             : 
     133           1 :         CATCH_REQUIRE(join_concat == std::string());
     134             :     }
     135             :     CATCH_END_SECTION()
     136          10 : }
     137             : 
     138             : 
     139             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13