str_replace_all.c 4.8 KB

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