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-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/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     str_init_strings (NULL);
 155 
 156     vfs_init ();
 157     vfs_init_localfs ();
 158     vfs_setup_work_dir ();
 159 
 160 #ifdef HAVE_CHARSET
 161     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
 162     load_codepages_list ();
 163 #endif /* HAVE_CHARSET */
 164 
 165     mc_global.main_config = mc_config_init ("edit_complete_word_cmd.ini", FALSE);
 166     mc_config_set_bool (mc_global.main_config, CONFIG_APP_SECTION,
 167                         "editor_wordcompletion_collect_all_files", TRUE);
 168 
 169     option_filesize_threshold = (char *) "64M";
 170 
 171     test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1);
 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 
 190 #ifdef HAVE_CHARSET
 191     free_codepages_list ();
 192 #endif /* HAVE_CHARSET */
 193 
 194     vfs_shut ();
 195 
 196     str_uninit_strings ();
 197 }
 198 
 199 /* --------------------------------------------------------------------------------------------- */
 200 
 201 #ifdef HAVE_CHARSET
 202 /* @DataSource("test_autocomplete_ds") */
 203 /* *INDENT-OFF* */
 204 static const struct test_autocomplete_ds
 205 {
 206     off_t input_position;
 207     const char *input_system_code_page;
 208     int input_source_codepage_id;
 209     const char *input_editor_code_page;
 210     int input_display_codepage_id;
 211     const char *input_completed_word;
 212 
 213     int expected_max_width;
 214     int expected_compl_word_count;
 215     int input_completed_word_start_pos;
 216     const char *expected_completed_word;
 217 } test_autocomplete_ds[] =
 218 {
 219     { /* 0. */
 220         102,
 221         "KOI8-R",
 222         0,
 223         "UTF-8",
 224         1,
 225         "эъйцукен",
 226 
 227         16,
 228         2,
 229         98,
 230         "эъйцукен"
 231     },
 232     { /* 1. */
 233         138,
 234         "UTF-8",
 235         1,
 236         "KOI8-R",
 237         0,
 238         "",
 239 
 240         8,
 241         2,
 242         136,
 243         ""
 244     },
 245 };
 246 /* *INDENT-ON* */
 247 
 248 /* @Test(dataSource = "test_autocomplete_ds") */
 249 /* *INDENT-OFF* */
 250 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 251 /* *INDENT-ON* */
 252 {
 253     /* given */
 254     edit_completion_dialog_show__return_value = g_strdup (data->input_completed_word);
 255 
 256 
 257     mc_global.source_codepage = data->input_source_codepage_id;
 258     mc_global.display_codepage = data->input_display_codepage_id;
 259     cp_source = data->input_editor_code_page;
 260     cp_display = data->input_system_code_page;
 261 
 262     do_set_codepage (0);
 263     edit_set_codeset (test_edit);
 264 
 265     /* when */
 266     edit_cursor_move (test_edit, data->input_position);
 267     edit_complete_word_cmd (test_edit);
 268 
 269     /* then */
 270     mctest_assert_ptr_eq (edit_completion_dialog_show__edit, test_edit);
 271     mctest_assert_int_eq (g_queue_get_length (edit_completion_dialog_show__compl),
 272                           data->expected_compl_word_count);
 273     mctest_assert_int_eq (edit_completion_dialog_show__max_width, data->expected_max_width);
 274 
 275     {
 276         off_t i = 0;
 277         GString *actual_completed_str;
 278 
 279         actual_completed_str = g_string_new ("");
 280 
 281         while (TRUE)
 282         {
 283             int chr;
 284 
 285             chr =
 286                 edit_buffer_get_byte (&test_edit->buffer,
 287                                       data->input_completed_word_start_pos + i++);
 288             if (isspace (chr))
 289                 break;
 290             g_string_append_c (actual_completed_str, chr);
 291         }
 292         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 293         g_string_free (actual_completed_str, TRUE);
 294     }
 295 }
 296 /* *INDENT-OFF* */
 297 END_PARAMETRIZED_TEST
 298 /* *INDENT-ON* */
 299 
 300 /* --------------------------------------------------------------------------------------------- */
 301 
 302 /* @DataSource("test_autocomplete_single_ds") */
 303 /* *INDENT-OFF* */
 304 static const struct test_autocomplete_single_ds
 305 {
 306     off_t input_position;
 307     const char *input_system_code_page;
 308     int input_source_codepage_id;
 309     const char *input_editor_code_page;
 310     int input_display_codepage_id;
 311 
 312     int input_completed_word_start_pos;
 313 
 314     const char *expected_completed_word;
 315 } test_autocomplete_single_ds[] =
 316 {
 317     { /* 0. */
 318         146,
 319         "UTF-8",
 320         1,
 321         "KOI8-R",
 322         0,
 323 
 324         145,
 325         ""
 326     },
 327 };
 328 /* *INDENT-ON* */
 329 
 330 /* @Test(dataSource = "test_autocomplete_single_ds") */
 331 /* *INDENT-OFF* */
 332 START_PARAMETRIZED_TEST (test_autocomplete_single, test_autocomplete_single_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 333 /* *INDENT-ON* */
 334 {
 335     /* given */
 336     mc_global.source_codepage = data->input_source_codepage_id;
 337     mc_global.display_codepage = data->input_display_codepage_id;
 338     cp_source = data->input_editor_code_page;
 339     cp_display = data->input_system_code_page;
 340 
 341     do_set_codepage (0);
 342     edit_set_codeset (test_edit);
 343 
 344     /* when */
 345     edit_cursor_move (test_edit, data->input_position);
 346     edit_complete_word_cmd (test_edit);
 347 
 348     /* then */
 349     {
 350         off_t i = 0;
 351         GString *actual_completed_str;
 352 
 353         actual_completed_str = g_string_new ("");
 354 
 355         while (TRUE)
 356         {
 357             int chr;
 358 
 359             chr =
 360                 edit_buffer_get_byte (&test_edit->buffer,
 361                                       data->input_completed_word_start_pos + i++);
 362             if (isspace (chr))
 363                 break;
 364             g_string_append_c (actual_completed_str, chr);
 365         }
 366         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 367         g_string_free (actual_completed_str, TRUE);
 368     }
 369 }
 370 /* *INDENT-OFF* */
 371 END_PARAMETRIZED_TEST
 372 /* *INDENT-ON* */
 373 
 374 
 375 #endif /* HAVE_CHARSET */
 376 
 377 /* --------------------------------------------------------------------------------------------- */
 378 
 379 int
 380 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 381 {
 382     TCase *tc_core;
 383 
 384     tc_core = tcase_create ("Core");
 385 
 386     tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
 387 
 388     /* Add new tests here: *************** */
 389 #ifdef HAVE_CHARSET
 390     mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
 391     mctest_add_parameterized_test (tc_core, test_autocomplete_single, test_autocomplete_single_ds);
 392 #endif /* HAVE_CHARSET */
 393     /* *********************************** */
 394 
 395     return mctest_run_all (tc_core);
 396 }
 397 
 398 /* --------------------------------------------------------------------------------------------- */

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