2 libmc - checks for processing esc sequences in replace string
4 Copyright (C) 2011-2016
5 Free Software Foundation, Inc.
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 ) \
35 fail_unless( a == b, "ret_value != %s", (b) ? "TRUE": "FALSE" ); \
36 fail_unless( c == d, "skip_len(%d) != %d", c, d ); \
37 if (f!=0) fail_unless( e == f, "ret(%d) != %d", e, f ); \
40 #define test_helper_handle_esc_seq( pos, r, skip, flag ) \
43 test_helper_check_valid_data(\
44 mc_search_regex__replace_handle_esc_seq( replace_str, pos, &skip_len, &ret ), r,\
50 /* --------------------------------------------------------------------------------------------- */
52 /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
54 static const struct test_regex_replace_esc_seq_prepare_ds
56 const char *input_string
;
57 const size_t input_pos
;
59 const gboolean expected_result
;
60 const gsize expected_skipped_len
;
61 const int expected_flags
;
62 } test_regex_replace_esc_seq_prepare_ds
[] =
65 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
69 REPLACE_PREPARE_T_ESCAPE_SEQ
72 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
76 REPLACE_PREPARE_T_ESCAPE_SEQ
78 { /* 2. \\x{456abcd} */
79 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
83 REPLACE_PREPARE_T_ESCAPE_SEQ
86 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
90 REPLACE_PREPARE_T_NOTHING_SPECIAL
93 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
97 REPLACE_PREPARE_T_ESCAPE_SEQ
100 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
104 REPLACE_PREPARE_T_ESCAPE_SEQ
107 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
111 REPLACE_PREPARE_T_ESCAPE_SEQ
114 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
118 REPLACE_PREPARE_T_ESCAPE_SEQ
121 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
125 REPLACE_PREPARE_T_ESCAPE_SEQ
128 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
132 REPLACE_PREPARE_T_ESCAPE_SEQ
135 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
139 REPLACE_PREPARE_T_ESCAPE_SEQ
142 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
146 REPLACE_PREPARE_T_NOTHING_SPECIAL
148 { /* 12. \\x{qwerty} */
149 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
153 REPLACE_PREPARE_T_NOTHING_SPECIAL
156 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
162 { /* 14. \\x{456a-bcd} */
163 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
167 REPLACE_PREPARE_T_NOTHING_SPECIAL
170 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
179 /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
181 START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare
, test_regex_replace_esc_seq_prepare_ds
)
185 GString
*replace_str
;
186 gsize actual_skipped_len
= 0;
187 int actual_flags
= 0;
188 gboolean actual_result
;
190 replace_str
= g_string_new (data
->input_string
);
194 mc_search_regex__replace_handle_esc_seq (replace_str
, data
->input_pos
, &actual_skipped_len
,
198 mctest_assert_int_eq (actual_result
, data
->expected_result
);
199 mctest_assert_int_eq (actual_skipped_len
, data
->expected_skipped_len
);
200 mctest_assert_int_eq (actual_flags
, data
->expected_flags
);
202 g_string_free (replace_str
, TRUE
);
205 END_PARAMETRIZED_TEST
208 /* --------------------------------------------------------------------------------------------- */
215 Suite
*s
= suite_create (TEST_SUITE_NAME
);
216 TCase
*tc_core
= tcase_create ("Core");
219 /* Add new tests here: *************** */
220 mctest_add_parameterized_test (tc_core
, test_regex_replace_esc_seq_prepare
,
221 test_regex_replace_esc_seq_prepare_ds
);
222 /* *********************************** */
224 suite_add_tcase (s
, tc_core
);
225 sr
= srunner_create (s
);
226 srunner_set_log (sr
, "regex_replace_esc_seq.log");
227 srunner_run_all (sr
, CK_ENV
);
228 number_failed
= srunner_ntests_failed (sr
);
230 return (number_failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
233 /* --------------------------------------------------------------------------------------------- */