regex_replace_esc_seq.c 6.7 KB

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