regex_replace_esc_seq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. libmc - checks for processing esc sequences in replace string
  3. Copyright (C) 2011-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 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/search/regex"
  20. #include "tests/mctest.h"
  21. #include "regex.c" // for testing static functions
  22. /* --------------------------------------------------------------------------------------------- */
  23. #define test_helper_check_valid_data(a, b, c, d, e, f) \
  24. { \
  25. ck_assert_msg (a == b, "ret_value != %s", (b) ? "TRUE" : "FALSE"); \
  26. ck_assert_msg (c == d, "skip_len(%d) != %d", c, d); \
  27. if (f != 0) \
  28. ck_assert_msg (e == f, "ret(%d) != %d", e, f); \
  29. }
  30. #define test_helper_handle_esc_seq(pos, r, skip, flag) \
  31. { \
  32. skip_len = 0; \
  33. test_helper_check_valid_data ( \
  34. mc_search_regex__replace_handle_esc_seq (replace_str, pos, &skip_len, &ret), r, \
  35. skip_len, skip, ret, flag); \
  36. }
  37. /* --------------------------------------------------------------------------------------------- */
  38. /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
  39. static const struct test_regex_replace_esc_seq_prepare_ds
  40. {
  41. const char *input_string;
  42. const size_t input_pos;
  43. const gboolean expected_result;
  44. const gsize expected_skipped_len;
  45. const int expected_flags;
  46. } test_regex_replace_esc_seq_prepare_ds[] = {
  47. {
  48. // 0. \\{123}
  49. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  50. 7,
  51. FALSE,
  52. 6,
  53. REPLACE_PREPARE_T_ESCAPE_SEQ,
  54. },
  55. {
  56. // 1. \\xab
  57. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  58. 20,
  59. FALSE,
  60. 4,
  61. REPLACE_PREPARE_T_ESCAPE_SEQ,
  62. },
  63. {
  64. // 2. \\x{456abcd}
  65. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  66. 36,
  67. FALSE,
  68. 11,
  69. REPLACE_PREPARE_T_ESCAPE_SEQ,
  70. },
  71. {
  72. // 3. \\xtre
  73. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  74. 54,
  75. FALSE,
  76. 2,
  77. REPLACE_PREPARE_T_NOTHING_SPECIAL,
  78. },
  79. {
  80. // 4. \\n
  81. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  82. 59,
  83. FALSE,
  84. 2,
  85. REPLACE_PREPARE_T_ESCAPE_SEQ,
  86. },
  87. {
  88. // 5. \\t
  89. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  90. 61,
  91. FALSE,
  92. 2,
  93. REPLACE_PREPARE_T_ESCAPE_SEQ,
  94. },
  95. {
  96. // 6. \\v
  97. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  98. 63,
  99. FALSE,
  100. 2,
  101. REPLACE_PREPARE_T_ESCAPE_SEQ,
  102. },
  103. {
  104. // 7. \\b
  105. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  106. 65,
  107. FALSE,
  108. 2,
  109. REPLACE_PREPARE_T_ESCAPE_SEQ,
  110. },
  111. {
  112. // 8. \\r
  113. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  114. 67,
  115. FALSE,
  116. 2,
  117. REPLACE_PREPARE_T_ESCAPE_SEQ,
  118. },
  119. {
  120. // 9. \\f
  121. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  122. 69,
  123. FALSE,
  124. 2,
  125. REPLACE_PREPARE_T_ESCAPE_SEQ,
  126. },
  127. {
  128. // 10. \\a
  129. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  130. 71,
  131. FALSE,
  132. 2,
  133. REPLACE_PREPARE_T_ESCAPE_SEQ,
  134. },
  135. {
  136. // 11. \\{123
  137. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  138. 0,
  139. TRUE,
  140. 5,
  141. REPLACE_PREPARE_T_NOTHING_SPECIAL,
  142. },
  143. {
  144. // 12. \\x{qwerty}
  145. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  146. 6,
  147. TRUE,
  148. 3,
  149. REPLACE_PREPARE_T_NOTHING_SPECIAL,
  150. },
  151. {
  152. // 13. \\12}
  153. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  154. 17,
  155. TRUE,
  156. 0,
  157. 0,
  158. },
  159. {
  160. // 14. \\x{456a-bcd}
  161. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  162. 22,
  163. TRUE,
  164. 7,
  165. REPLACE_PREPARE_T_NOTHING_SPECIAL,
  166. },
  167. {
  168. // 15. \\satre
  169. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  170. 41,
  171. TRUE,
  172. 0,
  173. 0,
  174. },
  175. };
  176. /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
  177. START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare, test_regex_replace_esc_seq_prepare_ds)
  178. {
  179. // given
  180. GString *replace_str;
  181. gsize actual_skipped_len = 0;
  182. int actual_flags = 0;
  183. gboolean actual_result;
  184. replace_str = g_string_new (data->input_string);
  185. // when
  186. actual_result = mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos,
  187. &actual_skipped_len, &actual_flags);
  188. // then
  189. ck_assert_int_eq (actual_result, data->expected_result);
  190. ck_assert_int_eq (actual_skipped_len, data->expected_skipped_len);
  191. ck_assert_int_eq (actual_flags, data->expected_flags);
  192. g_string_free (replace_str, TRUE);
  193. }
  194. END_PARAMETRIZED_TEST
  195. /* --------------------------------------------------------------------------------------------- */
  196. int
  197. main (void)
  198. {
  199. TCase *tc_core;
  200. tc_core = tcase_create ("Core");
  201. // Add new tests here: ***************
  202. mctest_add_parameterized_test (tc_core, test_regex_replace_esc_seq_prepare,
  203. test_regex_replace_esc_seq_prepare_ds);
  204. // ***********************************
  205. return mctest_run_all (tc_core);
  206. }
  207. /* --------------------------------------------------------------------------------------------- */