Manual pages: mcmcdiffmceditmcview

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 #include "lib/charsets.h"
  34 #include "lib/strutil.h"
  35 
  36 #include "src/vfs/local/local.c"
  37 #include "src/selcodepage.h"
  38 #include "src/editor/editwidget.h"
  39 #include "src/editor/editmacros.h"  // edit_load_macro_cmd()
  40 #include "src/editor/editcomplete.h"
  41 
  42 static WGroup owner;
  43 static WEdit *test_edit;
  44 
  45 /* --------------------------------------------------------------------------------------------- */
  46 /* @Mock */
  47 void
  48 mc_refresh (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  49 {
  50 }
  51 
  52 /* --------------------------------------------------------------------------------------------- */
  53 /* @Mock */
  54 void
  55 edit_load_syntax (WEdit *_edit, GPtrArray *_pnames, const char *_type)
     /* [previous][next][first][last][top][bottom][index][help]  */
  56 {
  57     (void) _edit;
  58     (void) _pnames;
  59     (void) _type;
  60 }
  61 
  62 /* --------------------------------------------------------------------------------------------- */
  63 
  64 /* @Mock */
  65 int
  66 edit_get_syntax_color (WEdit *_edit, off_t _byte_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     (void) _edit;
  69     (void) _byte_index;
  70 
  71     return 0;
  72 }
  73 
  74 /* --------------------------------------------------------------------------------------------- */
  75 
  76 /* @Mock */
  77 gboolean
  78 edit_load_macro_cmd (WEdit *_edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
  79 {
  80     (void) _edit;
  81 
  82     return FALSE;
  83 }
  84 
  85 /* --------------------------------------------------------------------------------------------- */
  86 
  87 /* @CapturedValue */
  88 static const WEdit *edit_completion_dialog_show__edit;
  89 /* @CapturedValue */
  90 static int edit_completion_dialog_show__max_width;
  91 /* @CapturedValue */
  92 static GQueue *edit_completion_dialog_show__compl;
  93 
  94 /* @ThenReturnValue */
  95 static char *edit_completion_dialog_show__return_value;
  96 
  97 /* @Mock */
  98 char *
  99 edit_completion_dialog_show (const WEdit *edit, GQueue * compl, int max_width)
     /* [previous][next][first][last][top][bottom][index][help]  */
 100 {
 101 
 102     edit_completion_dialog_show__edit = edit;
 103     edit_completion_dialog_show__max_width = max_width;
 104 
 105     {
 106         GList *i;
 107 
 108         edit_completion_dialog_show__compl = g_queue_new ();
 109 
 110         for (i = g_queue_peek_tail_link (compl); i != NULL; i = g_list_previous (i))
 111         {
 112             GString *s = (GString *) i->data;
 113 
 114             g_queue_push_tail (edit_completion_dialog_show__compl, mc_g_string_dup (s));
 115         }
 116     }
 117 
 118     return edit_completion_dialog_show__return_value;
 119 }
 120 
 121 static void
 122 edit_completion_dialog_show__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 123 {
 124     edit_completion_dialog_show__edit = NULL;
 125     edit_completion_dialog_show__max_width = 0;
 126     edit_completion_dialog_show__compl = NULL;
 127     edit_completion_dialog_show__return_value = NULL;
 128 }
 129 
 130 static void
 131 edit_completion_dialog_show__string_free (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 132 {
 133     g_string_free ((GString *) data, TRUE);
 134 }
 135 
 136 static void
 137 edit_completion_dialog_show__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 138 {
 139     if (edit_completion_dialog_show__compl != NULL)
 140         g_queue_free_full (edit_completion_dialog_show__compl,
 141                            edit_completion_dialog_show__string_free);
 142 }
 143 
 144 /* --------------------------------------------------------------------------------------------- */
 145 
 146 /* @Before */
 147 static void
 148 my_setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 149 {
 150     WRect r;
 151     edit_arg_t arg;
 152 
 153     str_init_strings (NULL);
 154 
 155     vfs_init ();
 156     vfs_init_localfs ();
 157     vfs_setup_work_dir ();
 158 
 159     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
 160     load_codepages_list ();
 161 
 162     mc_global.main_config = mc_config_init ("edit_complete_word_cmd.ini", FALSE);
 163     mc_config_set_bool (mc_global.main_config, CONFIG_APP_SECTION,
 164                         "editor_wordcompletion_collect_all_files", TRUE);
 165 
 166     edit_options.filesize_threshold = (char *) "64M";
 167 
 168     rect_init (&r, 0, 0, 24, 80);
 169     arg.file_vpath = vfs_path_from_str ("edit_complete_word_cmd_test_data.txt");
 170     arg.line_number = 1;
 171     test_edit = edit_init (NULL, &r, &arg);
 172     memset (&owner, 0, sizeof (owner));
 173     group_add_widget (&owner, WIDGET (test_edit));
 174     edit_completion_dialog_show__init ();
 175 }
 176 
 177 /* --------------------------------------------------------------------------------------------- */
 178 
 179 /* @After */
 180 static void
 181 my_teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 182 {
 183     edit_completion_dialog_show__deinit ();
 184     edit_clean (test_edit);
 185     group_remove_widget (test_edit);
 186     g_free (test_edit);
 187 
 188     mc_config_deinit (mc_global.main_config);
 189     free_codepages_list ();
 190     vfs_shut ();
 191     str_uninit_strings ();
 192 }
 193 
 194 /* --------------------------------------------------------------------------------------------- */
 195 
 196 /* @DataSource("test_autocomplete_ds") */
 197 static const struct test_autocomplete_ds
 198 {
 199     off_t input_position;
 200     const char *input_system_code_page;
 201     int input_source_codepage_id;
 202     const char *input_editor_code_page;
 203     int input_display_codepage_id;
 204     const char *input_completed_word;
 205 
 206     int expected_max_width;
 207     int expected_compl_word_count;
 208     int input_completed_word_start_pos;
 209     const char *expected_completed_word;
 210 } test_autocomplete_ds[] = {
 211     {
 212         // 0. KOI8-R/UTF-8 - эъйцукен
 213         102,
 214         "KOI8-R",
 215         0,
 216         "UTF-8",
 217         1,
 218         "эъйцукен",
 219 
 220         16,
 221         2,
 222         98,
 223         "эъйцукен",
 224     },
 225     {
 226         // 1. UTF-8/KOI8-R - эъйцукен
 227         138,
 228         "UTF-8",
 229         1,
 230         "KOI8-R",
 231         0,
 232         "\xDC\xDF\xCA\xC3\xD5\xCB\xC5\xCE",
 233 
 234         8,
 235         2,
 236         136,
 237         "\xDC\xDF\xCA\xC3\xD5\xCB\xC5\xCE",
 238     },
 239 };
 240 
 241 /* @Test(dataSource = "test_autocomplete_ds") */
 242 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 243 {
 244     // given
 245     edit_completion_dialog_show__return_value = g_strdup (data->input_completed_word);
 246 
 247     mc_global.source_codepage = data->input_source_codepage_id;
 248     mc_global.display_codepage = data->input_display_codepage_id;
 249     cp_source = data->input_editor_code_page;
 250     cp_display = data->input_system_code_page;
 251 
 252     do_set_codepage (0);
 253     edit_set_codeset (test_edit);
 254 
 255     // when
 256     edit_cursor_move (test_edit, data->input_position);
 257     edit_complete_word_cmd (test_edit);
 258 
 259     // then
 260     mctest_assert_ptr_eq (edit_completion_dialog_show__edit, test_edit);
 261     ck_assert_int_eq (g_queue_get_length (edit_completion_dialog_show__compl),
 262                       data->expected_compl_word_count);
 263     ck_assert_int_eq (edit_completion_dialog_show__max_width, data->expected_max_width);
 264 
 265     {
 266         off_t i = 0;
 267         GString *actual_completed_str;
 268 
 269         actual_completed_str = g_string_new ("");
 270 
 271         while (TRUE)
 272         {
 273             int chr;
 274 
 275             chr = edit_buffer_get_byte (&test_edit->buffer,
 276                                         data->input_completed_word_start_pos + i++);
 277             if (isspace (chr))
 278                 break;
 279             g_string_append_c (actual_completed_str, chr);
 280         }
 281         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 282         g_string_free (actual_completed_str, TRUE);
 283     }
 284 }
 285 END_PARAMETRIZED_TEST
 286 
 287 /* --------------------------------------------------------------------------------------------- */
 288 
 289 /* @DataSource("test_autocomplete_single_ds") */
 290 static const struct test_autocomplete_single_ds
 291 {
 292     off_t input_position;
 293     const char *input_system_code_page;
 294     int input_source_codepage_id;
 295     const char *input_editor_code_page;
 296     int input_display_codepage_id;
 297 
 298     int input_completed_word_start_pos;
 299 
 300     const char *expected_completed_word;
 301 } test_autocomplete_single_ds[] = {
 302     {
 303         // 0. UTF-8/KOI8-R - фыва
 304         146,
 305         "UTF-8",
 306         1,
 307         "KOI8-R",
 308         0,
 309 
 310         145,
 311         "\xC6\xD9\xD7\xC1",
 312     },
 313 };
 314 
 315 /* @Test(dataSource = "test_autocomplete_single_ds") */
 316 START_PARAMETRIZED_TEST (test_autocomplete_single, test_autocomplete_single_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 317 {
 318     // given
 319     mc_global.source_codepage = data->input_source_codepage_id;
 320     mc_global.display_codepage = data->input_display_codepage_id;
 321     cp_source = data->input_editor_code_page;
 322     cp_display = data->input_system_code_page;
 323 
 324     do_set_codepage (0);
 325     edit_set_codeset (test_edit);
 326 
 327     // when
 328     edit_cursor_move (test_edit, data->input_position);
 329     edit_complete_word_cmd (test_edit);
 330 
 331     // then
 332     {
 333         off_t i = 0;
 334         GString *actual_completed_str;
 335 
 336         actual_completed_str = g_string_new ("");
 337 
 338         while (TRUE)
 339         {
 340             int chr;
 341 
 342             chr = edit_buffer_get_byte (&test_edit->buffer,
 343                                         data->input_completed_word_start_pos + i++);
 344             if (isspace (chr))
 345                 break;
 346             g_string_append_c (actual_completed_str, chr);
 347         }
 348         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 349         g_string_free (actual_completed_str, TRUE);
 350     }
 351 }
 352 END_PARAMETRIZED_TEST
 353 
 354 /* --------------------------------------------------------------------------------------------- */
 355 
 356 int
 357 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 358 {
 359     TCase *tc_core;
 360 
 361     tc_core = tcase_create ("Core");
 362 
 363     tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
 364 
 365     // Add new tests here: ***************
 366     mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
 367     mctest_add_parameterized_test (tc_core, test_autocomplete_single, test_autocomplete_single_ds);
 368     // ***********************************
 369 
 370     return mctest_run_all (tc_core);
 371 }
 372 
 373 /* --------------------------------------------------------------------------------------------- */

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