root/tests/lib/strutil/str_replace_all.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. main

   1 /*
   2    lib/strutil - tests for lib/strutil/replace.c:str_replace_all() function.
   3 
   4    Copyright (C) 2013-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/lib/strutil"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/strutil.h"
  31 
  32 /* --------------------------------------------------------------------------------------------- */
  33 
  34 /* @Before */
  35 static void
  36 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  37 {
  38     str_init_strings (NULL);
  39 }
  40 
  41 /* --------------------------------------------------------------------------------------------- */
  42 
  43 /* @After */
  44 static void
  45 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  46 {
  47     str_uninit_strings ();
  48 }
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 /* @DataSource("str_replace_all_test_ds") */
  53 /* *INDENT-OFF* */
  54 static const struct str_replace_all_test_ds
  55 {
  56     const char *haystack;
  57     const char *needle;
  58     const char *replacement;
  59     const char *expected_result;
  60 } str_replace_all_test_ds[] =
  61 {
  62     {
  63         /* 0. needle not found*/
  64         "needle not found",
  65         "blablablabla",
  66         "1234567890",
  67         "needle not found",
  68     },
  69     {
  70         /* 1.  replacement is less rather that needle */
  71         "some string blablablabla string",
  72        "blablablabla",
  73         "1234",
  74         "some string 1234 string",
  75     },
  76     {
  77         /* 2.  replacement is great rather that needle */
  78         "some string bla string",
  79         "bla",
  80         "1234567890",
  81         "some string 1234567890 string",
  82     },
  83     {
  84         /* 3.  replace few substrings in a start of string */
  85         "blabla blabla string",
  86         "blabla",
  87         "111111111",
  88         "111111111 111111111 string",
  89     },
  90     {
  91         /* 4.  replace few substrings in a middle of string */
  92         "some string blabla str blabla string",
  93         "blabla",
  94         "111111111",
  95         "some string 111111111 str 111111111 string",
  96     },
  97     {
  98         /* 5.  replace few substrings in an end of string */
  99         "some string blabla str blabla",
 100         "blabla",
 101         "111111111",
 102         "some string 111111111 str 111111111",
 103     },
 104     {
 105         /* 6.  escaped substring */
 106         "\\blabla blabla",
 107         "blabla",
 108         "111111111",
 109         "blabla 111111111",
 110     },
 111     {
 112         /* 7.  escaped substring */
 113         "str \\blabla blabla",
 114         "blabla",
 115         "111111111",
 116         "str blabla 111111111",
 117     },
 118     {
 119         /* 8.  escaped substring */
 120         "str \\\\\\blabla blabla",
 121         "blabla",
 122         "111111111",
 123         "str \\\\blabla 111111111",
 124     },
 125     {
 126         /* 9.  double-escaped substring (actually non-escaped) */
 127         "\\\\blabla blabla",
 128         "blabla",
 129         "111111111",
 130         "\\\\111111111 111111111",
 131     },
 132     {
 133         /* 10.  partial substring */
 134         "blablabla",
 135         "blabla",
 136         "111111111",
 137         "111111111bla",
 138     },
 139     {
 140         /* 11.  special symbols */
 141         "bla bla",
 142         "bla",
 143         "111\t1 1\n1111",
 144         "111\t1 1\n1111 111\t1 1\n1111",
 145     },
 146     {
 147         /* 12. empty string */
 148         "",
 149         "blablablabla",
 150         "1234567890",
 151         NULL,
 152     },
 153 };
 154 /* *INDENT-ON* */
 155 
 156 /* @Test(dataSource = "str_replace_all_test_ds") */
 157 /* *INDENT-OFF* */
 158 START_PARAMETRIZED_TEST (str_replace_all_test, str_replace_all_test_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 159 /* *INDENT-ON* */
 160 {
 161     /* given */
 162     char *actual_result;
 163 
 164     /* when */
 165     actual_result = str_replace_all (data->haystack, data->needle, data->replacement);
 166 
 167     /* then */
 168     mctest_assert_str_eq (actual_result, data->expected_result);
 169     g_free (actual_result);
 170 }
 171 /* *INDENT-OFF* */
 172 END_PARAMETRIZED_TEST
 173 /* *INDENT-ON* */
 174 
 175 /* --------------------------------------------------------------------------------------------- */
 176 
 177 int
 178 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 179 {
 180     TCase *tc_core;
 181 
 182     tc_core = tcase_create ("Core");
 183 
 184     tcase_add_checked_fixture (tc_core, setup, teardown);
 185 
 186     /* Add new tests here: *************** */
 187     mctest_add_parameterized_test (tc_core, str_replace_all_test, str_replace_all_test_ds);
 188     /* *********************************** */
 189 
 190     return mctest_run_all (tc_core);
 191 }
 192 
 193 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */