str_replace_all.c 4.7 KB

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