Merge branch '4562_mcedit_macros_paste'
[midnight-commander.git] / tests / lib / search / regex_replace_esc_seq.c
blob39298d77338d9daca6b2c99dea4ea520c244496f
1 /*
2 libmc - checks for processing esc sequences in replace string
4 Copyright (C) 2011-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "lib/search/regex"
28 #include "tests/mctest.h"
30 #include "regex.c" /* for testing static functions */
32 /* --------------------------------------------------------------------------------------------- */
33 #define test_helper_check_valid_data( a, b, c, d, e, f ) \
34 { \
35 ck_assert_msg (a == b, "ret_value != %s", (b) ? "TRUE": "FALSE"); \
36 ck_assert_msg (c == d, "skip_len(%d) != %d", c, d); \
37 if (f != 0) \
38 ck_assert_msg (e == f, "ret(%d) != %d", e, f); \
41 #define test_helper_handle_esc_seq( pos, r, skip, flag ) \
42 { \
43 skip_len = 0;\
44 test_helper_check_valid_data(\
45 mc_search_regex__replace_handle_esc_seq( replace_str, pos, &skip_len, &ret ), r,\
46 skip_len, skip,\
47 ret, flag\
48 ); \
51 /* --------------------------------------------------------------------------------------------- */
53 /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
54 /* *INDENT-OFF* */
55 static const struct test_regex_replace_esc_seq_prepare_ds
57 const char *input_string;
58 const size_t input_pos;
60 const gboolean expected_result;
61 const gsize expected_skipped_len;
62 const int expected_flags;
63 } test_regex_replace_esc_seq_prepare_ds[] =
65 { /* 0. \\{123} */
66 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
68 FALSE,
70 REPLACE_PREPARE_T_ESCAPE_SEQ
72 { /* 1. \\xab */
73 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
74 20,
75 FALSE,
77 REPLACE_PREPARE_T_ESCAPE_SEQ
79 { /* 2. \\x{456abcd} */
80 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
81 36,
82 FALSE,
83 11,
84 REPLACE_PREPARE_T_ESCAPE_SEQ
86 { /* 3. \\xtre */
87 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
88 54,
89 FALSE,
91 REPLACE_PREPARE_T_NOTHING_SPECIAL
93 { /* 4. \\n */
94 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
95 59,
96 FALSE,
98 REPLACE_PREPARE_T_ESCAPE_SEQ
100 { /* 5. \\t */
101 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
103 FALSE,
105 REPLACE_PREPARE_T_ESCAPE_SEQ
107 { /* 6. \\v */
108 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
110 FALSE,
112 REPLACE_PREPARE_T_ESCAPE_SEQ
114 { /* 7. \\b */
115 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
117 FALSE,
119 REPLACE_PREPARE_T_ESCAPE_SEQ
121 { /* 8. \\r */
122 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
124 FALSE,
126 REPLACE_PREPARE_T_ESCAPE_SEQ
128 { /* 9. \\f */
129 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
131 FALSE,
133 REPLACE_PREPARE_T_ESCAPE_SEQ
135 { /* 10. \\a */
136 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
138 FALSE,
140 REPLACE_PREPARE_T_ESCAPE_SEQ
142 { /* 11. \\{123 */
143 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
145 TRUE,
147 REPLACE_PREPARE_T_NOTHING_SPECIAL
149 { /* 12. \\x{qwerty} */
150 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
152 TRUE,
154 REPLACE_PREPARE_T_NOTHING_SPECIAL
156 { /* 13. \\12} */
157 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
159 TRUE,
163 { /* 14. \\x{456a-bcd} */
164 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
166 TRUE,
168 REPLACE_PREPARE_T_NOTHING_SPECIAL
170 { /* 15. \\satre */
171 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
173 TRUE,
178 /* *INDENT-ON* */
180 /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
181 /* *INDENT-OFF* */
182 START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare, test_regex_replace_esc_seq_prepare_ds)
183 /* *INDENT-ON* */
185 /* given */
186 GString *replace_str;
187 gsize actual_skipped_len = 0;
188 int actual_flags = 0;
189 gboolean actual_result;
191 replace_str = g_string_new (data->input_string);
193 /* when */
194 actual_result =
195 mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos, &actual_skipped_len,
196 &actual_flags);
198 /* then */
199 ck_assert_int_eq (actual_result, data->expected_result);
200 ck_assert_int_eq (actual_skipped_len, data->expected_skipped_len);
201 ck_assert_int_eq (actual_flags, data->expected_flags);
203 g_string_free (replace_str, TRUE);
205 /* *INDENT-OFF* */
206 END_PARAMETRIZED_TEST
207 /* *INDENT-ON* */
209 /* --------------------------------------------------------------------------------------------- */
212 main (void)
214 TCase *tc_core;
216 tc_core = tcase_create ("Core");
218 /* Add new tests here: *************** */
219 mctest_add_parameterized_test (tc_core, test_regex_replace_esc_seq_prepare,
220 test_regex_replace_esc_seq_prepare_ds);
221 /* *********************************** */
223 return mctest_run_all (tc_core);
226 /* --------------------------------------------------------------------------------------------- */