(examine_cd): split in two functions to be unit test friendly.
[midnight-commander.git] / tests / lib / search / regex_replace_esc_seq.c
blob8326cb6a0ef0ced91029d343457451aa10d99750
1 /*
2 libmc - checks for processing esc sequences in replace string
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011
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 <config.h>
30 #include <check.h>
32 #include "regex.c" /* for testing static functions*/
34 /* --------------------------------------------------------------------------------------------- */
35 #define test_helper_check_valid_data( a, b, c, d, e, f ) \
36 { \
37 fail_unless( a == b, "ret_value != %s", (b) ? "TRUE": "FALSE" ); \
38 fail_unless( c == d, "skip_len(%d) != %d", c, d ); \
39 if (f!=0) fail_unless( e == f, "ret(%d) != %d", e, f ); \
42 #define test_helper_handle_esc_seq( pos, r, skip, flag ) \
43 { \
44 skip_len = 0;\
45 test_helper_check_valid_data(\
46 mc_search_regex__replace_handle_esc_seq( replace_str, pos, &skip_len, &ret ), r,\
47 skip_len, skip,\
48 ret, flag\
49 ); \
52 /* --------------------------------------------------------------------------------------------- */
54 START_TEST (test_regex_replace_esc_seq_prepare_valid)
56 GString *replace_str;
57 gsize skip_len;
58 int ret;
60 replace_str = g_string_new("bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a");
62 test_helper_handle_esc_seq( 7, FALSE, 6, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\{123} */
63 test_helper_handle_esc_seq( 20, FALSE, 4, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\xab */
64 test_helper_handle_esc_seq( 36, FALSE, 11, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\x{456abcd} */
65 test_helper_handle_esc_seq( 54, FALSE, 2, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\xtre */
66 test_helper_handle_esc_seq( 59, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\n */
67 test_helper_handle_esc_seq( 61, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\t */
68 test_helper_handle_esc_seq( 63, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\v */
69 test_helper_handle_esc_seq( 65, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\b */
70 test_helper_handle_esc_seq( 67, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\r */
71 test_helper_handle_esc_seq( 69, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\f */
72 test_helper_handle_esc_seq( 71, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\a */
74 g_string_free(replace_str, TRUE);
76 END_TEST
78 /* --------------------------------------------------------------------------------------------- */
80 START_TEST (test_regex_replace_esc_seq_prepare_invalid)
83 GString *replace_str;
84 gsize skip_len;
85 int ret;
87 replace_str = g_string_new("\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre");
89 test_helper_handle_esc_seq( 0, TRUE, 5, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\{123 */
90 test_helper_handle_esc_seq( 6, TRUE, 3, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\x{qwerty} */
91 test_helper_handle_esc_seq( 17, TRUE, 0, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\12} */
92 test_helper_handle_esc_seq( 22, TRUE, 7, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\x{456a-bcd} */
93 test_helper_handle_esc_seq( 41, TRUE, 0, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\satre */
95 g_string_free(replace_str, TRUE);
97 END_TEST
99 /* --------------------------------------------------------------------------------------------- */
102 main (void)
104 int number_failed;
106 Suite *s = suite_create (TEST_SUITE_NAME);
107 TCase *tc_core = tcase_create ("Core");
108 SRunner *sr;
110 /* Add new tests here: *************** */
111 tcase_add_test (tc_core, test_regex_replace_esc_seq_prepare_valid);
112 tcase_add_test (tc_core, test_regex_replace_esc_seq_prepare_invalid);
113 /* *********************************** */
115 suite_add_tcase (s, tc_core);
116 sr = srunner_create (s);
117 srunner_run_all (sr, CK_NORMAL);
118 number_failed = srunner_ntests_failed (sr);
119 srunner_free (sr);
120 return (number_failed == 0) ? 0 : 1;
123 /* --------------------------------------------------------------------------------------------- */