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-2025
   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 <https://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 static const struct str_replace_all_test_ds
  54 {
  55     const char *haystack;
  56     const char *needle;
  57     const char *replacement;
  58     const char *expected_result;
  59 } str_replace_all_test_ds[] = {
  60     {
  61         // 0. needle not found
  62         "needle not found",
  63         "blablablabla",
  64         "1234567890",
  65         "needle not found",
  66     },
  67     {
  68         // 1.  replacement is less rather that needle
  69         "some string blablablabla string",
  70         "blablablabla",
  71         "1234",
  72         "some string 1234 string",
  73     },
  74     {
  75         // 2.  replacement is great rather that needle
  76         "some string bla string",
  77         "bla",
  78         "1234567890",
  79         "some string 1234567890 string",
  80     },
  81     {
  82         // 3.  replace few substrings in a start of string
  83         "blabla blabla string",
  84         "blabla",
  85         "111111111",
  86         "111111111 111111111 string",
  87     },
  88     {
  89         // 4.  replace few substrings in a middle of string
  90         "some string blabla str blabla string",
  91         "blabla",
  92         "111111111",
  93         "some string 111111111 str 111111111 string",
  94     },
  95     {
  96         // 5.  replace few substrings in an end of string
  97         "some string blabla str blabla",
  98         "blabla",
  99         "111111111",
 100         "some string 111111111 str 111111111",
 101     },
 102     {
 103         // 6.  escaped substring
 104         "\\blabla blabla",
 105         "blabla",
 106         "111111111",
 107         "blabla 111111111",
 108     },
 109     {
 110         // 7.  escaped substring
 111         "str \\blabla blabla",
 112         "blabla",
 113         "111111111",
 114         "str blabla 111111111",
 115     },
 116     {
 117         // 8.  escaped substring
 118         "str \\\\\\blabla blabla",
 119         "blabla",
 120         "111111111",
 121         "str \\\\blabla 111111111",
 122     },
 123     {
 124         // 9.  double-escaped substring (actually non-escaped)
 125         "\\\\blabla blabla",
 126         "blabla",
 127         "111111111",
 128         "\\\\111111111 111111111",
 129     },
 130     {
 131         // 10.  partial substring
 132         "blablabla",
 133         "blabla",
 134         "111111111",
 135         "111111111bla",
 136     },
 137     {
 138         // 11.  special symbols
 139         "bla bla",
 140         "bla",
 141         "111\t1 1\n1111",
 142         "111\t1 1\n1111 111\t1 1\n1111",
 143     },
 144     {
 145         // 12. empty string
 146         "",
 147         "blablablabla",
 148         "1234567890",
 149         NULL,
 150     },
 151 };
 152 
 153 /* @Test(dataSource = "str_replace_all_test_ds") */
 154 START_PARAMETRIZED_TEST (str_replace_all_test, str_replace_all_test_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 155 {
 156     // given
 157     char *actual_result;
 158 
 159     // when
 160     actual_result = str_replace_all (data->haystack, data->needle, data->replacement);
 161 
 162     // then
 163     mctest_assert_str_eq (actual_result, data->expected_result);
 164     g_free (actual_result);
 165 }
 166 END_PARAMETRIZED_TEST
 167 
 168 /* --------------------------------------------------------------------------------------------- */
 169 
 170 int
 171 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     TCase *tc_core;
 174 
 175     tc_core = tcase_create ("Core");
 176 
 177     tcase_add_checked_fixture (tc_core, setup, teardown);
 178 
 179     // Add new tests here: ***************
 180     mctest_add_parameterized_test (tc_core, str_replace_all_test, str_replace_all_test_ds);
 181     // ***********************************
 182 
 183     return mctest_run_all (tc_core);
 184 }
 185 
 186 /* --------------------------------------------------------------------------------------------- */

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