regex_replace_esc_seq.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. libmc - checks for processing esc sequences in replace string
  3. Copyright (C) 2011-2021
  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 <http://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,\
  36. ret, flag\
  37. ); \
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
  41. /* *INDENT-OFF* */
  42. static const struct test_regex_replace_esc_seq_prepare_ds
  43. {
  44. const char *input_string;
  45. const size_t input_pos;
  46. const gboolean expected_result;
  47. const gsize expected_skipped_len;
  48. const int expected_flags;
  49. } test_regex_replace_esc_seq_prepare_ds[] =
  50. {
  51. { /* 0. \\{123} */
  52. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  53. 7,
  54. FALSE,
  55. 6,
  56. REPLACE_PREPARE_T_ESCAPE_SEQ
  57. },
  58. { /* 1. \\xab */
  59. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  60. 20,
  61. FALSE,
  62. 4,
  63. REPLACE_PREPARE_T_ESCAPE_SEQ
  64. },
  65. { /* 2. \\x{456abcd} */
  66. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  67. 36,
  68. FALSE,
  69. 11,
  70. REPLACE_PREPARE_T_ESCAPE_SEQ
  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. { /* 4. \\n */
  80. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  81. 59,
  82. FALSE,
  83. 2,
  84. REPLACE_PREPARE_T_ESCAPE_SEQ
  85. },
  86. { /* 5. \\t */
  87. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  88. 61,
  89. FALSE,
  90. 2,
  91. REPLACE_PREPARE_T_ESCAPE_SEQ
  92. },
  93. { /* 6. \\v */
  94. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  95. 63,
  96. FALSE,
  97. 2,
  98. REPLACE_PREPARE_T_ESCAPE_SEQ
  99. },
  100. { /* 7. \\b */
  101. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  102. 65,
  103. FALSE,
  104. 2,
  105. REPLACE_PREPARE_T_ESCAPE_SEQ
  106. },
  107. { /* 8. \\r */
  108. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  109. 67,
  110. FALSE,
  111. 2,
  112. REPLACE_PREPARE_T_ESCAPE_SEQ
  113. },
  114. { /* 9. \\f */
  115. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  116. 69,
  117. FALSE,
  118. 2,
  119. REPLACE_PREPARE_T_ESCAPE_SEQ
  120. },
  121. { /* 10. \\a */
  122. "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  123. 71,
  124. FALSE,
  125. 2,
  126. REPLACE_PREPARE_T_ESCAPE_SEQ
  127. },
  128. { /* 11. \\{123 */
  129. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  130. 0,
  131. TRUE,
  132. 5,
  133. REPLACE_PREPARE_T_NOTHING_SPECIAL
  134. },
  135. { /* 12. \\x{qwerty} */
  136. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  137. 6,
  138. TRUE,
  139. 3,
  140. REPLACE_PREPARE_T_NOTHING_SPECIAL
  141. },
  142. { /* 13. \\12} */
  143. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  144. 17,
  145. TRUE,
  146. 0,
  147. 0
  148. },
  149. { /* 14. \\x{456a-bcd} */
  150. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  151. 22,
  152. TRUE,
  153. 7,
  154. REPLACE_PREPARE_T_NOTHING_SPECIAL
  155. },
  156. { /* 15. \\satre */
  157. "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
  158. 41,
  159. TRUE,
  160. 0,
  161. 0
  162. },
  163. };
  164. /* *INDENT-ON* */
  165. /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
  166. /* *INDENT-OFF* */
  167. START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare, test_regex_replace_esc_seq_prepare_ds)
  168. /* *INDENT-ON* */
  169. {
  170. /* given */
  171. GString *replace_str;
  172. gsize actual_skipped_len = 0;
  173. int actual_flags = 0;
  174. gboolean actual_result;
  175. replace_str = g_string_new (data->input_string);
  176. /* when */
  177. actual_result =
  178. mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos, &actual_skipped_len,
  179. &actual_flags);
  180. /* then */
  181. mctest_assert_int_eq (actual_result, data->expected_result);
  182. mctest_assert_int_eq (actual_skipped_len, data->expected_skipped_len);
  183. mctest_assert_int_eq (actual_flags, data->expected_flags);
  184. g_string_free (replace_str, TRUE);
  185. }
  186. /* *INDENT-OFF* */
  187. END_PARAMETRIZED_TEST
  188. /* *INDENT-ON* */
  189. /* --------------------------------------------------------------------------------------------- */
  190. int
  191. main (void)
  192. {
  193. TCase *tc_core;
  194. tc_core = tcase_create ("Core");
  195. /* Add new tests here: *************** */
  196. mctest_add_parameterized_test (tc_core, test_regex_replace_esc_seq_prepare,
  197. test_regex_replace_esc_seq_prepare_ds);
  198. /* *********************************** */
  199. return mctest_run_all (tc_core);
  200. }
  201. /* --------------------------------------------------------------------------------------------- */