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

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