Add test_autocomplete_single() test
[midnight-commander.git] / tests / src / editor / editcmd__edit_complete_word_cmd.c
blob2efbf75650746464f91e156980b75025cf6a1fca
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 #ifdef HAVE_CHARSET
39 #include "src/selcodepage.h"
40 #endif
41 #include "src/editor/editwidget.h"
42 #include "src/editor/editcmd_dialogs.h"
45 static WEdit *test_edit;
47 /* --------------------------------------------------------------------------------------------- */
48 /* @Mock */
49 void
50 edit_load_syntax (WEdit * _edit, char ***_pnames, const char *_type)
52 (void) _edit;
53 (void) _pnames;
54 (void) _type;
57 /* --------------------------------------------------------------------------------------------- */
59 /* @Mock */
60 int
61 edit_get_syntax_color (WEdit * _edit, off_t _byte_index)
63 (void) _edit;
64 (void) _byte_index;
66 return 0;
69 /* --------------------------------------------------------------------------------------------- */
71 /* @Mock */
72 gboolean
73 edit_load_macro_cmd (WEdit * _edit)
75 (void) _edit;
77 return FALSE;
80 /* --------------------------------------------------------------------------------------------- */
82 /* @CapturedValue */
83 static const WEdit *editcmd_dialog_completion_show__edit;
84 /* @CapturedValue */
85 static int editcmd_dialog_completion_show__max_len;
86 /* @CapturedValue */
87 static GString **editcmd_dialog_completion_show__compl;
88 /* @CapturedValue */
89 static int editcmd_dialog_completion_show__num_compl;
91 /* @ThenReturnValue */
92 static char *editcmd_dialog_completion_show__return_value;
94 /* @Mock */
95 char *
96 editcmd_dialog_completion_show (const WEdit * edit, int max_len, GString ** compl, int num_compl)
99 editcmd_dialog_completion_show__edit = edit;
100 editcmd_dialog_completion_show__max_len = max_len;
101 editcmd_dialog_completion_show__num_compl = num_compl;
104 int iterator;
106 editcmd_dialog_completion_show__compl = g_new0 (GString *, num_compl);
108 for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
109 editcmd_dialog_completion_show__compl[iterator] =
110 g_string_new_len (compl[iterator]->str, compl[iterator]->len);
113 return editcmd_dialog_completion_show__return_value;
116 static void
117 editcmd_dialog_completion_show__init (void)
119 editcmd_dialog_completion_show__edit = NULL;
120 editcmd_dialog_completion_show__max_len = 0;
121 editcmd_dialog_completion_show__compl = NULL;
122 editcmd_dialog_completion_show__num_compl = 0;
123 editcmd_dialog_completion_show__return_value = NULL;
126 static void
127 editcmd_dialog_completion_show__deinit (void)
129 if (editcmd_dialog_completion_show__compl != NULL)
131 int iterator;
133 for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
134 g_string_free (editcmd_dialog_completion_show__compl[iterator], TRUE);
136 g_free (editcmd_dialog_completion_show__compl);
141 /* --------------------------------------------------------------------------------------------- */
143 /* @Before */
144 static void
145 my_setup (void)
147 str_init_strings (NULL);
149 vfs_init ();
150 init_localfs ();
151 vfs_setup_work_dir ();
153 #ifdef HAVE_CHARSET
154 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
155 load_codepages_list ();
156 #endif /* HAVE_CHARSET */
158 test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1);
159 editcmd_dialog_completion_show__init ();
162 /* --------------------------------------------------------------------------------------------- */
164 /* @After */
165 static void
166 my_teardown (void)
168 editcmd_dialog_completion_show__deinit ();
169 edit_clean (test_edit);
170 g_free (test_edit);
172 #ifdef HAVE_CHARSET
173 free_codepages_list ();
174 #endif /* HAVE_CHARSET */
176 vfs_shut ();
178 str_uninit_strings ();
181 /* --------------------------------------------------------------------------------------------- */
183 #ifdef HAVE_CHARSET
184 /* @DataSource("test_autocomplete_ds") */
185 /* *INDENT-OFF* */
186 static const struct test_autocomplete_ds
188 off_t input_position;
189 const char *input_system_code_page;
190 int input_source_codepage_id;
191 const char *input_editor_code_page;
192 int input_display_codepage_id;
193 const char *input_completed_word;
195 int expected_max_len;
196 int expected_compl_word_count;
197 int input_completed_word_start_pos;
198 const char *expected_completed_word;
199 } test_autocomplete_ds[] =
201 { /* 0. */
202 111,
203 "KOI8-R",
205 "UTF-8",
207 "эъйцукен",
211 107,
212 "эъйцукен"
214 { /* 1. */
215 147,
216 "UTF-8",
218 "KOI8-R",
220 "ÜßÊÃÕËÅÎ",
224 145,
225 "ÜßÊÃÕËÅÎ"
228 /* *INDENT-ON* */
230 /* @Test(dataSource = "test_autocomplete_ds") */
231 /* *INDENT-OFF* */
232 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
233 /* *INDENT-ON* */
235 /* given */
236 editcmd_dialog_completion_show__return_value = g_strdup (data->input_completed_word);
239 mc_global.source_codepage = data->input_source_codepage_id;
240 mc_global.display_codepage = data->input_display_codepage_id;
241 cp_source = data->input_editor_code_page;
242 cp_display = data->input_system_code_page;
244 do_set_codepage (0);
245 edit_set_codeset (test_edit);
247 /* when */
248 edit_cursor_move (test_edit, data->input_position);
249 edit_complete_word_cmd (test_edit);
251 /* then */
252 mctest_assert_ptr_eq (editcmd_dialog_completion_show__edit, test_edit);
253 mctest_assert_int_eq (editcmd_dialog_completion_show__num_compl,
254 data->expected_compl_word_count);
255 mctest_assert_int_eq (editcmd_dialog_completion_show__max_len, data->expected_max_len);
258 off_t i = 0;
259 GString *actual_completed_str;
261 actual_completed_str = g_string_new ("");
263 while (TRUE)
265 int chr;
267 chr = edit_get_byte (test_edit, data->input_completed_word_start_pos + i++);
268 if (isspace (chr))
269 break;
270 g_string_append_c (actual_completed_str, chr);
272 mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
273 g_string_free (actual_completed_str, TRUE);
276 /* *INDENT-OFF* */
277 END_PARAMETRIZED_TEST
278 /* *INDENT-ON* */
280 /* --------------------------------------------------------------------------------------------- */
282 /* @DataSource("test_autocomplete_single_ds") */
283 /* *INDENT-OFF* */
284 static const struct test_autocomplete_single_ds
286 off_t input_position;
287 const char *input_system_code_page;
288 int input_source_codepage_id;
289 const char *input_editor_code_page;
290 int input_display_codepage_id;
292 int input_completed_word_start_pos;
294 const char *expected_completed_word;
295 } test_autocomplete_single_ds[] =
297 { /* 0. */
298 155,
299 "UTF-8",
301 "KOI8-R",
304 154,
305 "ÆÙ×Á"
308 /* *INDENT-ON* */
310 /* @Test(dataSource = "test_autocomplete_single_ds") */
311 /* *INDENT-OFF* */
312 START_PARAMETRIZED_TEST (test_autocomplete_single, test_autocomplete_single_ds)
313 /* *INDENT-ON* */
315 /* given */
316 mc_global.source_codepage = data->input_source_codepage_id;
317 mc_global.display_codepage = data->input_display_codepage_id;
318 cp_source = data->input_editor_code_page;
319 cp_display = data->input_system_code_page;
321 do_set_codepage (0);
322 edit_set_codeset (test_edit);
324 /* when */
325 edit_cursor_move (test_edit, data->input_position);
326 edit_complete_word_cmd (test_edit);
328 /* then */
330 off_t i = 0;
331 GString *actual_completed_str;
333 actual_completed_str = g_string_new ("");
335 while (TRUE)
337 int chr;
339 chr = edit_get_byte (test_edit, data->input_completed_word_start_pos + i++);
340 if (isspace (chr))
341 break;
342 g_string_append_c (actual_completed_str, chr);
344 mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
345 g_string_free (actual_completed_str, TRUE);
348 /* *INDENT-OFF* */
349 END_PARAMETRIZED_TEST
350 /* *INDENT-ON* */
353 #endif /* HAVE_CHARSET */
355 /* --------------------------------------------------------------------------------------------- */
358 main (void)
360 int number_failed;
362 Suite *s = suite_create (TEST_SUITE_NAME);
363 TCase *tc_core = tcase_create ("Core");
364 SRunner *sr;
366 tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
368 /* Add new tests here: *************** */
369 #ifdef HAVE_CHARSET
370 mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
371 mctest_add_parameterized_test (tc_core, test_autocomplete_single, test_autocomplete_single_ds);
372 #endif /* HAVE_CHARSET */
373 /* *********************************** */
375 suite_add_tcase (s, tc_core);
376 sr = srunner_create (s);
377 srunner_set_log (sr, "edit_complete_word_cmd.log");
378 srunner_run_all (sr, CK_NORMAL);
379 number_failed = srunner_ntests_failed (sr);
380 srunner_free (sr);
381 return (number_failed == 0) ? 0 : 1;
384 /* --------------------------------------------------------------------------------------------- */