root/tests/src/editor/edit_complete_word_cmd.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_refresh
  2. edit_load_syntax
  3. edit_get_syntax_color
  4. edit_load_macro_cmd
  5. edit_completion_dialog_show
  6. edit_completion_dialog_show__init
  7. edit_completion_dialog_show__string_free
  8. edit_completion_dialog_show__deinit
  9. my_setup
  10. my_teardown
  11. START_PARAMETRIZED_TEST
  12. START_PARAMETRIZED_TEST
  13. main

   1 /*
   2    src/editor - tests for edit_complete_word_cmd() function
   3 
   4    Copyright (C) 2013-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9    Andrew Borodin <aborodin@vmail.ru>, 2021-2022
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  25  */
  26 
  27 #define TEST_SUITE_NAME "/src/editor"
  28 
  29 #include "tests/mctest.h"
  30 
  31 #include <ctype.h>
  32 
  33 #ifdef HAVE_CHARSET
  34 #include "lib/charsets.h"
  35 #endif
  36 #include "lib/strutil.h"
  37 
  38 #include "src/vfs/local/local.c"
  39 #ifdef HAVE_CHARSET
  40 #include "src/selcodepage.h"
  41 #endif
  42 #include "src/editor/editwidget.h"
  43 #include "src/editor/editmacros.h"  // edit_load_macro_cmd()
  44 #include "src/editor/editcomplete.h"
  45 
  46 static WGroup owner;
  47 static WEdit *test_edit;
  48 
  49 /* --------------------------------------------------------------------------------------------- */
  50 /* @Mock */
  51 void
  52 mc_refresh (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54 }
  55 
  56 /* --------------------------------------------------------------------------------------------- */
  57 /* @Mock */
  58 void
  59 edit_load_syntax (WEdit *_edit, GPtrArray *_pnames, const char *_type)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61     (void) _edit;
  62     (void) _pnames;
  63     (void) _type;
  64 }
  65 
  66 /* --------------------------------------------------------------------------------------------- */
  67 
  68 /* @Mock */
  69 int
  70 edit_get_syntax_color (WEdit *_edit, off_t _byte_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
  71 {
  72     (void) _edit;
  73     (void) _byte_index;
  74 
  75     return 0;
  76 }
  77 
  78 /* --------------------------------------------------------------------------------------------- */
  79 
  80 /* @Mock */
  81 gboolean
  82 edit_load_macro_cmd (WEdit *_edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84     (void) _edit;
  85 
  86     return FALSE;
  87 }
  88 
  89 /* --------------------------------------------------------------------------------------------- */
  90 
  91 /* @CapturedValue */
  92 static const WEdit *edit_completion_dialog_show__edit;
  93 /* @CapturedValue */
  94 static int edit_completion_dialog_show__max_width;
  95 /* @CapturedValue */
  96 static GQueue *edit_completion_dialog_show__compl;
  97 
  98 /* @ThenReturnValue */
  99 static char *edit_completion_dialog_show__return_value;
 100 
 101 /* @Mock */
 102 char *
 103 edit_completion_dialog_show (const WEdit *edit, GQueue * compl, int max_width)
     /* [previous][next][first][last][top][bottom][index][help]  */
 104 {
 105 
 106     edit_completion_dialog_show__edit = edit;
 107     edit_completion_dialog_show__max_width = max_width;
 108 
 109     {
 110         GList *i;
 111 
 112         edit_completion_dialog_show__compl = g_queue_new ();
 113 
 114         for (i = g_queue_peek_tail_link (compl); i != NULL; i = g_list_previous (i))
 115         {
 116             GString *s = (GString *) i->data;
 117 
 118             g_queue_push_tail (edit_completion_dialog_show__compl, mc_g_string_dup (s));
 119         }
 120     }
 121 
 122     return edit_completion_dialog_show__return_value;
 123 }
 124 
 125 static void
 126 edit_completion_dialog_show__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 127 {
 128     edit_completion_dialog_show__edit = NULL;
 129     edit_completion_dialog_show__max_width = 0;
 130     edit_completion_dialog_show__compl = NULL;
 131     edit_completion_dialog_show__return_value = NULL;
 132 }
 133 
 134 static void
 135 edit_completion_dialog_show__string_free (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 136 {
 137     g_string_free ((GString *) data, TRUE);
 138 }
 139 
 140 static void
 141 edit_completion_dialog_show__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 142 {
 143     if (edit_completion_dialog_show__compl != NULL)
 144         g_queue_free_full (edit_completion_dialog_show__compl,
 145                            edit_completion_dialog_show__string_free);
 146 }
 147 
 148 /* --------------------------------------------------------------------------------------------- */
 149 
 150 /* @Before */
 151 static void
 152 my_setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 153 {
 154     WRect r;
 155     edit_arg_t arg;
 156 
 157     str_init_strings (NULL);
 158 
 159     vfs_init ();
 160     vfs_init_localfs ();
 161     vfs_setup_work_dir ();
 162 
 163 #ifdef HAVE_CHARSET
 164     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
 165     load_codepages_list ();
 166 #endif
 167 
 168     mc_global.main_config = mc_config_init ("edit_complete_word_cmd.ini", FALSE);
 169     mc_config_set_bool (mc_global.main_config, CONFIG_APP_SECTION,
 170                         "editor_wordcompletion_collect_all_files", TRUE);
 171 
 172     edit_options.filesize_threshold = (char *) "64M";
 173 
 174     rect_init (&r, 0, 0, 24, 80);
 175     arg.file_vpath = vfs_path_from_str ("edit_complete_word_cmd_test_data.txt");
 176     arg.line_number = 1;
 177     test_edit = edit_init (NULL, &r, &arg);
 178     memset (&owner, 0, sizeof (owner));
 179     group_add_widget (&owner, WIDGET (test_edit));
 180     edit_completion_dialog_show__init ();
 181 }
 182 
 183 /* --------------------------------------------------------------------------------------------- */
 184 
 185 /* @After */
 186 static void
 187 my_teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 {
 189     edit_completion_dialog_show__deinit ();
 190     edit_clean (test_edit);
 191     group_remove_widget (test_edit);
 192     g_free (test_edit);
 193 
 194     mc_config_deinit (mc_global.main_config);
 195 
 196 #ifdef HAVE_CHARSET
 197     free_codepages_list ();
 198 #endif
 199 
 200     vfs_shut ();
 201 
 202     str_uninit_strings ();
 203 }
 204 
 205 /* --------------------------------------------------------------------------------------------- */
 206 
 207 #ifdef HAVE_CHARSET
 208 /* @DataSource("test_autocomplete_ds") */
 209 static const struct test_autocomplete_ds
 210 {
 211     off_t input_position;
 212     const char *input_system_code_page;
 213     int input_source_codepage_id;
 214     const char *input_editor_code_page;
 215     int input_display_codepage_id;
 216     const char *input_completed_word;
 217 
 218     int expected_max_width;
 219     int expected_compl_word_count;
 220     int input_completed_word_start_pos;
 221     const char *expected_completed_word;
 222 } test_autocomplete_ds[] = {
 223     {
 224         // 0. KOI8-R/UTF-8 - эъйцукен
 225         102,
 226         "KOI8-R",
 227         0,
 228         "UTF-8",
 229         1,
 230         "эъйцукен",
 231 
 232         16,
 233         2,
 234         98,
 235         "эъйцукен",
 236     },
 237     {
 238         // 1. UTF-8/KOI8-R - эъйцукен
 239         138,
 240         "UTF-8",
 241         1,
 242         "KOI8-R",
 243         0,
 244         "\xDC\xDF\xCA\xC3\xD5\xCB\xC5\xCE",
 245 
 246         8,
 247         2,
 248         136,
 249         "\xDC\xDF\xCA\xC3\xD5\xCB\xC5\xCE",
 250     },
 251 };
 252 
 253 /* @Test(dataSource = "test_autocomplete_ds") */
 254 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 255 {
 256     // given
 257     edit_completion_dialog_show__return_value = g_strdup (data->input_completed_word);
 258 
 259     mc_global.source_codepage = data->input_source_codepage_id;
 260     mc_global.display_codepage = data->input_display_codepage_id;
 261     cp_source = data->input_editor_code_page;
 262     cp_display = data->input_system_code_page;
 263 
 264     do_set_codepage (0);
 265     edit_set_codeset (test_edit);
 266 
 267     // when
 268     edit_cursor_move (test_edit, data->input_position);
 269     edit_complete_word_cmd (test_edit);
 270 
 271     // then
 272     mctest_assert_ptr_eq (edit_completion_dialog_show__edit, test_edit);
 273     ck_assert_int_eq (g_queue_get_length (edit_completion_dialog_show__compl),
 274                       data->expected_compl_word_count);
 275     ck_assert_int_eq (edit_completion_dialog_show__max_width, data->expected_max_width);
 276 
 277     {
 278         off_t i = 0;
 279         GString *actual_completed_str;
 280 
 281         actual_completed_str = g_string_new ("");
 282 
 283         while (TRUE)
 284         {
 285             int chr;
 286 
 287             chr = edit_buffer_get_byte (&test_edit->buffer,
 288                                         data->input_completed_word_start_pos + i++);
 289             if (isspace (chr))
 290                 break;
 291             g_string_append_c (actual_completed_str, chr);
 292         }
 293         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 294         g_string_free (actual_completed_str, TRUE);
 295     }
 296 }
 297 END_PARAMETRIZED_TEST
 298 
 299 /* --------------------------------------------------------------------------------------------- */
 300 
 301 /* @DataSource("test_autocomplete_single_ds") */
 302 static const struct test_autocomplete_single_ds
 303 {
 304     off_t input_position;
 305     const char *input_system_code_page;
 306     int input_source_codepage_id;
 307     const char *input_editor_code_page;
 308     int input_display_codepage_id;
 309 
 310     int input_completed_word_start_pos;
 311 
 312     const char *expected_completed_word;
 313 } test_autocomplete_single_ds[] = {
 314     {
 315         // 0. UTF-8/KOI8-R - фыва
 316         146,
 317         "UTF-8",
 318         1,
 319         "KOI8-R",
 320         0,
 321 
 322         145,
 323         "\xC6\xD9\xD7\xC1",
 324     },
 325 };
 326 
 327 /* @Test(dataSource = "test_autocomplete_single_ds") */
 328 START_PARAMETRIZED_TEST (test_autocomplete_single, test_autocomplete_single_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 329 {
 330     // given
 331     mc_global.source_codepage = data->input_source_codepage_id;
 332     mc_global.display_codepage = data->input_display_codepage_id;
 333     cp_source = data->input_editor_code_page;
 334     cp_display = data->input_system_code_page;
 335 
 336     do_set_codepage (0);
 337     edit_set_codeset (test_edit);
 338 
 339     // when
 340     edit_cursor_move (test_edit, data->input_position);
 341     edit_complete_word_cmd (test_edit);
 342 
 343     // then
 344     {
 345         off_t i = 0;
 346         GString *actual_completed_str;
 347 
 348         actual_completed_str = g_string_new ("");
 349 
 350         while (TRUE)
 351         {
 352             int chr;
 353 
 354             chr = edit_buffer_get_byte (&test_edit->buffer,
 355                                         data->input_completed_word_start_pos + i++);
 356             if (isspace (chr))
 357                 break;
 358             g_string_append_c (actual_completed_str, chr);
 359         }
 360         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 361         g_string_free (actual_completed_str, TRUE);
 362     }
 363 }
 364 END_PARAMETRIZED_TEST
 365 
 366 #endif
 367 
 368 /* --------------------------------------------------------------------------------------------- */
 369 
 370 int
 371 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 372 {
 373     TCase *tc_core;
 374 
 375     tc_core = tcase_create ("Core");
 376 
 377     tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
 378 
 379     // Add new tests here: ***************
 380 #ifdef HAVE_CHARSET
 381     mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
 382     mctest_add_parameterized_test (tc_core, test_autocomplete_single, test_autocomplete_single_ds);
 383 #endif
 384     // ***********************************
 385 
 386     return mctest_run_all (tc_core);
 387 }
 388 
 389 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */