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__deinit
  8. my_setup
  9. my_teardown
  10. START_PARAMETRIZED_TEST
  11. START_PARAMETRIZED_TEST
  12. main

   1 /*
   2    src/editor - tests for edit_complete_word_cmd() function
   3 
   4    Copyright (C) 2013-2019
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/src/editor"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include <ctype.h>
  31 
  32 #include "lib/timer.h"
  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 
  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_len;
  94 /* @CapturedValue */
  95 static GString **editcmd_dialog_completion_show__compl;
  96 /* @CapturedValue */
  97 static int editcmd_dialog_completion_show__num_compl;
  98 
  99 /* @ThenReturnValue */
 100 static char *editcmd_dialog_completion_show__return_value;
 101 
 102 /* @Mock */
 103 char *
 104 editcmd_dialog_completion_show (const WEdit * edit, int max_len, GString ** compl, int num_compl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 105 {
 106 
 107     editcmd_dialog_completion_show__edit = edit;
 108     editcmd_dialog_completion_show__max_len = max_len;
 109     editcmd_dialog_completion_show__num_compl = num_compl;
 110 
 111     {
 112         int iterator;
 113 
 114         editcmd_dialog_completion_show__compl = g_new0 (GString *, num_compl);
 115 
 116         for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
 117             editcmd_dialog_completion_show__compl[iterator] =
 118                 g_string_new_len (compl[iterator]->str, compl[iterator]->len);
 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_len = 0;
 129     editcmd_dialog_completion_show__compl = NULL;
 130     editcmd_dialog_completion_show__num_compl = 0;
 131     editcmd_dialog_completion_show__return_value = NULL;
 132 }
 133 
 134 static void
 135 editcmd_dialog_completion_show__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 136 {
 137     if (editcmd_dialog_completion_show__compl != NULL)
 138     {
 139         int iterator;
 140 
 141         for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
 142             g_string_free (editcmd_dialog_completion_show__compl[iterator], TRUE);
 143 
 144         g_free (editcmd_dialog_completion_show__compl);
 145     }
 146 }
 147 
 148 
 149 /* --------------------------------------------------------------------------------------------- */
 150 
 151 /* @Before */
 152 static void
 153 my_setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 154 {
 155     mc_global.timer = mc_timer_new ();
 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     option_filesize_threshold = (char *) "64M";
 168 
 169     test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1);
 170     editcmd_dialog_completion_show__init ();
 171 }
 172 
 173 /* --------------------------------------------------------------------------------------------- */
 174 
 175 /* @After */
 176 static void
 177 my_teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 178 {
 179     editcmd_dialog_completion_show__deinit ();
 180     edit_clean (test_edit);
 181     g_free (test_edit);
 182 
 183 #ifdef HAVE_CHARSET
 184     free_codepages_list ();
 185 #endif /* HAVE_CHARSET */
 186 
 187     vfs_shut ();
 188 
 189     str_uninit_strings ();
 190     mc_timer_destroy (mc_global.timer);
 191 }
 192 
 193 /* --------------------------------------------------------------------------------------------- */
 194 
 195 #ifdef HAVE_CHARSET
 196 /* @DataSource("test_autocomplete_ds") */
 197 /* *INDENT-OFF* */
 198 static const struct test_autocomplete_ds
 199 {
 200     off_t input_position;
 201     const char *input_system_code_page;
 202     int input_source_codepage_id;
 203     const char *input_editor_code_page;
 204     int input_display_codepage_id;
 205     const char *input_completed_word;
 206 
 207     int expected_max_len;
 208     int expected_compl_word_count;
 209     int input_completed_word_start_pos;
 210     const char *expected_completed_word;
 211 } test_autocomplete_ds[] =
 212 {
 213     { /* 0. */
 214         111,
 215         "KOI8-R",
 216         0,
 217         "UTF-8",
 218         1,
 219         "эъйцукен",
 220 
 221         16,
 222         2,
 223         107,
 224         "эъйцукен"
 225     },
 226     { /* 1. */
 227         147,
 228         "UTF-8",
 229         1,
 230         "KOI8-R",
 231         0,
 232         "",
 233 
 234         8,
 235         2,
 236         145,
 237         ""
 238     },
 239 };
 240 /* *INDENT-ON* */
 241 
 242 /* @Test(dataSource = "test_autocomplete_ds") */
 243 /* *INDENT-OFF* */
 244 START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 245 /* *INDENT-ON* */
 246 {
 247     /* given */
 248     editcmd_dialog_completion_show__return_value = g_strdup (data->input_completed_word);
 249 
 250 
 251     mc_global.source_codepage = data->input_source_codepage_id;
 252     mc_global.display_codepage = data->input_display_codepage_id;
 253     cp_source = data->input_editor_code_page;
 254     cp_display = data->input_system_code_page;
 255 
 256     do_set_codepage (0);
 257     edit_set_codeset (test_edit);
 258 
 259     /* when */
 260     edit_cursor_move (test_edit, data->input_position);
 261     edit_complete_word_cmd (test_edit);
 262 
 263     /* then */
 264     mctest_assert_ptr_eq (editcmd_dialog_completion_show__edit, test_edit);
 265     mctest_assert_int_eq (editcmd_dialog_completion_show__num_compl,
 266                           data->expected_compl_word_count);
 267     mctest_assert_int_eq (editcmd_dialog_completion_show__max_len, data->expected_max_len);
 268 
 269     {
 270         off_t i = 0;
 271         GString *actual_completed_str;
 272 
 273         actual_completed_str = g_string_new ("");
 274 
 275         while (TRUE)
 276         {
 277             int chr;
 278 
 279             chr =
 280                 edit_buffer_get_byte (&test_edit->buffer,
 281                                       data->input_completed_word_start_pos + i++);
 282             if (isspace (chr))
 283                 break;
 284             g_string_append_c (actual_completed_str, chr);
 285         }
 286         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 287         g_string_free (actual_completed_str, TRUE);
 288     }
 289 }
 290 /* *INDENT-OFF* */
 291 END_PARAMETRIZED_TEST
 292 /* *INDENT-ON* */
 293 
 294 /* --------------------------------------------------------------------------------------------- */
 295 
 296 /* @DataSource("test_autocomplete_single_ds") */
 297 /* *INDENT-OFF* */
 298 static const struct test_autocomplete_single_ds
 299 {
 300     off_t input_position;
 301     const char *input_system_code_page;
 302     int input_source_codepage_id;
 303     const char *input_editor_code_page;
 304     int input_display_codepage_id;
 305 
 306     int input_completed_word_start_pos;
 307 
 308     const char *expected_completed_word;
 309 } test_autocomplete_single_ds[] =
 310 {
 311     { /* 0. */
 312         155,
 313         "UTF-8",
 314         1,
 315         "KOI8-R",
 316         0,
 317 
 318         154,
 319         ""
 320     },
 321 };
 322 /* *INDENT-ON* */
 323 
 324 /* @Test(dataSource = "test_autocomplete_single_ds") */
 325 /* *INDENT-OFF* */
 326 START_PARAMETRIZED_TEST (test_autocomplete_single, test_autocomplete_single_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 327 /* *INDENT-ON* */
 328 {
 329     /* given */
 330     mc_global.source_codepage = data->input_source_codepage_id;
 331     mc_global.display_codepage = data->input_display_codepage_id;
 332     cp_source = data->input_editor_code_page;
 333     cp_display = data->input_system_code_page;
 334 
 335     do_set_codepage (0);
 336     edit_set_codeset (test_edit);
 337 
 338     /* when */
 339     edit_cursor_move (test_edit, data->input_position);
 340     edit_complete_word_cmd (test_edit);
 341 
 342     /* then */
 343     {
 344         off_t i = 0;
 345         GString *actual_completed_str;
 346 
 347         actual_completed_str = g_string_new ("");
 348 
 349         while (TRUE)
 350         {
 351             int chr;
 352 
 353             chr =
 354                 edit_buffer_get_byte (&test_edit->buffer,
 355                                       data->input_completed_word_start_pos + i++);
 356             if (isspace (chr))
 357                 break;
 358             g_string_append_c (actual_completed_str, chr);
 359         }
 360         mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
 361         g_string_free (actual_completed_str, TRUE);
 362     }
 363 }
 364 /* *INDENT-OFF* */
 365 END_PARAMETRIZED_TEST
 366 /* *INDENT-ON* */
 367 
 368 
 369 #endif /* HAVE_CHARSET */
 370 
 371 /* --------------------------------------------------------------------------------------------- */
 372 
 373 int
 374 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 375 {
 376     int number_failed;
 377 
 378     Suite *s = suite_create (TEST_SUITE_NAME);
 379     TCase *tc_core = tcase_create ("Core");
 380     SRunner *sr;
 381 
 382     tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
 383 
 384     /* Add new tests here: *************** */
 385 #ifdef HAVE_CHARSET
 386     mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
 387     mctest_add_parameterized_test (tc_core, test_autocomplete_single, test_autocomplete_single_ds);
 388 #endif /* HAVE_CHARSET */
 389     /* *********************************** */
 390 
 391     suite_add_tcase (s, tc_core);
 392     sr = srunner_create (s);
 393     srunner_set_log (sr, "edit_complete_word_cmd.log");
 394     srunner_run_all (sr, CK_ENV);
 395     number_failed = srunner_ntests_failed (sr);
 396     srunner_free (sr);
 397     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 398 }
 399 
 400 /* --------------------------------------------------------------------------------------------- */

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