Manual pages: mcmcdiffmceditmcview

root/tests/src/editor/edit_insert_column_of_text.c

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

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. test_load_text
  4. test_get_block
  5. test_insert_column_check
  6. START_PARAMETRIZED_TEST
  7. main

   1 /*
   2    src/editor - tests for edit_insert_column_of_text() 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 "lib/charsets.h"
  31 #include "src/selcodepage.h"
  32 
  33 #include "src/editor/editwidget.h"
  34 
  35 static WGroup owner;
  36 static WEdit *test_edit;
  37 
  38 // input text
  39 static const char test_text_in[] = "11111\n"  //
  40                                    "22\n"     //
  41                                    "aa\n"     //
  42                                    "bb\n";    //
  43 
  44 // result of colunm cipy to the middle of line:
  45 static const char test_insert_column_out1[] = "11111\n"   //
  46                                               "22\n"      //
  47                                               "a111a\n"   //
  48                                               "b2  b\n";  //
  49 
  50 // result colunm copy to the end of line, no trailing spaces
  51 static const char test_insert_column_out2[] = "11111\n"  //
  52                                               "22\n"     //
  53                                               "aa111\n"  //
  54                                               "bb2\n";   //
  55 
  56 /* --------------------------------------------------------------------------------------------- */
  57 
  58 // Function under test
  59 void edit_insert_column_of_text (WEdit *edit, GString *data, long width, off_t *start_pos,
  60                                  off_t *end_pos, long *col1, long *col2);
  61 
  62 /* --------------------------------------------------------------------------------------------- */
  63 
  64 /* @Before */
  65 static void
  66 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     WRect r;
  69 
  70     str_init_strings (NULL);
  71 
  72     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  73     load_codepages_list ();
  74 
  75     edit_options.filesize_threshold = (char *) "64M";
  76 
  77     rect_init (&r, 0, 0, 24, 80);
  78     test_edit = edit_init (NULL, &r, NULL);
  79     memset (&owner, 0, sizeof (owner));
  80     group_add_widget (&owner, WIDGET (test_edit));
  81 
  82     mc_global.source_codepage = 0;
  83     mc_global.display_codepage = 0;
  84     cp_source = "ASCII";
  85     cp_display = "ASCII";
  86 
  87     do_set_codepage (0);
  88     edit_set_codeset (test_edit);
  89 }
  90 
  91 /* --------------------------------------------------------------------------------------------- */
  92 
  93 /* @After */
  94 static void
  95 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  96 {
  97     edit_clean (test_edit);
  98     group_remove_widget (test_edit);
  99     g_free (test_edit);
 100 
 101     free_codepages_list ();
 102     str_uninit_strings ();
 103 }
 104 
 105 /* --------------------------------------------------------------------------------------------- */
 106 
 107 static void
 108 test_load_text (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 109 {
 110     for (const char *ti = test_text_in; *ti != '\0'; ti++)
 111         edit_buffer_insert (&test_edit->buffer, *ti);
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 
 116 static GString *
 117 test_get_block (const off_t start, const off_t finish)
     /* [previous][next][first][last][top][bottom][index][help]  */
 118 {
 119     GString *r;
 120 
 121     r = g_string_sized_new (finish - start);
 122 
 123     // copy from buffer, excluding chars that are out of the column 'margins'
 124     for (off_t i = start; i < finish; i++)
 125     {
 126         off_t x;
 127 
 128         x = edit_buffer_get_bol (&test_edit->buffer, i);
 129         x = edit_move_forward3 (test_edit, x, 0, i);
 130 
 131         const int c = edit_buffer_get_byte (&test_edit->buffer, i);
 132 
 133         if ((x >= test_edit->column1 && x < test_edit->column2)
 134             || (x >= test_edit->column2 && x < test_edit->column1) || c == '\n')
 135             g_string_append_c (r, (gchar) c);
 136     }
 137 
 138     return r;
 139 }
 140 
 141 /* --------------------------------------------------------------------------------------------- */
 142 
 143 static void
 144 test_insert_column_check (const char *test_out)
     /* [previous][next][first][last][top][bottom][index][help]  */
 145 {
 146     GString *actual_text;
 147 
 148     actual_text = g_string_new ("");
 149 
 150     for (off_t i = 0; i < test_edit->buffer.size; i++)
 151     {
 152         const int chr = edit_buffer_get_byte (&test_edit->buffer, i);
 153 
 154         g_string_append_c (actual_text, chr);
 155     }
 156 
 157     mctest_assert_str_eq (actual_text->str, test_out);
 158     g_string_free (actual_text, TRUE);
 159 }
 160 
 161 /* --------------------------------------------------------------------------------------------- */
 162 
 163 static const struct test_insert_column_ds
 164 {
 165     off_t offset;
 166     const char *expected_text;
 167 } test_insert_column_ds[] = {
 168     {
 169         // 0. Insert column at middle of line
 170         10,
 171         test_insert_column_out1,
 172     },
 173     {
 174         // 1. Insert column at end of line
 175         11,
 176         test_insert_column_out2,
 177     },
 178 };
 179 
 180 /* @Test(dataSource = "test_insert_column_ds") */
 181 START_PARAMETRIZED_TEST (test_insert_column, test_insert_column_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 182 {
 183     off_t start_mark, end_mark;
 184     GString *copy_buf;
 185     off_t start_pos, end_pos;
 186     long col1, col2;
 187 
 188     // given
 189     test_load_text ();
 190 
 191     test_edit->column_highlight = 1;
 192     test_edit->mark1 = 1;
 193     test_edit->mark2 = 8;
 194     test_edit->column1 = 1;
 195     test_edit->column2 = 4;
 196 
 197     // when
 198     edit_cursor_move (test_edit, -test_edit->buffer.curs1 + data->offset);
 199 
 200     const gboolean eval_marks_result = eval_marks (test_edit, &start_mark, &end_mark);
 201 
 202     mctest_assert_true (eval_marks_result);
 203 
 204     copy_buf = test_get_block (start_mark, end_mark);
 205     edit_push_markers (test_edit);
 206 
 207     const long col_delta = labs (test_edit->column2 - test_edit->column1);
 208 
 209     edit_insert_column_of_text (test_edit, copy_buf, col_delta, &start_pos, &end_pos, &col1, &col2);
 210 
 211     g_string_free (copy_buf, TRUE);
 212 
 213     // then
 214     test_insert_column_check (data->expected_text);
 215 }
 216 END_PARAMETRIZED_TEST
 217 
 218 /* --------------------------------------------------------------------------------------------- */
 219 
 220 int
 221 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 222 {
 223     TCase *tc_core;
 224 
 225     tc_core = tcase_create ("Core");
 226 
 227     tcase_add_checked_fixture (tc_core, setup, teardown);
 228 
 229     // Add new tests here: ***************
 230     mctest_add_parameterized_test (tc_core, test_insert_column, test_insert_column_ds);
 231     // ***********************************
 232 
 233     return mctest_run_all (tc_core);
 234 }
 235 
 236 /* --------------------------------------------------------------------------------------------- */

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