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

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