2 lib/strutil - tests for lib/strutil/replace.c:str_replace_all() function.
4 Copyright (C) 2013-2024
5 Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2013
10 This file is part of the Midnight Commander.
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.
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.
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/>.
26 #define TEST_SUITE_NAME "/lib/strutil"
28 #include "tests/mctest.h"
30 #include "lib/strutil.h"
32 /* --------------------------------------------------------------------------------------------- */
38 str_init_strings (NULL
);
41 /* --------------------------------------------------------------------------------------------- */
47 str_uninit_strings ();
50 /* --------------------------------------------------------------------------------------------- */
52 /* @DataSource("str_replace_all_test_ds") */
54 static const struct str_replace_all_test_ds
58 const char *replacement
;
59 const char *expected_result
;
60 } str_replace_all_test_ds
[] =
63 /* 0. needle not found*/
70 /* 1. replacement is less rather that needle */
71 "some string blablablabla string",
74 "some string 1234 string",
77 /* 2. replacement is great rather that needle */
78 "some string bla string",
81 "some string 1234567890 string",
84 /* 3. replace few substrings in a start of string */
85 "blabla blabla string",
88 "111111111 111111111 string",
91 /* 4. replace few substrings in a middle of string */
92 "some string blabla str blabla string",
95 "some string 111111111 str 111111111 string",
98 /* 5. replace few substrings in an end of string */
99 "some string blabla str blabla",
102 "some string 111111111 str 111111111",
105 /* 6. escaped substring */
112 /* 7. escaped substring */
113 "str \\blabla blabla",
116 "str blabla 111111111",
119 /* 8. escaped substring */
120 "str \\\\\\blabla blabla",
123 "str \\\\blabla 111111111",
126 /* 9. double-escaped substring (actually non-escaped) */
130 "\\\\111111111 111111111",
133 /* 10. partial substring */
140 /* 11. special symbols */
144 "111\t1 1\n1111 111\t1 1\n1111",
147 /* 12. empty string */
156 /* @Test(dataSource = "str_replace_all_test_ds") */
158 START_PARAMETRIZED_TEST (str_replace_all_test
, str_replace_all_test_ds
)
165 actual_result
= str_replace_all (data
->haystack
, data
->needle
, data
->replacement
);
168 mctest_assert_str_eq (actual_result
, data
->expected_result
);
169 g_free (actual_result
);
172 END_PARAMETRIZED_TEST
175 /* --------------------------------------------------------------------------------------------- */
182 tc_core
= tcase_create ("Core");
184 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
186 /* Add new tests here: *************** */
187 mctest_add_parameterized_test (tc_core
, str_replace_all_test
, str_replace_all_test_ds
);
188 /* *********************************** */
190 return mctest_run_all (tc_core
);
193 /* --------------------------------------------------------------------------------------------- */