Added tests for regex search
[midnight-commander.git] / tests / lib / search / regex_search.c
blobfa315317cb924fc0397f8c22a51bc14758f50f34
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 "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
37 static mc_search_t *search;
39 static void
40 setup (void)
42 str_init_strings (NULL);
43 search = mc_search_new ("<a", -1);
44 search->search_type = MC_SEARCH_T_REGEX;
47 static void
48 teardown (void)
50 mc_search_free (search);
51 str_uninit_strings ();
55 /* --------------------------------------------------------------------------------------------- */
56 START_TEST (regex_search_test)
58 // given
59 gsize actual_found_len;
60 gboolean actual_result;
63 // when
64 actual_result = mc_search_run (
65 search,
66 "some string with <a for searching",
68 -1,
69 &actual_found_len
72 // then
73 ck_assert_int_eq (actual_result, TRUE);
74 ck_assert_int_eq (actual_found_len, 2);
75 ck_assert_int_eq (search->normal_offset, 17);
77 END_TEST
79 /* --------------------------------------------------------------------------------------------- */
81 static mc_search_cbret_t
82 regex_search_callback (const void *user_data, gsize char_offset, int *current_char) {
83 static const char *data = "some string with <a for searching";
85 (void) user_data;
87 *current_char = data[char_offset];
88 return MC_SEARCH_CB_OK;
91 START_TEST (regex_search_callback_test)
93 // given
94 gsize actual_found_len;
95 gboolean actual_result;
97 search->search_fn = regex_search_callback;
99 // when
100 actual_result = mc_search_run (
101 search,
102 NULL,
105 &actual_found_len
107 // then
108 ck_assert_int_eq (actual_result, TRUE);
109 ck_assert_int_eq (actual_found_len, 2);
110 ck_assert_int_eq (search->normal_offset, 17);
113 END_TEST
115 /* --------------------------------------------------------------------------------------------- */
119 main (void)
121 int number_failed;
123 Suite *s = suite_create (TEST_SUITE_NAME);
124 TCase *tc_core = tcase_create ("Core");
125 SRunner *sr;
127 tcase_add_checked_fixture (tc_core, setup, teardown);
129 /* Add new tests here: *************** */
130 tcase_add_test (tc_core, regex_search_test);
131 tcase_add_test (tc_core, regex_search_callback_test);
132 /* *********************************** */
134 suite_add_tcase (s, tc_core);
135 sr = srunner_create (s);
136 srunner_run_all (sr, CK_NORMAL);
137 number_failed = srunner_ntests_failed (sr);
138 srunner_free (sr);
139 return (number_failed == 0) ? 0 : 1;
142 /* --------------------------------------------------------------------------------------------- */