51b47e7d0fed2d58c905c72459f9c669a5739e56
[midnight-commander.git] / tests / src / editor / editcmd__edit_complete_word_cmd.c
blob51b47e7d0fed2d58c905c72459f9c669a5739e56
1 /*
2 src/editor - tests for edit_complete_word_cmd() function
4 Copyright (C) 2013
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 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 "/src/editor"
28 #include "tests/mctest.h"
30 #include <ctype.h>
32 #ifdef HAVE_CHARSET
33 #include "lib/charsets.h"
34 #endif
35 #include "lib/strutil.h"
37 #include "src/vfs/local/local.c"
38 #include "src/editor/editwidget.h"
39 #include "src/editor/editcmd_dialogs.h"
42 static WEdit *test_edit;
44 /* --------------------------------------------------------------------------------------------- */
45 /* @Mock */
46 void
47 edit_load_syntax (WEdit * _edit, char ***_pnames, const char *_type)
49 (void) _edit;
50 (void) _pnames;
51 (void) _type;
54 /* --------------------------------------------------------------------------------------------- */
56 /* @Mock */
57 int
58 edit_get_syntax_color (WEdit * _edit, off_t _byte_index)
60 (void) _edit;
61 (void) _byte_index;
63 return 0;
66 /* --------------------------------------------------------------------------------------------- */
68 /* @Mock */
69 gboolean
70 edit_load_macro_cmd (WEdit * _edit)
72 (void) _edit;
74 return FALSE;
77 /* --------------------------------------------------------------------------------------------- */
79 /* @CapturedValue */
80 static const WEdit *editcmd_dialog_completion_show__edit;
81 /* @CapturedValue */
82 static int editcmd_dialog_completion_show__max_len;
83 /* @CapturedValue */
84 static GString **editcmd_dialog_completion_show__compl;
85 /* @CapturedValue */
86 static int editcmd_dialog_completion_show__num_compl;
88 /* @ThenReturnValue */
89 static char *editcmd_dialog_completion_show__return_value;
91 /* @Mock */
92 char *
93 editcmd_dialog_completion_show (const WEdit * edit, int max_len, GString ** compl, int num_compl)
96 editcmd_dialog_completion_show__edit = edit;
97 editcmd_dialog_completion_show__max_len = max_len;
98 editcmd_dialog_completion_show__num_compl = num_compl;
101 int iterator;
103 editcmd_dialog_completion_show__compl = g_new0 (GString *, num_compl);
105 for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
106 editcmd_dialog_completion_show__compl[iterator] =
107 g_string_new_len (compl[iterator]->str, compl[iterator]->len);
110 return editcmd_dialog_completion_show__return_value;
113 static void
114 editcmd_dialog_completion_show__init (void)
116 editcmd_dialog_completion_show__edit = NULL;
117 editcmd_dialog_completion_show__max_len = 0;
118 editcmd_dialog_completion_show__compl = NULL;
119 editcmd_dialog_completion_show__num_compl = 0;
120 editcmd_dialog_completion_show__return_value = NULL;
123 static void
124 editcmd_dialog_completion_show__deinit (void)
126 if (editcmd_dialog_completion_show__compl != NULL)
128 int iterator;
130 for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
131 g_string_free (editcmd_dialog_completion_show__compl[iterator], TRUE);
133 g_free (editcmd_dialog_completion_show__compl);
138 /* --------------------------------------------------------------------------------------------- */
140 /* @Before */
141 static void
142 my_setup (void)
144 str_init_strings (NULL);
146 vfs_init ();
147 init_localfs ();
148 vfs_setup_work_dir ();
150 #ifdef HAVE_CHARSET
151 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
152 load_codepages_list ();
153 #endif /* HAVE_CHARSET */
155 test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1);
156 editcmd_dialog_completion_show__init ();
159 /* --------------------------------------------------------------------------------------------- */
161 /* @After */
162 static void
163 my_teardown (void)
165 editcmd_dialog_completion_show__deinit ();
166 edit_clean (test_edit);
167 g_free (test_edit);
169 #ifdef HAVE_CHARSET
170 free_codepages_list ();
171 #endif /* HAVE_CHARSET */
173 vfs_shut ();
175 str_uninit_strings ();
178 /* --------------------------------------------------------------------------------------------- */
180 #ifdef HAVE_CHARSET
181 /* @DataSource("test_autocomplete_ds") */
182 /* *INDENT-OFF* */
183 static const struct test_autocomplete_ds
185 off_t input_position;
186 const char *input_system_code_page;
187 const char *input_editor_code_page;
188 const char *input_completed_word;
190 int expected_max_len;
191 int expected_compl_word_count;
192 int input_completed_word_start_pos;
193 const char *expected_completed_word;
194 } test_autocomplete_ds[] =
196 { /* 0. */
197 111,
198 "KOI8-R",
199 "UTF-8",
200 "эъйцукен",
204 107,
205 "эъйцукен"
207 { /* 0. */
208 147,
209 "KOI8-R",
210 "UTF-8",
211 "ÜßÊÃÕËÅÎ",
215 145,
216 "ÜßÊÃÕËÅÎ"
219 /* *INDENT-ON* */
221 /* @Test(dataSource = "test_autocomplete_ds") */
222 /* *INDENT-OFF* */
223 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
224 /* *INDENT-ON* */
226 /* given */
227 editcmd_dialog_completion_show__return_value = g_strdup (data->input_completed_word);
228 cp_display = data->input_system_code_page;
229 cp_source = data->input_editor_code_page;
231 /* when */
232 edit_cursor_move (test_edit, data->input_position);
233 edit_complete_word_cmd (test_edit);
235 /* then */
236 mctest_assert_ptr_eq (editcmd_dialog_completion_show__edit, test_edit);
237 mctest_assert_int_eq (editcmd_dialog_completion_show__num_compl,
238 data->expected_compl_word_count);
239 mctest_assert_int_eq (editcmd_dialog_completion_show__max_len, data->expected_max_len);
242 off_t i = 0;
243 GString *actual_completed_str;
245 actual_completed_str = g_string_new ("");
247 while (TRUE)
249 int chr;
251 chr = edit_get_byte (test_edit, data->input_completed_word_start_pos + i++);
252 if (isspace (chr))
253 break;
254 g_string_append_c (actual_completed_str, chr);
256 mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
257 g_string_free (actual_completed_str, TRUE);
260 /* *INDENT-OFF* */
261 END_PARAMETRIZED_TEST
262 /* *INDENT-ON* */
263 #endif /* HAVE_CHARSET */
265 /* --------------------------------------------------------------------------------------------- */
268 main (void)
270 int number_failed;
272 Suite *s = suite_create (TEST_SUITE_NAME);
273 TCase *tc_core = tcase_create ("Core");
274 SRunner *sr;
276 tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
278 /* Add new tests here: *************** */
279 #ifdef HAVE_CHARSET
280 mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
281 #endif /* HAVE_CHARSET */
282 /* *********************************** */
284 suite_add_tcase (s, tc_core);
285 sr = srunner_create (s);
286 srunner_set_log (sr, "edit_complete_word_cmd.log");
287 srunner_run_all (sr, CK_NORMAL);
288 number_failed = srunner_ntests_failed (sr);
289 srunner_free (sr);
290 return (number_failed == 0) ? 0 : 1;
293 /* --------------------------------------------------------------------------------------------- */