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-2024
   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 <http://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 /* HAVE_CHARSET */
 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 ("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 /* HAVE_CHARSET */
 199 
 200     vfs_shut ();
 201 
 202     str_uninit_strings ();
 203 }
 204 
 205 /* --------------------------------------------------------------------------------------------- */
 206 
 207 #ifdef HAVE_CHARSET
 208 /* @DataSource("test_autocomplete_ds") */
 209 /* *INDENT-OFF* */
 210 static const struct test_autocomplete_ds
 211 {
 212     off_t input_position;
 213     const char *input_system_code_page;
 214     int input_source_codepage_id;
 215     const char *input_editor_code_page;
 216     int input_display_codepage_id;
 217     const char *input_completed_word;
 218 
 219     int expected_max_width;
 220     int expected_compl_word_count;
 221     int input_completed_word_start_pos;
 222     const char *expected_completed_word;
 223 } test_autocomplete_ds[] =
 224 {
 225     { /* 0. */
 226         102,
 227         "KOI8-R",
 228         0,
 229         "UTF-8",
 230         1,
 231         "эъйцукен",
 232 
 233         16,
 234         2,
 235         98,
 236         "эъйцукен"
 237     },
 238     { /* 1. */
 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 /* *INDENT-ON* */
 253 
 254 /* @Test(dataSource = "test_autocomplete_ds") */
 255 /* *INDENT-OFF* */
 256 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 257 /* *INDENT-ON* */
 258 {
 259     /* given */
 260     edit_completion_dialog_show__return_value = g_strdup (data->input_completed_word);
 261 
 262 
 263     mc_global.source_codepage = data->input_source_codepage_id;
 264     mc_global.display_codepage = data->input_display_codepage_id;
 265     cp_source = data->input_editor_code_page;
 266     cp_display = data->input_system_code_page;
 267 
 268     do_set_codepage (0);
 269     edit_set_codeset (test_edit);
 270 
 271     /* when */
 272     edit_cursor_move (test_edit, data->input_position);
 273     edit_complete_word_cmd (test_edit);
 274 
 275     /* then */
 276     mctest_assert_ptr_eq (edit_completion_dialog_show__edit, test_edit);
 277     ck_assert_int_eq (g_queue_get_length (edit_completion_dialog_show__compl),
 278                       data->expected_compl_word_count);
 279     ck_assert_int_eq (edit_completion_dialog_show__max_width, data->expected_max_width);
 280 
 281     {
 282         off_t i = 0;
 283         GString *actual_completed_str;
 284 
 285         actual_completed_str = g_string_new ("");
 286 
 287         while (TRUE)
 288         {
 289             int chr;
 290 
 291             chr =
 292                 edit_buffer_get_byte (&test_edit->buffer,
 293                                       data->input_completed_word_start_pos + i++);
 294             if (isspace (chr))
 295                 break;
 296             g_string_append_c (actual_completed_str, chr);
 297         }
 298         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 299         g_string_free (actual_completed_str, TRUE);
 300     }
 301 }
 302 /* *INDENT-OFF* */
 303 END_PARAMETRIZED_TEST
 304 /* *INDENT-ON* */
 305 
 306 /* --------------------------------------------------------------------------------------------- */
 307 
 308 /* @DataSource("test_autocomplete_single_ds") */
 309 /* *INDENT-OFF* */
 310 static const struct test_autocomplete_single_ds
 311 {
 312     off_t input_position;
 313     const char *input_system_code_page;
 314     int input_source_codepage_id;
 315     const char *input_editor_code_page;
 316     int input_display_codepage_id;
 317 
 318     int input_completed_word_start_pos;
 319 
 320     const char *expected_completed_word;
 321 } test_autocomplete_single_ds[] =
 322 {
 323     { /* 0. */
 324         146,
 325         "UTF-8",
 326         1,
 327         "KOI8-R",
 328         0,
 329 
 330         145,
 331         "\xC6\xD9\xD7\xC1" // фыва
 332     },
 333 };
 334 /* *INDENT-ON* */
 335 
 336 /* @Test(dataSource = "test_autocomplete_single_ds") */
 337 /* *INDENT-OFF* */
 338 START_PARAMETRIZED_TEST (test_autocomplete_single, test_autocomplete_single_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 339 /* *INDENT-ON* */
 340 {
 341     /* given */
 342     mc_global.source_codepage = data->input_source_codepage_id;
 343     mc_global.display_codepage = data->input_display_codepage_id;
 344     cp_source = data->input_editor_code_page;
 345     cp_display = data->input_system_code_page;
 346 
 347     do_set_codepage (0);
 348     edit_set_codeset (test_edit);
 349 
 350     /* when */
 351     edit_cursor_move (test_edit, data->input_position);
 352     edit_complete_word_cmd (test_edit);
 353 
 354     /* then */
 355     {
 356         off_t i = 0;
 357         GString *actual_completed_str;
 358 
 359         actual_completed_str = g_string_new ("");
 360 
 361         while (TRUE)
 362         {
 363             int chr;
 364 
 365             chr =
 366                 edit_buffer_get_byte (&test_edit->buffer,
 367                                       data->input_completed_word_start_pos + i++);
 368             if (isspace (chr))
 369                 break;
 370             g_string_append_c (actual_completed_str, chr);
 371         }
 372         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 373         g_string_free (actual_completed_str, TRUE);
 374     }
 375 }
 376 /* *INDENT-OFF* */
 377 END_PARAMETRIZED_TEST
 378 /* *INDENT-ON* */
 379 
 380 
 381 #endif /* HAVE_CHARSET */
 382 
 383 /* --------------------------------------------------------------------------------------------- */
 384 
 385 int
 386 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 387 {
 388     TCase *tc_core;
 389 
 390     tc_core = tcase_create ("Core");
 391 
 392     tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
 393 
 394     /* Add new tests here: *************** */
 395 #ifdef HAVE_CHARSET
 396     mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
 397     mctest_add_parameterized_test (tc_core, test_autocomplete_single, test_autocomplete_single_ds);
 398 #endif /* HAVE_CHARSET */
 399     /* *********************************** */
 400 
 401     return mctest_run_all (tc_core);
 402 }
 403 
 404 /* --------------------------------------------------------------------------------------------- */

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