Manual pages: mcmcdiffmceditmcview

root/tests/src/editor/edit_replace_cmd.c

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

DEFINITIONS

This source file includes following definitions.
  1. message
  2. status_msg_init
  3. status_msg_deinit
  4. edit_search_update_callback
  5. edit_dialog_replace_show
  6. edit_dialog_replace_prompt_show
  7. edit_load_syntax
  8. edit_get_syntax_color
  9. edit_load_macro_cmd
  10. setup
  11. teardown
  12. test_replace_check
  13. START_TEST
  14. START_TEST
  15. START_TEST
  16. main

   1 /*
   2    src/editor - tests for edit_replace_cmd() function
   3 
   4    Copyright (C) 2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Andrew Borodin <aborodin@vmail.ru>, 2025
   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 <https://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/charsets.h"
  33 #include "src/selcodepage.h"
  34 
  35 #include "src/editor/editwidget.h"
  36 #include "src/editor/editmacros.h"  // edit_load_macro_cmd()
  37 #include "src/editor/editsearch.h"
  38 
  39 static WGroup owner;
  40 static WEdit *test_edit;
  41 static gboolean only_in_selection = FALSE;
  42 
  43 static const char *test_regex_in = "qwe\n"     //
  44                                    "qwe\n"     //
  45                                    "qwe\n"     //
  46                                    "qwe\n"     //
  47                                    "qwe\n"     //
  48                                    "qwe\n"     //
  49                                    "qwe\n"     //
  50                                    "qwe\n";    //
  51 static const char *test_regex_out = "Xqwe\n"   //
  52                                     "Xqwe\n"   //
  53                                     "Xqwe\n"   //
  54                                     "Xqwe\n"   //
  55                                     "Xqwe\n"   //
  56                                     "Xqwe\n"   //
  57                                     "Xqwe\n"   //
  58                                     "Xqwe\n";  //
  59 
  60 static const char *test_regex_selected_in = "qwe\n"     //
  61                                             "qwe\n"     //
  62                                             "qwe\n"     // selected, mark1 = 8 (begin of line) or 9
  63                                             "qwe\n"     // selected
  64                                             "qwe\n"     // selected
  65                                             "qwe\n"     // mark2 = 20, begin of line
  66                                             "qwe\n"     //
  67                                             "qwe\n";    //
  68 static const char *test1_regex_selected_out = "qwe\n"   //
  69                                               "qwe\n"   //
  70                                               "Xqwe\n"  // selected, mark1 = 8 (begin of line)
  71                                               "Xqwe\n"  // selected
  72                                               "Xqwe\n"  // selected
  73                                               "qwe\n"   // mark2 = 20, begin of line
  74                                               "qwe\n"   //
  75                                               "qwe\n";  //
  76 static const char *test2_regex_selected_out = "qwe\n"   //
  77                                               "qwe\n"   //
  78                                               "qwe\n"   // selected, mark1 = 9 (not begin of line)
  79                                               "Xqwe\n"  // selected
  80                                               "Xqwe\n"  // selected
  81                                               "qwe\n"   // mark2 = 20, begin of line
  82                                               "qwe\n"   //
  83                                               "qwe\n";  //
  84 
  85 static const char *replace_regex_from = "^.*";
  86 static const char *replace_regex_to = "X\\0";
  87 
  88 /* --------------------------------------------------------------------------------------------- */
  89 
  90 void edit_dialog_replace_show (WEdit *edit, const char *search_default, const char *replace_default,
  91                                char **search_text, char **replace_text);
  92 int edit_dialog_replace_prompt_show (WEdit *edit, char *from_text, char *to_text, int xpos,
  93                                      int ypos);
  94 
  95 /* --------------------------------------------------------------------------------------------- */
  96 /* @Mock */
  97 void
  98 message (int flags, const char *title, const char *text, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
  99 {
 100     (void) flags;
 101     (void) title;
 102     (void) text;
 103 }
 104 
 105 /* --------------------------------------------------------------------------------------------- */
 106 /* @Mock */
 107 void
 108 status_msg_init (status_msg_t *sm, const char *title, double delay, status_msg_cb init_cb,
     /* [previous][next][first][last][top][bottom][index][help]  */
 109                  status_msg_update_cb update_cb, status_msg_cb deinit_cb)
 110 {
 111     (void) sm;
 112     (void) title;
 113     (void) delay;
 114     (void) init_cb;
 115     (void) update_cb;
 116     (void) deinit_cb;
 117 }
 118 
 119 /* --------------------------------------------------------------------------------------------- */
 120 /* @Mock */
 121 void
 122 status_msg_deinit (status_msg_t *sm)
     /* [previous][next][first][last][top][bottom][index][help]  */
 123 {
 124     (void) sm;
 125 }
 126 
 127 /* --------------------------------------------------------------------------------------------- */
 128 /* @Mock */
 129 mc_search_cbret_t
 130 edit_search_update_callback (const void *user_data, off_t char_offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 131 {
 132     (void) user_data;
 133     (void) char_offset;
 134 
 135     return MC_SEARCH_CB_OK;
 136 }
 137 
 138 /* --------------------------------------------------------------------------------------------- */
 139 /* @Mock */
 140 void
 141 edit_dialog_replace_show (WEdit *edit, const char *search_default, const char *replace_default,
     /* [previous][next][first][last][top][bottom][index][help]  */
 142                           char **search_text, char **replace_text)
 143 {
 144     (void) edit;
 145     (void) search_default;
 146     (void) replace_default;
 147 
 148     *search_text = g_strdup (replace_regex_from);
 149     *replace_text = g_strdup (replace_regex_to);
 150 
 151     edit_search_options.type = MC_SEARCH_T_REGEX;
 152     edit_search_options.only_in_selection = only_in_selection;
 153 }
 154 
 155 /* --------------------------------------------------------------------------------------------- */
 156 /* @Mock */
 157 int
 158 edit_dialog_replace_prompt_show (WEdit *edit, char *from_text, char *to_text, int xpos, int ypos)
     /* [previous][next][first][last][top][bottom][index][help]  */
 159 {
 160     (void) edit;
 161     (void) from_text;
 162     (void) to_text;
 163     (void) xpos;
 164     (void) ypos;
 165 
 166     return B_REPLACE_ALL;
 167 }
 168 
 169 /* --------------------------------------------------------------------------------------------- */
 170 /* @Mock */
 171 void
 172 edit_load_syntax (WEdit *edit, GPtrArray *pnames, const char *type)
     /* [previous][next][first][last][top][bottom][index][help]  */
 173 {
 174     (void) edit;
 175     (void) pnames;
 176     (void) type;
 177 }
 178 
 179 /* --------------------------------------------------------------------------------------------- */
 180 
 181 /* @Mock */
 182 int
 183 edit_get_syntax_color (WEdit *edit, off_t byte_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 184 {
 185     (void) edit;
 186     (void) byte_index;
 187 
 188     return 0;
 189 }
 190 
 191 /* --------------------------------------------------------------------------------------------- */
 192 
 193 /* @Mock */
 194 gboolean
 195 edit_load_macro_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 196 {
 197     (void) edit;
 198 
 199     return FALSE;
 200 }
 201 
 202 /* --------------------------------------------------------------------------------------------- */
 203 
 204 /* @Before */
 205 static void
 206 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     WRect r;
 209 
 210     str_init_strings (NULL);
 211 
 212     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
 213     load_codepages_list ();
 214 
 215     edit_options.filesize_threshold = (char *) "64M";
 216 
 217     rect_init (&r, 0, 0, 24, 80);
 218     test_edit = edit_init (NULL, &r, NULL);
 219     memset (&owner, 0, sizeof (owner));
 220     group_add_widget (&owner, WIDGET (test_edit));
 221 
 222     mc_global.source_codepage = 0;
 223     mc_global.display_codepage = 0;
 224     cp_source = "ASCII";
 225     cp_display = "ASCII";
 226 
 227     do_set_codepage (0);
 228     edit_set_codeset (test_edit);
 229 }
 230 
 231 /* --------------------------------------------------------------------------------------------- */
 232 
 233 /* @After */
 234 static void
 235 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 236 {
 237     edit_clean (test_edit);
 238     group_remove_widget (test_edit);
 239     g_free (test_edit);
 240 
 241     free_codepages_list ();
 242     str_uninit_strings ();
 243 }
 244 
 245 /* --------------------------------------------------------------------------------------------- */
 246 
 247 static void
 248 test_replace_check (const char *test_out)
     /* [previous][next][first][last][top][bottom][index][help]  */
 249 {
 250     GString *actual_replaced_str;
 251 
 252     actual_replaced_str = g_string_new ("");
 253 
 254     for (off_t i = 0; i < test_edit->buffer.size; i++)
 255     {
 256         const int chr = edit_buffer_get_byte (&test_edit->buffer, i);
 257 
 258         g_string_append_c (actual_replaced_str, chr);
 259     }
 260 
 261     mctest_assert_str_eq (actual_replaced_str->str, test_out);
 262     g_string_free (actual_replaced_str, TRUE);
 263 }
 264 
 265 /* --------------------------------------------------------------------------------------------- */
 266 
 267 START_TEST (test_replace_regex)
     /* [previous][next][first][last][top][bottom][index][help]  */
 268 {
 269     // given
 270     only_in_selection = FALSE;
 271     test_edit->mark1 = 0;
 272     test_edit->mark2 = 0;
 273 
 274     for (const char *ti = test_regex_in; *ti != '\0'; ti++)
 275         edit_buffer_insert (&test_edit->buffer, *ti);
 276 
 277     // when
 278     edit_cursor_move (test_edit, 0);
 279     edit_replace_cmd (test_edit, FALSE);
 280 
 281     // then
 282     test_replace_check (test_regex_out);
 283 }
 284 END_TEST
 285 
 286 /* --------------------------------------------------------------------------------------------- */
 287 
 288 START_TEST (test1_replace_regex_in_selection)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     // given
 291     only_in_selection = TRUE;
 292     test_edit->mark1 = 8;
 293     test_edit->mark2 = 20;
 294 
 295     for (const char *ti = test_regex_selected_in; *ti != '\0'; ti++)
 296         edit_buffer_insert (&test_edit->buffer, *ti);
 297 
 298     // when
 299     edit_cursor_move (test_edit, 0);
 300     edit_replace_cmd (test_edit, FALSE);
 301 
 302     // then
 303     test_replace_check (test1_regex_selected_out);
 304 }
 305 END_TEST
 306 
 307 /* --------------------------------------------------------------------------------------------- */
 308 
 309 START_TEST (test2_replace_regex_in_selection)
     /* [previous][next][first][last][top][bottom][index][help]  */
 310 {
 311     // given
 312     only_in_selection = TRUE;
 313     test_edit->mark1 = 9;
 314     test_edit->mark2 = 20;
 315 
 316     for (const char *ti = test_regex_selected_in; *ti != '\0'; ti++)
 317         edit_buffer_insert (&test_edit->buffer, *ti);
 318 
 319     // when
 320     edit_cursor_move (test_edit, 0);
 321     edit_replace_cmd (test_edit, FALSE);
 322 
 323     // then
 324     test_replace_check (test2_regex_selected_out);
 325 }
 326 END_TEST
 327 
 328 /* --------------------------------------------------------------------------------------------- */
 329 
 330 int
 331 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 332 {
 333     TCase *tc_core;
 334 
 335     tc_core = tcase_create ("Core");
 336 
 337     tcase_add_checked_fixture (tc_core, setup, teardown);
 338 
 339     // Add new tests here: ***************
 340     tcase_add_test (tc_core, test_replace_regex);
 341     tcase_add_test (tc_core, test1_replace_regex_in_selection);
 342     tcase_add_test (tc_core, test2_replace_regex_in_selection);
 343     // ***********************************
 344 
 345     return mctest_run_all (tc_core);
 346 }
 347 
 348 /* --------------------------------------------------------------------------------------------- */

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