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

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